Skip to content
Merged
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
26 changes: 17 additions & 9 deletions todoman/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,23 @@ def compact_multiple(self, todos):

return tabulate(table, tablefmt='plain')

def _columnize(self, label, text):
def _columnize_text(self, label, text):
"""Display text, split text by line-endings, on multiple colums,"""
"""do nothing if text is empty or None"""
lines = text.splitlines() if text else None

return self._columnize_list(label, lines)

def _columnize_list(self, label, lst):
Copy link
Member

Choose a reason for hiding this comment

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

I'd move this function's logic inside _columnize_text, since it's not really something we're reusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I externalized this part on purpose, because we will use it for the categories. (see PR #323)

"""Display list on multiple columns,"""
"""do nothing if list is empty or None"""

rows = []

lines = text.splitlines()
rows.append([label, lines[0]])
for line in lines[1:]:
rows.append([None, line])
if lst:
rows.append([label, lst[0]])
for line in lst[1:]:
rows.append([None, line])

return rows

Expand All @@ -102,10 +112,8 @@ def detailed(self, todo):
:param Todo todo: The todo component.
"""
extra_rows = []
if todo.description:
extra_rows += self._columnize('Description', todo.description)
if todo.location:
extra_rows += self._columnize('Location', todo.location)
extra_rows += self._columnize_text('Description', todo.description)
extra_rows += self._columnize_text('Location', todo.location)

if extra_rows:
return '{}\n\n{}'.format(
Expand Down
51 changes: 38 additions & 13 deletions todoman/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,44 @@ def clone(self):
)

def __setattr__(self, name, value):
# Avoids accidentally setting a field to None when that's not a valid
# attribute.
if not value:
if name in Todo.RRULE_FIELDS:
return object.__setattr__(self, name, '')
if name in Todo.STRING_FIELDS:
return object.__setattr__(self, name, '')
if name in Todo.INT_FIELDS:
return object.__setattr__(self, name, 0)
if name in Todo.LIST_FIELDS:
return object.__setattr__(self, name, [])

return object.__setattr__(self, name, value)
"""Check type and avoid setting fields to None"""
"""when that is not a valid attribue."""

v = value

if name in Todo.RRULE_FIELDS:
if value is None:
v = ''
else:
assert isinstance(value, str), (
"Got {0} for {1} where str was expected"
.format(type(value), name))

if name in Todo.STRING_FIELDS:
if value is None:
v = ''
else:
assert isinstance(value, str), (
"Got {0} for {1} where str was expected"
.format(type(value), name))

if name in Todo.INT_FIELDS:
if value is None:
v = 0
else:
assert isinstance(value, int), (
"Got {0} for {1} where int was expected"
.format(type(value), name))

if name in Todo.LIST_FIELDS:
if value is None:
v = []
else:
assert isinstance(value, list), (
"Got {0} for {1} where list was expected"
.format(type(value), name))

return object.__setattr__(self, name, v)

@property
def is_completed(self):
Expand Down