def get_localhost_auth(): """Grabs the localclient auth line from the 'auth' file and creates a localhost uri. Returns: tuple: With the username and password to login as. """ from deluge.configmanager import get_config_dir auth_file = get_config_dir('auth') if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() with open(auth_file) as auth: for line in auth: line = line.strip() if line.startswith('#') or not line: # This is a comment or blank line continue lsplit = line.split(':') if len(lsplit) == 2: username, password = lsplit elif len(lsplit) == 3: username, password, level = lsplit else: log.error( 'Your auth file is malformed: Incorrect number of fields!') continue if username == 'localclient': return (username, password)
def get_localhost_auth(): """ Grabs the localclient auth line from the 'auth' file and creates a localhost uri :returns: with the username and password to login as :rtype: tuple """ auth_file = deluge.configmanager.get_config_dir("auth") if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() 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: log.error("Your auth file is malformed: %s", e) continue if len(lsplit) == 2: username, password = lsplit elif len(lsplit) == 3: username, password, level = lsplit else: log.error("Your auth file is malformed: Incorrect number of fields!") continue if username == "localclient": return (username, password)
def get_localhost_auth(): """Grabs the localclient auth line from the 'auth' file and creates a localhost uri. Returns: tuple: With the username and password to login as. """ from deluge.configmanager import get_config_dir auth_file = get_config_dir('auth') if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() with open(auth_file) as auth: for line in auth: line = line.strip() if line.startswith('#') or not line: # This is a comment or blank line continue lsplit = line.split(':') if len(lsplit) == 2: username, password = lsplit elif len(lsplit) == 3: username, password, level = lsplit else: log.error('Your auth file is malformed: Incorrect number of fields!') continue if username == 'localclient': return (username, password)
def get_localhost_auth(): """ Grabs the localclient auth line from the 'auth' file and creates a localhost uri :returns: with the username and password to login as :rtype: tuple """ auth_file = deluge.configmanager.get_config_dir("auth") if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() 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: log.error("Your auth file is malformed: %s", e) continue if len(lsplit) == 2: username, password = lsplit elif len(lsplit) == 3: username, password, level = lsplit else: log.error( "Your auth file is malformed: Incorrect number of fields!") continue if username == "localclient": return (username, password)
def __load_config(self): auth_file = deluge.configmanager.get_config_dir("auth") if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() localclient_username, localclient_password = get_localhost_auth() DEFAULT_CONFIG = { "hosts": [(hashlib.sha1(str(time.time())).hexdigest(), DEFAULT_HOST, DEFAULT_PORT, localclient_username, localclient_password)] } config = ConfigManager("hostlist.conf.1.2", DEFAULT_CONFIG) config.run_converter((0, 1), 2, self.__migrate_config_1_to_2) return config
def __load_config(self): auth_file = deluge.configmanager.get_config_dir("auth") if not os.path.exists(auth_file): from deluge.common import create_localclient_account create_localclient_account() localclient_username, localclient_password = get_localhost_auth() DEFAULT_CONFIG = { "hosts": [( hashlib.sha1(str(time.time())).hexdigest(), DEFAULT_HOST, DEFAULT_PORT, localclient_username, localclient_password )] } config = ConfigManager("hostlist.conf.1.2", DEFAULT_CONFIG) config.run_converter((0, 1), 2, self.__migrate_config_1_to_2) return config
def __load_auth_file(self): save_and_reload = False filename = 'auth' auth_file = configmanager.get_config_dir(filename) auth_file_bak = auth_file + '.bak' # Check for auth file and create if necessary if not os.path.isfile(auth_file): create_localclient_account() return self.__load_auth_file() auth_file_modification_time = os.stat(auth_file).st_mtime if self.__auth_modification_time is None: self.__auth_modification_time = auth_file_modification_time elif self.__auth_modification_time == auth_file_modification_time: # File didn't change, no need for re-parsing's return for _filepath in (auth_file, auth_file_bak): log.info('Opening %s for load: %s', filename, _filepath) try: with open(_filepath, 'r', encoding='utf8') as _file: file_data = _file.readlines() except IOError as ex: log.warning('Unable to load %s: %s', _filepath, ex) file_data = [] else: log.info('Successfully loaded %s: %s', filename, _filepath) break # Load the auth file into a dictionary: {username: Account(...)} for line in file_data: line = line.strip() if line.startswith('#') or not line: # This line is a comment or empty continue lsplit = line.split(':') if len(lsplit) == 2: username, password = lsplit log.warning( 'Your auth entry for %s contains no auth level, ' 'using AUTH_LEVEL_DEFAULT(%s)..', username, AUTH_LEVEL_DEFAULT, ) if username == 'localclient': authlevel = AUTH_LEVEL_ADMIN else: authlevel = AUTH_LEVEL_DEFAULT # This is probably an old auth file save_and_reload = True elif len(lsplit) == 3: username, password, authlevel = lsplit else: log.error( 'Your auth file is malformed: Incorrect number of fields!') continue username = username.strip() password = password.strip() try: authlevel = int(authlevel) except ValueError: try: authlevel = AUTH_LEVELS_MAPPING[authlevel] except KeyError: log.error( 'Your auth file is malformed: %r is not a valid auth level', authlevel, ) continue self.__auth[username] = Account(username, password, authlevel) if 'localclient' not in self.__auth: create_localclient_account(True) return self.__load_auth_file() if save_and_reload: log.info('Re-writing auth file (upgrade)') self.write_auth_file() self.__auth_modification_time = auth_file_modification_time
def __load_auth_file(self): save_and_reload = False filename = 'auth' auth_file = configmanager.get_config_dir(filename) auth_file_bak = auth_file + '.bak' # Check for auth file and create if necessary if not os.path.isfile(auth_file): create_localclient_account() return self.__load_auth_file() auth_file_modification_time = os.stat(auth_file).st_mtime if self.__auth_modification_time is None: self.__auth_modification_time = auth_file_modification_time elif self.__auth_modification_time == auth_file_modification_time: # File didn't change, no need for re-parsing's return for _filepath in (auth_file, auth_file_bak): log.info('Opening %s for load: %s', filename, _filepath) try: with open(_filepath, 'r', encoding='utf8') as _file: file_data = _file.readlines() except IOError as ex: log.warning('Unable to load %s: %s', _filepath, ex) file_data = [] else: log.info('Successfully loaded %s: %s', filename, _filepath) break # Load the auth file into a dictionary: {username: Account(...)} for line in file_data: line = line.strip() if line.startswith('#') or not line: # This line is a comment or empty continue lsplit = line.split(':') if len(lsplit) == 2: username, password = lsplit log.warning('Your auth entry for %s contains no auth level, ' 'using AUTH_LEVEL_DEFAULT(%s)..', username, AUTH_LEVEL_DEFAULT) if username == 'localclient': authlevel = AUTH_LEVEL_ADMIN else: authlevel = AUTH_LEVEL_DEFAULT # This is probably an old auth file save_and_reload = True elif len(lsplit) == 3: username, password, authlevel = lsplit else: log.error('Your auth file is malformed: Incorrect number of fields!') continue username = username.strip() password = password.strip() try: authlevel = int(authlevel) except ValueError: try: authlevel = AUTH_LEVELS_MAPPING[authlevel] except KeyError: log.error('Your auth file is malformed: %r is not a valid auth level', authlevel) continue self.__auth[username] = Account(username, password, authlevel) if 'localclient' not in self.__auth: create_localclient_account(True) return self.__load_auth_file() if save_and_reload: log.info('Re-writing auth file (upgrade)') self.write_auth_file() self.__auth_modification_time = auth_file_modification_time