Skip to content

Commit

Permalink
Merge pull request #46 from lyz-code/fix/pep8-guidelines
Browse files Browse the repository at this point in the history
[fix] Follow pep8 guidelines
  • Loading branch information
robgolding authored Jan 18, 2018
2 parents 617cc1f + 8f4fabf commit 79e9a98
Show file tree
Hide file tree
Showing 4 changed files with 434 additions and 305 deletions.
25 changes: 16 additions & 9 deletions tasklib/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class TaskWarrior(Backend):
VERSION_2_4_4 = six.u('2.4.4')
VERSION_2_4_5 = six.u('2.4.5')

def __init__(self, data_location=None, create=True, taskrc_location='~/.taskrc'):
def __init__(self, data_location=None, create=True,
taskrc_location='~/.taskrc'):
self.taskrc_location = os.path.expanduser(taskrc_location)

# If taskrc does not exist, pass / to use defaults and avoid creating
Expand Down Expand Up @@ -214,7 +215,9 @@ def format_description(self, task):
if self.version < self.VERSION_2_4_0:
return task._data['description']
else:
return six.u("description:'{0}'").format(task._data['description'] or '')
return six.u("description:'{0}'").format(
task._data['description'] or '',
)

def convert_datetime_string(self, value):

Expand All @@ -226,9 +229,11 @@ def convert_datetime_string(self, value):
naive = datetime.datetime.strptime(result[0], DATE_FORMAT_CALC)
localized = local_zone.localize(naive)
else:
raise ValueError("Provided value could not be converted to "
"datetime, its type is not supported: {}"
.format(type(value)))
raise ValueError(
'Provided value could not be converted to '
'datetime, its type is not supported: {}'
.format(type(value)),
)

return localized

Expand Down Expand Up @@ -338,8 +343,10 @@ def save_task(self, task):
# Expected output: Created task 1.
# Created task 1 (recurrence template).
if len(id_lines) != 1 or len(id_lines[0].split(' ')) not in (3, 5):
raise TaskWarriorException("Unexpected output when creating "
"task: %s" % '\n'.join(id_lines))
raise TaskWarriorException(
'Unexpected output when creating '
'task: %s' % '\n'.join(id_lines),
)

