Skip to content

Commit

Permalink
Use six to make utils/services Python 3 compatible (#69)
Browse files Browse the repository at this point in the history
* Use ensure_binary to make xml data Python 3 compatible
* Add ensure_text to string concat in _parse_envelopes
* Add ensure_text for stdout
* Sort event_types for consistency
  • Loading branch information
andrew-jung authored Sep 1, 2021
1 parent 027875a commit 831ced9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 4 additions & 3 deletions exchangelib/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from sys import stdout
import traceback

from six import text_type
from six import ensure_text, text_type

from . import errors
from .errors import EWSWarning, TransportError, SOAPError, ErrorTimeoutExpired, ErrorBatchProcessingStopped, \
Expand Down Expand Up @@ -270,7 +270,7 @@ def _write_xml(self, ftype, req_id, xml_str):
elif ftype == 'streaming-response':
stdout.write(u'STREAMING RESPONSE {} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< {}\n'.format(req_id, now))

stdout.write(PrettyXmlHandler.prettify_xml(xml_str) + b'\n')
stdout.write(ensure_text(PrettyXmlHandler.prettify_xml(xml_str) + b'\n'))

def _parse_envelopes(self, response):
try:
Expand All @@ -279,6 +279,7 @@ def _parse_envelopes(self, response):
for chunk in response.iter_content():
if not chunk:
continue
chunk = ensure_text(chunk)
curr_envelope += chunk
index = curr_envelope.find(envelope_str)
if index == -1:
Expand Down Expand Up @@ -1577,7 +1578,7 @@ def get_payload(self, folders, event_types):
assert event_type in CONCRETE_EVENT_TYPES
deduped_event_types.add(event_type.ELEMENT_NAME)

for event_type_name in deduped_event_types:
for event_type_name in sorted(deduped_event_types):
if event_type_name == 'StatusEvent':
continue
event_type_elem = create_element('t:EventType')
Expand Down
7 changes: 3 additions & 4 deletions exchangelib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import requests.auth
import requests.exceptions
from requests import Request
from six import text_type, string_types
from six import ensure_binary, string_types, text_type

from .errors import TransportError, RateLimitError, RedirectError, RelativeRedirect, CASError, UnauthorizedError, \
InvalidTokenError, ErrorInvalidSchemaVersionForMailboxVersion
Expand Down Expand Up @@ -296,8 +296,7 @@ def parse_bytes(xml_bytes):
@classmethod
def prettify_xml(cls, xml_bytes):
# Re-formats an XML document to a consistent style
if isinstance(xml_bytes, unicode):
xml_bytes = xml_bytes.encode('utf-8')
xml_bytes = ensure_binary(xml_bytes)
return tostring(
cls.parse_bytes(xml_bytes),
xml_declaration=True,
Expand Down Expand Up @@ -512,7 +511,7 @@ def post_ratelimited(protocol, session, url, headers, data, allow_redirects=Fals
# Always create a dummy response for logging purposes, in case we fail in the following
r = DummyResponse(url=url, headers={}, request_headers=headers)
try:
data = data.encode('utf-8')
data = ensure_binary(data)
except UnicodeDecodeError:
try:
data = data.decode('utf-8').encode('utf-8')
Expand Down

0 comments on commit 831ced9

Please sign in to comment.