From b3cc67c01fd04976c08ba86d881afdebda9032bc Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 11 Oct 2013 18:17:23 -0700 Subject: [PATCH 1/5] Removing relative imports. --- pyicloud/__init__.py | 2 +- pyicloud/base.py | 4 ++-- pyicloud/services/__init__.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyicloud/__init__.py b/pyicloud/__init__.py index ede4fbb..a47350d 100644 --- a/pyicloud/__init__.py +++ b/pyicloud/__init__.py @@ -1 +1 @@ -from base import PyiCloudService +from pyicloud.base import PyiCloudService diff --git a/pyicloud/base.py b/pyicloud/base.py index e700c73..3e02044 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -4,8 +4,8 @@ import hashlib import json import requests -from exceptions import PyiCloudFailedLoginException -from services import ( +from pyicloud.exceptions import PyiCloudFailedLoginException +from pyicloud.services import ( FindMyiPhoneServiceManager, CalendarService, UbiquityService diff --git a/pyicloud/services/__init__.py b/pyicloud/services/__init__.py index fdafa36..3911d9b 100644 --- a/pyicloud/services/__init__.py +++ b/pyicloud/services/__init__.py @@ -1,3 +1,3 @@ -from calendar import CalendarService -from findmyiphone import FindMyiPhoneServiceManager -from ubiquity import UbiquityService +from pyicloud.services.calendar import CalendarService +from pyicloud.services.findmyiphone import FindMyiPhoneServiceManager +from pyicloud.services.ubiquity import UbiquityService From af9f53eca66e962725d77dec63e2e454bac0685e Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 11 Oct 2013 18:25:47 -0700 Subject: [PATCH 2/5] Handling __str__ and __unicode__ in a Python2/3-agnostic manner. --- pyicloud/base.py | 9 +++++++-- pyicloud/services/findmyiphone.py | 21 ++++++++++++++++----- pyicloud/services/ubiquity.py | 11 ++++++++--- requirements.txt | 1 + 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index 3e02044..2e3b75a 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -3,6 +3,7 @@ import uuid import hashlib import json import requests +import sys from pyicloud.exceptions import PyiCloudFailedLoginException from pyicloud.services import ( @@ -118,10 +119,14 @@ class PyiCloudService(object): return CalendarService(service_root, self.session, self.params) def __unicode__(self): - return u'iCloud API: %s' % self.user.get('apple_id') + return 'iCloud API: %s' % self.user.get('apple_id') def __str__(self): - return unicode(self).encode('ascii', 'ignore') + as_unicode = self.__unicode__() + if sys.version_info[0] >= 3: + return as_unicode + else: + return as_unicode.encode('ascii', 'ignore') def __repr__(self): return '<%s>' % str(self) diff --git a/pyicloud/services/findmyiphone.py b/pyicloud/services/findmyiphone.py index 02e8fad..7a0a14c 100755 --- a/pyicloud/services/findmyiphone.py +++ b/pyicloud/services/findmyiphone.py @@ -1,4 +1,7 @@ import json +import sys + +import six from pyicloud.exceptions import PyiCloudNoDevicesException @@ -62,13 +65,17 @@ class FindMyiPhoneServiceManager(object): return getattr(self._devices, attr) def __unicode__(self): - return unicode(self._devices) + return six.text_type(self._devices) def __str__(self): - return unicode(self).encode('ascii', 'ignore') + as_unicode = self.__unicode__() + if sys.version_info[0] >= 3: + return as_unicode + else: + return as_unicode.encode('ascii', 'ignore') def __repr__(self): - return str(self) + return six.text_type(self) class AppleDevice(object): @@ -169,13 +176,17 @@ class AppleDevice(object): def __unicode__(self): display_name = self['deviceDisplayName'] name = self['name'] - return u'%s: %s' % ( + return '%s: %s' % ( display_name, name, ) def __str__(self): - return unicode(self).encode('ascii', 'ignore') + as_unicode = self.__unicode__() + if sys.version_info[0] >= 3: + return as_unicode + else: + return as_unicode.encode('ascii', 'ignore') def __repr__(self): return '' % str(self) diff --git a/pyicloud/services/ubiquity.py b/pyicloud/services/ubiquity.py index 5ba0150..b04ae08 100644 --- a/pyicloud/services/ubiquity.py +++ b/pyicloud/services/ubiquity.py @@ -1,4 +1,5 @@ from datetime import datetime +import sys class UbiquityService(object): @@ -108,10 +109,14 @@ class UbiquityNode(object): return self.name def __str__(self): - return self.name.encode('unicode-escape') + as_unicode = self.__unicode__() + if sys.version_info[0] >= 3: + return as_unicode + else: + return as_unicode.encode('ascii', 'ignore') def __repr__(self): - return "<%s: u'%s'>" % ( + return "<%s: '%s'>" % ( self.type.capitalize(), - str(self) + self ) diff --git a/requirements.txt b/requirements.txt index a167244..ed7dc8f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests>=1.2 +six From 565146ac3dc532ba96be6cc95a059ec94a239f0c Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 11 Oct 2013 18:29:15 -0700 Subject: [PATCH 3/5] Encode to utf-8 before hashing. --- pyicloud/base.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index 2e3b75a..b19ba4e 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -64,8 +64,13 @@ class PyiCloudService(object): if 'dsInfo' in resp: dsid = resp['dsInfo']['dsid'] self.params.update({'dsid': dsid}) - instance = resp.get('instance', uuid.uuid4().hex) - sha = hashlib.sha1(self.user.get('apple_id') + instance) + instance = resp.get( + 'instance', + uuid.uuid4().hex.encode('utf-8') + ) + sha = hashlib.sha1( + self.user.get('apple_id').encode('utf-8') + instance + ) self.params.update({'id': sha.hexdigest().upper()}) def authenticate(self): From 3d87dac040da97279bdf8c6d9b73b636d1330149 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 11 Oct 2013 18:33:07 -0700 Subject: [PATCH 4/5] PEP-8 style fixes; removing missing imports; fixing ValueError when raising PyiCloudNoDevicesException --- pyicloud/base.py | 7 ++++-- pyicloud/services/findmyiphone.py | 36 ++++++++++++++++++++----------- pyicloud/services/ubiquity.py | 4 +++- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index b19ba4e..23e774e 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -1,4 +1,3 @@ -import time import uuid import hashlib import json @@ -115,7 +114,11 @@ class PyiCloudService(object): def files(self): if not hasattr(self, '_files'): service_root = self.webservices['ubiquity']['url'] - self._files = UbiquityService(service_root, self.session, self.params) + self._files = UbiquityService( + service_root, + self.session, + self.params + ) return self._files @property diff --git a/pyicloud/services/findmyiphone.py b/pyicloud/services/findmyiphone.py index 7a0a14c..29cfb5e 100755 --- a/pyicloud/services/findmyiphone.py +++ b/pyicloud/services/findmyiphone.py @@ -54,7 +54,7 @@ class FindMyiPhoneServiceManager(object): self._devices[device_id].update(device_info) if not self._devices: - raise PyiCloudNoDevicesException(message) + raise PyiCloudNoDevicesException() def __getitem__(self, key): if isinstance(key, int): @@ -79,8 +79,10 @@ class FindMyiPhoneServiceManager(object): class AppleDevice(object): - def __init__(self, content, session, params, manager, - sound_url=None, lost_url=None, message_url=None): + def __init__( + self, content, session, params, manager, + sound_url=None, lost_url=None, message_url=None + ): self.content = content self.manager = manager self.session = session @@ -122,26 +124,34 @@ class AppleDevice(object): data=data ) - def display_message (self, subject='Find My iPhone Alert', message="This is a note", sounds=False): + def display_message( + self, subject='Find My iPhone Alert', message="This is a note", + sounds=False + ): """ Send a request to the device to play a sound. It's possible to pass a custom message by changing the `subject`. """ - data = json.dumps({'device': self.content['id'], - 'subject': subject, - 'sound':sounds, - 'userText':True, - 'text':message - }) + data = json.dumps( + { + 'device': self.content['id'], + 'subject': subject, + 'sound': sounds, + 'userText': True, + 'text': message + } + ) self.session.post( self.message_url, params=self.params, data=data ) - def lost_device(self, number, - text='This iPhone has been lost. Please call me.', - newpasscode=""): + def lost_device( + self, number, + text='This iPhone has been lost. Please call me.', + newpasscode="" + ): """ Send a request to the device to trigger 'lost mode'. The device will show the message in `text`, and if a number has diff --git a/pyicloud/services/ubiquity.py b/pyicloud/services/ubiquity.py index b04ae08..86878e2 100644 --- a/pyicloud/services/ubiquity.py +++ b/pyicloud/services/ubiquity.py @@ -97,7 +97,9 @@ class UbiquityNode(object): return self.connection.get_file(self.item_id, **kwargs) def get(self, name): - return [child for child in self.get_children() if child.name == name][0] + return [ + child for child in self.get_children() if child.name == name + ][0] def __getitem__(self, key): try: From 6b02b599672d4588dafaa329d0bf72e756d62494 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Fri, 11 Oct 2013 18:35:26 -0700 Subject: [PATCH 5/5] Bumping version number; adding trove classifiers (including those for python-version limitations). --- setup.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 139a6b7..87420f2 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('requirements.txt') as f: setup( name='pyicloud', - version='0.2.1', + version='0.3.0', url='https://github.com/picklepete/pyicloud', description=( 'PyiCloud is a module which allows pythonistas to ' @@ -16,5 +16,13 @@ setup( author='Peter Evans', author_email='evans.peter@gmail.com', packages=find_packages(), - install_requires=required + install_requires=required, + classifiers=[ + 'Intended Audience :: Developers', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + ], )