Skip to content

Commit ab526dd

Browse files
author
James Watt
committed
run 2to3 and fix types in contacts client
1 parent 3541dfa commit ab526dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+980
-984
lines changed

src/atom/__init__.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def optional_warn_function(*args, **kwargs):
9393
# Preserve the original name to avoid masking all decorated functions as
9494
# 'deprecated_function'
9595
try:
96-
optional_warn_function.func_name = f.func_name
96+
optional_warn_function.__name__ = f.__name__
9797
except TypeError:
9898
pass # In Python2.3 we can't set the func_name
9999
return optional_warn_function
@@ -121,7 +121,7 @@ def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
121121
match those of the target class.
122122
"""
123123
encoding = string_encoding or XML_STRING_ENCODING
124-
if encoding and isinstance(xml_string, unicode):
124+
if encoding and isinstance(xml_string, str):
125125
xml_string = xml_string.encode(encoding)
126126
tree = ElementTree.fromstring(xml_string)
127127
return _CreateClassFromElementTree(target_class, tree)
@@ -183,11 +183,11 @@ def _HarvestElementTree(self, tree):
183183
# Fill in the instance members from the contents of the XML tree.
184184
for child in tree:
185185
self._ConvertElementTreeToMember(child)
186-
for attribute, value in tree.attrib.iteritems():
186+
for attribute, value in tree.attrib.items():
187187
self._ConvertElementAttributeToMember(attribute, value)
188188
# Encode the text string according to the desired encoding type. (UTF-8)
189189
if tree.text:
190-
if MEMBER_STRING_ENCODING is unicode:
190+
if MEMBER_STRING_ENCODING is str:
191191
self.text = tree.text
192192
else:
193193
self.text = tree.text.encode(MEMBER_STRING_ENCODING)
@@ -199,7 +199,7 @@ def _ConvertElementTreeToMember(self, child_tree, current_class=None):
199199
def _ConvertElementAttributeToMember(self, attribute, value):
200200
# Encode the attribute value's string with the desired type Default UTF-8
201201
if value:
202-
if MEMBER_STRING_ENCODING is unicode:
202+
if MEMBER_STRING_ENCODING is str:
203203
self.extension_attributes[attribute] = value
204204
else:
205205
self.extension_attributes[attribute] = value.encode(
@@ -209,15 +209,15 @@ def _ConvertElementAttributeToMember(self, attribute, value):
209209
def _AddMembersToElementTree(self, tree):
210210
for child in self.extension_elements:
211211
child._BecomeChildElement(tree)
212-
for attribute, value in self.extension_attributes.iteritems():
212+
for attribute, value in self.extension_attributes.items():
213213
if value:
214-
if isinstance(value, unicode) or MEMBER_STRING_ENCODING is unicode:
214+
if isinstance(value, str) or MEMBER_STRING_ENCODING is str:
215215
tree.attrib[attribute] = value
216216
else:
217217
# Decode the value from the desired encoding (default UTF-8).
218218
tree.attrib[attribute] = value.decode(MEMBER_STRING_ENCODING)
219219
if self.text:
220-
if isinstance(self.text, unicode) or MEMBER_STRING_ENCODING is unicode:
220+
if isinstance(self.text, str) or MEMBER_STRING_ENCODING is str:
221221
tree.text = self.text
222222
else:
223223
tree.text = self.text.decode(MEMBER_STRING_ENCODING)
@@ -278,7 +278,7 @@ def __init__(self, extension_elements=None, extension_attributes=None,
278278

279279
def _ConvertElementTreeToMember(self, child_tree):
280280
# Find the element's tag in this class's list of child members
281-
if self.__class__._children.has_key(child_tree.tag):
281+
if child_tree.tag in self.__class__._children:
282282
member_name = self.__class__._children[child_tree.tag][0]
283283
member_class = self.__class__._children[child_tree.tag][1]
284284
# If the class member is supposed to contain a list, make sure the
@@ -297,13 +297,13 @@ def _ConvertElementTreeToMember(self, child_tree):
297297

298298
def _ConvertElementAttributeToMember(self, attribute, value):
299299
# Find the attribute in this class's list of attributes.
300-
if self.__class__._attributes.has_key(attribute):
300+
if attribute in self.__class__._attributes:
301301
# Find the member of this class which corresponds to the XML attribute
302302
# (lookup in current_class._attributes) and set this member to the
303303
# desired value (using self.__dict__).
304304
if value:
305305
# Encode the string to capture non-ascii characters (default UTF-8)
306-
if MEMBER_STRING_ENCODING is unicode:
306+
if MEMBER_STRING_ENCODING is str:
307307
setattr(self, self.__class__._attributes[attribute], value)
308308
else:
309309
setattr(self, self.__class__._attributes[attribute],
@@ -318,7 +318,7 @@ def _AddMembersToElementTree(self, tree):
318318
# This uses the class's _children dictionary to find the members which
319319
# should become XML child nodes.
320320
member_node_names = [values[0] for tag, values in
321-
self.__class__._children.iteritems()]
321+
self.__class__._children.items()]
322322
for member_name in member_node_names:
323323
member = getattr(self, member_name)
324324
if member is None:
@@ -329,10 +329,10 @@ def _AddMembersToElementTree(self, tree):
329329
else:
330330
member._BecomeChildElement(tree)
331331
# Convert the members of this class which are XML attributes.
332-
for xml_attribute, member_name in self.__class__._attributes.iteritems():
332+
for xml_attribute, member_name in self.__class__._attributes.items():
333333
member = getattr(self, member_name)
334334
if member is not None:
335-
if isinstance(member, unicode) or MEMBER_STRING_ENCODING is unicode:
335+
if isinstance(member, str) or MEMBER_STRING_ENCODING is str:
336336
tree.attrib[xml_attribute] = member
337337
else:
338338
tree.attrib[xml_attribute] = member.decode(MEMBER_STRING_ENCODING)
@@ -1374,7 +1374,7 @@ def _TransferToElementTree(self, element_tree):
13741374
else:
13751375
element_tree.tag = self.tag
13761376

1377-
for key, value in self.attributes.iteritems():
1377+
for key, value in self.attributes.items():
13781378
element_tree.attrib[key] = value
13791379

13801380
for child in self.children:
@@ -1451,7 +1451,7 @@ def _ExtensionElementFromElementTree(element_tree):
14511451
namespace = None
14521452
tag = element_tag
14531453
extension = ExtensionElement(namespace=namespace, tag=tag)
1454-
for key, value in element_tree.attrib.iteritems():
1454+
for key, value in element_tree.attrib.items():
14551455
extension.attributes[key] = value
14561456
for child in element_tree:
14571457
extension.children.append(_ExtensionElementFromElementTree(child))
@@ -1475,7 +1475,7 @@ def deprecated_function(*args, **kwargs):
14751475
# Preserve the original name to avoid masking all decorated functions as
14761476
# 'deprecated_function'
14771477
try:
1478-
deprecated_function.func_name = f.func_name
1478+
deprecated_function.__name__ = f.__name__
14791479
except TypeError:
14801480
# Setting the func_name is not allowed in Python2.3.
14811481
pass

src/atom/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ def request(self, method=None, uri=None, auth_token=None,
8585
# Modify the request based on the AtomPubClient settings and parameters
8686
# passed in to the request.
8787
http_request = self.modify_request(http_request)
88-
if isinstance(uri, (str, unicode)):
88+
if isinstance(uri, str):
8989
uri = atom.http_core.Uri.parse_uri(uri)
9090
if uri is not None:
9191
uri.modify_request(http_request)
92-
if isinstance(method, (str, unicode)):
92+
if isinstance(method, str):
9393
http_request.method = method
9494
# Any unrecognized arguments are assumed to be capable of modifying the
9595
# HTTP request.
96-
for name, value in kwargs.iteritems():
96+
for name, value in kwargs.items():
9797
if value is not None:
9898
if hasattr(value, 'modify_request'):
9999
value.modify_request(http_request)
@@ -218,7 +218,7 @@ def modify_request(self, http_request):
218218
An atom.http_core.HttpRequest() with the added custom headers.
219219
"""
220220

