Skip to content

Commit

Permalink
Show error message when parsing of -F fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwek committed Feb 20, 2017
1 parent edc11ee commit 8b9e387
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
8 changes: 8 additions & 0 deletions test/test_list_format.py
Expand Up @@ -710,6 +710,14 @@ def test_list_format46(self):
"""
self.assertEqual(self.output, result)

def test_list_format47(self):
command = ListCommand(["-x", "-F", "%(r)"], self.todolist, self.out, self.error)
command.execute()

error = 'Error while parsing format string (list_format config option or -F)\n'
self.assertEqual(self.output, '')
self.assertEqual(self.errors, error)


if __name__ == '__main__':
unittest.main()
13 changes: 9 additions & 4 deletions topydo/commands/ListCommand.py
Expand Up @@ -21,6 +21,7 @@
from topydo.lib.Config import config
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Filter import HiddenTagFilter, InstanceFilter
from topydo.lib.ListFormat import ListFormatError
from topydo.lib.printers.PrettyPrinter import pretty_printer_factory
from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter
from topydo.lib.TodoListBase import InvalidTodoException
Expand Down Expand Up @@ -148,10 +149,14 @@ def _print(self):

self.printer = pretty_printer_factory(self.todolist, filters)

if self.group_expression:
self.out(self.printer.print_groups(self._view().groups))
else:
self.out(self.printer.print_list(self._view().todos))
try:
if self.group_expression:
self.out(self.printer.print_groups(self._view().groups))
else:
self.out(self.printer.print_list(self._view().todos))
except ListFormatError:
self.error('Error while parsing format string (list_format config'
' option or -F)')

def _view(self):
sorter = Sorter(self.sort_expression, self.group_expression)
Expand Down
18 changes: 13 additions & 5 deletions topydo/lib/ListFormat.py
Expand Up @@ -129,6 +129,11 @@ def color_block(p_todo):
config().priority_color(p_todo.priority()).as_ansi(),
)


class ListFormatError(Exception):
pass


class ListFormatParser(object):
""" Parser of format string. """
def __init__(self, p_todolist, p_format=None):
Expand Down Expand Up @@ -264,11 +269,14 @@ def parse(self, p_todo):
if placeholder == 'S':
repl_trunc = repl

if repl == '':
substr = re.sub(pattern, '', substr)
else:
substr = re.sub(pattern, _strip_placeholder_braces, substr)
substr = re.sub(r'(?<!\\)%({ph}|\[{ph}\])'.format(ph=placeholder), repl, substr)
try:
if repl == '':
substr = re.sub(pattern, '', substr)
else:
substr = re.sub(pattern, _strip_placeholder_braces, substr)
substr = re.sub(r'(?<!\\)%({ph}|\[{ph}\])'.format(ph=placeholder), repl, substr)
except re.error:
raise ListFormatError

parsed_list.append(substr)

Expand Down

0 comments on commit 8b9e387

Please sign in to comment.