Skip to content

Commit f2a006b

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request #329 from penguineer/fixes
Small fixes
2 parents a9e95a1 + ef6ec99 commit f2a006b

2 files changed

Lines changed: 55 additions & 22 deletions

File tree

todoman/formatters.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,23 @@ def compact_multiple(self, todos):
8585

8686
return tabulate(table, tablefmt='plain')
8787

88-
def _columnize(self, label, text):
88+
def _columnize_text(self, label, text):
89+
"""Display text, split text by line-endings, on multiple colums,"""
90+
"""do nothing if text is empty or None"""
91+
lines = text.splitlines() if text else None
92+
93+
return self._columnize_list(label, lines)
94+
95+
def _columnize_list(self, label, lst):
96+
"""Display list on multiple columns,"""
97+
"""do nothing if list is empty or None"""
98+
8999
rows = []
90100

91-
lines = text.splitlines()
92-
rows.append([label, lines[0]])
93-
for line in lines[1:]:
94-
rows.append([None, line])
101+
if lst:
102+
rows.append([label, lst[0]])
103+
for line in lst[1:]:
104+
rows.append([None, line])
95105

96106
return rows
97107

@@ -102,10 +112,8 @@ def detailed(self, todo):
102112
:param Todo todo: The todo component.
103113
"""
104114
extra_rows = []
105-
if todo.description:
106-
extra_rows += self._columnize('Description', todo.description)
107-
if todo.location:
108-
extra_rows += self._columnize('Location', todo.location)
115+
extra_rows += self._columnize_text('Description', todo.description)
116+
extra_rows += self._columnize_text('Location', todo.location)
109117

110118
if extra_rows:
111119
return '{}\n\n{}'.format(

todoman/model.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,44 @@ def clone(self):
160160
)
161161

162162
def __setattr__(self, name, value):
163-
# Avoids accidentally setting a field to None when that's not a valid
164-
# attribute.
165-
if not value:
166-
if name in Todo.RRULE_FIELDS:
167-
return object.__setattr__(self, name, '')
168-
if name in Todo.STRING_FIELDS:
169-
return object.__setattr__(self, name, '')
170-
if name in Todo.INT_FIELDS:
171-
return object.__setattr__(self, name, 0)
172-
if name in Todo.LIST_FIELDS:
173-
return object.__setattr__(self, name, [])
174-
175-
return object.__setattr__(self, name, value)
163+
"""Check type and avoid setting fields to None"""
164+
"""when that is not a valid attribue."""
165+
166+
v = value
167+
168+
if name in Todo.RRULE_FIELDS:
169+
if value is None:
170+
v = ''
171+
else:
172+
assert isinstance(value, str), (
173+
"Got {0} for {1} where str was expected"
174+
.format(type(value), name))
175+
176+
if name in Todo.STRING_FIELDS:
177+
if value is None:
178+
v = ''
179+
else:
180+
assert isinstance(value, str), (
181+
"Got {0} for {1} where str was expected"
182+
.format(type(value), name))
183+
184+
if name in Todo.INT_FIELDS:
185+
if value is None:
186+
v = 0
187+
else:
188+
assert isinstance(value, int), (
189+
"Got {0} for {1} where int was expected"
190+
.format(type(value), name))
191+
192+
if name in Todo.LIST_FIELDS:
193+
if value is None:
194+
v = []
195+
else:
196+
assert isinstance(value, list), (
197+
"Got {0} for {1} where list was expected"
198+
.format(type(value), name))
199+
200+
return object.__setattr__(self, name, v)
176201

177202
@property
178203
def is_completed(self):

0 commit comments

Comments
 (0)