# Circumvent the ID storage, since ID is considered read-only
identifier = id_lines[0].split(' ')[2].rstrip('.')
Expand Down Expand Up @@ -411,8 +418,8 @@ def valid(output):
# If more than 1 task has been matched still, raise an exception
if not valid(output):
raise TaskWarriorException(
"Unique identifiers {0} with description: {1} matches "
"multiple tasks: {2}".format(
'Unique identifiers {0} with description: {1} matches '
'multiple tasks: {2}'.format(
task['uuid'] or task['id'], task['description'], output)
)

Expand Down
34 changes: 22 additions & 12 deletions tasklib/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __hash__(self):
return self['uuid'].__hash__()

def __repr__(self):
return "LazyUUIDTask: {0}".format(self._uuid)
return 'LazyUUIDTask: {0}'.format(self._uuid)

def __copy__(self):
return LazyUUIDTask(self._tw, self._uuid)
Expand Down Expand Up @@ -103,7 +103,7 @@ def __getattr__(self, name):
return getattr(self, name)

def __repr__(self):
return "LazyUUIDTaskSet([{0}])".format(', '.join(self._uuids))
return 'LazyUUIDTaskSet([{0}])'.format(', '.join(self._uuids))

def __eq__(self, other):
return set(t['uuid'] for t in other) == self._uuids
Expand All @@ -128,8 +128,10 @@ def __isub__(self, other):
return self.difference_update(other)

def __rsub__(self, other):
return LazyUUIDTaskSet(self._tw,
set(t['uuid'] for t in other) - self._uuids)
return LazyUUIDTaskSet(
self._tw,
set(t['uuid'] for t in other) - self._uuids,
)

def __or__(self, other):
return self.union(other)
Expand Down Expand Up @@ -171,20 +173,28 @@ def issuperset(self, other):
return all([task in self for task in other])

def union(self, other):
return LazyUUIDTaskSet(self._tw,
self._uuids | set(t['uuid'] for t in other))
return LazyUUIDTaskSet(
self._tw,
self._uuids | set(t['uuid'] for t in other),
)

def intersection(self, other):
return LazyUUIDTaskSet(self._tw,
self._uuids & set(t['uuid'] for t in other))
return LazyUUIDTaskSet(
self._tw,
self._uuids & set(t['uuid'] for t in other),
)

def difference(self, other):
return LazyUUIDTaskSet(self._tw,
self._uuids - set(t['uuid'] for t in other))
return LazyUUIDTaskSet(
self._tw,
self._uuids - set(t['uuid'] for t in other),
)

def symmetric_difference(self, other):
return LazyUUIDTaskSet(self._tw,
self._uuids ^ set(t['uuid'] for t in other))
return LazyUUIDTaskSet(
self._tw,
self._uuids ^ set(t['uuid'] for t in other),
)

def update(self, other):
self._uuids |= set(t['uuid'] for t in other)
Expand Down
53 changes: 33 additions & 20 deletions tasklib/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,20 +326,24 @@ def serialize_depends(self, cur_dependencies):
# Check that all the tasks are saved
for task in (cur_dependencies or set()):
if not task.saved:
raise Task.NotSaved('Task \'%s\' needs to be saved before '
'it can be set as dependency.' % task)
raise Task.NotSaved(
'Task \'%s\' needs to be saved before '
'it can be set as dependency.' % task,
)

return super(Task, self).serialize_depends(cur_dependencies)

def delete(self):
if not self.saved:
raise Task.NotSaved("Task needs to be saved before it can be deleted")
raise Task.NotSaved(
'Task needs to be saved before it can be deleted',
)

# Refresh the status, and raise exception if the task is deleted
self.refresh(only_fields=['status'])

if self.deleted:
raise Task.DeletedTask("Task was already deleted")
raise Task.DeletedTask('Task was already deleted')

self.backend.delete_task(self)

Expand All @@ -348,17 +352,19 @@ def delete(self):

def start(self):
if not self.saved:
raise Task.NotSaved("Task needs to be saved before it can be started")
raise Task.NotSaved(
'Task needs to be saved before it can be started',
)

# Refresh, and raise exception if task is already completed/deleted
self.refresh(only_fields=['status'])

if self.completed:
raise Task.CompletedTask("Cannot start a completed task")
raise Task.CompletedTask('Cannot start a completed task')
elif self.deleted:
raise Task.DeletedTask("Deleted task cannot be started")
raise Task.DeletedTask('Deleted task cannot be started')
elif self.active:
raise Task.ActiveTask("Task is already active")
raise Task.ActiveTask('Task is already active')

self.backend.start_task(self)

Expand All @@ -367,13 +373,15 @@ def start(self):

def stop(self):
if not self.saved:
raise Task.NotSaved("Task needs to be saved before it can be stopped")
raise Task.NotSaved(
'Task needs to be saved before it can be stopped',
)

# Refresh, and raise exception if task is already completed/deleted
self.refresh(only_fields=['status'])

if not self.active:
raise Task.InactiveTask("Cannot stop an inactive task")
raise Task.InactiveTask('Cannot stop an inactive task')

self.backend.stop_task(self)

Expand All @@ -382,15 +390,17 @@ def stop(self):

def done(self):
if not self.saved:
raise Task.NotSaved("Task needs to be saved before it can be completed")
raise Task.NotSaved(
'Task needs to be saved before it can be completed',
)

# Refresh, and raise exception if task is already completed/deleted
self.refresh(only_fields=['status'])

if self.completed:
raise Task.CompletedTask("Cannot complete a completed task")
raise Task.CompletedTask('Cannot complete a completed task')
elif self.deleted:
raise Task.DeletedTask("Deleted task cannot be completed")
raise Task.DeletedTask('Deleted task cannot be completed')

self.backend.complete_task(self)

Expand All @@ -406,14 +416,14 @@ def save(self):

def add_annotation(self, annotation):
if not self.saved:
raise Task.NotSaved("Task needs to be saved to add annotation")
raise Task.NotSaved('Task needs to be saved to add annotation')

self.backend.annotate_task(self, annotation)
self.refresh(only_fields=['annotations'])

def remove_annotation(self, annotation):
if not self.saved:
raise Task.NotSaved("Task needs to be saved to remove annotation")
raise Task.NotSaved('Task needs to be saved to remove annotation')

if isinstance(annotation, TaskAnnotation):
annotation = annotation['description']
Expand All @@ -424,13 +434,14 @@ def remove_annotation(self, annotation):
def refresh(self, only_fields=None, after_save=False):
# Raise error when trying to refresh a task that has not been saved
if not self.saved:
raise Task.NotSaved("Task needs to be saved to be refreshed")
raise Task.NotSaved('Task needs to be saved to be refreshed')

new_data = self.backend.refresh_task(self, after_save=after_save)

if only_fields:
to_update = dict(
[(k, new_data.get(k)) for k in only_fields])
[(k, new_data.get(k)) for k in only_fields],
)
self._update_data(to_update, update_original=True)
else:
self._load_data(new_data)
Expand Down Expand Up @@ -461,7 +472,7 @@ def __deepcopy__(self, memo):
def __repr__(self):
data = list(self[:REPR_OUTPUT_SIZE + 1])
if len(data) > REPR_OUTPUT_SIZE:
data[-1] = "...(remaining elements truncated)..."
data[-1] = '...(remaining elements truncated)...'
return repr(data)

def __len__(self):
Expand Down Expand Up @@ -549,7 +560,9 @@ def get(self, **kwargs):
if not num:
raise Task.DoesNotExist(
'Task matching query does not exist. '
'Lookup parameters were {0}'.format(kwargs))
'Lookup parameters were {0}'.format(kwargs),
)
raise ValueError(
'get() returned more than one Task -- it returned {0}! '
'Lookup parameters were {1}'.format(num, kwargs))
'Lookup parameters were {1}'.format(num, kwargs),
)
Loading

0 comments on commit 79e9a98

Please sign in to comment.