From 919c7c1c5a2f3bd67bbb41145433c77f52a84a7e Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Fri, 28 Jun 2013 14:58:27 +0200 Subject: [PATCH] method parse for algebric tags --- papers/commands/tag_cmd.py | 21 +++++++++++++++++++++ tests/test_tag.py | 15 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/test_tag.py diff --git a/papers/commands/tag_cmd.py b/papers/commands/tag_cmd.py index 398d8a2..0304d84 100644 --- a/papers/commands/tag_cmd.py +++ b/papers/commands/tag_cmd.py @@ -30,6 +30,27 @@ def parser(subparsers, config): # indistinguisable for argparse. (fabien, 201306) return parser + +import re +def _parse_tags(s): + tags = [] + if s[0] not in ['+', '-']: + s = '+' + s + last = 0 + for m in re.finditer(r'[+-]', s): + if m.start() == last: + if last != 0: + raise ValueError, 'could not match tag expression' + else: + tags.append(s[last:(m.start())]) + last = m.start() + if last == len(s): + raise ValueError, 'could not match tag expression' + else: + tags.append(s[last:]) + return tags + + def command(config, ui, referenceOrTag, tags): """Add, remove and show tags""" rp = Repository.from_directory(config) diff --git a/tests/test_tag.py b/tests/test_tag.py new file mode 100644 index 0000000..4dc28d6 --- /dev/null +++ b/tests/test_tag.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +import unittest + +import testenv +from papers.commands.tag_cmd import _parse_tags + +class TestCreateCitekey(unittest.TestCase): + + def test_tag_parsing(self): + + self.assertEqual(['+abc', '+def9'], _parse_tags( 'abc+def9')) + self.assertEqual(['+abc', '-def9'], _parse_tags( 'abc-def9')) + self.assertEqual(['-abc', '-def9'], _parse_tags('-abc-def9')) + self.assertEqual(['+abc', '-def9'], _parse_tags('+abc-def9')) +