Texnote robust. Should implement test now.

main
jgrizou 12 years ago
parent 248055ded9
commit 4f4a58d81c

@ -0,0 +1,26 @@
%DO_NOT_MODIFY{
%This file is the starting point for all notes
%TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile
%All texnote much share the same style so that we can compile them all together without problem
%You can edit the style file using: papers texnote edit_template -S
\documentclass{article}
\usepackage{template/style}
\begin{document}
\begin{center}
\Large{\textbf{TITLE}} \\ [0.2cm]
\small{\textsc{AUTHOR}} \\ [0.2cm]
\normalsize{\textsc{YEAR}} \\ [1cm]
\end{center}
\begin{abstract}
ABSTRACT
\end{abstract}
%You can edit the following of this file using: papers texnote edit_template -B
%}
%WRITE YOUR NOTES FROM HERE
%\section{Notes}
% Write your notes here
%DO_NOT_MODIFY{
\end{document}
%}

@ -1,20 +0,0 @@
\documentclass{article}
\usepackage{style}
\begin{document}
\begin{center}
\Large{\textbf{TITLE}} \\ [0.2cm]
\small{\textsc{AUTHOR}} \\ [0.2cm]
\normalsize{\textsc{YEAR}} \\ [1cm]
\end{center}
\begin{abstract}
ABSTRACT
\end{abstract}
\section{Notes}
Write your notes here.
\end{document}

