From 529e4e5950601fd5da1cfeb3df1bf7fcc1e051be Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Sun, 17 Nov 2013 00:25:33 +0100 Subject: [PATCH] added timestamps --- pubs/commands/add_cmd.py | 59 +++++++++++++++++++++---------------- pubs/commands/import_cmd.py | 2 ++ pubs/paper.py | 26 ++++++++++++---- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 60afce5..0613568 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -1,3 +1,5 @@ +import datetime + from ..uis import get_ui from ..configs import config from .. import bibstruct @@ -23,6 +25,32 @@ def parser(subparsers): return parser +def bibdata_from_editor(rp): + again = True + try: + bibstr = content.editor_input(config().edit_cmd, + templates.add_bib, + suffix='.bib') + if bibstr == templates.add_bib: + cont = ui.input_yn( + question='Bibfile not edited. Edit again ?', + default='y') + if not cont: + ui.exit(0) + else: + bibdata = rp.databroker.verify(bibstr) + bibstruct.verify_bibdata(bibdata) + # REFACTOR Generate citykey + cont = False + except ValueError: + again = ui.input_yn( + question='Invalid bibfile. Edit again ?', + default='y') + if not again: + ui.exit(0) + + return bibdata + def command(args): """ :param bibfile: bibtex file (in .bib, .bibml or .yaml format. @@ -37,32 +65,10 @@ def command(args): rp = repo.Repository(config()) - # get bibfile - + # get bibfile + if bibfile is None: - cont = True - bibstr = '' - try: - bibstr = content.editor_input(config().edit_cmd, - templates.add_bib, - suffix='.bib') - if bibstr == templates.add_bib: - cont = ui.input_yn( - question='Bibfile not edited. Edit again ?', - default='y') - if not cont: - ui.exit(0) - else: - bibdata = rp.databroker.verify(bibstr) - bibstruct.verify_bibdata(bibdata) - # REFACTOR Generate citykey - cont = False - except ValueError: - cont = ui.input_yn( - question='Invalid bibfile. Edit again ?', - default='y') - if not cont: - ui.exit(0) + bibdata = bibdata_from_editor(rp) else: bibdata_raw = content.get_content(bibfile) bibdata = rp.databroker.verify(bibdata_raw) @@ -82,8 +88,9 @@ def command(args): if tags is not None: p.tags = set(tags.split(',')) - + p = paper.Paper(bibdata, citekey=citekey) + p.added = datetime.datetime.now() # document file diff --git a/pubs/commands/import_cmd.py b/pubs/commands/import_cmd.py index 0f94ad4..4dc54d3 100644 --- a/pubs/commands/import_cmd.py +++ b/pubs/commands/import_cmd.py @@ -1,4 +1,5 @@ import os +import datetime from pybtex.database import Entry, BibliographyData, FieldDict, Person @@ -58,6 +59,7 @@ def many_from_path(bibpath): bibdata.entries[k] = b.entries[k] papers[k] = Paper(bibdata, citekey=k) + p.added = datetime.datetime.now() except ValueError, e: papers[k] = e return papers diff --git a/pubs/paper.py b/pubs/paper.py index feb8aa9..7f6c345 100644 --- a/pubs/paper.py +++ b/pubs/paper.py @@ -1,17 +1,21 @@ import copy import collections +import datetime from . import bibstruct -DEFAULT_META = collections.OrderedDict([('docfile', None), ('tags', set()), ('notes', [])]) -DEFAULT_META = {'docfile': None, 'tags': set(), 'notes': []} +#DEFAULT_META = collections.OrderedDict([('docfile', None), ('tags', set()), ('added', )]) +DEFAULT_META = {'docfile': None, 'tags': set(), 'added': None} class Paper(object): - """ Paper class. The object is responsible for the integrity of its data + """ Paper class. - The object is not responsible of any disk i/o. - self.bibdata is a pybtex.database.BibliographyData object + The object is not responsible of any disk I/O. + self.bibdata is a pybtex.database.BibliographyData object self.metadata is a dictionary + + The paper class provides methods to access the fields for its metadata + in a pythonic manner. """ def __init__(self, bibdata, citekey=None, metadata=None): @@ -42,6 +46,8 @@ class Paper(object): metadata=copy.deepcopy(self.metadata), bibdata=copy.deepcopy(self.bibdata)) + # docpath + @property def docpath(self): return self.metadata.get('docfile', '') @@ -69,3 +75,13 @@ class Paper(object): def remove_tag(self, tag): """Remove a tag from a paper if present.""" self.tags.discard(tag) + + # added date + + @property + def added(self): + datetime.datetime.strptime(self.metadata['added'], '%Y-%m-%d %H:%M:%S') + + @added.setter + def added(self, value): + self.metadata['added'] = value.strftime('%Y-%m-%d %H:%M:%S')