commit
7d5f7e54c3
@ -0,0 +1,16 @@
|
|||||||
|
[header]
|
||||||
|
title = remove configparser for internal mapping
|
||||||
|
id = ac8c05c020d251c6fa9ac5fc239038837b636b3b
|
||||||
|
status = closed
|
||||||
|
type = bug
|
||||||
|
author = Fabien Benureau
|
||||||
|
mail = fabien.benureau+git@gmail.com
|
||||||
|
date = 2012-10-11 at 07:25 UCT
|
||||||
|
|
||||||
|
[eventlog]
|
||||||
|
opened[0] = opened the 2012-10-11 at 07:25 UCT by Fabien Benureau
|
||||||
|
closed[1] = closed the 2012-10-11 at 17:57(UCT) by Fabien Benureau
|
||||||
|
|
||||||
|
[discussion]
|
||||||
|
desc = # enter your description here
|
||||||
|
|
@ -1,30 +1,26 @@
|
|||||||
try:
|
|
||||||
import ConfigParser as configparser
|
|
||||||
except ImportError:
|
|
||||||
import configparser
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from .. import files
|
|
||||||
from .. import color
|
from .. import color
|
||||||
|
from .. import repo
|
||||||
|
from ..paper import NoDocumentFile
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('open', help="open the paper in a pdf viewer")
|
parser = subparsers.add_parser('open', help='{}open the paper in a pdf viewer{}'.format(color.normal, color.end))
|
||||||
parser.add_argument("citekey", help="the paper associated citekey")
|
parser.add_argument('citekey', help='{}the paper associated citekey{}'.format(color.normal, color.end))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def command(config, citekey):
|
def command(config, citekey):
|
||||||
papers = files.load_papers()
|
rp = repo.Repository()
|
||||||
|
paper = rp.paper_from_any(citekey, fatal = True)
|
||||||
try:
|
try:
|
||||||
filename = papers.get('papers', str(citekey))
|
if paper.check_file():
|
||||||
except configparser.NoOptionError:
|
filepath = paper.get_file_path()
|
||||||
try:
|
|
||||||
ck = papers.get('citekeys', 'ck'+str(citekey))
|
p = subprocess.Popen(['open', filepath])
|
||||||
filename = papers.get('papers', str(ck))
|
print('{}{}{} opened.{}'.format(
|
||||||
except configparser.NoOptionError:
|
color.filepath, filepath, color.normal, color.end))
|
||||||
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
|
except NoDocumentFile:
|
||||||
color.red, color.grey, color.cyan, citekey, color.grey, color.end))
|
print('{}error{}: No document associated to this entry {}{}{}'.format(
|
||||||
exit(-1)
|
color.error, color.normal, color.citekey, citekey, color.end))
|
||||||
meta_data = files.load_meta(filename)
|
exit(-1)
|
||||||
filepath = meta_data.get('metadata', 'path')
|
|
||||||
p = subprocess.Popen(['open', filepath])
|
|
||||||
print('{}{}{} opened.{}'.format(color.cyan, filepath, color.grey, color.end))
|
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import files
|
||||||
|
import color
|
||||||
|
import pretty
|
||||||
|
|
||||||
|
class Paper(object):
|
||||||
|
"""Paper class. The object is responsible for the integrity of its own data,
|
||||||
|
and for loading and writing it to disc.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_disc(cls, name, citekey = None, number = None):
|
||||||
|
bib_data = files.load_bibdata(name)
|
||||||
|
metadata = files.load_meta(name)
|
||||||
|
p = Paper(name, bib_data = bib_data, metadata = metadata,
|
||||||
|
citekey = citekey, number = number)
|
||||||
|
return p
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_bibpdffiles(cls, pdfpath, bibpath):
|
||||||
|
bib_data = cls.import_bibdata(bibpath)
|
||||||
|
name, meta = cls.create_meta(pdfpath, bib_data)
|
||||||
|
p = Paper(name, bib_data = bib_data, metadata = meta)
|
||||||
|
|
||||||
|
return p
|
||||||
|
|
||||||
|
def __init__(self, name, bib_data = None, metadata = None,
|
||||||
|
citekey = None, number = None):
|
||||||
|
self.name = name
|
||||||
|
self.bib_data = bib_data
|
||||||
|
self.metadata = metadata
|
||||||
|
self.citekey = citekey
|
||||||
|
self.number = number
|
||||||
|
|
||||||
|
def save_to_disc(self):
|
||||||
|
files.save_bibdata(self.bib_data, self.name)
|
||||||
|
files.save_meta(self.metadata, self.name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def import_bibdata(cls, bibfile):
|
||||||
|
"""Import bibligraphic data from a .bibyaml, .bib or .bibtex file"""
|
||||||
|
fullbibpath = os.path.abspath(bibfile)
|
||||||
|
|
||||||
|
bib_data = files.load_externalbibfile(fullbibpath)
|
||||||
|
print('{}bibliographic data present in {}{}{}'.format(
|
||||||
|
color.grey, color.cyan, bibfile, color.end))
|
||||||
|
print(pretty.bib_desc(bib_data))
|
||||||
|
|
||||||
|
return bib_data
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_meta(cls, pdfpath, bib_data):
|
||||||
|
|
||||||
|
fullpdfpath = os.path.abspath(pdfpath)
|
||||||
|
files.check_file(fullpdfpath)
|
||||||
|
|
||||||
|
name, ext = files.name_from_path(pdfpath)
|
||||||
|
|
||||||
|
meta = {}
|
||||||
|
|
||||||
|
meta['name'] = name
|
||||||
|
meta['extension'] = ext
|
||||||
|
meta['path'] = fullpdfpath
|
||||||
|
|
||||||
|
meta['notes'] = []
|
||||||
|
|
||||||
|
return name, meta
|
@ -0,0 +1,100 @@
|
|||||||
|
import files
|
||||||
|
import color
|
||||||
|
from paper import Paper
|
||||||
|
|
||||||
|
alphabet = 'abcdefghijklmopqrstuvwxyz'
|
||||||
|
|
||||||
|
|
||||||
|
class Repository(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.paperdir = files.find_papersdir()
|
||||||
|
self.papers_config = files.load_papers()
|
||||||
|
self.citekeys = self.papers_config['citekeys']
|
||||||
|
self.numbers = self.papers_config['numbers']
|
||||||
|
|
||||||
|
# loading existing papers
|
||||||
|
|
||||||
|
def paper_from_number(self, number, fatal = True):
|
||||||
|
try:
|
||||||
|
citekey = self.numbers[int(number)]
|
||||||
|
paper = self.paper_from_citekey(citekey)
|
||||||
|
paper.number = int(number)
|
||||||
|
return paper
|
||||||
|
except KeyError:
|
||||||
|
if fatal:
|
||||||
|
print('{}error{}: no paper with number {}{}{}'.format(
|
||||||
|
color.error, color.normal, color.citekey, citekey, color.end))
|
||||||
|
exit(-1)
|
||||||
|
raise IOError, 'file not found'
|
||||||
|
|
||||||
|
def paper_from_citekey(self, citekey, fatal = True):
|
||||||
|
"""Load a paper by its citekey from disk, if necessary."""
|
||||||
|
try:
|
||||||
|
name = self.citekeys[citekey]
|
||||||
|
paper = Paper.from_disc(name, citekey = citekey)
|
||||||
|
paper.citekey = citekey
|
||||||
|
return paper
|
||||||
|
except KeyError:
|
||||||
|
if fatal:
|
||||||
|
print('{}error{}: no paper with citekey {}{}{}'.format(
|
||||||
|
color.error, color.normal, color.citekey, citekey, color.end))
|
||||||
|
exit(-1)
|
||||||
|
raise IOError, 'file not found'
|
||||||
|
|
||||||
|
def paper_from_any(self, key, fatal = True):
|
||||||
|
try:
|
||||||
|
return self.paper_from_citekey(key, fatal = False)
|
||||||
|
except IOError:
|
||||||
|
try:
|
||||||
|
return self.paper_from_number(key, fatal = False)
|
||||||
|
except IOError:
|
||||||
|
if fatal:
|
||||||
|
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
|
||||||
|
color.error, color.normal, color.citekey, key, color.normal, color.end))
|
||||||
|
exit(-1)
|
||||||
|
raise IOError, 'file not found'
|
||||||
|
|
||||||
|
# creating new papers
|
||||||
|
|
||||||
|
def add_paper(self, pdfpath, bibpath):
|
||||||
|
|
||||||
|
p = Paper.from_bibpdffiles(pdfpath, bibpath)
|
||||||
|
|
||||||
|
# updating papersconfig
|
||||||
|
p.citekey = self.create_citekey(p.bib_data)
|
||||||
|
p.number = self.create_number()
|
||||||
|
|
||||||
|
self.papers_config['citekeys'][p.citekey] = p.name
|
||||||
|
self.papers_config['numbers'][p.number] = p.citekey
|
||||||
|
|
||||||
|
self.citekeys[p.citekey] = p.name
|
||||||
|
self.numbers[p.number] = p.citekey
|
||||||
|
|
||||||
|
# writing all to disk
|
||||||
|
files.save_papers(self.papers_config)
|
||||||
|
p.save_to_disc()
|
||||||
|
|
||||||
|
return p
|
||||||
|
|
||||||
|
def create_citekey(self, bib_data, allowed = tuple()):
|
||||||
|
"""Create a cite key unique to a given bib_data"""
|
||||||
|
article = bib_data.entries[list(bib_data.entries.keys())[0]]
|
||||||
|
first_author = article.persons['author'][0]
|
||||||
|
year = article.fields['year']
|
||||||
|
prefix = '{}{}'.format(first_author.last()[0][:6], year[2:])
|
||||||
|
|
||||||
|
letter = 0
|
||||||
|
citekey = None
|
||||||
|
|
||||||
|
citekey = prefix
|
||||||
|
while citekey in self.citekeys and citekey not in allowed:
|
||||||
|
citekey = prefix + alphabet[letter]
|
||||||
|
letter += 1
|
||||||
|
|
||||||
|
return citekey
|
||||||
|
|
||||||
|
def create_number(self):
|
||||||
|
count = int(self.papers_config['count'])
|
||||||
|
self.papers_config['count'] = count + 1
|
||||||
|
return count
|
Loading…
Reference in new issue