From 01fd368c9792dd9794684c0bddb553a4695572e6 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Sat, 19 Apr 2014 16:44:28 +0200 Subject: [PATCH 1/5] Fix import and except syntax for python3. --- pubs/beets_ui.py | 3 +-- pubs/commands/__init__.py | 28 ++++++++++++++-------------- pubs/commands/add_cmd.py | 2 +- pubs/commands/attach_cmd.py | 4 ++-- pubs/repo.py | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/pubs/beets_ui.py b/pubs/beets_ui.py index bb839bc..1adf87a 100644 --- a/pubs/beets_ui.py +++ b/pubs/beets_ui.py @@ -18,8 +18,7 @@ import locale import sys -from . import p3 -from p3 import input +from .p3 import input class UserError(Exception): diff --git a/pubs/commands/__init__.py b/pubs/commands/__init__.py index a68ab16..60f514f 100644 --- a/pubs/commands/__init__.py +++ b/pubs/commands/__init__.py @@ -1,19 +1,19 @@ # core -import init_cmd -import add_cmd -import rename_cmd -import remove_cmd -import list_cmd +from . import init_cmd +from . import add_cmd +from . import rename_cmd +from . import remove_cmd +from . import list_cmd # doc -import attach_cmd -import open_cmd -import tag_cmd -import note_cmd +from . import attach_cmd +from . import open_cmd +from . import tag_cmd +from . import note_cmd # bulk -import export_cmd -import import_cmd +from . import export_cmd +from . import import_cmd # bonus -import websearch_cmd +from . import websearch_cmd -import edit_cmd -# import update_cmd +from . import edit_cmd +# from . import update_cmd diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 69861e5..b12dcef 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -112,6 +112,6 @@ def command(args): try: p.docpath = docfile rp.push_paper(p) - except ValueError, v: + except ValueError as v: ui.error(v.message) ui.exit(1) diff --git a/pubs/commands/attach_cmd.py b/pubs/commands/attach_cmd.py index dc5e6bf..0ecf1d8 100644 --- a/pubs/commands/attach_cmd.py +++ b/pubs/commands/attach_cmd.py @@ -39,9 +39,9 @@ def command(args): pass # TODO warn if file does not exists paper.docpath = document rp.push_paper(paper, overwrite=True, event=False) - except ValueError, v: + except ValueError as v: ui.error(v.message) ui.exit(1) - except IOError, v: + except IOError as v: ui.error(v.message) ui.exit(1) diff --git a/pubs/repo.py b/pubs/repo.py index 3c2058a..1f21b70 100644 --- a/pubs/repo.py +++ b/pubs/repo.py @@ -3,7 +3,7 @@ from datetime import datetime from . import bibstruct from . import events -from datacache import DataCache +from .datacache import DataCache from .paper import Paper From 571162a54d9e0f7525fd19a9f6177f1fe1809eb0 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Sat, 19 Apr 2014 17:27:42 +0200 Subject: [PATCH 2/5] Adds renamed libs for python3. --- pubs/bibstruct.py | 2 +- pubs/content.py | 10 ++++------ pubs/filebroker.py | 6 +++--- pubs/p3.py | 8 ++++++++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pubs/bibstruct.py b/pubs/bibstruct.py index c3e481e..c8f942a 100644 --- a/pubs/bibstruct.py +++ b/pubs/bibstruct.py @@ -1,7 +1,7 @@ import unicodedata import re -from .p3 import ustr +from .p3 import ustr, unichr # citekey stuff diff --git a/pubs/content.py b/pubs/content.py index 8b89b31..f113f9b 100644 --- a/pubs/content.py +++ b/pubs/content.py @@ -3,9 +3,7 @@ import subprocess import tempfile import shutil -import urlparse -import httplib -import urllib2 +from .p3 import urlparse, HTTPConnection, urlopen # files i/o @@ -62,7 +60,7 @@ def system_path(path): # dealing with formatless content def content_type(path): - parsed = urlparse.urlparse(path) + parsed = urlparse(path) if parsed.scheme == 'http': return 'url' else: @@ -70,8 +68,8 @@ def content_type(path): def url_exists(url): - parsed = urlparse.urlparse(url) - conn = httplib.HTTPConnection(parsed.netloc) + parsed = urlparse(url) + conn = HTTPConnection(parsed.netloc) conn.request('HEAD', parsed.path) response = conn.getresponse() conn.close() diff --git a/pubs/filebroker.py b/pubs/filebroker.py index fe1218c..73b3ad6 100644 --- a/pubs/filebroker.py +++ b/pubs/filebroker.py @@ -1,6 +1,6 @@ import os import re -import urlparse +from .p3 import urlparse from .content import (check_file, check_directory, read_file, write_file, system_path, check_content, content_type, get_content) @@ -122,7 +122,7 @@ class DocBroker(object): def in_docsdir(self, docpath): try: - parsed = urlparse.urlparse(docpath) + parsed = urlparse(docpath) except Exception: return False return parsed.scheme == self.scheme @@ -136,7 +136,7 @@ class DocBroker(object): Return absoluted paths of regular ones otherwise. """ if self.in_docsdir(docpath): - parsed = urlparse.urlparse(docpath) + parsed = urlparse(docpath) if parsed.path == '': docpath = os.path.join(self.docdir, parsed.netloc) else: diff --git a/pubs/p3.py b/pubs/p3.py index 4897960..ed9c406 100644 --- a/pubs/p3.py +++ b/pubs/p3.py @@ -5,11 +5,19 @@ if sys.version_info[0] == 2: import StringIO as io input = raw_input ustr = unicode + from urlparse import urlparse + from urllib2 import urlopen + from httplib import HTTPConnection else: import configparser import io ustr = str + from urllib.parse import urlparse + from urllib.request import urlopen + from http.client import HTTPConnection + unichr = chr configparser = configparser io = io input = input +unichr = unichr From bf2cce1c13ae9d0c2752d40621e41419dd9cc721 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Sat, 19 Apr 2014 18:09:46 +0200 Subject: [PATCH 3/5] Only use print_function. --- tests/test_endecoder.py | 6 ++++-- tests/test_usecase.py | 17 +++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/test_endecoder.py b/tests/test_endecoder.py index f176f9b..4dd72be 100644 --- a/tests/test_endecoder.py +++ b/tests/test_endecoder.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from __future__ import print_function import unittest import yaml @@ -8,6 +9,7 @@ from pubs import endecoder from str_fixtures import bibtex_raw0, metadata_raw0, turing_bib + def compare_yaml_str(s1, s2): if s1 == s2: return True @@ -72,9 +74,9 @@ class TestEnDecode(unittest.TestCase): biblines = turing_bib.splitlines() biblines.insert(-3, keyword_str) bibsrc = '\n'.join(biblines) - print bibsrc + print(bibsrc) entry = decoder.decode_bibdata(bibsrc)['turing1950computing'] - print entry + print(entry) self.assertNotIn(u'keywords', entry) self.assertIn(u'keyword', entry) self.assertEqual(set(keywords), set(entry[u'keyword'])) diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 3856624..bf60fe6 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -1,3 +1,4 @@ +from __future__ import print_function import unittest import re import os @@ -145,9 +146,9 @@ class TestList(DataCommandTestCase): 'pubs list', ] outs = self.execute_cmds(cmds) - print outs[1].splitlines() + print(outs[1].splitlines()) self.assertEquals(0, len(outs[1].splitlines())) - print outs[3].splitlines() + print(outs[3].splitlines()) self.assertEquals(1, len(outs[3].splitlines())) def test_list_several_no_date(self): @@ -162,11 +163,11 @@ class TestList(DataCommandTestCase): 'pubs list', ] outs = self.execute_cmds(cmds) - print outs[0].splitlines() + print(outs[0].splitlines()) self.assertEquals(4, len(outs[0].splitlines())) - print outs[2].splitlines() + print(outs[2].splitlines()) self.assertEquals(3, len(outs[2].splitlines())) - print outs[4].splitlines() + print(outs[4].splitlines()) self.assertEquals(4, len(outs[4].splitlines())) # Last added should be last self.assertEquals('[Page99]', outs[4].splitlines()[-1][:8]) @@ -178,7 +179,7 @@ class TestList(DataCommandTestCase): 'pubs list title:language author:Saunders', ] outs = self.execute_cmds(cmds) - print outs[-1] + print(outs[-1]) self.assertEquals(1, len(outs[-1].splitlines())) def test_list_ignore_case(self): @@ -188,7 +189,7 @@ class TestList(DataCommandTestCase): 'pubs list --ignore-case title:lAnguAge author:saunders', ] outs = self.execute_cmds(cmds) - print outs[-1] + print(outs[-1]) self.assertEquals(1, len(outs[-1].splitlines())) def test_list_force_case(self): @@ -249,7 +250,7 @@ class TestUsecase(DataCommandTestCase): ] self.execute_cmds(cmds) docdir = self.fs['os'].path.expanduser('~/.pubs/doc/') - print self.fs['os'].listdir(docdir) + print(self.fs['os'].listdir(docdir)) self.assertNotIn('turing-mind-1950.pdf', self.fs['os'].listdir(docdir)) def test_editor_abort(self): From 8136e0906ea51d951e1e0d651f31633a7a84ad1a Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Sat, 19 Apr 2014 18:14:01 +0200 Subject: [PATCH 4/5] Fix use of range as list. --- pubs/bibstruct.py | 3 ++- tests/test_color.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pubs/bibstruct.py b/pubs/bibstruct.py index c8f942a..ee98b10 100644 --- a/pubs/bibstruct.py +++ b/pubs/bibstruct.py @@ -5,7 +5,8 @@ from .p3 import ustr, unichr # citekey stuff -CONTROL_CHARS = ''.join(map(unichr, range(0, 32) + range(127, 160))) +CONTROL_CHARS = ''.join(map(unichr, + list(range(0, 32)) + list(range(127, 160)))) CITEKEY_FORBIDDEN_CHARS = '@\'\\,#}{~%/' # '/' is OK for bibtex but forbidden # here since we transform citekeys into filenames CITEKEY_EXCLUDE_RE = re.compile('[%s]' diff --git a/tests/test_color.py b/tests/test_color.py index 58ba672..0590ecf 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -1,10 +1,12 @@ import dotdot from pubs import color + def perf_color(): - s = str(range(1000)) + s = str(list(range(1000))) for _ in range(5000000): color.dye(s, color.red) + if __name__ == '__main__': perf_color() From c9637b76073d8316158df9bacac329cac5abaf16 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Sat, 19 Apr 2014 18:38:49 +0200 Subject: [PATCH 5/5] Minor: unichr <- uchr. --- pubs/bibstruct.py | 5 ++--- pubs/p3.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pubs/bibstruct.py b/pubs/bibstruct.py index ee98b10..9b85728 100644 --- a/pubs/bibstruct.py +++ b/pubs/bibstruct.py @@ -1,12 +1,11 @@ import unicodedata import re -from .p3 import ustr, unichr +from .p3 import ustr, uchr # citekey stuff -CONTROL_CHARS = ''.join(map(unichr, - list(range(0, 32)) + list(range(127, 160)))) +CONTROL_CHARS = ''.join(map(uchr, list(range(0, 32)) + list(range(127, 160)))) CITEKEY_FORBIDDEN_CHARS = '@\'\\,#}{~%/' # '/' is OK for bibtex but forbidden # here since we transform citekeys into filenames CITEKEY_EXCLUDE_RE = re.compile('[%s]' diff --git a/pubs/p3.py b/pubs/p3.py index ed9c406..9257eab 100644 --- a/pubs/p3.py +++ b/pubs/p3.py @@ -5,6 +5,7 @@ if sys.version_info[0] == 2: import StringIO as io input = raw_input ustr = unicode + uchr = unichr from urlparse import urlparse from urllib2 import urlopen from httplib import HTTPConnection @@ -12,12 +13,11 @@ else: import configparser import io ustr = str + uchr = chr from urllib.parse import urlparse from urllib.request import urlopen from http.client import HTTPConnection - unichr = chr configparser = configparser io = io input = input -unichr = unichr