-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedRequestObj.py
182 lines (147 loc) · 6.41 KB
/
RedRequestObj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
print 'RedRequestObj imported'
import RedDot as red
import xml.etree.ElementTree as ET
import xml, httplib, urllib
import pprint
loginguid=''
sessionkey=''
class RedRequestObj(object):
"""
To be exended by specific request type classes
"""
guid = None
redResponse = None
RQL = None
def __init__(self, guid_in=None):
if guid_in is not None:
self.guid = guid_in
@staticmethod
def getXpath(xml, path):
#print 'getXpath:'
#pprint.pprint(xml)
#pprint.pprint(path)
try:
node = ET.fromstring(xml)
ns = node.findall(path)
return ns
except xml.parsers.expat.ExpatError:
print 'invalid xml\n'# + xml
raise
def fetch(self, path, attr=None, limit=None):
elems = self.getXpath(self.redResponse, path)
if len(elems) == 0:
print 'Warning: elements not found: ' + path
return
if attr is None:
elems = map(lambda x:x.tag, elems)
else:
#pprint.pprint(elems[0].attrib)
results = []
for e in elems:
try:
results.append(e.attrib[attr])
#elems = map(lambda x:x.attrib[attr], elems)
except KeyError:
#raise Exception(path + ' has no attribute called '+attr)
print(path + ' has no attribute called '+attr)
pass
if len(results) == 0:
raise Exception(path + ' has no attribute called '+attr)
else:
elems = results
if limit is not None:
elems = elems[0:limit]
return elems
def request(self, nocache=False):
if nocache:
print 'no cache request '
conn = httplib.HTTPSConnection(red.host)
params = urllib.urlencode({'RQL': self.RQL})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/x-ms-application,", "Connection": "Keep-Alive", "POST": params}
try:
conn.request('POST', red.aspconnecturl, params, headers)
self.redResponse = conn.getresponse().read()
self.redResponse = self.redResponse.decode('latin-1').encode('ascii', 'ignore')#handling unicode characters
self.err()
return self.redResponse
except Exception as ex:
raise ex
else:
if red.cached(self.getguid()):
self.redResponse = red.getcached(self.getguid()).decode('latin-1').encode('ascii', 'ignore')
return self.redResponse
else:
print 'new request'
conn = httplib.HTTPSConnection(red.host)
params = urllib.urlencode({'RQL': self.RQL})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/x-ms-application,", "Connection": "Keep-Alive", "POST": params}
try:
conn.request('POST', red.aspconnecturl, params, headers)
self.redResponse = conn.getresponse().read().decode('latin-1').encode('ascii', 'ignore')#handling unicode characters
self.err()
red.cache(self.getguid(), self.redResponse)
return self.redResponse
except Exception as ex:
raise ex
def setnode(nodein):
self.node = nodein
def setguid(self, guid_in):
self.guid = guid_in
def getguid(self):
return self.guid
def setrql(self, rql, padded=False):
if not padded:
self.RQL = '<IODATA loginguid="'+loginguid+'" sessionkey="'+sessionkey+'">'+rql+'</IODATA>'
else:
self.RQL = rql
def getrql(self):
return self.RQL
def err(self):
errorhappened = False
if self.redResponse is None:
print 'No response received from RedDot'
errorhappened = True
else:
try:
node = ET.fromstring(self.redResponse)
if node.tag == 'ERROR':
errorhappened = True
errcode = node.text.upper()
if red.RD_Error_Messages.has_key(errcode):
raise Exception( red.RD_Error_Messages[errcode] )
else:
raise Exception( errcode )
else:
pass
#print 'Unknown Error>>>\n' + self.redResponse
except Exception as ex:
errorhappened = True
print 'Warning: RedDot returned invalid xml:\n' + self.redResponse
pass
return errorhappened
class RedRequestLink(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<LINK guid="'+self.getguid()+'" action="load"><PAGES action="list" /><URL action="load" /></LINK>')
class RedRequestPage(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<PAGE guid="'+self.getguid()+'" action="load" option="extendedinfo" /><PAGE guid="'+self.getguid()+'"><ELEMENTS action="load" /><LINKS action="load" /></PAGE>')
class RedRequestElement(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<ELT guid="'+self.getguid()+'" action="load" subelements="1"><SELECTIONS action="list" /></ELT>')
#if has attr 'referenceelementguid' - must follow guid
#issue folder request on 'eltsrcsubdirguid' to get path
class RedRequestText(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<IODATA loginguid="'+loginguid+'" sessionkey="'+sessionkey+'" format="1"><PROJECT><TEXT action="load" guid="'+self.getguid()+'" texttype="1" /></PROJECT></IODATA>', True)
class RedRequestFolder(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<PROJECT><FOLDER action="load" guid="'+self.getguid()+'" /></PROJECT>')
class RedRequestReference(RedRequestObj):
def __init__(self, guid_in=''):
RedRequestObj.__init__(self, guid_in)
self.setrql('<TREESEGMENT action="gototreereference" guid="'+self.getguid()+'" type="link" />')