Updates commands. Many bug fix.

main
Olivier Mangin 12 years ago
parent 1fad286517
commit 0d53619e1f

@ -12,5 +12,5 @@ def command(config, pdffile, bibfile):
:param pdffilepath path (no url yet) to a pdf or ps file :param pdffilepath path (no url yet) to a pdf or ps file
:param bibtex bibtex file (in .bib, .bibml or .yaml format. :param bibtex bibtex file (in .bib, .bibml or .yaml format.
""" """
rp = repo.Repository() rp = repo.Repository.from_directory()
rp.add_paper_from_paths(pdffile, bibfile) rp.add_paper_from_paths(pdffile, bibfile)

@ -11,5 +11,5 @@ def command(config, bibfile):
""" """
:param bibtex bibtex file (in .bib, .bibml or .yaml format. :param bibtex bibtex file (in .bib, .bibml or .yaml format.
""" """
rp = repo.Repository() rp = repo.Repository.from_directory()
rp.add_papers(bibfile) rp.add_papers(bibfile)

@ -1,17 +1,14 @@
# init command # init command
import os import os
try:
import ConfigParser as configparser
except ImportError:
import configparser
from ..repo import Repository from ..repo import Repository
from .. import color from .. import color
def parser(subparsers, config): def parser(subparsers, config):
parser = subparsers.add_parser('init', help="initialize the .papers directory") parser = subparsers.add_parser('init',
help="initialize the .papers directory")
return parser return parser

@ -5,18 +5,19 @@ from .. import pretty
from .. import color from .. import color
from .. import repo from .. import repo
def parser(subparsers, config): def parser(subparsers, config):
parser = subparsers.add_parser('list', help="list all papers") parser = subparsers.add_parser('list', help="list all papers")
return parser return parser
def command(config): def command(config):
rp = repo.Repository() rp = repo.Repository.from_directory()
articles = [] articles = []
for n in range(rp.size()): for n in range(rp.size()):
paper = rp.paper_from_number(n, fatal=True) paper = rp.paper_from_number(n, fatal=True)
bibdesc = pretty.bib_oneliner(paper.bib_data) bibdesc = pretty.bib_oneliner(paper.bibentry)
articles.append((u'{:3d} {}{}{}{} {}'.format(int(paper.number), color.purple, paper.citekey, color.end, (10 - len(paper.citekey))*' ', bibdesc)).encode('utf-8')) articles.append((u'{:3d} {}{}{}{} {}'.format(int(n), color.purple, rp.citekeys[n], color.end, (10 - len(paper.citekey))*' ', bibdesc)).encode('utf-8'))
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf: with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
tmpf.write('\n'.join(articles)) tmpf.write('\n'.join(articles))

@ -10,17 +10,17 @@ def parser(subparsers, config):
return parser return parser
def command(config, citekey): def command(config, citekey):
rp = repo.Repository() rp = repo.Repository.from_directory()
paper = rp.paper_from_any(citekey, fatal = True) paper = rp.paper_from_any(citekey, fatal=True)
try: try:
if paper.check_file(): if paper.check_file():
filepath = paper.get_file_path() filepath = paper.get_file_path()
subprocess.Popen(['open', filepath])
p = subprocess.Popen(['open', filepath])
print('{}{}{} opened.{}'.format( print('{}{}{} opened.{}'.format(
color.filepath, filepath, color.normal, color.end)) color.filepath, filepath, color.normal, color.end))
else:
raise NoDocumentFile
except NoDocumentFile: except NoDocumentFile:
print('{}error{}: No document associated to this entry {}{}{}'.format( print('{}error{}: No document associated to this entry {}{}{}'.format(
color.error, color.normal, color.citekey, citekey, color.end)) color.error, color.normal, color.citekey, citekey, color.end))
exit(-1) exit(-1)

@ -1,11 +1,13 @@
import webbrowser import webbrowser
import urllib import urllib
def parser(subparsers, config): def parser(subparsers, config):
parser = subparsers.add_parser('websearch', help="launch a search on Google Scholar") parser = subparsers.add_parser('websearch', help="launch a search on Google Scholar")
parser.add_argument("search_string", help="the search query (anything googly is possible)") parser.add_argument("search_string", help="the search query (anything googly is possible)")
return parser return parser
def command(config, search_string): def command(config, search_string):
url = 'https://scholar.google.fr/scholar?q={}&lr='.format(urllib.quote_plus(search_string)) url = 'https://scholar.google.fr/scholar?q={}&lr='.format(urllib.quote_plus(search_string))
webbrowser.open(url) webbrowser.open(url)

@ -5,8 +5,6 @@ import re
from pybtex.database import Entry, BibliographyData from pybtex.database import Entry, BibliographyData
import files import files
import color
import pretty
DEFAULT_TYPE = 'article' DEFAULT_TYPE = 'article'
@ -17,6 +15,13 @@ CITEKEY_FORBIDDEN_CHARS = '@\'\\,#}{~%/' # '/' is OK for bibtex but forbidden
CITEKEY_EXCLUDE_RE = re.compile('[%s]' CITEKEY_EXCLUDE_RE = re.compile('[%s]'
% re.escape(CONTROL_CHARS + CITEKEY_FORBIDDEN_CHARS)) % re.escape(CONTROL_CHARS + CITEKEY_FORBIDDEN_CHARS))
BASE_META = {
'filename': None,
'extension': None,
'path': None,
'notes': []
}
def str2citekey(s): def str2citekey(s):
key = unicodedata.normalize('NFKD', unicode(s)).encode('ascii', 'ignore') key = unicodedata.normalize('NFKD', unicode(s)).encode('ascii', 'ignore')
@ -37,14 +42,6 @@ class Paper(object):
biblography data and an additional dictionary to store meta data. biblography data and an additional dictionary to store meta data.
""" """
# @classmethod
# def from_bibpdffiles(cls, pdfpath, bibpath):
# bib_data = cls.import_bibdata(bibpath)
# name, meta = cls.create_meta(bib_data, pdfpath=pdfpath)
# p = Paper(name, bib_data = bib_data, metadata = meta)
#
# return p
def __init__(self, bibentry=None, metadata=None, citekey=None): def __init__(self, bibentry=None, metadata=None, citekey=None):
if not bibentry: if not bibentry:
bibentry = Entry(DEFAULT_TYPE) bibentry = Entry(DEFAULT_TYPE)
@ -54,7 +51,7 @@ class Paper(object):
self.metadata = metadata self.metadata = metadata
# TODO This is not the right way to test that (17/12/2012) # TODO This is not the right way to test that (17/12/2012)
if unicode(citekey) != str2citekey(citekey): if unicode(citekey) != str2citekey(citekey):
raise(ValueError, "Wrong citekey: %s" % citekey) raise(ValueError("Invalid citekey: %s" % citekey))
self.citekey = citekey self.citekey = citekey
def __eq__(self, other): def __eq__(self, other):
@ -84,7 +81,8 @@ class Paper(object):
raise NoDocumentFile raise NoDocumentFile
def check_file(self): def check_file(self):
return files.check_file(self.get_file_path()) path = self.get_file_path()
return os.path.exists(path) and os.path.isfile(path)
def generate_citekey(self): def generate_citekey(self):
"""Generate a citekey from bib_data. """Generate a citekey from bib_data.
@ -98,8 +96,8 @@ class Paper(object):
try: try:
first_author = self.bibentry.persons[author_key][0] first_author = self.bibentry.persons[author_key][0]
except KeyError: except KeyError:
raise(ValueError, raise(ValueError(
'No author or editor defined: cannot generate a citekey.') 'No author or editor defined: cannot generate a citekey.'))
try: try:
year = self.bibentry.fields['year'] year = self.bibentry.fields['year']
except KeyError: except KeyError:
@ -107,48 +105,44 @@ class Paper(object):
citekey = u'{}{}'.format(u''.join(first_author.last()), year) citekey = u'{}{}'.format(u''.join(first_author.last()), year)
return str2citekey(citekey) return str2citekey(citekey)
def set_pdf(self, pdfpath):
fullpdfpath = os.path.abspath(pdfpath)
files.check_file(fullpdfpath)
name, ext = files.name_from_path(pdfpath)
self.metadata['filename'] = name
self.metadata['extension'] = ext
self.metadata['path'] = fullpdfpath
def save_to_disc(self, bib_filepath, meta_filepath): def save_to_disc(self, bib_filepath, meta_filepath):
"""Creates a BibliographyData object containing a single entry and """Creates a BibliographyData object containing a single entry and
saves it to disc. saves it to disc.
""" """
if self.citekey is None: if self.citekey is None:
raise(ValueError, raise(ValueError(
'No valid citekey initialized. Cannot save paper') 'No valid citekey initialized. Cannot save paper'))
bibdata = BibliographyData(entries={self.citekey: self.bibentry}) bibdata = BibliographyData(entries={self.citekey: self.bibentry})
files.save_bibdata(bibdata, bib_filepath) files.save_bibdata(bibdata, bib_filepath)
files.save_meta(self.metadata, meta_filepath) files.save_meta(self.metadata, meta_filepath)
@classmethod @classmethod
def load(cls, bibpath, metapath): def load(cls, bibpath, metapath=None):
bib_data = files.load_externalbibfile(bibpath) key, entry = cls.get_bibentry(bibpath)
metadata = files.read_yamlfile(metapath) if metapath is None:
# Extract first entry (supposed to be the only one) metadata = None
first_key = bib_data.entries.keys()[0] else:
first_entry = bib_data.entries[first_key] metadata = files.read_yamlfile(metapath)
p = Paper(bibentry=first_entry, metadata=metadata, citekey=first_key) p = Paper(bibentry=entry, metadata=metadata, citekey=key)
return p return p
@classmethod @classmethod
def import_bibdata(cls, bibfile): def get_bibentry(cls, bibfile):
"""Import bibligraphic data from a .bibyaml, .bib or .bibtex file""" """Extract first entry (supposed to be the only one) from given file.
fullbibpath = os.path.abspath(bibfile) """
bib_data = files.load_externalbibfile(fullbibpath) bib_data = files.load_externalbibfile(bibfile)
print('{}bibliographic data present in {}{}{}'.format( first_key = bib_data.entries.keys()[0]
color.grey, color.cyan, bibfile, color.end)) first_entry = bib_data.entries[first_key]
print(pretty.bib_desc(bib_data)) return first_key, first_entry
return bib_data
@classmethod @classmethod
def create_meta(cls, pdfpath=None): def create_meta(cls):
if pdfpath is None: return BASE_META.copy()
name, fullpdfpath, ext = None, None, None
else:
fullpdfpath = os.path.abspath(pdfpath)
files.check_file(fullpdfpath)
name, ext = files.name_from_path(pdfpath)
meta = {}
meta['filename'] = name # TODO remove ?
meta['extension'] = ext
meta['path'] = fullpdfpath
meta['notes'] = []
return meta

@ -9,16 +9,15 @@ def person_repr(p):
' '.join(p.last(abbr = False)), ' '.join(p.last(abbr = False)),
' '.join(p.lineage(abbr = True))] if s) ' '.join(p.lineage(abbr = True))] if s)
def bib_oneliner(bib_data): def bib_oneliner(bibentry):
article = bib_data.entries[list(bib_data.entries.keys())[0]] authors = ', '.join(person_repr(p) for p in bibentry.persons['author'])
authors = ', '.join(person_repr(p) for p in article.persons['author']) title = bibentry.fields['title']
title = article.fields['title'] year = bibentry.fields.get('year', '')
year = article.fields.get('year', '')
journal = '' journal = ''
field = 'journal' field = 'journal'
if article.type == 'inproceedings': if bibentry.type == 'inproceedings':
field = 'booktitle' field = 'booktitle'
journal = article.fields.get(field, '') journal = bibentry.fields.get(field, '')
return u'{}{}{} \"{}{}{}\" {}{}{} {}({}{}{}){}'.format( return u'{}{}{} \"{}{}{}\" {}{}{} {}({}{}{}){}'.format(
color.green, authors, color.grey, color.bcyan, title, color.grey, color.green, authors, color.grey, color.bcyan, title, color.grey,
color.yellow, journal, color.end, color.grey, color.end, year, color.yellow, journal, color.end, color.grey, color.end, year,

@ -33,20 +33,20 @@ class Repository(object):
color.error, color.normal, color.citekey, citekey, color.error, color.normal, color.citekey, citekey,
color.end)) color.end))
exit(-1) exit(-1)
raise(IOError, 'file not found') raise(IOError('file not found'))
def paper_from_citekey(self, citekey, fatal=True): def paper_from_citekey(self, citekey, fatal=True):
"""Load a paper by its citekey from disk, if necessary.""" """Load a paper by its citekey from disk, if necessary."""
if citekey in self.citekeys: if citekey in self.citekeys:
return Paper.load(self.path_to_paper_file(citekey, 'bib'), return Paper.load(self.path_to_paper_file(citekey, 'bib'),
self.path_to_paper_file(citekey, 'meta')) metapath=self.path_to_paper_file(citekey, 'meta'))
else: else:
if fatal: if fatal:
print('{}error{}: no paper with citekey {}{}{}'.format( print('{}error{}: no paper with citekey {}{}{}'.format(
color.error, color.normal, color.citekey, citekey, color.error, color.normal, color.citekey, citekey,
color.end)) color.end))
exit(-1) exit(-1)
raise(IOError, 'file not found') raise(IOError('file not found'))
def paper_from_any(self, key, fatal=True): def paper_from_any(self, key, fatal=True):
try: try:
@ -59,20 +59,21 @@ class Repository(object):
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format( print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
color.error, color.normal, color.citekey, key, color.normal, color.end)) color.error, color.normal, color.citekey, key, color.normal, color.end))
exit(-1) exit(-1)
raise(IOError, 'file not found') raise(IOError('file not found'))
# creating new papers # creating new papers
def add_paper_from_paths(self, pdfpath, bibpath): def add_paper_from_paths(self, pdfpath, bibpath):
p = Paper.from_bibpdffiles(pdfpath, bibpath) p = Paper.load(bibpath)
p.set_pdf(pdfpath)
self.add_paper(p) self.add_paper(p)
def add_paper(self, p): def add_paper(self, p):
if p.citekey is None: # TODO also test if citekey is valid if p.citekey is None: # TODO also test if citekey is valid
raise(ValueError, "Invalid citekey: %s." % p.citekey) raise(ValueError("Invalid citekey: %s." % p.citekey))
elif p.citekey in self.citekeys: elif p.citekey in self.citekeys:
raise(ValueError, "Citekey already exists in repository: %s" raise(ValueError("Citekey already exists in repository: %s"
% p.citekey) % p.citekey))
self.citekeys.append(p.citekey) self.citekeys.append(p.citekey)
# write paper files # write paper files
self.save_paper(p) self.save_paper(p)
@ -89,14 +90,14 @@ class Repository(object):
def save_paper(self, paper): def save_paper(self, paper):
if not paper.citekey in self.citekeys: if not paper.citekey in self.citekeys:
raise(ValueError, 'Paper not in repository, first add it.') raise(ValueError('Paper not in repository, first add it.'))
paper.save_to_disc(self.path_to_paper_file(paper.citekey, 'bib'), paper.save_to_disc(self.path_to_paper_file(paper.citekey, 'bib'),
self.path_to_paper_file(paper.citekey, 'meta')) self.path_to_paper_file(paper.citekey, 'meta'))
def add_papers(self, bibpath): def add_papers(self, bibpath):
bib_data = Paper.import_bibdata(bibpath) bib_data = files.load_externalbibfile(bibpath)
for k in bib_data.entries: for k in bib_data.entries:
sub_bib = type(bib_data)(preamble=bib_data._preamble) sub_bib = bib_data.entries[k]
p = Paper(bibentry=sub_bib, citekey=k) p = Paper(bibentry=sub_bib, citekey=k)
self.add_paper(p) self.add_paper(p)
@ -135,11 +136,11 @@ class Repository(object):
elif file_ == 'meta': elif file_ == 'meta':
return os.path.join(self.papersdir, META_DIR, citekey + '.meta') return os.path.join(self.papersdir, META_DIR, citekey + '.meta')
else: else:
raise(ValueError, "%s is not a valid paper file." % file_) raise(ValueError("%s is not a valid paper file." % file_))
@classmethod @classmethod
def from_directory(cls, papersdir=None): def from_directory(cls, papersdir=None):
repo = cls.__init__(papersdir=papersdir) repo = cls(papersdir=papersdir)
repo.load() repo.load()
return repo return repo

@ -68,7 +68,7 @@ class TestSaveLoad(unittest.TestCase):
self.dest_metafile = os.path.join(self.tmpdir, 'written_meta.yaml') self.dest_metafile = os.path.join(self.tmpdir, 'written_meta.yaml')
def test_load_valid(self): def test_load_valid(self):
p = Paper.load(self.bibfile, self.metafile) p = Paper.load(self.bibfile, metapath=self.metafile)
self.assertEqual(fixtures.turing1950, p) self.assertEqual(fixtures.turing1950, p)
def test_save_fails_with_no_citekey(self): def test_save_fails_with_no_citekey(self):

Loading…
Cancel
Save