From 4f4a58d81c22cfedee27555cb2c495fa0df54ed5 Mon Sep 17 00:00:00 2001 From: jgrizou Date: Fri, 5 Jul 2013 21:49:21 +0200 Subject: [PATCH] Texnote robust. Should implement test now. --- papers/plugs/texnote/default_body.tex | 26 ++++ .../texnote/{style.sty => default_style.sty} | 0 papers/plugs/texnote/template.tex | 20 --- papers/plugs/texnote/texnote.py | 123 +++++++++--------- papers/repo.py | 8 +- 5 files changed, 91 insertions(+), 86 deletions(-) create mode 100644 papers/plugs/texnote/default_body.tex rename papers/plugs/texnote/{style.sty => default_style.sty} (100%) delete mode 100644 papers/plugs/texnote/template.tex diff --git a/papers/plugs/texnote/default_body.tex b/papers/plugs/texnote/default_body.tex new file mode 100644 index 0000000..b7b9dc4 --- /dev/null +++ b/papers/plugs/texnote/default_body.tex @@ -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} +%} diff --git a/papers/plugs/texnote/style.sty b/papers/plugs/texnote/default_style.sty similarity index 100% rename from papers/plugs/texnote/style.sty rename to papers/plugs/texnote/default_style.sty diff --git a/papers/plugs/texnote/template.tex b/papers/plugs/texnote/template.tex deleted file mode 100644 index 41aa3e0..0000000 --- a/papers/plugs/texnote/template.tex +++ /dev/null @@ -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} diff --git a/papers/plugs/texnote/texnote.py b/papers/plugs/texnote/texnote.py index 65bd4de..934ffcf 100644 --- a/papers/plugs/texnote/texnote.py +++ b/papers/plugs/texnote/texnote.py @@ -11,34 +11,36 @@ from ...commands.helpers import add_references_argument, parse_reference from ...events import RemoveEvent, RenameEvent, AddEvent -TEXNOTE_SECTION = 'texnote' -TEXNOTE_DIR = os.path.join(config().papers_dir, 'texnote') -TEXNOTE_TEMPLATE = os.path.join(TEXNOTE_DIR, 'template.tex') -TEXNOTE_STYLE = os.path.join(TEXNOTE_DIR, 'style.sty') +SECTION = 'texnote' +DIR = os.path.join(config().papers_dir, 'texnote') +TPL_DIR = os.path.join(DIR, 'template') +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') -TEXNOTE_DEFAULT_STYLE = os.path.join(os.path.dirname(__file__), 'style.sty') +DFT_BODY = os.path.join(os.path.dirname(__file__), 'default_body.tex') +DFT_STYLE = os.path.join(os.path.dirname(__file__), 'default_style.sty') class TexnotePlugin(PapersPlugin): def __init__(self): - self.name = TEXNOTE_SECTION + self.name = SECTION self.texcmds = collections.OrderedDict([ ('remove', self.remove), ('edit', self.edit), - ('edit_style', self.edit_style), ('edit_template', self.edit_template), ]) def _ensure_init(self): - if not files.check_directory(TEXNOTE_DIR): - os.mkdir(TEXNOTE_DIR) - if not files.check_file(TEXNOTE_TEMPLATE): - shutil.copy(TEXNOTE_DEFAULT_TEMPLATE, TEXNOTE_TEMPLATE) - if not files.check_file(TEXNOTE_STYLE): - shutil.copy(TEXNOTE_DEFAULT_STYLE, TEXNOTE_STYLE) + if not files.check_directory(DIR): + os.mkdir(DIR) + if not files.check_directory(TPL_DIR): + os.mkdir(TPL_DIR) + if not files.check_file(TPL_BODY): + shutil.copy(DFT_BODY, TPL_BODY) + if not files.check_file(TPL_STYLE): + shutil.copy(DFT_STYLE, TPL_STYLE) def parser(self, subparsers): 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, help='command to use to open the file') 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 p = sub.add_parser('edit_template', help='edit the latex template used by texnote') p.add_argument('-w', '--with', dest='with_command', default=None, 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 def command(self, args): @@ -71,55 +75,53 @@ class TexnotePlugin(PapersPlugin): self.texcmds[texcmd](**vars(args)) def _texfile(self, citekey): - return os.path.join(TEXNOTE_DIR, citekey + '.tex') + return os.path.join(DIR, citekey + '.tex') def _ensure_texfile(self, 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): self._ensure_texfile(citekey) - with open(self._texfile(citekey)) as f: text = f.read() - - # modify with bib info rp = repo.Repository(config()) - paper = rp.get_paper(citekey) - fields = paper.bibentry.fields - persons = paper.bibentry.persons + if citekey in rp: + paper = rp.get_paper(citekey) + fields = paper.bibentry.fields + persons = paper.bibentry.persons - if 'title' in fields: - title_str = fields['title'] - text = text.replace("TITLE", title_str) + if 'title' in fields: + title_str = fields['title'] + text = text.replace("TITLE", title_str) - if 'year' in fields: - year_str = fields['year'] - text = text.replace("YEAR", year_str) + if 'year' in fields: + year_str = fields['year'] + text = text.replace("YEAR", year_str) - if 'abstract' in fields: - abstract_str = fields['abstract'] - text = text.replace("ABSTRACT", abstract_str) + if 'abstract' in fields: + abstract_str = fields['abstract'] + text = text.replace("ABSTRACT", abstract_str) - if 'author' in persons: - authors = [] - for author in persons['author']: - authors.append(format_author(author)) - author_str = concatenate_authors(authors) - text = text.replace("AUTHOR", author_str) + if 'author' in persons: + authors = [] + for author in persons['author']: + authors.append(format_author(author)) + author_str = concatenate_authors(authors) + text = text.replace("AUTHOR", author_str) - with open(self._texfile(citekey), "w") as f: - f.write(text) + with open(self._texfile(citekey), "w") as f: + f.write(text) def get_texfile(self, citekey): """ 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) return self._texfile(citekey) def get_edit_cmd(self): 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): if view is not None: @@ -131,33 +133,26 @@ class TexnotePlugin(PapersPlugin): citekey = parse_reference(ui, rp, reference) 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: with_command = self.get_edit_cmd() - files.edit_file(with_command, TEXNOTE_STYLE, temporary=False) - - def edit_template(self, ui, with_command=None): - if with_command is None: - with_command = self.get_edit_cmd() - files.edit_file(with_command, TEXNOTE_TEMPLATE, temporary=False) + if body is not None: + files.edit_file(with_command, TPL_BODY, temporary=False) + if style is not None: + files.edit_file(with_command, TPL_STYLE, temporary=False) def create(self, citekey): self._autofill_texfile(citekey) - def remove(self, citekey): - try: - os.remove(self._texfile(citekey)) - except OSError: - pass # For some reason, the texnote file didn't exist + def remove(self, reference, ui=None): + citekey = reference + if ui is not None: + rp = repo.Repository(config()) + citekey = parse_reference(ui, rp, reference) + os.remove(self.get_texfile(citekey)) def rename(self, old_citekey, new_citekey, overwrite=False): - if files.check_file(self._texfile(old_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)) + shutil.move(self.get_texfile(old_citekey), self.get_texfile(new_citekey)) @AddEvent.listen() diff --git a/papers/repo.py b/papers/repo.py index 9f74d09..d721322 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -100,8 +100,11 @@ class Repository(object): def get_paper(self, citekey): """Load a paper by its citekey from disk, if necessary.""" - return PaperInRepo.load(self, self._bibfile(citekey), - self._metafile(citekey)) + if citekey in self.citekeys: + return PaperInRepo.load(self, self._bibfile(citekey), + self._metafile(citekey)) + else: + raise InvalidReference def _add_citekey(self, citekey): if citekey not in self.citekeys: @@ -162,6 +165,7 @@ class Repository(object): self._write_paper(paper) else: 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._move_doc(old_citekey, paper) RenameEvent(paper, old_citekey).send()