support py2 and 3

This commit is contained in:
j 2016-08-23 10:55:24 +02:00
parent b6412c116d
commit a6a35e18d4
6 changed files with 24 additions and 19 deletions

View file

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2012 # GPL 2012
from __future__ import print_function
import os import os
import sys import sys
@ -27,11 +28,11 @@ if __name__ == '__main__':
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
if opts.version: if opts.version:
print "%s %s" % (os.path.basename(sys.argv[0]), pandora_client.__version__) print("%s %s" % (os.path.basename(sys.argv[0]), pandora_client.__version__))
sys.exit(0) sys.exit(0)
opts.config = os.path.expanduser(opts.config) opts.config = os.path.expanduser(opts.config)
if (args and args[0] not in ('config', 'client') and not os.path.exists(opts.config)): if (args and args[0] not in ('config', 'client') and not os.path.exists(opts.config)):
print 'no configuration found, run "%s config" or specify one with -c' % sys.argv[0] print('no configuration found, run "%s config" or specify one with -c' % sys.argv[0])
sys.exit(1) sys.exit(1)
if None in (opts.config, ): if None in (opts.config, ):

View file

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2012-2016 # GPL 2012-2016
from __future__ import division, with_statement, print_function from __future__ import division, with_statement, print_function, absolute_import
import getpass import getpass
from glob import glob from glob import glob
@ -16,12 +16,14 @@ import sys
import tempfile import tempfile
import time import time
import pkg_resources import pkg_resources
from urlparse import urlparse
from six.moves.urllib.parse import urlparse
from six import string_types
import ox import ox
import extract from . import extract
import utils from . import utils
DEBUG = False DEBUG = False
@ -142,7 +144,7 @@ class Client(object):
_configfile = None _configfile = None
def __init__(self, config, offline=False): def __init__(self, config, offline=False):
if isinstance(config, basestring): if isinstance(config, string_types):
self._configfile = os.path.expanduser(config) self._configfile = os.path.expanduser(config)
with open(config) as f: with open(config) as f:
try: try:
@ -235,7 +237,7 @@ class Client(object):
def _conn(self): def _conn(self):
db_conn = self._config['cache'] db_conn = self._config['cache']
if isinstance(db_conn, str): if isinstance(db_conn, bytes):
db_conn = db_conn.decode('utf-8') db_conn = db_conn.decode('utf-8')
db_conn = os.path.expanduser(db_conn) db_conn = os.path.expanduser(db_conn)
if not os.path.exists(os.path.dirname(db_conn)): if not os.path.exists(os.path.dirname(db_conn)):
@ -277,7 +279,7 @@ class Client(object):
prefixes = [prefix] prefixes = [prefix]
else: else:
prefixes = self.active_volumes().values() prefixes = self.active_volumes().values()
prefixes = [p.decode('utf-8') if isinstance(prefix, str) else p for p in prefixes] prefixes = [p.decode('utf-8') if isinstance(prefix, bytes) else p for p in prefixes]
_info = self.info(oshash) _info = self.info(oshash)
for path in self.path(oshash): for path in self.path(oshash):
for prefix in prefixes: for prefix in prefixes:
@ -385,7 +387,7 @@ class Client(object):
modified = time.mktime(time.localtime()) modified = time.mktime(time.localtime())
created = modified created = modified
if isinstance(path, str): if isinstance(path, bytes):
path = path.decode('utf-8') path = path.decode('utf-8')
sql = u'SELECT atime, ctime, mtime, size, created, info FROM file WHERE deleted < 0 AND path=?' sql = u'SELECT atime, ctime, mtime, size, created, info FROM file WHERE deleted < 0 AND path=?'
@ -507,7 +509,7 @@ class Client(object):
path = os.path.normpath(path) path = os.path.normpath(path)
if not path.endswith(os.sep): if not path.endswith(os.sep):
path += os.sep path += os.sep
if isinstance(path, str): if isinstance(path, bytes):
path = path.decode('utf-8') path = path.decode('utf-8')
if os.path.exists(path): if os.path.exists(path):
volumes[name] = path volumes[name] = path
@ -1029,7 +1031,7 @@ class API(ox.API):
form.add_field('action', 'upload') form.add_field('action', 'upload')
form.add_field('id', str(oshash)) form.add_field('id', str(oshash))
fname = os.path.basename(filename) fname = os.path.basename(filename)
if isinstance(fname, unicode): if not isinstance(fname, bytes):
fname = fname.encode('utf-8') fname = fname.encode('utf-8')
form.add_file('file', fname, open(filename, 'rb')) form.add_file('file', fname, open(filename, 'rb'))
r = self._json_request(self.url, form) r = self._json_request(self.url, form)

View file

@ -1,6 +1,7 @@
# encoding: utf-8 # encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import print_function from __future__ import division, with_statement, print_function, absolute_import
import os import os
import json import json
import subprocess import subprocess
@ -10,7 +11,7 @@ import sys
import requests import requests
import extract from . import extract
class DistributedClient: class DistributedClient:

View file

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2010 # GPL 2010
from __future__ import division, with_statement, print_function from __future__ import division, with_statement, print_function, absolute_import
import os import os
import subprocess import subprocess
@ -11,7 +11,7 @@ import shutil
import ox import ox
from utils import AspectRatio, run_command, basedir from .utils import AspectRatio, run_command, basedir
def command(program): def command(program):

View file

@ -1,6 +1,6 @@
# encoding: utf-8 # encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import print_function from __future__ import division, with_statement, print_function, absolute_import
import os import os
import json import json
@ -16,8 +16,8 @@ from twisted.web.static import File
from twisted.web.server import Site from twisted.web.server import Site
from twisted.internet import reactor from twisted.internet import reactor
import extract from . import extract
from utils import hash_prefix from .utils import hash_prefix
class UploadThread(Thread): class UploadThread(Thread):
def __init__(self, server): def __init__(self, server):

View file

@ -51,6 +51,7 @@ setup(
], ],
install_requires=[ install_requires=[
'ox >= 2.1.541,<3', 'ox >= 2.1.541,<3',
'six',
'requests >= 1.1.0' 'requests >= 1.1.0'
], ],
keywords = [ keywords = [