Merge pull request #11 from latestrevision/python-3-support

Add Python3 Support!
This commit is contained in:
Peter Evans 2013-10-12 13:47:33 -07:00
commit 79751fbbd1
7 changed files with 86 additions and 36 deletions

View file

@ -1 +1 @@
from base import PyiCloudService
from pyicloud.base import PyiCloudService

View file

@ -1,11 +1,11 @@
import time
import uuid
import hashlib
import json
import requests
import sys
from exceptions import PyiCloudFailedLoginException
from services import (
from pyicloud.exceptions import PyiCloudFailedLoginException
from pyicloud.services import (
FindMyiPhoneServiceManager,
CalendarService,
UbiquityService
@ -63,8 +63,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):
@ -109,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
@ -118,10 +127,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)

View file

@ -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

View file

@ -1,4 +1,7 @@
import json
import sys
import six
from pyicloud.exceptions import PyiCloudNoDevicesException
@ -51,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):
@ -62,18 +65,24 @@ 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):
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
@ -115,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
@ -169,13 +186,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 '<AppleDevice(%s)>' % str(self)

View file

@ -1,4 +1,5 @@
from datetime import datetime
import sys
class UbiquityService(object):
@ -96,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:
@ -108,10 +111,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
)

View file

@ -1 +1,2 @@
requests>=1.2
six

View file

@ -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',
],
)