From 344029ca278b9cb6ebe015b3f06b168726b7047c Mon Sep 17 00:00:00 2001 From: Phyks Date: Fri, 2 May 2014 12:46:37 +0200 Subject: [PATCH] Added function to open file by id + try / except when writing index file --- README.md | 3 +-- backend.py | 65 ++++++++++++++++++++++++++++++++++++------------------ main.py | 25 +++++++++++++++++++++ 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index dd50cb7..7d04434 100644 --- a/README.md +++ b/README.md @@ -107,14 +107,13 @@ Here are some sources of inspirations for this project : A list of ideas and TODO. Don't hesitate to give feedback on the ones you really want or to propose your owns. -20. No DOI for HAL 30. Parameter to disable remote search -40. Open file 45. Doc / Man 50. Webserver interface 60. Categories 70. Edit an entry instead of deleting it and adding it again 80. Automatic download of new arXiv version +90. No DOI for HAL => thanks all for metadata with SOAP APIā€¦ don't want to handle it for now :/ ## Issues ? diff --git a/backend.py b/backend.py index 0103b8d..60b2147 100644 --- a/backend.py +++ b/backend.py @@ -53,17 +53,25 @@ def bibtexAppend(data): data is a dict for one entry in bibtex, as the one from bibtexparser output """ - with open(params.folder+'index.bib', 'a') as fh: - fh.write(parsed2Bibtex(data)+"\n") + try: + with open(params.folder+'index.bib', 'a') as fh: + fh.write(parsed2Bibtex(data)+"\n") + except: + tools.warning("Unable to open index file.") + return False def bibtexEdit(ident, modifs): """Update ident key in bibtex file, modifications are in modifs dict""" - with open(params.folder+'index.bib', 'r') as fh: - bibtex = BibTexParser(fh.read(), - customization=homogeneize_latex_encoding) - bibtex = bibtex.get_entry_dict() + try: + with open(params.folder+'index.bib', 'r') as fh: + bibtex = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + bibtex = bibtex.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False for key in modifs.keys(): bibtex[ident][key] = modifs[key] @@ -78,16 +86,24 @@ def bibtexRewrite(data): bibtex = '' for entry in data.keys(): bibtex += parsed2Bibtex(data[entry])+"\n" - with open(params.folder+'index.bib', 'w') as fh: - fh.write(bibtex) + try: + with open(params.folder+'index.bib', 'w') as fh: + fh.write(bibtex) + except: + tools.warning("Unable to open index file.") + return False def deleteId(ident): """Delete a file based on its id in the bibtex file""" - with open(params.folder+'index.bib', 'r') as fh: - bibtex = BibTexParser(fh.read(), - customization=homogeneize_latex_encoding) - bibtex = bibtex.get_entry_dict() + try: + with open(params.folder+'index.bib', 'r') as fh: + bibtex = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + bibtex = bibtex.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False if ident not in bibtex.keys(): return False @@ -108,10 +124,14 @@ def deleteId(ident): def deleteFile(filename): """Delete a file based on its filename""" - with open(params.folder+'index.bib', 'r') as fh: - bibtex = BibTexParser(fh.read(), - customization=homogeneize_latex_encoding) - bibtex = bibtex.get_entry_dict() + try: + with open(params.folder+'index.bib', 'r') as fh: + bibtex = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + bibtex = bibtex.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False found = False for key in bibtex.keys(): @@ -141,11 +161,14 @@ def diffFilesIndex(): """ files = tools.listDir(params.folder) - with open(params.folder+'index.bib', 'r') as fh: - index = BibTexParser(fh.read(), - customization=homogeneize_latex_encoding) - - index_diff = index.get_entry_dict() + try: + with open(params.folder+'index.bib', 'r') as fh: + index = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + index_diff = index.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False for key in index_diff.keys(): if index_diff[key]['file'] not in files: diff --git a/main.py b/main.py index 89cdd53..e65edbe 100755 --- a/main.py +++ b/main.py @@ -155,6 +155,23 @@ def downloadFile(url, filetype): return False +def openFile(ident): + try: + with open(params.folder+'index.bib', 'r') as fh: + bibtex = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + bibtex = bibtex.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False + + if ident not in bibtex.keys(): + return False + else: + subprocess.Popen(['xdg-open', bibtex[ident]['file']]) + return True + + def resync(): diff = backend.diffFilesIndex() @@ -284,6 +301,14 @@ if __name__ == '__main__': elif sys.argv[1] == 'search': raise Exception('TODO') + elif sys.argv[1] == 'open': + if len(sys.argv) < 3: + sys.exit("Usage: " + sys.argv[0] + + " open ID") + if not openFile(sys.argv[2]): + sys.exit("Unable to open file associated " + + "to ident "+sys.argv[2]) + elif sys.argv[1] == 'resync': if len(sys.argv) > 2 and sys.argv[2] == 'help': sys.exit("Usage: " + sys.argv[0] + " resync")