Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iso8601==0.1.10
7 changes: 6 additions & 1 deletion toggl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def __init__(self, api, **kwargs):
self._update_attrs(kwargs)

def _update_attrs(self, attrs):
for k, v in attrs.iteritems():
try:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid checking for items/iteritems everywhere, let's just use items everywhere (it works in both 2 and 3, and for the amount of data that's likely to be handled here, just as well).

iteritems = dict.items # Python 3
except AttributeError:
iteritems = dict.iteritems # Python 2

for k, v in iteritems(attrs):
try:
v = iso8601.parse_date(v)
attrs[k] = v
Expand Down
55 changes: 35 additions & 20 deletions toggl/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .session import Session

import sys

__all__ = ['Reports']


Expand All @@ -16,31 +18,36 @@ def parse_val(v):
else:
return v

for k, v in attribs.iteritems():
try:
iteritems = dict.items # Python 3
except AttributeError:
iteritems = dict.iteritems # Python 2

for k, v in iteritems(attribs):
if isinstance(v, list):
attribs[k] = [parse_val(el) for el in v]
elif isinstance(v, dict):
attribs[k] = parse_val(v)
self.__dict__.update(attribs)

@property
def keys(self):
return self.__dict__.keys()

def __unicode__(self):
if hasattr(self, '_unicode_value'):
if self._unicode_value is None:
return '(none)'
if sys.version_info[0] < 3:
@property
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be missing (not defined) if Python3 is used.

def keys(self):
return self.__dict__.keys()

if sys.version_info[0] < 3:
def __unicode__(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it'd be cleaner if six (a 3rd-party package helping writing python 2&3 compatible code) were used here to check for string types.

if hasattr(self, '_unicode_value'):
if self._unicode_value is None:
return '(none)'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have been removed so the tests are failing.

else:
return self._unicode_value
else:
return self._unicode_value
else:
return super(Node, self).__unicode__()
return super(Node, self).__unicode__()

def __str__(self):
return unicode(self).encode('utf-8')

def __contains__(self, key):
return key in self.__dict__
if sys.version_info[0] < 3:
def __str__(self):
return self.__unicode__().encode('utf-8')


class Reports(object):
Expand Down Expand Up @@ -68,15 +75,23 @@ def request(self, type, workspace_id=None, **params):
raise ValueError('missing workspace ID')
workspace_id = self.workspace_id

for k, v in params.iteritems():
try:
iteritems = dict.items # Python 3
except AttributeError:
iteritems = dict.iteritems # Python 2

for k, v in iteritems(params):
if isinstance(v, datetime) or isinstance(v, date):
params[k] = v.strftime('%Y-%m-%d')

params['workspace_id'] = workspace_id
params['user_agent'] = self.USER_AGENT

data = self.session.get(type, **params)
return Node(**data)
if sys.version_info[0] < 3:
data = self.session.get(type, **params)
return Node(**data)
else:
return self.session.get(type, **params)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python3 code should also be using Node instead of just returning a dict.


@classmethod
def _get_totals(cls, data):
Expand Down
5 changes: 4 additions & 1 deletion toggl/session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import requests
from urllib import urlencode
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
import logging
import json

Expand Down