From 7b53486821da1eaf75404b09d5f753dc4f430ef4 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Fri, 18 Jul 2014 15:07:46 +0100 Subject: [PATCH] Use stdlib json Some flake8 changes and DEPENDS tidyup --- DEPENDS | 22 +++++++---------- deluge/common.py | 50 ++++++++++++--------------------------- deluge/config.py | 23 +++++++++--------- deluge/ui/web/json_api.py | 34 +++++++++++++++----------- 4 files changed, 54 insertions(+), 75 deletions(-) diff --git a/DEPENDS b/DEPENDS index 8cdbab2c6..b28fc7f65 100644 --- a/DEPENDS +++ b/DEPENDS @@ -1,30 +1,24 @@ === Core === + * libtorrent (rasterbar) >= 0.16.7 * python >= 2.6 + * setuptools * twisted >= 8.1 * pyopenssl - * setuptools - * gettext - * intltool * pyxdg * chardet + * gettext * geoip-database (optional) * setproctitle (optional) - * rencode >= 1.0.2 (optional), Python port is included + * rencode >= 1.0.2 (optional), python port bundled. - * libtorrent (rasterbar) >= 0.16.7 - - * If building libtorrent: - * boost >= 1.40 - * openssl - * zlib - -=== Gtk === +=== Gtk UI === * pygtk >= 2.16 * librsvg * xdg-utils + * intltool * python-notify (optional) * pygame (optional) -=== Web === +=== Web UI === * mako - * slimit + * slimit (optional), minifies JS files. diff --git a/deluge/common.py b/deluge/common.py index 1f1a87687..873d3f223 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -49,30 +49,10 @@ import locale import base64 import urllib -try: - import json -except ImportError: - import simplejson as json - from deluge.error import InvalidPathError log = logging.getLogger(__name__) -# Do a little hack here just in case the user has json-py installed since it -# has a different api -if not hasattr(json, "dumps"): - json.dumps = json.write - json.loads = json.read - - def dump(obj, fp, **kw): - fp.write(json.dumps(obj)) - - def load(fp, **kw): - return json.loads(fp.read()) - - json.dump = dump - json.load = load - LT_TORRENT_STATE = { "Queued": 0, "Checking": 1, @@ -142,15 +122,15 @@ def get_default_config_dir(filename=None): if windows_check(): def save_config_path(resource): - appDataPath = os.environ.get("APPDATA") - if not appDataPath: + app_data_path = os.environ.get("APPDATA") + if not app_data_path: import _winreg hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") - appDataReg = _winreg.QueryValueEx(hkey, "AppData") - appDataPath = appDataReg[0] + app_data_reg = _winreg.QueryValueEx(hkey, "AppData") + app_data_path = app_data_reg[0] _winreg.CloseKey(hkey) - return os.path.join(appDataPath, resource) + return os.path.join(app_data_path, resource) else: from xdg.BaseDirectory import save_config_path if not filename: @@ -785,7 +765,7 @@ class VersionSplit(object): """ def __init__(self, ver): import re - VERSION_RE = re.compile(r''' + version_re = re.compile(r''' ^ (?P\d+\.\d+) # minimum 'N.N' (?P(?:\.\d+)*) # any number of extra '.N' segments @@ -798,7 +778,7 @@ class VersionSplit(object): $''', re.VERBOSE) # Check for PEP 386 compliant version - match = re.search(VERSION_RE, ver) + match = re.search(version_re, ver) if match: group = [(x if x is not None else '') for x in match.group(1, 2, 3, 4, 8)] vs = [''.join(group[0:2]), ''.join(group[2:4]), group[4].lstrip('.')] @@ -1024,17 +1004,17 @@ def unicode_argv(): from ctypes import POINTER, byref, cdll, c_int, windll from ctypes.wintypes import LPCWSTR, LPWSTR - GetCommandLineW = cdll.kernel32.GetCommandLineW - GetCommandLineW.argtypes = [] - GetCommandLineW.restype = LPCWSTR + get_cmd_linew = cdll.kernel32.GetCommandLineW + get_cmd_linew.argtypes = [] + get_cmd_linew.restype = LPCWSTR - CommandLineToArgvW = windll.shell32.CommandLineToArgvW - CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)] - CommandLineToArgvW.restype = POINTER(LPWSTR) + cmdline_to_argvw = windll.shell32.CommandLineToArgvW + cmdline_to_argvw.argtypes = [LPCWSTR, POINTER(c_int)] + cmdline_to_argvw.restype = POINTER(LPWSTR) - cmd = GetCommandLineW() + cmd = get_cmd_linew() argc = c_int(0) - argv = CommandLineToArgvW(cmd, byref(argc)) + argv = cmdline_to_argvw(cmd, byref(argc)) if argc.value > 0: # Remove Python executable and commands if present start = argc.value - len(sys.argv) diff --git a/deluge/config.py b/deluge/config.py index 017d970a6..159ab7535 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -71,12 +71,13 @@ import cPickle as pickle import logging import shutil import os +import json -import deluge.common +from deluge.common import get_default_config_dir, utf8_encoded -json = deluge.common.json log = logging.getLogger(__name__) -callLater = None # Necessary for the config tests +callLater = None # Necessary for the config tests + def prop(func): """Function decorator for defining property attributes @@ -94,6 +95,7 @@ def prop(func): """ return property(doc=func.__doc__, **func()) + def find_json_objects(s): """ Find json objects in a string. @@ -157,7 +159,7 @@ class Config(object): if config_dir: self.__config_file = os.path.join(config_dir, filename) else: - self.__config_file = deluge.common.get_default_config_dir(filename) + self.__config_file = get_default_config_dir(filename) self.load() @@ -193,8 +195,7 @@ what is currently in the config and it could not convert the value """ if isinstance(value, basestring): - value = deluge.common.utf8_encoded(value) - + value = utf8_encoded(value) if not self.__config.has_key(key): self.__config[key] = value @@ -274,7 +275,6 @@ what is currently in the config and it could not convert the value else: return self.__config[key] - def get(self, key, default=None): """ Gets the value of item 'key' if key is in the config, else default. @@ -299,7 +299,6 @@ what is currently in the config and it could not convert the value except KeyError: return default - def __delitem__(self, key): """ See @@ -329,7 +328,6 @@ what is currently in the config and it could not convert the value if not self._save_timer or not self._save_timer.active(): self._save_timer = callLater(5, self.save) - def register_change_callback(self, callback): """ Registers a callback function that will be called when a value is changed in the config dictionary @@ -452,7 +450,7 @@ what is currently in the config and it could not convert the value log.warning("Unable to load config file: %s", filename) log.debug("Config %s version: %s.%s loaded: %s", filename, - self.__version["format"], self.__version["file"], self.__config) + self.__version["format"], self.__version["file"], self.__config) def save(self, filename=None): """ @@ -535,7 +533,7 @@ what is currently in the config and it could not convert the value if self.__version["file"] not in input_range: log.debug("File version %s is not in input_range %s, ignoring converter function..", - self.__version["file"], input_range) + self.__version["file"], input_range) return try: @@ -543,7 +541,7 @@ what is currently in the config and it could not convert the value except Exception, e: log.exception(e) log.error("There was an exception try to convert config file %s %s to %s", - self.__config_file, self.__version["file"], output_version) + self.__config_file, self.__version["file"], output_version) raise e else: self.__version["file"] = output_version @@ -558,6 +556,7 @@ what is currently in the config and it could not convert the value """The config dictionary""" def fget(self): return self.__config + def fdel(self): return self.save() return locals() diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index ebec870e9..cef4c261c 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -40,6 +40,7 @@ import shutil import logging import hashlib import tempfile +import json from urlparse import urljoin from urllib import unquote_plus @@ -50,7 +51,8 @@ from twisted.web import http, resource, server import twisted.web.client import twisted.web.error -from deluge import common, component, httpdownloader +from deluge.common import is_magnet +from deluge import component, httpdownloader from deluge.configmanager import ConfigManager, get_config_dir from deluge.ui import common as uicommon from deluge.ui.client import client, Client @@ -58,19 +60,20 @@ from deluge.ui.coreconfig import CoreConfig from deluge.ui.sessionproxy import SessionProxy from deluge.ui.web.common import _, compress -json = common.json log = logging.getLogger(__name__) AUTH_LEVEL_DEFAULT = None AuthError = None + class JSONComponent(component.Component): def __init__(self, name, interval=1, depend=None): super(JSONComponent, self).__init__(name, interval, depend) self._json = component.get("JSON") self._json.register_object(self, name) + def export(auth_level=AUTH_LEVEL_DEFAULT): """ Decorator function to register an object's method as a RPC. The object @@ -84,7 +87,7 @@ def export(auth_level=AUTH_LEVEL_DEFAULT): """ global AUTH_LEVEL_DEFAULT, AuthError if AUTH_LEVEL_DEFAULT is None: - from deluge.ui.web.auth import AUTH_LEVEL_DEFAULT, AuthError + from deluge.ui.web.auth import AUTH_LEVEL_DEFAULT def wrap(func, *args, **kwargs): func._json_export = True @@ -98,11 +101,13 @@ def export(auth_level=AUTH_LEVEL_DEFAULT): else: return wrap + class JSONException(Exception): def __init__(self, inner_exception): self.inner_exception = inner_exception Exception.__init__(self, str(inner_exception)) + class JSON(resource.Resource, component.Component): """ A Twisted Web resource that exposes a JSON-RPC interface for web clients \ @@ -290,7 +295,7 @@ class JSON(resource.Resource, component.Component): try: request.content.seek(0) request.json = request.content.read() - d = self._on_json_request(request) + self._on_json_request(request) return server.NOT_DONE_YET except Exception, e: return self._on_json_request_failed(e, request) @@ -336,6 +341,7 @@ HOSTS_INFO = 4 FILES_KEYS = ["files", "file_progress", "file_priorities"] + class EventQueue(object): """ This class subscribes to events from the core and stores them until all @@ -418,6 +424,7 @@ class EventQueue(object): del self.__events[event] del self.__handlers[event] + class WebApi(JSONComponent): """ The component that implements all the methods required for managing @@ -467,6 +474,7 @@ class WebApi(JSONComponent): :rtype: list """ d = Deferred() + def on_connected(methods): d.callback(methods) host = self.get_host(host_id) @@ -737,7 +745,7 @@ class WebApi(JSONComponent): if info_hash: if not name: name = info_hash - return {"name":name, "info_hash":info_hash, "files_tree":''} + return {"name": name, "info_hash": info_hash, "files_tree": ''} return False @export @@ -751,14 +759,14 @@ class WebApi(JSONComponent): **Usage** - >>> json_api.web.add_torrents([{ + json_api.web.add_torrents([{ "path": "/tmp/deluge-web/some-torrent-file.torrent", "options": {"download_location": "/home/deluge/"} }]) """ for torrent in torrents: - if common.is_magnet(torrent["path"]): + if is_magnet(torrent["path"]): log.info("Adding torrent from magnet uri `%s` with options `%r`", torrent["path"], torrent["options"]) client.core.add_torrent_magnet(torrent["path"], torrent["options"]) @@ -792,7 +800,7 @@ class WebApi(JSONComponent): try: host_id, host, port, user, password = self.get_host(host_id) - except TypeError, e: + except TypeError: host = None port = None return response(_("Offline")) @@ -809,16 +817,14 @@ class WebApi(JSONComponent): if not connected: return response(_("Offline")) - return c.daemon.info( - ).addCallback(on_info, c - ).addErrback(on_info_fail, c) + return c.daemon.info().addCallback(on_info, c).addErrback(on_info_fail, c) def on_connect_failed(reason, host_id): return response(_("Offline")) if client.connected() and (host, port, "localclient" if not user and host in ("127.0.0.1", "localhost") else - user) == client.connection_info(): + user) == client.connection_info(): def on_info(info): return response(_("Connected"), info) @@ -826,8 +832,8 @@ class WebApi(JSONComponent): else: c = Client() return c.connect(host, port, user, password - ).addCallback(on_connect, c, host_id - ).addErrback(on_connect_failed, host_id) + ).addCallback(on_connect, c, host_id + ).addErrback(on_connect_failed, host_id) @export def start_daemon(self, port):