Beispiel #1
0
    def test_configparser_config1(self):
        ccp = CallbackConfigParser()
        ccp.read_file(os.path.join(self.CONFIG_FILES_DIR, 'config1.conf'))

        self.assertEqual(ccp.get('general', 'version'), 11)
        self.assertTrue(ccp.get('search_community', 'enabled'))
        self.assertIsInstance(
            ccp.get('tunnel_community', 'socks5_listen_ports'), list)
        self.assertFalse(ccp.get('foo', 'bar'))
Beispiel #2
0
    def test_configparser_set_callback(self):
        def parser_callback(section, option, old_value, new_value):
            return True

        ccp = CallbackConfigParser()
        ccp.set_callback(parser_callback)
        ccp.read_file(os.path.join(self.CONFIG_FILES_DIR, 'config1.conf'))

        ccp.set('search_community', 'enabled', False)
        ccp.set('search_community', 'bar', 42)

        self.assertFalse(ccp.get('search_community', 'enabled'))
        self.assertEquals(ccp.get('search_community', 'bar'), 42)
Beispiel #3
0
    def test_configparser_write_file_defaults(self):
        ccp = CallbackConfigParser(defaults={'foo': 'bar'})

        new_path = os.path.join(self.session_base_dir, 'config_new.conf')
        ccp.write_file(new_path)

        self.assertTrue(os.path.isfile(new_path))
        ccp.read_file(new_path)
        self.assertEqual(ccp.get('DEFAULT', 'foo'), 'bar')
Beispiel #4
0
        def resume_ready(_):
            """
            check if resume data is ready
            """
            basename = binascii.hexlify(tdef.get_infohash()) + '.state'
            filename = os.path.join(self.session.get_downloads_pstate_dir(), basename)

            engine_data = CallbackConfigParser()
            engine_data.read_file(filename)

            self.assertEqual(tdef.get_infohash(), engine_data.get('state', 'engineresumedata').get('info-hash'))
Beispiel #5
0
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)