finished implementing the classes
a lowercase bug remain that force to move to another format than configparser. Which is just as well.
This commit is contained in:
parent
b413be687d
commit
cdd2796638
15
.pit/pit-ac8c05c020d251c6
Normal file
15
.pit/pit-ac8c05c020d251c6
Normal file
@ -0,0 +1,15 @@
|
||||
[header]
|
||||
title = remove configparser for internal mapping
|
||||
id = ac8c05c020d251c6fa9ac5fc239038837b636b3b
|
||||
status = open
|
||||
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
|
||||
|
||||
[discussion]
|
||||
desc = # enter your description here
|
||||
|
@ -21,41 +21,5 @@ def command(config, pdffile, bibfile):
|
||||
:param pdffilepath path (no url yet) to a pdf or ps file
|
||||
:param bibtex bibtex file (in .bib, .bibml or .yaml format.
|
||||
"""
|
||||
papersdir = files.find_papersdir()
|
||||
|
||||
fullpdfpath = os.path.abspath(pdffile)
|
||||
fullbibpath = os.path.abspath(bibfile)
|
||||
files.check_file(fullpdfpath)
|
||||
files.check_file(fullbibpath)
|
||||
|
||||
filename, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||
if ext != '.pdf' and ext != '.ps':
|
||||
print('{}warning{}: extention {}{}{} not recognized{}'.format(
|
||||
color.yellow, color.grey, color.cyan, ext, color.grey, color.end))
|
||||
|
||||
meta = configparser.ConfigParser()
|
||||
meta.add_section('metadata')
|
||||
|
||||
meta.set('metadata', 'filename', filename)
|
||||
meta.set('metadata', 'extension', ext)
|
||||
meta.set('metadata', 'path', os.path.normpath(fullpdfpath))
|
||||
|
||||
meta.add_section('notes')
|
||||
|
||||
if bibfile is not None:
|
||||
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))
|
||||
files.write_bibdata(bib_data, filename)
|
||||
|
||||
papers = files.load_papers()
|
||||
count = papers.get('header', 'count')
|
||||
papers.set('header', 'count', int(count) + 1)
|
||||
|
||||
citekey = pretty.create_citekey(bib_data)
|
||||
papers.set('citekeys', citekey, filename)
|
||||
papers.set('numbers', 'ck' + count, citekey)
|
||||
|
||||
files.write_papers(papers)
|
||||
files.write_meta(meta, filename)
|
||||
rp = repo.Repository()
|
||||
rp.add_paper(pdffile, bibfile)
|
@ -14,9 +14,9 @@ def command(config):
|
||||
|
||||
articles = []
|
||||
for n in rp.numbers:
|
||||
paper = paper_from_number(n, fatal = True)
|
||||
paper = rp.paper_from_number(n, fatal = True)
|
||||
bibdesc = pretty.bib_oneliner(paper.bibdata)
|
||||
articles.append('{:3d} {}{}{}{} {}'.format(int(number), color.purple, citekey, color.end, (8-len(citekey))*' ', bibdesc))
|
||||
articles.append('{:3d} {}{}{}{} {}'.format(int(paper.number), color.purple, citekey, color.end, (8-len(paper.citekey))*' ', bibdesc))
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".tmp", delete=True) as tmpf:
|
||||
tmpf.write('\n'.join(articles))
|
||||
|
@ -49,6 +49,13 @@ def find_papersdir():
|
||||
|
||||
return _papersdir
|
||||
|
||||
def name_from_path(fullpdfpath, verbose = False):
|
||||
name, ext = os.path.splitext(os.path.split(fullpdfpath)[1])
|
||||
if verbose:
|
||||
if ext != '.pdf' and ext != '.ps':
|
||||
print('{}warning{}: extension {}{}{} not recognized{}'.format(
|
||||
color.yellow, color.grey, color.cyan, ext, color.grey, color.end))
|
||||
return name, ext
|
||||
|
||||
def check_file(filepath):
|
||||
if not os.path.exists(filepath):
|
||||
|
@ -1,5 +1,12 @@
|
||||
import os
|
||||
try:
|
||||
import ConfigParser as configparser
|
||||
except ImportError:
|
||||
import configparser
|
||||
|
||||
import files
|
||||
import color
|
||||
import pretty
|
||||
|
||||
class Paper(object):
|
||||
"""Paper class. The object is responsible for the integrity of its own data,
|
||||
@ -7,17 +14,61 @@ class Paper(object):
|
||||
"""
|
||||
|
||||
@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')
|
||||
def from_disc(cls, name, citekey = None, number = None):
|
||||
bib_data = files.load_bibdata(self.name)
|
||||
metadata = files.load_meta(self.name)
|
||||
p = Paper(name, bib_data = bib_data, metadata = metadata,
|
||||
citekey = citekey, number = number)
|
||||
return p
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_bibpdffiles(cls, pdfpath, bibpath):
|
||||
bib_data = cls.import_bibdata(bibpath)
|
||||
name, meta = cls.import_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.write_bibdata(self.bib_data, self.name)
|
||||
files.write_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)
|
||||
files.check_file(fullbibpath)
|
||||
|
||||
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 import_meta(cls, pdfpath, bib_data):
|
||||
|
||||
fullpdfpath = os.path.abspath(pdfpath)
|
||||
files.check_file(fullpdfpath)
|
||||
|
||||
name, ext = files.name_from_path(pdfpath)
|
||||
|
||||
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 name, meta
|
||||
|
@ -3,6 +3,8 @@ try:
|
||||
except ImportError:
|
||||
import configparser
|
||||
|
||||
import files
|
||||
import color
|
||||
from paper import Paper
|
||||
|
||||
alphabet = 'abcdefghijklmopqrstuvwxyz'
|
||||
@ -13,19 +15,19 @@ 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'))
|
||||
self.citekeys = dict((ck, name) for ck, name in self.papers_config.items('citekeys'))
|
||||
self.numbers = sorted(n[2:] for n in self.papers_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)
|
||||
return self.paper_from_citekey(citekey)
|
||||
except configparser.NoOptionError:
|
||||
if fatal:
|
||||
print('{}error{}: no paper with number {}{}{}'.format(
|
||||
color.error, color.normal, color.citekey, citekey, color.end)
|
||||
color.error, color.normal, color.citekey, citekey, color.end))
|
||||
exit(-1)
|
||||
raise IOError, 'file not found'
|
||||
|
||||
@ -35,22 +37,22 @@ class Repository(object):
|
||||
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
|
||||
paper = Paper.from_disc(name, citekey = citekey)
|
||||
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)
|
||||
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)
|
||||
return self.paper_from_citekey(key, fatal = False)
|
||||
except IOError:
|
||||
try:
|
||||
return rp.paper_from_number(key, fatal = False)
|
||||
return self.paper_from_number(key, fatal = False)
|
||||
except IOError:
|
||||
if fatal:
|
||||
print('{}error{}: paper with citekey or number {}{}{} not found{}'.format(
|
||||
@ -60,53 +62,28 @@ class Repository(object):
|
||||
|
||||
# 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)
|
||||
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.set('citekeys', p.citekey, p.name)
|
||||
self.papers_config.set('numbers', 'ck' + str(p.number), p.citekey)
|
||||
|
||||
self.citekeys[p.citekey] = p.name
|
||||
self.numbers.append(str(p.number))
|
||||
self.numbers.sort()
|
||||
|
||||
# writing all to disk
|
||||
files.write_bibdata(bib_data, name)
|
||||
files.write_papers(papers)
|
||||
files.write_meta(meta, name)
|
||||
files.write_papers(self.papers_config)
|
||||
p.save_to_disc()
|
||||
|
||||
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
|
||||
return p
|
||||
|
||||
def create_citekey(self, bib_data, allowed = (,)):
|
||||
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]
|
||||
@ -123,8 +100,7 @@ class Repository(object):
|
||||
|
||||
return citekey
|
||||
|
||||
def create_number(self, bib_data, allowed = []):
|
||||
count = self.papers_config.get('header', 'count')
|
||||
def create_number(self):
|
||||
count = self.papers_config.getint('header', 'count')
|
||||
self.papers_config.set('header', 'count', count + 1)
|
||||
return count
|
||||
|
||||
return count
|
Loading…
x
Reference in New Issue
Block a user