imcomplete implementation of Repository, Paper classes
This commit is contained in:
parent
fc65d2e76e
commit
b413be687d
@ -7,6 +7,7 @@ except ImportError:
|
|||||||
from .. import color
|
from .. import color
|
||||||
from .. import files
|
from .. import files
|
||||||
from .. import pretty
|
from .. import pretty
|
||||||
|
from .. import repo
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -28,7 +29,7 @@ def command(config, pdffile, bibfile):
|
|||||||
files.check_file(fullbibpath)
|
files.check_file(fullbibpath)
|
||||||
|
|
||||||
filename, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
filename, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||||
if ext != '.pdf' and ext != '.ps':
|
if ext != '.pdf' and ext != '.ps':
|
||||||
print('{}warning{}: extention {}{}{} not recognized{}'.format(
|
print('{}warning{}: extention {}{}{} not recognized{}'.format(
|
||||||
color.yellow, color.grey, color.cyan, ext, color.grey, color.end))
|
color.yellow, color.grey, color.cyan, ext, color.grey, color.end))
|
||||||
|
|
||||||
@ -53,8 +54,8 @@ def command(config, pdffile, bibfile):
|
|||||||
papers.set('header', 'count', int(count) + 1)
|
papers.set('header', 'count', int(count) + 1)
|
||||||
|
|
||||||
citekey = pretty.create_citekey(bib_data)
|
citekey = pretty.create_citekey(bib_data)
|
||||||
papers.set('papers', citekey, filename)
|
papers.set('citekeys', citekey, filename)
|
||||||
papers.set('citekeys', 'ck' + count, citekey)
|
papers.set('numbers', 'ck' + count, citekey)
|
||||||
|
|
||||||
files.write_papers(papers)
|
files.write_papers(papers)
|
||||||
files.write_meta(meta, filename)
|
files.write_meta(meta, filename)
|
||||||
|
@ -29,8 +29,8 @@ def command(config):
|
|||||||
papers = configparser.ConfigParser()
|
papers = configparser.ConfigParser()
|
||||||
papers.add_section('header')
|
papers.add_section('header')
|
||||||
papers.set('header', 'count', 0)
|
papers.set('header', 'count', 0)
|
||||||
papers.add_section('papers')
|
|
||||||
papers.add_section('citekeys')
|
papers.add_section('citekeys')
|
||||||
|
papers.add_section('numbers')
|
||||||
files.write_papers(papers)
|
files.write_papers(papers)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -1,24 +1,21 @@
|
|||||||
from .. import files
|
|
||||||
from .. import pretty
|
|
||||||
from .. import color
|
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
from .. import pretty
|
||||||
|
from .. import color
|
||||||
|
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):
|
||||||
papers = files.load_papers()
|
rp = repo.Repository()
|
||||||
|
|
||||||
articles = []
|
articles = []
|
||||||
for p in papers.options('citekeys'):
|
for n in rp.numbers:
|
||||||
number = p[2:]
|
paper = paper_from_number(n, fatal = True)
|
||||||
citekey = papers.get('citekeys', p)
|
bibdesc = pretty.bib_oneliner(paper.bibdata)
|
||||||
filename = papers.get('papers', citekey)
|
|
||||||
bibdata = files.load_bibdata(filename + '.bibyaml')
|
|
||||||
bibdesc = pretty.bib_oneliner(bibdata)
|
|
||||||
articles.append('{:3d} {}{}{}{} {}'.format(int(number), color.purple, citekey, color.end, (8-len(citekey))*' ', bibdesc))
|
articles.append('{:3d} {}{}{}{} {}'.format(int(number), color.purple, citekey, color.end, (8-len(citekey))*' ', bibdesc))
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
|
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
|
||||||
|
@ -1,30 +1,17 @@
|
|||||||
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
|
||||||
|
|
||||||
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()
|
||||||
try:
|
paper = rp.paper_from_any(citekey, fatal = True)
|
||||||
filename = papers.get('papers', str(citekey))
|
filepath = paper.metadata.get('metadata', 'path')
|
||||||
except configparser.NoOptionError:
|
|
||||||
try:
|
|
||||||
ck = papers.get('citekeys', 'ck'+str(citekey))
|
|
||||||
filename = papers.get('papers', str(ck))
|
|
||||||
except configparser.NoOptionError:
|
|
||||||
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
|
|
||||||
color.red, color.grey, color.cyan, citekey, color.grey, color.end))
|
|
||||||
exit(-1)
|
|
||||||
meta_data = files.load_meta(filename)
|
|
||||||
filepath = meta_data.get('metadata', 'path')
|
|
||||||
p = subprocess.Popen(['open', filepath])
|
p = subprocess.Popen(['open', filepath])
|
||||||
print('{}{}{} opened.{}'.format(color.cyan, filepath, color.grey, color.end))
|
print('{}{}{} opened.{}'.format(color.filepath, filepath, color.normal, color.end))
|
@ -110,7 +110,7 @@ def load_papers():
|
|||||||
return read_configfile(find_papersdir() + os.sep + 'papers')
|
return read_configfile(find_papersdir() + os.sep + 'papers')
|
||||||
|
|
||||||
def load_bibdata(filename):
|
def load_bibdata(filename):
|
||||||
fullbibpath = find_papersdir() + os.sep + 'bibdata' + os.sep + filename
|
fullbibpath = find_papersdir() + os.sep + 'bibdata' + os.sep + filename + '.bibyaml'
|
||||||
return load_externalbibfile(fullbibpath)
|
return load_externalbibfile(fullbibpath)
|
||||||
|
|
||||||
def write_bibdata(bib_data, filename):
|
def write_bibdata(bib_data, filename):
|
||||||
|
23
papers/paper.py
Normal file
23
papers/paper.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import files
|
||||||
|
import color
|
||||||
|
|
||||||
|
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):
|
||||||
|
p = Paper(name)
|
||||||
|
self.bib_data = files.load_bibdata(self.name)
|
||||||
|
self.metadata = files.load_meta(self.name)
|
||||||
|
self.citekey = self.metadata.get('metadata', 'citekey')
|
||||||
|
self.number = self.metadata.get('metadata', 'number')
|
||||||
|
return p
|
||||||
|
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,33 +26,3 @@ def bib_desc(bib_data):
|
|||||||
s += '\n'
|
s += '\n'
|
||||||
s += '\n'.join('{}: {}'.format(k, v) for k, v in article.fields.items())
|
s += '\n'.join('{}: {}'.format(k, v) for k, v in article.fields.items())
|
||||||
return s
|
return s
|
||||||
|
|
||||||
alphabet = 'abcdefghijklmopqrstuvwxyz'
|
|
||||||
|
|
||||||
try:
|
|
||||||
import ConfigParser as configparser
|
|
||||||
except ImportError:
|
|
||||||
import configparser
|
|
||||||
import files
|
|
||||||
|
|
||||||
def create_citekey(bib_data):
|
|
||||||
"""Create a cite key unique to the paper"""
|
|
||||||
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:])
|
|
||||||
|
|
||||||
papers = files.load_papers()
|
|
||||||
letter = 0, False
|
|
||||||
citekey = None
|
|
||||||
|
|
||||||
citekey = prefix
|
|
||||||
while not letter[1]:
|
|
||||||
try:
|
|
||||||
papers.get('papers', citekey)
|
|
||||||
citekey = prefix + alphabet[letter[0]]
|
|
||||||
letter = letter[0]+1, False
|
|
||||||
except configparser.NoOptionError:
|
|
||||||
letter = letter[0], True
|
|
||||||
|
|
||||||
return citekey
|
|
||||||
|
130
papers/repo.py
Normal file
130
papers/repo.py
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
try:
|
||||||
|
import ConfigParser as configparser
|
||||||
|
except ImportError:
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
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 = dict(ck, None for ck in self.paper_config.options('citekeys')
|
||||||
|
self.numbers = sorted(n[2:] for n in self.paper_config.options('numbers'))
|
||||||
|
|
||||||
|
# loading existing papers
|
||||||
|
|
||||||
|
def paper_from_number(self, number, fatal = True):
|
||||||
|
try:
|
||||||
|
citekey = self.papers_config.get('numbers', 'ck'+number)
|
||||||
|
return self.load_paper(citekey)
|
||||||
|
except configparser.NoOptionError:
|
||||||
|
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:
|
||||||
|
paper = self.citekeys[citekey]
|
||||||
|
if paper is None:
|
||||||
|
name = self.papers_config.get('citekeys', citekey)
|
||||||
|
paper = Paper.from_disc(name)
|
||||||
|
self.citekeys[citekey] = paper
|
||||||
|
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 rp.paper_from_citekey(key, fatal = False)
|
||||||
|
except IOError:
|
||||||
|
try:
|
||||||
|
return rp.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, pdffile, bibfile):
|
||||||
|
|
||||||
|
fullpdfpath = os.path.abspath(pdffile)
|
||||||
|
fullbibpath = os.path.abspath(bibfile)
|
||||||
|
files.check_file(fullpdfpath)
|
||||||
|
files.check_file(fullbibpath)
|
||||||
|
|
||||||
|
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||||
|
if ext != '.pdf' and ext != '.ps':
|
||||||
|
print('{}warning{}: extension {}{}{} not recognized{}'.format(
|
||||||
|
color.yellow, color.grey, color.cyan, ext, color.grey, color.end))
|
||||||
|
|
||||||
|
# creating meta file
|
||||||
|
meta = create_meta(fullpdfpath, name, ext, bib_data)
|
||||||
|
|
||||||
|
# creating bibyaml file
|
||||||
|
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))
|
||||||
|
|
||||||
|
# updating papersconfig
|
||||||
|
citekey = pretty.create_citekey(bib_data)
|
||||||
|
papers.set('citekeys', citekey, name)
|
||||||
|
papers.set('numbers', 'ck' + count, citekey)
|
||||||
|
|
||||||
|
# writing all to disk
|
||||||
|
files.write_bibdata(bib_data, name)
|
||||||
|
files.write_papers(papers)
|
||||||
|
files.write_meta(meta, name)
|
||||||
|
|
||||||
|
def create_meta(self, path, name, ext, bib_data):
|
||||||
|
citekey = create_citekey(bib_data, allowed = (,))
|
||||||
|
number = create_number()
|
||||||
|
|
||||||
|
meta = configparser.ConfigParser()
|
||||||
|
meta.add_section('metadata')
|
||||||
|
|
||||||
|
meta.set('metadata', 'name', name)
|
||||||
|
meta.set('metadata', 'extension', ext)
|
||||||
|
meta.set('metadata', 'path', os.path.normpath(fullpdfpath))
|
||||||
|
|
||||||
|
meta.add_section('notes')
|
||||||
|
|
||||||
|
return meta
|
||||||
|
|
||||||
|
def create_citekey(self, bib_data, allowed = (,)):
|
||||||
|
"""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, False
|
||||||
|
citekey = None
|
||||||
|
|
||||||
|
citekey = prefix
|
||||||
|
while citekey in self.citekeys and citekey not in allowed:
|
||||||
|
citekey = prefix + alphabet[letter[0]]
|
||||||
|
letter += 1
|
||||||
|
|
||||||
|
return citekey
|
||||||
|
|
||||||
|
def create_number(self, bib_data, allowed = []):
|
||||||
|
count = self.papers_config.get('header', 'count')
|
||||||
|
self.papers_config.set('header', 'count', count + 1)
|
||||||
|
return count
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user