1 from __future__
import print_function
6 from urllib2
import HTTPError
13 print(
"Failed to import hashlib or md5")
20 import xml.etree.ElementTree
as etree
25 import elementtree.ElementTree
as etree
28 print(
"Failed to import ElementTree from any known place")
36 return repr(self.
value)
45 return '%s %s' % (self.
Code, self.
Body)
49 SignatureMethod =
"md5" 51 def __init__(self, url, username=None, password=None, xml_user=True):
60 m = hashlib.new(
'md5')
61 m.update(
"%s" % (random.randint(1,1234567890),))
66 text =
'%s:%s:%s' % (args, self.
_password, body)
72 elif method ==
'sha512':
84 req.add_header(
"X-Signature",
92 params = entry.xshow().strip()
94 req = urllib2.Request(url=self.
_url +
'/E/xml_post' +
95 '?' + args, data=params)
98 req.add_header(
"Content-type",
"text/xml")
99 if self._url.startswith(
"https:"):
100 req.add_header(
"X-Password", self.
_password)
103 response = urllib2.urlopen(req)
104 except HTTPError, err:
105 return err.code, err.msg, err.fp.read()
107 data = response.read()
110 return response.code, response.msg, data
115 def list(self, category=None, form=None, tag=None,
116 limit=
None, after=
None, substring=
None, text=
None):
117 args = [
'o=ids',
'salt=%s' % (self.
_make_salt(),)]
119 args.append(
'c=%s' % (urllib.quote_plus(category,)))
121 args.append(
'f=%s' % (urllib.quote_plus(form,)))
124 after = after.strptime(
'%Y-%m-%d %H:%M:%S')
125 args.append(
'a=%s' % (urllib.quote_plus(after),))
127 args.append(
't=%s' % (tag,))
129 args.append(
'l=%s' % (limit,))
131 args.append(
'st=%s' % (urllib.quote_plus(substring),))
133 args.append(
'si=%s' % (urllib.quote_plus(text),))
135 url = self.
_url +
'/E/xml_search' 136 args =
'&'.join(args)
140 req = urllib2.Request(url=url)
146 response = urllib2.urlopen(req)
147 except HTTPError, error:
148 raise ECLHTTPError(error.code, error.msg, error.fp.read())
149 tree = etree.parse(response)
150 entries = tree.findall(
'entry')
151 return [
int(e.attrib.get(
'id'))
for e
in entries]
154 args = [
'e=%s' % (eid,),
'salt=%s' % (self.
_make_salt(),)]
155 url = self.
_url +
'/E/xml_get' 156 args =
'&'.join(args)
159 req = urllib2.Request(url=url)
161 response = urllib2.urlopen(req)
162 return response.read()
168 def __init__(self, category, tags=[], formname='default', text='', preformatted=False,
169 private=
False, textile=
False, related=
None):
175 self.
_entry = etree.Element(
'entry', category=category)
177 self._entry.attrib[
'formatted']=
'yes' 179 self._entry.attrib[
'html_safe']=
'no' 181 self._entry.attrib[
'private'] =
'yes' 183 self._entry.attrib[
'related'] =
'%s' % (related,)
188 textfield = etree.SubElement(self.
_form,
'field', name=
'text')
190 textfield.text = text
192 etree.SubElement(self.
_entry,
'tag', name=tag)
196 field = etree.SubElement(self.
_form,
'field', name=name)
201 self._entry.attrib[
'author'] = name
205 field = etree.SubElement(self.
_entry,
'attachment', type=
'file', name=name, filename=os.path.basename(filename))
208 field.text = base64.b64encode(data)
210 f =
open(filename,
'r') 212 field.text = base64.b64encode(b) 217 field = etree.SubElement(self.
_entry,
'attachment', type=
'image', name=name, filename=os.path.basename(filename))
221 field.text = base64.b64encode(image)
223 f =
open(filename,
'r') 225 field.text = base64.b64encode(b) 229 self._entry.attrib[
'subject'] = text
232 return etree.tostring(self.
_entry)
234 if __name__ ==
'__main__':
239 search_substring =
None 241 opts, args = getopt.getopt(sys.argv[1:],
't:s:')
242 for opt, val
in opts:
243 if opt ==
'-t': search_text = val
244 if opt ==
'-s': search_substring = val
249 lst = c.list(text=search_text, substring=search_substring, limit=10)
def addAttachment(self, name, filename, data=None)
def _add_signature(self, req, args, body)
int open(const char *, int)
Opens a file descriptor.
def __init__(self, code, msg, body)
def setAuthor(self, name)
def _signature(self, method, args, body)
def list(self, category=None, form=None, tag=None, limit=None, after=None, substring=None, text=None)
def addSubject(self, text)
def addImage(self, name, filename, image=None)
def __init__(self, value)
def setValue(self, name, value)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
def __init__(self, url, username=None, password=None, xml_user=True)
def __init__(self, category, tags=[], formname='default', text='', preformatted=False, private=False, textile=False, related=None)