Skip to content

Commit 1577572

Browse files
Dmitry V SelitskyDmitry V Selitsky
authored andcommitted
thanks Łukasz Rekucki (lqc) for more fixes
1 parent a16a04b commit 1577572

21 files changed

Lines changed: 949 additions & 1055 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
from distutils.core import setup
99

10-
required = ['pycrypto', 'tlslite']
10+
required = ['pycrypto', 'tlslite', 'lxml']
1111

1212
if sys.version_info[:3] < (2, 9, 0):
1313
raise NotImplemented('Python 3.5+ required, bye-bye')

src/atom/__init__.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,7 @@
3434

3535
# __author__ = 'api.jscudder (Jeffrey Scudder)'
3636

37-
try:
38-
from xml.etree import cElementTree as ElementTree
39-
except ImportError:
40-
try:
41-
import cElementTree as ElementTree
42-
except ImportError:
43-
try:
44-
from xml.etree import ElementTree
45-
except ImportError:
46-
from elementtree import ElementTree
37+
import lxml.etree as ElementTree
4738
import warnings
4839

4940
# XML namespaces which are often used in Atom entities.
@@ -334,7 +325,7 @@ def _BecomeChildElement(self, tree):
334325
not be called on instances of AtomBase.
335326
336327
"""
337-
new_child = ElementTree.Element('')
328+
new_child = ElementTree.Element('tag__')
338329
tree.append(new_child)
339330
new_child.tag = '{%s}%s' % (self.__class__._namespace,
340331
self.__class__._tag)
@@ -353,7 +344,7 @@ def _ToElementTree(self):
353344
self._AddMembersToElementTree(new_tree)
354345
return new_tree
355346

356-
def ToString(self, string_encoding='UTF-8'):
347+
def ToString(self, string_encoding=str):
357348
"""Converts the Atom object to a string containing XML."""
358349
return ElementTree.tostring(self._ToElementTree(), encoding=string_encoding)
359350

@@ -1350,7 +1341,7 @@ def __init__(self, tag, namespace=None, attributes=None,
13501341
self.text = text
13511342

13521343
def ToString(self):
1353-
element_tree = self._TransferToElementTree(ElementTree.Element(''))
1344+
element_tree = self._TransferToElementTree(ElementTree.Element('tag__'))
13541345
return ElementTree.tostring(element_tree, encoding="UTF-8")
13551346

13561347
def _TransferToElementTree(self, element_tree):
@@ -1382,7 +1373,7 @@ def _BecomeChildElement(self, element_tree):
13821373
element_tree: ElementTree._Element The element to which this object's XML
13831374
will be added.
13841375
"""
1385-
new_element = ElementTree.Element('')
1376+
new_element = ElementTree.Element('tag__') # uh, uhm... empty tag name - sorry google, this is bogus? (c)https://github.com/lqc
13861377
element_tree.append(new_element)
13871378
self._TransferToElementTree(new_element)
13881379

src/atom/core.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222

2323
import inspect
2424

25-
try:
26-
from xml.etree import cElementTree as ElementTree
27-
except ImportError:
28-
try:
29-
import cElementTree as ElementTree
30-
except ImportError:
31-
try:
32-
from xml.etree import ElementTree
33-
except ImportError:
34-
from elementtree import ElementTree
25+
import lxml.etree as ElementTree
3526

3627
try:
3728
from xml.dom.minidom import parseString as xmlString
@@ -295,12 +286,12 @@ def _harvest_tree(self, tree, version=1):
295286
if tree.text:
296287
self.text = tree.text
297288

298-
def _to_tree(self, version=1, encoding=None):
289+
def _to_tree(self, version=1):
299290
new_tree = ElementTree.Element(_get_qname(self, version))
300-
self._attach_members(new_tree, version, encoding)
291+
self._attach_members(new_tree, version)
301292
return new_tree
302293

303-
def _attach_members(self, tree, version=1, encoding=None):
294+
def _attach_members(self, tree, version=1):
304295
"""Convert members to XML elements/attributes and add them to the tree.
305296
306297
Args:
@@ -314,7 +305,7 @@ def _attach_members(self, tree, version=1, encoding=None):
314305
encoding: str (optional)
315306
"""
316307
qname, elements, attributes = self.__class__._get_rules(version)
317-
encoding = encoding or STRING_ENCODING
308+
encoding = STRING_ENCODING
318309
# Add the expected elements and attributes to the tree.
319310
if elements:
320311
for tag, element_def in elements.items():
@@ -348,7 +339,7 @@ def _attach_members(self, tree, version=1, encoding=None):
348339
def to_string(self, version=1, encoding=None, pretty_print=None):
349340
"""Converts this object to XML."""
350341

351-
tree_string = ElementTree.tostring(self._to_tree(version, encoding))
342+
tree_string = ElementTree.tostring(self._to_tree(version))
352343

353344
if pretty_print and xmlString is not None:
354345
return xmlString(tree_string).toprettyxml()
@@ -362,7 +353,7 @@ def __str__(self):
362353

363354
def _become_child(self, tree, version=1):
364355
"""Adds a child element to tree with the XML data in self."""
365-
new_child = ElementTree.Element('')
356+
new_child = ElementTree.Element(_get_qname(self, version))
366357
tree.append(new_child)
367358
new_child.tag = _get_qname(self, version)
368359
self._attach_members(new_child, version)
@@ -497,11 +488,11 @@ def _qname_matches(tag, namespace, qname):
497488
and member_namespace is None))
498489

