PEP8 compatibility

This commit is contained in:
yannickulrich 2016-01-18 09:38:14 +01:00
parent 9982477759
commit 420c9a783c

View file

@ -1,147 +1,147 @@
from __future__ import absolute_import from __future__ import absolute_import
from datetime import datetime, timedelta from datetime import datetime, timedelta
from calendar import monthrange
import time import time
import uuid import uuid
import pytz import pytz
import json import json
class RemindersService(object): class RemindersService(object):
def __init__(self, service_root, session, params): def __init__(self, service_root, session, params):
self.session = session self.session = session
self.params = params self.params = params
self._service_root = service_root self._service_root = service_root
self.lists={} self.lists = {}
self.collections = {} self.collections = {}
self.refresh()
def get_all_possible_timezones_of_local_machine(self):
"""
Return all possible timezones in Olson TZ notation
This has been taken from
http://stackoverflow.com/questions/7669938
"""
local_names = []
if time.daylight:
local_offset = time.altzone
localtz = time.tzname[1]
else:
local_offset = time.timezone
localtz = time.tzname[0]
local_offset = timedelta(seconds=-local_offset) self.refresh()
for name in pytz.all_timezones: def get_all_possible_timezones_of_local_machine(self):
timezone = pytz.timezone(name) """
if not hasattr(timezone, '_tzinfos'): Return all possible timezones in Olson TZ notation
continue This has been taken from
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.items(): http://stackoverflow.com/questions/7669938
if utcoffset == local_offset and tzname == localtz: """
local_names.append(name) local_names = []
return local_names if time.daylight:
local_offset = time.altzone
localtz = time.tzname[1]
else:
local_offset = time.timezone
localtz = time.tzname[0]
local_offset = timedelta(seconds=-local_offset)
for name in pytz.all_timezones:
timezone = pytz.timezone(name)
if not hasattr(timezone, '_tzinfos'):
continue
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.items():
if utcoffset == local_offset and tzname == localtz:
local_names.append(name)
return local_names
def get_system_tz(self): def get_system_tz(self):
""" """
Retrieves the system's timezone from a list of possible options. Retrieves the system's timezone from a list of possible options.
Just take the first one Just take the first one
""" """
return self.get_all_possible_timezones_of_local_machine()[0] return self.get_all_possible_timezones_of_local_machine()[0]
def refresh(self):
host = self._service_root.split('//')[1].split(':')[0]
self.session.headers.update({'host': host})
params_reminders = dict(self.params)
def refresh(self): params_reminders.update({
host = self._service_root.split('//')[1].split(':')[0] 'clientVersion': '4.0',
self.session.headers.update({'host': host}) 'lang': 'en-us',
'usertz': self.get_system_tz()
params_reminders=dict(self.params) })
params_reminders.update({
'clientVersion': '4.0',
'lang': 'en-us',
'usertz':self.get_system_tz()
})
# Open reminders # Open reminders
req = self.session.get( req = self.session.get(
self._service_root + '/rd/startup', self._service_root + '/rd/startup',
params=params_reminders params=params_reminders
) )
startup = req.json()
self.lists={}
self.collections = {}
for collection in startup['Collections']:
temp = []
self.collections[collection['title']] = {
'guid': collection['guid'],
'ctag': collection['ctag']
}
for reminder in startup['Reminders']:
if reminder['pGuid'] != collection['guid']:
continue
if reminder.has_key("dueDate"):
if reminder['dueDate']:
due = datetime(
reminder['dueDate'][1],
reminder['dueDate'][2],reminder['dueDate'][3],
reminder['dueDate'][4],reminder['dueDate'][5]
)
else:
due = None
else:
due = None
desc=reminder['description'] if reminder['description'] else ""
temp.append({
"title": reminder['title'],
"desc": desc,
"due": due
})
self.lists[collection['title']] = temp
def post(self, title, description = "", collection=None): startup = req.json()
pguid = 'tasks'
if collection:
if self.collections.has_key(collection):
pguid = self.collections[collection]['guid']
host = self._service_root.split('//')[1].split(':')[0]
self.session.headers.update({'host': host})
params_reminders=dict(self.params) self.lists = {}
params_reminders.update({ self.collections = {}
'clientVersion': '4.0', for collection in startup['Collections']:
'lang': 'en-us', temp = []
'usertz':self.get_system_tz() self.collections[collection['title']] = {
}) 'guid': collection['guid'],
'ctag': collection['ctag']
req = self.session.post( }
self._service_root + '/rd/reminders/tasks', for reminder in startup['Reminders']:
data=json.dumps({
"Reminders":{ if reminder['pGuid'] != collection['guid']:
'title': title, continue
"description":description, if 'dueDate' in reminder:
"pGuid":pguid, if reminder['dueDate']:
"etag":None, due = datetime(
"order":None, reminder['dueDate'][1],
"priority":0, reminder['dueDate'][2], reminder['dueDate'][3],
"recurrence":None, reminder['dueDate'][4], reminder['dueDate'][5]
"alarms":[], )
"startDate":None, else:
"startDateTz":None, due = None
"startDateIsAllDay":False, else:
"completedDate":None, due = None
"dueDate":None, if reminder['description']:
"dueDateIsAllDay":False, desc = reminder['description']
"lastModifiedDate":None, else:
"createdDate":None, desc = ""
"isFamily":None, temp.append({
"createdDateExtended":int(time.time()*1000), "title": reminder['title'],
"guid":str(uuid.uuid4()) "desc": desc,
}, "due": due
"ClientState": {"Collections": self.collections.values() } })
}), self.lists[collection['title']] = temp
params=params_reminders)
def post(self, title, description="", collection=None):
pguid = 'tasks'
if collection:
if collection in self.collections:
pguid = self.collections[collection]['guid']
host = self._service_root.split('//')[1].split(':')[0]
self.session.headers.update({'host': host})
params_reminders = dict(self.params)
params_reminders.update({
'clientVersion': '4.0',
'lang': 'en-us',
'usertz': self.get_system_tz()
})
req = self.session.post(
self._service_root + '/rd/reminders/tasks',
data=json.dumps({
"Reminders": {
'title': title,
"description": description,
"pGuid": pguid,
"etag": None,
"order": None,
"priority": 0,
"recurrence": None,
"alarms": [],
"startDate": None,
"startDateTz": None,
"startDateIsAllDay": False,
"completedDate": None,
"dueDate": None,
"dueDateIsAllDay": False,
"lastModifiedDate": None,
"createdDate": None,
"isFamily": None,
"createdDateExtended": int(time.time()*1000),
"guid": str(uuid.uuid4())
},
"ClientState": {"Collections": self.collections.values()}
}),
params=params_reminders)
return req.ok