Fixes issue #35.
pubs tag non-existing-citekey some-tag Know fails as expected. The commit however removes the option of listing tags as separate arguments.
This commit is contained in:
parent
5480f79d8d
commit
681ae65c9e
@ -30,7 +30,7 @@ def parser(subparsers):
|
|||||||
parser = subparsers.add_parser('tag', help="add, remove and show tags")
|
parser = subparsers.add_parser('tag', help="add, remove and show tags")
|
||||||
parser.add_argument('citekeyOrTag', nargs='?', default=None,
|
parser.add_argument('citekeyOrTag', nargs='?', default=None,
|
||||||
help='citekey or tag.')
|
help='citekey or tag.')
|
||||||
parser.add_argument('tags', nargs='*', default=None,
|
parser.add_argument('tags', nargs='?', default=None,
|
||||||
help='If the previous argument was a citekey, then '
|
help='If the previous argument was a citekey, then '
|
||||||
'a list of tags separated by a +.')
|
'a list of tags separated by a +.')
|
||||||
# TODO find a way to display clear help for multiple command semantics,
|
# TODO find a way to display clear help for multiple command semantics,
|
||||||
@ -38,14 +38,6 @@ def parser(subparsers):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def _parse_tags(list_tags):
|
|
||||||
"""Transform 'math-ai network -search' in ['+math', '-ai', '+network', '-search']"""
|
|
||||||
tags = []
|
|
||||||
for s in list_tags:
|
|
||||||
tags += _parse_tag_seq(s)
|
|
||||||
return tags
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_tag_seq(s):
|
def _parse_tag_seq(s):
|
||||||
"""Transform 'math-ai' in ['+math', '-ai']"""
|
"""Transform 'math-ai' in ['+math', '-ai']"""
|
||||||
tags = []
|
tags = []
|
||||||
@ -93,20 +85,21 @@ def command(args):
|
|||||||
else:
|
else:
|
||||||
if rp.databroker.exists(citekeyOrTag):
|
if rp.databroker.exists(citekeyOrTag):
|
||||||
p = rp.pull_paper(citekeyOrTag)
|
p = rp.pull_paper(citekeyOrTag)
|
||||||
if tags == []:
|
if tags is None:
|
||||||
ui.message(color.dye_out(' '.join(sorted(p.tags)), color.tag))
|
ui.message(color.dye_out(' '.join(sorted(p.tags)), color.tag))
|
||||||
else:
|
else:
|
||||||
add_tags, remove_tags = _tag_groups(_parse_tags(tags))
|
add_tags, remove_tags = _tag_groups(_parse_tag_seq(tags))
|
||||||
for tag in add_tags:
|
for tag in add_tags:
|
||||||
p.add_tag(tag)
|
p.add_tag(tag)
|
||||||
for tag in remove_tags:
|
for tag in remove_tags:
|
||||||
p.remove_tag(tag)
|
p.remove_tag(tag)
|
||||||
rp.push_paper(p, overwrite=True)
|
rp.push_paper(p, overwrite=True)
|
||||||
|
elif tags is not None:
|
||||||
|
ui.error(ui.error('no entry found for citekey {}.'.format(citekeyOrTag)))
|
||||||
|
ui.exit()
|
||||||
else:
|
else:
|
||||||
# case where we want to find papers with specific tags
|
# case where we want to find papers with specific tags
|
||||||
all_tags = [citekeyOrTag]
|
included, excluded = _tag_groups(_parse_tag_seq(citekeyOrTag))
|
||||||
all_tags += tags
|
|
||||||
included, excluded = _tag_groups(_parse_tags(all_tags))
|
|
||||||
papers_list = []
|
papers_list = []
|
||||||
for p in rp.all_papers():
|
for p in rp.all_papers():
|
||||||
if (p.tags.issuperset(included) and
|
if (p.tags.issuperset(included) and
|
||||||
|
@ -27,11 +27,13 @@ def _get_encoding(conf):
|
|||||||
return enc or 'utf-8'
|
return enc or 'utf-8'
|
||||||
return conf.get('terminal-encoding', enc or 'utf-8')
|
return conf.get('terminal-encoding', enc or 'utf-8')
|
||||||
|
|
||||||
|
|
||||||
def get_ui():
|
def get_ui():
|
||||||
if _ui is None:
|
if _ui is None:
|
||||||
return PrintUI() # no editor support. (#FIXME?)
|
return PrintUI() # no editor support. (#FIXME?)
|
||||||
return _ui
|
return _ui
|
||||||
|
|
||||||
|
|
||||||
def init_ui(conf):
|
def init_ui(conf):
|
||||||
global _ui
|
global _ui
|
||||||
_ui = InputUI(conf)
|
_ui = InputUI(conf)
|
||||||
|
@ -2,25 +2,25 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import dotdot
|
import dotdot
|
||||||
from pubs.commands.tag_cmd import _parse_tags, _tag_groups
|
from pubs.commands.tag_cmd import _parse_tag_seq, _tag_groups
|
||||||
|
|
||||||
class TestTag(unittest.TestCase):
|
class TestTag(unittest.TestCase):
|
||||||
|
|
||||||
def test_parse_tags(self):
|
def test_parse_tags(self):
|
||||||
self.assertEqual(['+abc', '+def9'], _parse_tags([ 'abc+def9']))
|
self.assertEqual(['+abc', '+def9'], _parse_tag_seq('abc+def9'))
|
||||||
self.assertEqual(['+abc', '-def9'], _parse_tags([ 'abc-def9']))
|
self.assertEqual(['+abc', '-def9'], _parse_tag_seq('abc-def9'))
|
||||||
self.assertEqual(['-abc', '-def9'], _parse_tags(['-abc-def9']))
|
self.assertEqual(['-abc', '-def9'], _parse_tag_seq('-abc-def9'))
|
||||||
self.assertEqual(['+abc', '-def9'], _parse_tags(['+abc-def9']))
|
self.assertEqual(['+abc', '-def9'], _parse_tag_seq('+abc-def9'))
|
||||||
|
|
||||||
def test_tag_groups(self):
|
def test_tag_groups(self):
|
||||||
self.assertEqual(({'math', 'romance'}, {'war'}),
|
self.assertEqual(({'math', 'romance'}, {'war'}),
|
||||||
_tag_groups(_parse_tags(['-war+math+romance'])))
|
_tag_groups(_parse_tag_seq('-war+math+romance')))
|
||||||
self.assertEqual(({'math', 'romance'}, {'war'}),
|
self.assertEqual(({'math', 'romance'}, {'war'}),
|
||||||
_tag_groups(_parse_tags([':war+math+romance'])))
|
_tag_groups(_parse_tag_seq(':war+math+romance')))
|
||||||
self.assertEqual(({'math', 'romance'}, {'war'}),
|
self.assertEqual(({'math', 'romance'}, {'war'}),
|
||||||
_tag_groups(_parse_tags(['+math+romance-war'])))
|
_tag_groups(_parse_tag_seq('+math+romance-war')))
|
||||||
self.assertEqual(({'math', 'romance'}, {'war'}),
|
self.assertEqual(({'math', 'romance'}, {'war'}),
|
||||||
_tag_groups(_parse_tags(['math+romance-war'])))
|
_tag_groups(_parse_tag_seq('math+romance-war')))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user