You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

11 years ago
import copy
import collections
11 years ago
from . import bibstruct
11 years ago
DEFAULT_META = collections.OrderedDict([('docfile', None), ('tags', set()), ('notes', [])])
DEFAULT_META = {'docfile': None, 'tags': set(), 'notes': []}
class Paper(object):
11 years ago
""" Paper class. The object is responsible for the integrity of its data
11 years ago
The object is not responsible of any disk i/o.
self.bibdata is a pybtex.database.BibliographyData object
self.metadata is a dictionary
"""
11 years ago
def __init__(self, bibdata, citekey=None, metadata=None):
self.citekey = citekey
self.metadata = metadata
11 years ago
self.bibdata = bibdata
if self.metadata is None:
self.metadata = copy.deepcopy(DEFAULT_META)
if self.citekey is None:
self.citekey = bibstruct.extract_citekey(self.bibdata)
bibstruct.check_citekey(self.citekey)
def __eq__(self, other):
return (isinstance(self, Paper) and type(other) is type(self)
11 years ago
and self.bibdata == other.bibdata
and self.metadata == other.metadata
11 years ago
and self.citekey == other.citekey)
def __repr__(self):
return 'Paper(%s, %s, %s)' % (
self.citekey, self.bibentry, self.metadata)
11 years ago
def deepcopy(self):
return Paper(citekey =self.citekey,
metadata=copy.deepcopy(self.metadata),
bibdata=copy.deepcopy(self.bibdata))
11 years ago
@property
def docpath(self):
return self.metadata.get('docfile', '')
11 years ago
@docpath.setter
def docpath(self, path):
"""Does not verify if the path exists."""
self.metadata['docfile'] = path
11 years ago
# 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 if present."""
self.tags.discard(tag)