From d0c71b43671de40930438acde622cec1943c214b Mon Sep 17 00:00:00 2001 From: hut Date: Wed, 3 Dec 2014 23:38:48 +0100 Subject: ext.papermanager: implemented writing to .paperinfo --- ranger/ext/papermanager.py | 78 +++++++++++++++++++++++++++++++++++-- ranger/gui/widgets/browsercolumn.py | 2 +- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/ranger/ext/papermanager.py b/ranger/ext/papermanager.py index 6c422659..932505c9 100644 --- a/ranger/ext/papermanager.py +++ b/ranger/ext/papermanager.py @@ -52,15 +52,87 @@ class PaperManager(object): return result def set_paper_info(self, filename, update_dict): - pass + result = None + found = False + valid = (filename, basename(filename)) + first_metafile = None + + if not self.deep_search: + metafile = next(self._get_metafile_names(filename)) + return self._set_pager_info_raw(filename, update_dict, metafile) + + for i, metafile in enumerate(self._get_metafile_names(filename)): + if i == 0: + first_metafile = metafile + + csvfile = None + try: + csvfile = open(metafile, "r") + except: + # .paperinfo file doesn't exist... look for another one. + pass + else: + reader = csv.reader(csvfile, skipinitialspace=True) + for row in reader: + name, year, title, authors, url = row + if name in valid: + return self._set_pager_info_raw(filename, update_dict, + metafile) + finally: + if csvfile: + csvfile.close() + + # No .paperinfo file found, so let's create a new one in the same path + # as the given file. + if first_metafile: + return self._set_pager_info_raw(filename, update_dict, first_metafile) + + def _set_pager_info_raw(self, filename, update_dict, metafile): + valid = (filename, basename(filename)) + + try: + with open(metafile, "r") as infile: + reader = csv.reader(infile, skipinitialspace=True) + rows = list(reader) + except IOError: + rows = [] + + with open(metafile, "w") as outfile: + writer = csv.writer(outfile) + found = False + + # Iterate through all rows and write them back to the file. + for row in rows: + if not found and row[0] in valid: + # When finding the row that corresponds to the given filename, + # update the items with the information from update_dict. + self._fill_row_with_ostruct(row, update_dict) + found = True + writer.writerow(row) + + # If the row was never found, create a new one. + if not found: + row = [basename(filename), None, None, None, None] + self._fill_row_with_ostruct(row, update_dict) + writer.writerow(row) + + def _fill_row_with_ostruct(self, row, update_dict): + for key, value in update_dict.items(): + if key == "year": + row[1] = value + elif key == "title": + row[2] = value + elif key == "authors": + row[3] = value + elif key == "url": + row[4] = value def _get_metafile_content(self, metafile): if metafile in self.metafile_cache: return self.metafile_cache[metafile] else: if exists(metafile): - reader = csv.reader(open(metafile, "r"), - skipinitialspace=True) + reader = csv.reader(open(metafile, "r"), skipinitialspace=True) entries = list(entry for entry in reader if len(entry) == 5) self.metafile_cache[metafile] = entries diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py index 3757af4b..12523648 100644 --- a/ranger/gui/widgets/browsercolumn.py +++ b/ranger/gui/widgets/browsercolumn.py @@ -310,7 +310,7 @@ class BrowserColumn(Pager): if use_linemode == "filename": infostring = self._draw_infostring_display(drawn, space) elif use_linemode == "papertitle": - if paperinfo: + if paperinfo and paperinfo.authors: authorstring = paperinfo.authors if ',' in authorstring: authorstring = authorstring[0:authorstring.find(",")] -- cgit 1.4.1-2-gfad0