@ -11,34 +11,36 @@ from ...commands.helpers import add_references_argument, parse_reference
from ...events import RemoveEvent, RenameEvent, AddEvent from ...events import RemoveEvent, RenameEvent, AddEvent
TEXNOTE_SECTION = 'texnote' SECTION = 'texnote'
TEXNOTE_DIR = os.path.join(config().papers_dir, 'texnote') DIR = os.path.join(config().papers_dir, 'texnote')
TEXNOTE_TEMPLATE = os.path.join(TEXNOTE_DIR, 'template.tex') TPL_DIR = os.path.join(DIR, 'template')
TEXNOTE_STYLE = os.path.join(TEXNOTE_DIR, 'style.sty') TPL_BODY = os.path.join(TPL_DIR, 'body.tex')
TPL_STYLE = os.path.join(TPL_DIR, 'style.sty')
TEXNOTE_DEFAULT_TEMPLATE = os.path.join(os.path.dirname(__file__), 'template.tex') DFT_BODY = os.path.join(os.path.dirname(__file__), 'default_body.tex')
TEXNOTE_DEFAULT_STYLE = os.path.join(os.path.dirname(__file__), 'style.sty') DFT_STYLE = os.path.join(os.path.dirname(__file__), 'default_style.sty')
class TexnotePlugin(PapersPlugin): class TexnotePlugin(PapersPlugin):
def __init__(self): def __init__(self):
self.name = TEXNOTE_SECTION self.name = SECTION
self.texcmds = collections.OrderedDict([ self.texcmds = collections.OrderedDict([
('remove', self.remove), ('remove', self.remove),
('edit', self.edit), ('edit', self.edit),
('edit_style', self.edit_style),
('edit_template', self.edit_template), ('edit_template', self.edit_template),
]) ])
def _ensure_init(self): def _ensure_init(self):
if not files.check_directory(TEXNOTE_DIR): if not files.check_directory(DIR):
os.mkdir(TEXNOTE_DIR) os.mkdir(DIR)
if not files.check_file(TEXNOTE_TEMPLATE): if not files.check_directory(TPL_DIR):
shutil.copy(TEXNOTE_DEFAULT_TEMPLATE, TEXNOTE_TEMPLATE) os.mkdir(TPL_DIR)
if not files.check_file(TEXNOTE_STYLE): if not files.check_file(TPL_BODY):
shutil.copy(TEXNOTE_DEFAULT_STYLE, TEXNOTE_STYLE) shutil.copy(DFT_BODY, TPL_BODY)
if not files.check_file(TPL_STYLE):
shutil.copy(DFT_STYLE, TPL_STYLE)
def parser(self, subparsers): def parser(self, subparsers):
parser = subparsers.add_parser(self.name, help='edit advance note in latex') parser = subparsers.add_parser(self.name, help='edit advance note in latex')
@ -53,14 +55,16 @@ class TexnotePlugin(PapersPlugin):
p.add_argument('-w', '--with', dest='with_command', default=None, p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file') help='command to use to open the file')
add_references_argument(p, single=True) add_references_argument(p, single=True)
# edit_style
p = sub.add_parser('edit_style', help='edit the latex style used by texnote')
p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file')
#edit_template #edit_template
p = sub.add_parser('edit_template', help='edit the latex template used by texnote') p = sub.add_parser('edit_template', help='edit the latex template used by texnote')
p.add_argument('-w', '--with', dest='with_command', default=None, p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file') help='command to use to open the file')
p.add_argument('-B', '--body', action='store_true',
help='edit the main body', default=None)
p.add_argument('-S', '--style', action='store_true',
help='open the style', default=None)
p.add_argument('-H', '--header', action='store_true',
help='open the header', default=None)
return parser return parser
def command(self, args): def command(self, args):
@ -71,20 +75,18 @@ class TexnotePlugin(PapersPlugin):
self.texcmds[texcmd](**vars(args)) self.texcmds[texcmd](**vars(args))
def _texfile(self, citekey): def _texfile(self, citekey):
return os.path.join(TEXNOTE_DIR, citekey + '.tex') return os.path.join(DIR, citekey + '.tex')
def _ensure_texfile(self, citekey): def _ensure_texfile(self, citekey):
if not files.check_file(self._texfile(citekey)): if not files.check_file(self._texfile(citekey)):
shutil.copy(TEXNOTE_TEMPLATE, self._texfile(citekey)) shutil.copy(TPL_BODY, self._texfile(citekey))
def _autofill_texfile(self, citekey): def _autofill_texfile(self, citekey):
self._ensure_texfile(citekey) self._ensure_texfile(citekey)
with open(self._texfile(citekey)) as f: with open(self._texfile(citekey)) as f:
text = f.read() text = f.read()
# modify with bib info
rp = repo.Repository(config()) rp = repo.Repository(config())
if citekey in rp:
paper = rp.get_paper(citekey) paper = rp.get_paper(citekey)
fields = paper.bibentry.fields fields = paper.bibentry.fields
persons = paper.bibentry.persons persons = paper.bibentry.persons
@ -113,13 +115,13 @@ class TexnotePlugin(PapersPlugin):
def get_texfile(self, citekey): def get_texfile(self, citekey):
""" This function returns the name of the texfile and """ This function returns the name of the texfile and
ensure it exist and it is filled with info from the bibfile""" ensure it exist and it is filled with info from the bibfile if possible"""
self._autofill_texfile(citekey) self._autofill_texfile(citekey)
return self._texfile(citekey) return self._texfile(citekey)
def get_edit_cmd(self): def get_edit_cmd(self):
default = config().edit_cmd default = config().edit_cmd
return config(TEXNOTE_SECTION).get('edit_cmd', default) return config(SECTION).get('edit_cmd', default)
def edit(self, ui, reference, view=None, with_command=None): def edit(self, ui, reference, view=None, with_command=None):
if view is not None: if view is not None:
@ -131,33 +133,26 @@ class TexnotePlugin(PapersPlugin):
citekey = parse_reference(ui, rp, reference) citekey = parse_reference(ui, rp, reference)
files.edit_file(with_command, self.get_texfile(citekey), temporary=False) files.edit_file(with_command, self.get_texfile(citekey), temporary=False)
def edit_style(self, ui, with_command=None): def edit_template(self, ui, body=None, style=None, header=None, with_command=None):
if with_command is None: if with_command is None:
with_command = self.get_edit_cmd() with_command = self.get_edit_cmd()
files.edit_file(with_command, TEXNOTE_STYLE, temporary=False) if body is not None:
files.edit_file(with_command, TPL_BODY, temporary=False)
def edit_template(self, ui, with_command=None): if style is not None:
if with_command is None: files.edit_file(with_command, TPL_STYLE, temporary=False)
with_command = self.get_edit_cmd()
files.edit_file(with_command, TEXNOTE_TEMPLATE, temporary=False)
def create(self, citekey): def create(self, citekey):
self._autofill_texfile(citekey) self._autofill_texfile(citekey)
def remove(self, citekey): def remove(self, reference, ui=None):
try: citekey = reference
os.remove(self._texfile(citekey)) if ui is not None:
except OSError: rp = repo.Repository(config())
pass # For some reason, the texnote file didn't exist citekey = parse_reference(ui, rp, reference)
os.remove(self.get_texfile(citekey))
def rename(self, old_citekey, new_citekey, overwrite=False): def rename(self, old_citekey, new_citekey, overwrite=False):
if files.check_file(self._texfile(old_citekey)): shutil.move(self.get_texfile(old_citekey), self.get_texfile(new_citekey))
if not files.check_file(self._texfile(new_citekey)) or overwrite:
shutil.move(self._texfile(old_citekey), self._texfile(new_citekey))
else:
raise ValueError('citekey {} already exist'.format(new_citekey))
else:
raise ValueError('citekey {} does not exist'.format(old_citekey))
@AddEvent.listen() @AddEvent.listen()

@ -100,8 +100,11 @@ class Repository(object):
def get_paper(self, citekey): def get_paper(self, citekey):
"""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:
return PaperInRepo.load(self, self._bibfile(citekey), return PaperInRepo.load(self, self._bibfile(citekey),
self._metafile(citekey)) self._metafile(citekey))
else:
raise InvalidReference
def _add_citekey(self, citekey): def _add_citekey(self, citekey):
if citekey not in self.citekeys: if citekey not in self.citekeys:
@ -162,6 +165,7 @@ class Repository(object):
self._write_paper(paper) self._write_paper(paper)
else: else:
self._add_paper(paper, overwrite=overwrite) # This checks for collisions self._add_paper(paper, overwrite=overwrite) # This checks for collisions
# We do not want to send the RemoveEvent, associated documents should be moved
self._remove_paper(old_citekey, remove_doc=False) self._remove_paper(old_citekey, remove_doc=False)
self._move_doc(old_citekey, paper) self._move_doc(old_citekey, paper)
RenameEvent(paper, old_citekey).send() RenameEvent(paper, old_citekey).send()

Loading…
Cancel
Save