tags set are algebrical: math+romance-war
This commit is contained in:
parent
919c7c1c5a
commit
585e09e498
@ -9,10 +9,12 @@ The different use cases are :
|
|||||||
Add 'math' to the list of tags of the given citekey
|
Add 'math' to the list of tags of the given citekey
|
||||||
4. > papers tag citekey :math
|
4. > papers tag citekey :math
|
||||||
Remove 'math' for the list of tags of the given citekey
|
Remove 'math' for the list of tags of the given citekey
|
||||||
5. > papers tag citekey math,romance,:war
|
5. > papers tag citekey math+romance-war
|
||||||
Add 'math' and 'romance' tags to the given citekey, and remove the 'war' tag
|
Add 'math' and 'romance' tags to the given citekey, and remove the 'war' tag
|
||||||
6. > papers tag math
|
6. > papers tag math
|
||||||
If 'math' is not a citekey, then display all papers with the tag 'math'
|
If 'math' is not a citekey, then display all papers with the tag 'math'
|
||||||
|
7. > papers tag -war+math+romance
|
||||||
|
display all papers with the tag 'math', 'romance' but not 'war'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ..repo import Repository, InvalidReference
|
from ..repo import Repository, InvalidReference
|
||||||
@ -33,6 +35,7 @@ def parser(subparsers, config):
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
def _parse_tags(s):
|
def _parse_tags(s):
|
||||||
|
"""Transform 'math-ai' in ['+math', '-ai']"""
|
||||||
tags = []
|
tags = []
|
||||||
if s[0] not in ['+', '-']:
|
if s[0] not in ['+', '-']:
|
||||||
s = '+' + s
|
s = '+' + s
|
||||||
@ -50,6 +53,15 @@ def _parse_tags(s):
|
|||||||
tags.append(s[last:])
|
tags.append(s[last:])
|
||||||
return tags
|
return tags
|
||||||
|
|
||||||
|
def _tag_groups(tags):
|
||||||
|
plus_tags, minus_tags = [], []
|
||||||
|
for tag in tags:
|
||||||
|
if tag[0] == '+':
|
||||||
|
plus_tags.append(tag[1:])
|
||||||
|
else:
|
||||||
|
assert tag[0] == '-'
|
||||||
|
minus_tags.append(tag[1:])
|
||||||
|
return set(plus_tags), set(minus_tags)
|
||||||
|
|
||||||
def command(config, ui, referenceOrTag, tags):
|
def command(config, ui, referenceOrTag, tags):
|
||||||
"""Add, remove and show tags"""
|
"""Add, remove and show tags"""
|
||||||
@ -65,16 +77,20 @@ def command(config, ui, referenceOrTag, tags):
|
|||||||
if tags is None:
|
if tags is None:
|
||||||
ui.print_(' '.join(p.tags))
|
ui.print_(' '.join(p.tags))
|
||||||
else:
|
else:
|
||||||
tags = tags.split(',')
|
add_tags, remove_tags = _tag_groups(_parse_tags(tags))
|
||||||
for tag in tags:
|
for tag in add_tag:
|
||||||
if tag[0] == ':':
|
p.add_tag(tag)
|
||||||
p.remove_tag(tag[1:])
|
for tag in remove_tag:
|
||||||
else:
|
p.remove_tag(tag)
|
||||||
p.add_tag(tag)
|
|
||||||
rp.save_paper(p)
|
rp.save_paper(p)
|
||||||
except InvalidReference:
|
except InvalidReference:
|
||||||
tag = referenceOrTag
|
# case where we want to find paper with specific tags
|
||||||
papers_list = [(p, n) for n, p in enumerate(rp.all_papers())
|
included, excluded = _tag_groups(_parse_tags(referenceOrTag))
|
||||||
if tag in p.tags]
|
papers_list = []
|
||||||
|
for n, p in enumerate(rp.all_papers()):
|
||||||
|
if (p.tags.issuperset(included) and
|
||||||
|
len(p.tags.intersection(excluded)) == 0):
|
||||||
|
papers_list.append((p, n))
|
||||||
|
|
||||||
ui.print_('\n'.join(helpers.paper_oneliner(p, n)
|
ui.print_('\n'.join(helpers.paper_oneliner(p, n)
|
||||||
for p, n in papers_list))
|
for p, n in papers_list))
|
@ -2,7 +2,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import testenv
|
import testenv
|
||||||
from papers.commands.tag_cmd import _parse_tags
|
from papers.commands.tag_cmd import _parse_tags, _tag_groups
|
||||||
|
|
||||||
class TestCreateCitekey(unittest.TestCase):
|
class TestCreateCitekey(unittest.TestCase):
|
||||||
|
|
||||||
@ -13,3 +13,6 @@ class TestCreateCitekey(unittest.TestCase):
|
|||||||
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'))
|
||||||
|
|
||||||
|
self.assertEqual(({'math', 'romance'}, {'war'}), _tag_groups(_parse_tags('-war+math+romance')))
|
||||||
|
self.assertEqual(({'math', 'romance'}, {'war'}), _tag_groups(_parse_tags('+math+romance-war')))
|
||||||
|
self.assertEqual(({'math', 'romance'}, {'war'}), _tag_groups(_parse_tags('math+romance-war')))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user