499490

500-
def parse(xml_string, target_class=None, version=1, encoding=None):
491+
def parse(xml_string, target_class=None, version=1):
501492
"""Parses the XML string according to the rules for the target_class.
502493
503494
Args:
504-
xml_string: str or unicode
495+
xml_string: bytes
505496
target_class: XmlElement or a subclass. If None is specified, the
506497
XmlElement class is used.
507498
version: int (optional) The version of the schema which should be used when
@@ -511,11 +502,8 @@ def parse(xml_string, target_class=None, version=1, encoding=None):
511502
"""
512503
if target_class is None:
513504
target_class = XmlElement
514-
if isinstance(xml_string, str):
515-
if encoding is None:
516-
xml_string = xml_string.encode(STRING_ENCODING)
517-
else:
518-
xml_string = xml_string.encode(encoding)
505+
if not isinstance(xml_string, bytes):
506+
raise Exception("This function only accepts bytes")
519507
tree = ElementTree.fromstring(xml_string)
520508
return _xml_element_from_tree(tree, target_class, version)
521509

src/atom/http.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,18 @@ def request(self, operation, url, data=None, headers=None):
112112
return self.v2_http_client.request(http_request=http_request)
113113

114114
if not isinstance(url, atom.url.Url):
115-
if isinstance(url, (str,)):
115+
if isinstance(url, str):
116116
url = atom.url.parse_url(url)
117117
else:
118-
raise atom.http_interface.UnparsableUrlObject('Unable to parse url '
119-
'parameter because it was not a string or atom.url.Url')
118+
raise atom.http_interface.UnparsableUrlObject('Unable to parse url parameter because it was not a string or atom.url.Url')
120119

121120
connection = self._prepare_connection(url, all_headers)
122121

123122
if self.debug:
124123
connection.debuglevel = 1
125124

126-
connection.putrequest(operation, self._get_access_url(url),
127-
skip_host=True)
125+
connection.putrequest(operation, self._get_access_url(url), skip_host=True)
126+
128127
if url.port is not None:
129128
connection.putheader('Host', '%s:%s' % (url.host, url.port))
130129
else:

src/atom/http_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def add_form_inputs(self, form_data,
186186
to 'application/x-www-form-urlencoded'.
187187
"""
188188
body = urllib.parse.urlencode(form_data)
189-
self.add_body_part(body, mime_type)
189+
self.add_body_part(body, bytes(mime_type, 'ascii'))
190190

191191
AddFormInputs = add_form_inputs
192192

src/atom/service.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@
3333
import atom.token_store
3434
import atom.url
3535

36-
try:
37-
from xml.etree import cElementTree as ElementTree
38-
except ImportError:
39-
try:
40-
import cElementTree as ElementTree
41-
except ImportError:
42-
try:
43-
from xml.etree import ElementTree
44-
except ImportError:
45-
from elementtree import ElementTree
36+
import lxml.etree as ElementTree
4637
import atom
4738

4839

src/gdata/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@
1616

1717
import atom
1818

19-
try:
20-
from xml.etree import cElementTree as ElementTree
21-
except ImportError:
22-
try:
23-
import cElementTree as ElementTree
24-
except ImportError:
25-
try:
26-
from xml.etree import ElementTree
27-
except ImportError:
28-
from elementtree import ElementTree
19+
import lxml.etree as ElementTree
2920

3021
# XML namespaces which are often used in GData entities.
3122
GDATA_NAMESPACE = 'http://schemas.google.com/g/2005'

src/gdata/apps/service.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66

77
# __author__ = 'tmatsuo@sios.com (Takashi MATSUO)'
88

9-
try:
10-
from xml.etree import cElementTree as ElementTree
11-
except ImportError:
12-
try:
13-
import cElementTree as ElementTree
14-
except ImportError:
15-
try:
16-
from xml.etree import ElementTree
17-
except ImportError:
18-
from elementtree import ElementTree
9+
import lxml.etree as ElementTree
1910
import gdata
2011
import gdata.apps
2112
import gdata.service

src/gdata/calendar/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@
99

1010
# __author__ = 'api.vli (Vivian Li), api.rboyd (Ryan Boyd)'
1111

12-
try:
13-
from xml.etree import cElementTree as ElementTree
14-
except ImportError:
15-
try:
16-
import cElementTree as ElementTree
17-
except ImportError:
18-
try:
19-
from xml.etree import ElementTree
20-
except ImportError:
21-
from elementtree import ElementTree
12+
import lxml.etree as ElementTree
2213
import atom
2314
import gdata
2415

src/gdata/gauth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ def __init__(self, token_string):
282282
self.token_string = token_string
283283

284284
def modify_request(self, http_request):
285-
http_request.headers['Authorization'] = '%s%s' % (PROGRAMMATIC_AUTH_LABEL,
286-
self.token_string)
285+
http_request.headers['Authorization'] = '{}{}'.format(PROGRAMMATIC_AUTH_LABEL, str(self.token_string, 'ascii'))
287286

288287
ModifyRequest = modify_request
289288

0 commit comments

Comments
 (0)