diff --git a/TODO b/TODO index 2f22223..03f876f 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ TODO list ========= - manage cross-references -+ labels - find (authors) duplicates + remove command - stats command diff --git a/papers/commands/add_cmd.py b/papers/commands/add_cmd.py index 41d4126..bea1d6c 100644 --- a/papers/commands/add_cmd.py +++ b/papers/commands/add_cmd.py @@ -45,7 +45,7 @@ def command(config, ui, bibfile, docfile, label, copy): else: p = Paper.load(bibfile) if label is not None: - p.metadata['labels'] = label.split() + p.tags = set(label.split()) # Check if another doc file is specified in bibtex docfile2 = extract_doc_path_from_bibdata(p, ui) if docfile is None: diff --git a/papers/commands/list_cmd.py b/papers/commands/list_cmd.py index 37946d2..57f0e16 100644 --- a/papers/commands/list_cmd.py +++ b/papers/commands/list_cmd.py @@ -9,7 +9,7 @@ def parser(subparsers, config): default=False, dest='citekeys', help='Only returns citekeys of matching papers.') parser.add_argument('query', nargs='*', - help='Paper query (e.g. "year: 2000" or "labels: math")') + help='Paper query (e.g. "year: 2000" or "tags: math")') return parser @@ -23,12 +23,12 @@ def command(config, ui, citekeys, query): paper_strings = [] for n, p in papers: bibdesc = pretty.bib_oneliner(p.bibentry) - paper_strings.append((u'{num:d}: [{citekey}] {descr} {labels}'.format( + paper_strings.append((u'{num:d}: [{citekey}] {descr} {tags}'.format( num=int(n), citekey=color.dye(p.citekey, color.purple), descr=bibdesc, - labels=color.dye(' '.join(p.metadata.get('labels', [])), - color.purple, bold=True), + tags=color.dye(' '.join(p.tags), + color.purple, bold=True), )).encode('utf-8')) ui.print_('\n'.join(paper_strings)) @@ -45,8 +45,8 @@ def test_paper(query_string, p): field = tmp[0] value = tmp[1] - if field in ['labels', 'l', 'tags', 't']: - if value not in p.metadata['labels']: + if field in ['tags', 't']: + if value not in p.tags: return False elif field in ['author', 'authors', 'a']: # that is the very ugly if not 'author' in p.bibentry.persons: diff --git a/papers/commands/tags_cmd.py b/papers/commands/tags_cmd.py index ce43ee7..c56384d 100644 --- a/papers/commands/tags_cmd.py +++ b/papers/commands/tags_cmd.py @@ -9,5 +9,5 @@ def parser(subparsers, config): def command(config, ui): """List existing tags""" rp = Repository.from_directory(config) - for tag in rp.get_labels(): + for tag in rp.get_tags(): ui.print_(tag) diff --git a/papers/paper.py b/papers/paper.py index 2f003e6..55d5705 100644 --- a/papers/paper.py +++ b/papers/paper.py @@ -20,7 +20,7 @@ CITEKEY_EXCLUDE_RE = re.compile('[%s]' BASE_META = { 'external-document': None, - 'labels': [], + 'tags': [], 'notes': [], } @@ -250,7 +250,30 @@ class Paper(object): return papers -class PaperInRepo(Paper): + # tags + + @property + def tags(self): + return self.metadata.setdefault('tags', set()) + + @tags.setter + def tags(self, value): + if not hasattr(value, '__iter__'): + raise ValueError, 'tags must be iterables' + self.metadata['tags'] = set(value) + + def add_tag(self, tag): + self.tags.add(tag) + + def remove_tag(self, tag): + """Remove a tag from a paper. Fails silently.""" + try: + self.tags.pop(tag) + except KeyError: + pass + + +class PaperInRepo(Paper): # TODO document why this class exists (fabien, 2013/06) def __init__(self, repo, *args, **kwargs): Paper.__init__(self, *args, **kwargs) diff --git a/papers/repo.py b/papers/repo.py index 4db7a67..6486890 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -198,11 +198,11 @@ class Repository(object): new_doc_file = os.path.join(doc_path, citekey + ext) shutil.copy(doc_file, new_doc_file) - def get_labels(self): - labels = set() + def get_tags(self): + tags = set() for p in self.all_papers(): - labels = labels.union(p.metadata.get('labels', [])) - return labels + tags = tags.union(p.tags) + return tags @classmethod def from_directory(cls, config, papersdir=None): diff --git a/tests/test_paper.py b/tests/test_paper.py index be99a90..43b37c6 100644 --- a/tests/test_paper.py +++ b/tests/test_paper.py @@ -24,7 +24,7 @@ entries: META = """ external-document: null notes: [] -labels: [] +tags: [] """