def setup_tribler_gui_config(self): """ Initialize the TriblerGUI configuration file and make sure that we have all required values. """ configfilepath = os.path.join(self.get_state_dir(), STATEDIR_GUICONFIG) gui_config = CallbackConfigParser() DefaultDownloadStartupConfig.getInstance().set_dest_dir( get_default_dest_dir()) # Load the config file. if os.path.exists(configfilepath): gui_config.read_file(configfilepath, 'utf-8-sig') if not gui_config.has_section('Tribler'): gui_config.add_section('Tribler') for k, v in tribler_defaults['Tribler'].iteritems(): if not gui_config.has_option(k, v): gui_config.set('Tribler', k, v) if not gui_config.has_section('downloadconfig'): gui_config.add_section('downloadconfig') for k, v in DefaultDownloadStartupConfig.getInstance( ).dlconfig._sections['downloadconfig'].iteritems(): if not gui_config.has_option(k, v): gui_config.set('downloadconfig', k, v) # Make sure we use the same ConfigParser instance for both Utility and DefaultDownloadStartupConfig. DefaultDownloadStartupConfig.getInstance().dlconfig = gui_config gui_config.write_file(configfilepath)
class Utility(object): def __init__(self, abcpath, configpath, app=None, session=None): self.version = version_id self.abcpath = abcpath # Find the directory to save config files, etc. self.dir_root = configpath self.setupConfig() # Is ABC in the process of shutting down? self.abcquitting = False self.app = app self.session = session def setupConfig(self): self.configfilepath = os.path.join(self.getConfigPath(), STATEDIR_GUICONFIG) self.config = CallbackConfigParser() # Load the config file. if os.path.exists(self.configfilepath): self.config.read_file(self.configfilepath, 'utf-8-sig') if not self.config.has_section('Tribler'): self.config.add_section('Tribler') # Tribler.conf also contains the default download config. So we need to merge it now. if not self.config.has_section('downloadconfig'): self.config.add_section('downloadconfig') for k, v in DefaultDownloadStartupConfig.getInstance().dlconfig._sections['downloadconfig'].iteritems(): self.config.set('downloadconfig', k, v) # Make sure we use the same ConfigParser instance for both Utility and DefaultDownloadStartupConfig. DefaultDownloadStartupConfig.getInstance().dlconfig = self.config def getVersion(self): return self.version def getConfigPath(self): return self.dir_root def getPath(self): return self.abcpath def set_session(self, session): self.session = session def set_app(self, app): self.app = app def read_config(self, option, section='Tribler', literal_eval=True): if not self.config.has_option(section, option): return tribler_defaults.get(section, {}).get(option, None) return self.config.get(section, option, literal_eval=literal_eval) def write_config(self, option, value, section='Tribler', flush=True): self.config.set(section, option, value) if flush: self.flush_config() def flush_config(self): self.config.write_file(self.configfilepath)
class SettingsEndpoint(resource.Resource): """ This endpoint is reponsible for handing all requests regarding settings and configuration. """ def __init__(self, session): resource.Resource.__init__(self) self.session = session # Load the Tribler GUI configuration file self.gui_config_file_path = os.path.join(self.session.get_state_dir(), STATEDIR_GUICONFIG) self.tribler_gui_config = CallbackConfigParser() self.tribler_gui_config.read_file(self.gui_config_file_path, 'utf-8-sig') def render_GET(self, request): """ .. http:get:: /settings A GET request to this endpoint returns all the session settings that can be found in Tribler. Please note that a port with a value of -1 means that the port is randomly assigned at startup. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/settings **Example response**: .. sourcecode:: javascript { "settings": { "libtorrent": { "anon_listen_port": -1, ... }, ... } } """ libtribler_settings = self.session.sessconfig.get_config_as_json() tribler_settings = self.tribler_gui_config.get_config_as_json() # Merge the configuration of libtribler and the Tribler configuration settings_dict = libtribler_settings.copy() settings_dict.update(tribler_settings) settings_dict["general"][ "family_filter"] = self.session.tribler_config.config["general"][ "family_filter"] return json.dumps({"settings": settings_dict}) def render_POST(self, request): """ .. http:post:: /settings A POST request to this endpoint will update Tribler settings. A JSON-dictionary should be passed as body contents. **Example request**: .. sourcecode:: none curl -X POST http://localhost:8085/settings --data "{" **Example response**: .. sourcecode:: javascript { "modified": True } """ settings_dict = json.loads(request.content.read()) self.parse_settings_dict(settings_dict) self.session.save_session_config() return json.dumps({"modified": True}) def parse_setting(self, section, option, value): """ Set a specific Tribler setting. Throw a ValueError if this setting is not available. """ if section == "general" and option == "family_filter": self.session.tribler_config.set_family_filter_enabled(value) return if section == "Tribler" or section == "downloadconfig": # Write to the Tribler GUI config file if not self.tribler_gui_config.has_option(section, option): raise ValueError("Section %s with option %s does not exist" % (section, option)) RawConfigParser.set(self.tribler_gui_config, section, option, value) self.tribler_gui_config.write_file(self.gui_config_file_path) return if not RawConfigParser.has_option(self.session.sessconfig, section, option): raise ValueError("Section %s with option %s does not exist" % (section, option)) RawConfigParser.set(self.session.sessconfig, section, option, value) # Reload the GUI settings in Tribler (there might have been download settings that have changed) self.session.setup_tribler_gui_config() # Perform some actions when specific keys are set if section == "libtorrent" and (option == "max_download_rate" or option == "max_upload_rate"): for lt_session in self.session.lm.ltmgr.ltsessions.itervalues(): ltsession_settings = lt_session.get_settings() ltsession_settings[ 'upload_rate_limit'] = self.session.get_libtorrent_max_upload_rate( ) ltsession_settings[ 'download_rate_limit'] = self.session.get_libtorrent_max_download_rate( ) lt_session.set_settings(ltsession_settings) def parse_settings_dict(self, settings_dict, depth=1, root_key=None): """ Parse the settings dictionary. Throws an error if the options dictionary seems to be invalid (i.e. there are keys not available in the configuration or the depth of the dictionary is too high. """ if depth == 3: raise ValueError("Invalid settings dictionary depth (%d)" % depth) for key, value in settings_dict.iteritems(): if isinstance(value, dict): self.parse_settings_dict(value, depth=depth + 1, root_key=key) else: self.parse_setting(root_key, key, value)
class Utility(object): def __init__(self, abcpath, configpath, app=None, session=None): self.version = version_id self.abcpath = abcpath # Find the directory to save config files, etc. self.dir_root = configpath self.setupConfig() # Is ABC in the process of shutting down? self.abcquitting = False self.app = app self.session = session def setupConfig(self): self.configfilepath = os.path.join(self.getConfigPath(), STATEDIR_GUICONFIG) self.config = CallbackConfigParser() # Load the config file. if os.path.exists(self.configfilepath): self.config.read_file(self.configfilepath, 'utf-8-sig') if not self.config.has_section('Tribler'): self.config.add_section('Tribler') # Tribler.conf also contains the default download config. So we need to merge it now. if not self.config.has_section('downloadconfig'): self.config.add_section('downloadconfig') for k, v in DefaultDownloadStartupConfig.getInstance( ).dlconfig._sections['downloadconfig'].iteritems(): self.config.set('downloadconfig', k, v) # Make sure we use the same ConfigParser instance for both Utility and DefaultDownloadStartupConfig. DefaultDownloadStartupConfig.getInstance().dlconfig = self.config def getVersion(self): return self.version def getConfigPath(self): return self.dir_root def getPath(self): return self.abcpath def set_session(self, session): self.session = session def set_app(self, app): self.app = app def read_config(self, option, section='Tribler', literal_eval=True): if not self.config.has_option(section, option): return tribler_defaults.get(section, {}).get(option, None) return self.config.get(section, option, literal_eval=literal_eval) def write_config(self, option, value, section='Tribler', flush=True): self.config.set(section, option, value) if flush: self.flush_config() def flush_config(self): self.config.write_file(self.configfilepath)