221-
for name, value in self.headers.iteritems():
221+
for name, value in self.headers.items():
222222
if value is not None:
223223
http_request.headers[name] = value
224224
return http_request

src/atom/core.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _list_xml_members(cls):
9393
if not pair[0].startswith('_') and pair[0] != 'text':
9494
member_type = pair[1]
9595
if (isinstance(member_type, tuple) or isinstance(member_type, list)
96-
or isinstance(member_type, (str, unicode))
96+
or isinstance(member_type, str)
9797
or (inspect.isclass(member_type)
9898
and issubclass(member_type, XmlElement))):
9999
members.append(pair)
@@ -173,7 +173,7 @@ def _get_rules(cls, version):
173173
attributes[target[version-1]] = member_name
174174
else:
175175
attributes[target[-1]] = member_name
176-
elif isinstance(target, (str, unicode)):
176+
elif isinstance(target, str):
177177
# This member points to an XML attribute.
178178
attributes[target] = member_name
179179
elif issubclass(target, XmlElement):
@@ -207,7 +207,7 @@ def get_elements(self, tag=None, namespace=None, version=1):
207207
matches = []
208208
ignored1, elements, ignored2 = self.__class__._get_rules(version)
209209
if elements:
210-
for qname, element_def in elements.iteritems():
210+
for qname, element_def in elements.items():
211211
member = getattr(self, element_def[0])
212212
if member:
213213
if _qname_matches(tag, namespace, qname):
@@ -253,7 +253,7 @@ def get_attributes(self, tag=None, namespace=None, version=1):
253253
matches = []
254254
ignored1, ignored2, attributes = self.__class__._get_rules(version)
255255
if attributes:
256-
for qname, attribute_def in attributes.iteritems():
256+
for qname, attribute_def in attributes.items():
257257
if isinstance(attribute_def, (list, tuple)):
258258
attribute_def = attribute_def[0]
259259
member = getattr(self, attribute_def)
@@ -262,7 +262,7 @@ def get_attributes(self, tag=None, namespace=None, version=1):
262262
if member:
263263
if _qname_matches(tag, namespace, qname):
264264
matches.append(XmlAttribute(qname, member))
265-
for qname, value in self._other_attributes.iteritems():
265+
for qname, value in self._other_attributes.items():
266266
if _qname_matches(tag, namespace, qname):
267267
matches.append(XmlAttribute(qname, value))
268268
return matches
@@ -288,7 +288,7 @@ def _harvest_tree(self, tree, version=1):
288288
else:
289289
self._other_elements.append(_xml_element_from_tree(element, XmlElement,
290290
version))
291-
for attrib, value in tree.attrib.iteritems():
291+
for attrib, value in tree.attrib.items():
292292
if attributes and attrib in attributes:
293293
setattr(self, attributes[attrib], value)
294294
else:
@@ -318,7 +318,7 @@ def _attach_members(self, tree, version=1, encoding=None):
318318
encoding = encoding or STRING_ENCODING
319319
# Add the expected elements and attributes to the tree.
320320
if elements:
321-
for tag, element_def in elements.iteritems():
321+
for tag, element_def in elements.items():
322322
member = getattr(self, element_def[0])
323323
# If this is a repeating element and there are members in the list.
324324
if member and element_def[2]:
@@ -327,21 +327,21 @@ def _attach_members(self, tree, version=1, encoding=None):
327327
elif member:
328328
member._become_child(tree, version)
329329
if attributes:
330-
for attribute_tag, member_name in attributes.iteritems():
330+
for attribute_tag, member_name in attributes.items():
331331
value = getattr(self, member_name)
332332
if value:
333333
tree.attrib[attribute_tag] = value
334334
# Add the unexpected (other) elements and attributes to the tree.
335335
for element in self._other_elements:
336336
element._become_child(tree, version)
337-
for key, value in self._other_attributes.iteritems():
337+
for key, value in self._other_attributes.items():
338338
# I'm not sure if unicode can be used in the attribute name, so for now
339339
# we assume the encoding is correct for the attribute name.
340-
if not isinstance(value, unicode):
340+
if not isinstance(value, str):
341341
value = value.decode(encoding)
342342
tree.attrib[key] = value
343343
if self.text:
344-
if isinstance(self.text, unicode):
344+
if isinstance(self.text, str):
345345
tree.text = self.text
346346
else:
347347
tree.text = self.text.decode(encoding)
@@ -512,7 +512,7 @@ def parse(xml_string, target_class=None, version=1, encoding=None):
512512
"""
513513
if target_class is None:
514514
target_class = XmlElement
515-
if isinstance(xml_string, unicode):
515+
if isinstance(xml_string, str):
516516
if encoding is None:
517517
xml_string = xml_string.encode(STRING_ENCODING)
518518
else:

src/atom/http.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import types
3939
import os
40-
import httplib
40+
import http.client
4141
import atom.url
4242
import atom.http_interface
4343
import socket
@@ -103,7 +103,7 @@ def request(self, operation, url, data=None, headers=None):
103103
# If the list of headers does not include a Content-Length, attempt to
104104
# calculate it based on the data object.
105105
if data and 'Content-Length' not in all_headers:
106-
if isinstance(data, types.StringTypes):
106+
if isinstance(data, (str,)):
107107
all_headers['Content-Length'] = str(len(data))
108108
else:
109109
raise atom.http_interface.ContentLengthRequired('Unable to calculate '
@@ -123,7 +123,7 @@ def request(self, operation, url, data=None, headers=None):
123123
return self.v2_http_client.request(http_request=http_request)
124124

125125
if not isinstance(url, atom.url.Url):
126-
if isinstance(url, types.StringTypes):
126+
if isinstance(url, (str,)):
127127
url = atom.url.parse_url(url)
128128
else:
129129
raise atom.http_interface.UnparsableUrlObject('Unable to parse url '
@@ -175,19 +175,19 @@ def request(self, operation, url, data=None, headers=None):
175175

176176
def _prepare_connection(self, url, headers):
177177
if not isinstance(url, atom.url.Url):
178-
if isinstance(url, types.StringTypes):
178+
if isinstance(url, (str,)):
179179
url = atom.url.parse_url(url)
180180
else:
181181
raise atom.http_interface.UnparsableUrlObject('Unable to parse url '
182182
'parameter because it was not a string or atom.url.Url')
183183
if url.protocol == 'https':
184184
if not url.port:
185-
return httplib.HTTPSConnection(url.host)
186-
return httplib.HTTPSConnection(url.host, int(url.port))
185+
return http.client.HTTPSConnection(url.host)
186+
return http.client.HTTPSConnection(url.host, int(url.port))
187187
else:
188188
if not url.port:
189-
return httplib.HTTPConnection(url.host)
190-
return httplib.HTTPConnection(url.host, int(url.port))
189+
return http.client.HTTPConnection(url.host)
190+
return http.client.HTTPConnection(url.host, int(url.port))
191191

192192
def _get_access_url(self, url):
193193
return url.to_string()
@@ -259,10 +259,10 @@ def _prepare_connection(self, url, headers):
259259
sslobj = ssl.wrap_socket(p_sock, None, None)
260260
else:
261261
sock_ssl = socket.ssl(p_sock, None, None)
262-
sslobj = httplib.FakeSocket(p_sock, sock_ssl)
262+
sslobj = http.client.FakeSocket(p_sock, sock_ssl)
263263

264264
# Initalize httplib and replace with the proxy socket.
265-
connection = httplib.HTTPConnection(proxy_url.host)
265+
connection = http.client.HTTPConnection(proxy_url.host)
266266
connection.sock = sslobj
267267
return connection
268268
else:
@@ -275,7 +275,7 @@ def _prepare_connection(self, url, headers):
275275
if proxy_auth:
276276
headers['Proxy-Authorization'] = proxy_auth.strip()
277277

278-
return httplib.HTTPConnection(proxy_url.host, int(proxy_url.port))
278+
return http.client.HTTPConnection(proxy_url.host, int(proxy_url.port))
279279

280280
def _get_access_url(self, url):
281281
return url.to_string()
@@ -343,7 +343,7 @@ def _get_proxy_net_location(proxy_settings):
343343

344344

345345
def _send_data_part(data, connection):
346-
if isinstance(data, types.StringTypes):
346+
if isinstance(data, (str,)):
347347
connection.send(data)
348348
return
349349
# Check to see if data is a file-like object that has a read method.

0 commit comments

Comments
 (0)