From e9cb26f2e085230d14496f15be5ad6b2bb6e8c1e Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Tue, 2 Jul 2013 18:01:41 +0100 Subject: [PATCH] core file updated for python 3 --- papers/beets_ui.py | 11 +++++++---- papers/configs.py | 4 ++-- papers/files.py | 17 +++++++++-------- papers/p3.py | 4 ++++ papers/paper.py | 20 ++++++++++---------- papers/plugin.py | 2 +- papers/repo.py | 8 ++++---- papers/ui.py | 2 ++ 8 files changed, 39 insertions(+), 29 deletions(-) diff --git a/papers/beets_ui.py b/papers/beets_ui.py index 0840caf..32ba501 100644 --- a/papers/beets_ui.py +++ b/papers/beets_ui.py @@ -15,9 +15,12 @@ # included in all copies or substantial portions of the Software. -import locale +#import locale import sys -from ConfigParser import NoOptionError + +from . import p3 +from p3 import input +from p3 import configparser from . import configs @@ -34,7 +37,7 @@ def _encoding(config): # Configured override? try: return config.get(configs.MAIN_SECTION, 'terminal-encoding') - except NoOptionError: + except configparser.NoOptionError: # Determine from locale settings. try: return locale.getdefaultlocale()[1] or 'utf8' @@ -54,7 +57,7 @@ def input_(): # use print() explicitly to display prompts. # http://bugs.python.org/issue1927 try: - resp = raw_input() + resp = input() except EOFError: raise UserError('stdin stream ended while input required') return resp.decode(sys.stdin.encoding or 'utf8', 'ignore') diff --git a/papers/configs.py b/papers/configs.py index 5a9fbdf..a46ba3b 100644 --- a/papers/configs.py +++ b/papers/configs.py @@ -1,6 +1,6 @@ import os import copy -from p3 import configparser +from .p3 import configparser # constant stuff (DFT = DEFAULT) @@ -33,7 +33,7 @@ BOOLEANS = {'import_copy', 'import_move', 'color'} _config = None def config(section = MAIN_SECTION): if _config is None: - raise ValueError, 'not config instanciated yet' + raise ValueError('not config instanciated yet') _config._section = section return _config diff --git a/papers/files.py b/papers/files.py index b83a89a..8d0d386 100644 --- a/papers/files.py +++ b/papers/files.py @@ -2,12 +2,13 @@ This module can't depend on configs. If you feel the need to import configs, you are not in the right place. """ - +from __future__ import print_function import os import subprocess import tempfile -from cStringIO import StringIO +from .p3 import io +from io import StringIO import yaml @@ -65,9 +66,9 @@ def name_from_path(fullpdfpath, verbose=False): def check_file(path, fail=False): if fail: if not os.path.exists(path): - raise(IOError, "File does not exist: %s." % path) + raise IOError("File does not exist: {}.".format(path)) if not os.path.isfile(path): - raise(IOError, "%s is not a file." % path) + raise IOError("{} is not a file.".format(path)) return True else: return os.path.exists(path) and os.path.isfile(path) @@ -129,7 +130,7 @@ def load_meta(filepath): def load_externalbibfile(fullbibpath): check_file(fullbibpath, fail=True) filename, ext = os.path.splitext(os.path.split(fullbibpath)[1]) - if ext[1:] in FORMATS_INPUT.keys(): + if ext[1:] in list(FORMATS_INPUT.keys()): with open(fullbibpath) as f: return _parse_bibdata_formated_stream(f, ext[1:]) else: @@ -144,11 +145,11 @@ def _parse_bibdata_formated_stream(stream, fmt): try: parser = FORMATS_INPUT[fmt].Parser() data = parser.parse_stream(stream) - if data.entries.keys() > 0: + if len(list(data.entries.keys())) > 0: return data except Exception: pass - raise ValueError, 'content format is not recognized.' + raise ValueError('content format is not recognized.') def parse_bibdata(content, format_ = None): """Parse bib data from string or stream. @@ -170,7 +171,7 @@ def parse_bibdata(content, format_ = None): except Exception: pass - raise ValueError, 'content format is not recognized.' + raise ValueError('content format is not recognized.') def editor_input(editor, initial="", suffix=None): diff --git a/papers/p3.py b/papers/p3.py index d4b8702..a9aa1fa 100644 --- a/papers/p3.py +++ b/papers/p3.py @@ -7,3 +7,7 @@ if sys.version_info[0] == 2: else: import configparser import io + +configparser = configparser +io = io +input = input \ No newline at end of file diff --git a/papers/paper.py b/papers/paper.py index 679ada0..a5885db 100644 --- a/papers/paper.py +++ b/papers/paper.py @@ -36,7 +36,7 @@ def get_bibentry_from_file(bibfile): """Extract first entry (supposed to be the only one) from given file. """ bib_data = files.load_externalbibfile(bibfile) - first_key = bib_data.entries.keys()[0] + first_key = list(bib_data.entries.keys())[0] first_entry = bib_data.entries[first_key] return first_key, first_entry @@ -45,7 +45,7 @@ def get_bibentry_from_string(content): """Extract first entry (supposed to be the only one) from given file. """ bib_data = files.parse_bibdata(StringIO(content)) - first_key = bib_data.entries.keys()[0] + first_key = list(bib_data.entries.keys())[0] first_entry = bib_data.entries[first_key] return first_key, first_entry @@ -88,7 +88,7 @@ def get_safe_metadata_from_path(metapath): def check_citekey(citekey): # TODO This is not the right way to test that (17/12/2012) if unicode(citekey) != str2citekey(citekey): - raise(ValueError("Invalid citekey: %s" % citekey)) + raise ValueError("Invalid citekey: %s" % citekey) class NoDocumentFile(Exception): @@ -157,8 +157,8 @@ class Paper(object): try: first_author = self.bibentry.persons[author_key][0] except KeyError: - raise(ValueError( - 'No author or editor defined: cannot generate a citekey.')) + raise ValueError( + 'No author or editor defined: cannot generate a citekey.') try: year = self.bibentry.fields['year'] except KeyError: @@ -171,8 +171,8 @@ class Paper(object): saves it to disc. """ if self.citekey is None: - raise(ValueError( - 'No valid citekey initialized. Cannot save paper')) + raise ValueError( + 'No valid citekey initialized. Cannot save paper') bibdata = BibliographyData(entries={self.citekey: self.bibentry}) files.save_bibdata(bibdata, bib_filepath) files.save_meta(self.metadata, meta_filepath) @@ -208,7 +208,7 @@ class Paper(object): f = '/' + f return f except (KeyError, IndexError): - raise(NoDocumentFile('No file found in bib data.')) + raise NoDocumentFile('No file found in bib data.') def copy(self): return Paper(bibentry=copy_bibentry(self.bibentry), @@ -247,7 +247,7 @@ class Paper(object): try: papers.append(Paper(bibentry=b.entries[k], citekey=k)) except ValueError, e: - print "Warning, skipping paper (%s)." % e + print('Warning, skipping paper ({}).'.format(e)) return papers @@ -260,7 +260,7 @@ class Paper(object): @tags.setter def tags(self, value): if not hasattr(value, '__iter__'): - raise ValueError, 'tags must be iterables' + raise ValueError('tags must be iterables') self.metadata['tags'] = set(value) def add_tag(self, tag): diff --git a/papers/plugin.py b/papers/plugin.py index f5479a6..34ee947 100644 --- a/papers/plugin.py +++ b/papers/plugin.py @@ -37,7 +37,7 @@ class PapersPlugin(object): This is a basic example """ for s in strings: - print s + print(s) @classmethod def get_instance(cls): diff --git a/papers/repo.py b/papers/repo.py index 8f67424..b248fb8 100644 --- a/papers/repo.py +++ b/papers/repo.py @@ -97,7 +97,7 @@ class Repository(object): try: return self.citekeys[int(ref)] except (IndexError, ValueError): - raise(InvalidReference) + raise InvalidReference # papers @@ -116,7 +116,7 @@ class Repository(object): def add_paper(self, p, overwrite = False): if p.citekey is None: # TODO also test if citekey is valid - raise(ValueError("Invalid citekey: %s." % p.citekey)) + raise ValueError("Invalid citekey: {}.".format(p.citekey)) if not overwrite and p.citekey in self: raise CiteKeyCollision('citekey {} already in use'.format( p.citekey)) @@ -178,7 +178,7 @@ class Repository(object): def save_paper(self, paper): if not paper.citekey in self: - raise(ValueError('Paper not in repository, first add it.')) + raise ValueError('Paper not in repository, first add it.') paper.save(self._bibfile(paper.citekey), self._metafile(paper.citekey)) @@ -199,7 +199,7 @@ class Repository(object): def import_document(self, citekey, doc_file): if citekey not in self.citekeys: - raise(ValueError, "Unknown citekey: %s." % citekey) + raise ValueError("Unknown citekey: {}.".format(citekey)) else: if not os.path.isfile(doc_file): raise ValueError("No file {} found.".format(doc_file)) diff --git a/papers/ui.py b/papers/ui.py index fa6c1a4..9093021 100644 --- a/papers/ui.py +++ b/papers/ui.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import sys from .beets_ui import _encoding, input_