Commit 9e13f20b authored by Bram Schoenmakers's avatar Bram Schoenmakers Committed by GitHub

Merge pull request #171 from mruwek/fix-config-and-format

Fix config duplicates and catch ListFormatParser errors
parents 9fdbde19 0759b54a
[column_keymap]
k = foo
k = bar
[column_keymap]
z = foobar
......@@ -165,5 +165,12 @@ class ConfigTest(TopydoTest):
self.assertNotIn('<Esc', keystates)
self.assertIn('<Esc>', keystates)
def test_config28(self):
""" test duplicates. """
keymap, keystates = config("test/data/ConfigTest7.conf").column_keymap()
self.assertEqual(keymap['k'], 'bar')
self.assertEqual(keymap['z'], 'foobar')
if __name__ == '__main__':
unittest.main()
......@@ -710,6 +710,14 @@ x 2014-12-12 Completed but with date:2014-12-12
"""
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()
......@@ -21,6 +21,7 @@ import os
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
......@@ -148,10 +149,14 @@ class ListCommand(ExpressionCommand):
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)
......
......@@ -162,7 +162,7 @@ class _Config:
self.config = {}
self.cp = configparser.RawConfigParser()
self.cp = configparser.RawConfigParser(strict=False)
# allow uppercase config keys
self.cp.optionxform = lambda option: option
......
......@@ -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):
......@@ -264,11 +269,14 @@ class ListFormatParser(object):
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)
......
......@@ -49,7 +49,7 @@ def columns(p_alt_layout_path=None):
'show_all': '0',
}
cp = RawConfigParser(defaults)
cp = RawConfigParser(defaults, strict=False)
files = [
"topydo_columns.ini",
"topydo_columns.conf",
......
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