Commit 8b9e3877 authored by Jacek Sowiński's avatar Jacek Sowiński

Show error message when parsing of -F fails

parent edc11eeb
...@@ -710,6 +710,14 @@ x 2014-12-12 Completed but with date:2014-12-12 ...@@ -710,6 +710,14 @@ x 2014-12-12 Completed but with date:2014-12-12
""" """
self.assertEqual(self.output, result) 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__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -21,6 +21,7 @@ import os ...@@ -21,6 +21,7 @@ import os
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.ExpressionCommand import ExpressionCommand from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Filter import HiddenTagFilter, InstanceFilter 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.printers.PrettyPrinter import pretty_printer_factory
from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
...@@ -148,10 +149,14 @@ class ListCommand(ExpressionCommand): ...@@ -148,10 +149,14 @@ class ListCommand(ExpressionCommand):
self.printer = pretty_printer_factory(self.todolist, filters) self.printer = pretty_printer_factory(self.todolist, filters)
if self.group_expression: try:
self.out(self.printer.print_groups(self._view().groups)) if self.group_expression:
else: self.out(self.printer.print_groups(self._view().groups))
self.out(self.printer.print_list(self._view().todos)) 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): def _view(self):
sorter = Sorter(self.sort_expression, self.group_expression) sorter = Sorter(self.sort_expression, self.group_expression)
......
...@@ -129,6 +129,11 @@ def color_block(p_todo): ...@@ -129,6 +129,11 @@ def color_block(p_todo):
config().priority_color(p_todo.priority()).as_ansi(), config().priority_color(p_todo.priority()).as_ansi(),
) )
class ListFormatError(Exception):
pass
class ListFormatParser(object): class ListFormatParser(object):
""" Parser of format string. """ """ Parser of format string. """
def __init__(self, p_todolist, p_format=None): def __init__(self, p_todolist, p_format=None):
...@@ -264,11 +269,14 @@ class ListFormatParser(object): ...@@ -264,11 +269,14 @@ class ListFormatParser(object):
if placeholder == 'S': if placeholder == 'S':
repl_trunc = repl repl_trunc = repl
if repl == '': try:
substr = re.sub(pattern, '', substr) if repl == '':
else: substr = re.sub(pattern, '', substr)
substr = re.sub(pattern, _strip_placeholder_braces, substr) else:
substr = re.sub(r'(?<!\\)%({ph}|\[{ph}\])'.format(ph=placeholder), repl, substr) 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) parsed_list.append(substr)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment