From 8474d0d95dd2f2ac58f150c844a118bf6ef58f37 Mon Sep 17 00:00:00 2001 From: Techmunk Date: Wed, 25 Sep 2013 21:44:05 +1000 Subject: [PATCH 1/2] Fix the way the client auth file is found and processed to match the defaults in the deluge clients. --- libs/synchronousdeluge/client.py | 48 +++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/libs/synchronousdeluge/client.py b/libs/synchronousdeluge/client.py index 98a8084..afd5971 100644 --- a/libs/synchronousdeluge/client.py +++ b/libs/synchronousdeluge/client.py @@ -1,4 +1,5 @@ import os +import platform from collections import defaultdict from itertools import imap @@ -23,22 +24,47 @@ class DelugeClient(object): self._request_counter = 0 def _get_local_auth(self): - xdg_config = os.path.expanduser(os.environ.get("XDG_CONFIG_HOME", "~/.config")) - config_home = os.path.join(xdg_config, "deluge") - auth_file = os.path.join(config_home, "auth") - + auth_file = "" username = password = "" - with open(auth_file) as fd: - for line in fd: + if platform.system() in ('Windows', 'Microsoft'): + appDataPath = os.environ.get("APPDATA") + if not appDataPath: + import _winreg + hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders") + appDataReg = _winreg.QueryValueEx(hkey, "AppData") + appDataPath = appDataReg[0] + _winreg.CloseKey(hkey) + auth_file = os.path.join(appDataPath, "deluge", "auth") + else: + from xdg.BaseDirectory import save_config_path + try: + auth_file = os.path.join(save_config_path("deluge"), "auth") + except OSError, e: + return username, password + + + if os.path.exists(auth_file): + for line in open(auth_file): if line.startswith("#"): + # This is a comment line + continue + line = line.strip() + try: + lsplit = line.split(":") + except Exception, e: + continue + + if len(lsplit) == 2: + username, password = lsplit + elif len(lsplit) == 3: + username, password, level = lsplit + else: continue - auth = line.split(":") - if len(auth) >= 2 and auth[0] == "localclient": - username, password = auth[0], auth[1] - break + if username == "localclient": + return (username, password) - return username, password + return ("", "") def _create_module_method(self, module, method): fullname = "{0}.{1}".format(module, method) From 74a4e7d19df742e4d62781fcf39fab7fa924d7de Mon Sep 17 00:00:00 2001 From: Techmunk Date: Fri, 27 Sep 2013 14:59:03 +1000 Subject: [PATCH 2/2] Indenting on deluge auth fix was incorrect. --- libs/synchronousdeluge/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/synchronousdeluge/client.py b/libs/synchronousdeluge/client.py index afd5971..22419e8 100644 --- a/libs/synchronousdeluge/client.py +++ b/libs/synchronousdeluge/client.py @@ -34,7 +34,8 @@ class DelugeClient(object): appDataReg = _winreg.QueryValueEx(hkey, "AppData") appDataPath = appDataReg[0] _winreg.CloseKey(hkey) - auth_file = os.path.join(appDataPath, "deluge", "auth") + + auth_file = os.path.join(appDataPath, "deluge", "auth") else: from xdg.BaseDirectory import save_config_path try: