Python configobj.ConfigObj() Examples

The following are 30 code examples of configobj.ConfigObj(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module configobj , or try the search function .
Example #1
Source File: templates.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_template(path):
    """ Load the informations stored in a template.

    Parameters
    ----------
        path : unicode
            Location of the template file.

    Returns
    -------
    data : ConfigObj
        The data needed to rebuild the tasks.

    doc : unicode
        The doc of the template.

    """
    config = ConfigObj(path, encoding='utf-8', indent_type='    ')
    doc_list = [com[1:].strip() for com in config.initial_comment]
    doc = '\n'.join(doc_list)

    return config, doc 
Example #2
Source File: test_plugin_agent.py    From panoptes with Apache License 2.0 6 votes vote down vote up
def test_polling_no_matching_transform_type(self, message_producer, message_producer_property, kv_store):

        mock_panoptes_plugin = MockPanoptesObject()
        mock_panoptes_plugin.config = ConfigObj(
            pwd + u'/config_files/test_panoptes_polling_plugin_conf_diff_transform_callback.ini')
        kv_store.return_value = PanoptesMockRedis()

        mock_message_producer = MockPanoptesMessageProducer()

        message_producer.return_value = mock_message_producer
        message_producer_property.return_value = message_producer_property

        panoptes_context = PanoptesContext(config_file=os.path.join(path, u'config_files/test_panoptes_config.ini'))

        for i in range(1, 9):
            panoptes_metrics = self.prepare_panoptes_metrics_group_set(
                u'{}/metric_group_sets/interface_plugin_counter_{}.json'.format(pwd, i))
            _process_metrics_group_set(context=panoptes_context, results=panoptes_metrics, plugin=mock_panoptes_plugin)

        self.assertEqual(
            panoptes_context.message_producer.messages,
            []
        ) 
Example #3
Source File: Recipe.py    From OpenTrader with GNU Lesser General Public License v3.0 6 votes vote down vote up
def vReadIniFile(self):
        assert self.sIniFile, "ERROR: No INI file defined"
        sIniFile = self.sIniFile
        if not os.path.isabs(sIniFile):
            sIniFile = os.path.join(os.path.dirname(__file__), self.sIniFile)
        assert os.path.isfile(sIniFile)
        if os.path.isfile(sIniFile) and not self.oConfigObj:
            oConfigObj = ConfigObj(sIniFile, unrepr=True)
            self.oFd.write("INFO: Read INI file: " +oConfigObj.filename +'\n')
            self.oConfigObj = oConfigObj
            for sKey, gValue in oConfigObj['default'].items():
                setattr(self, sKey, gValue)

            self.lRequiredFeeds = [oConfigObj[s] for s in
                                   self.lRequiredFeedParams]
            self.lRequiredDishes = [oConfigObj[s] for s in
                                   self.lRequiredDishesParams]
            # eliminate
            self.lRequiredIngredients = []
            for sElt in self.lRequiredIngredients:
                self.lRequiredDishes += [sElt]
                for sKey in oConfigObj[sElt].keys():
                    if sKey not in self.lRequiredIngredients:
                        self.lRequiredIngredients += [sKey] 
Example #4
Source File: AirPwn.py    From MITMf-opt-plugins with GNU General Public License v2.0 6 votes vote down vote up
def initialize(self, options):
		self.options = options
		self.mon_interface = options.interface
		self.aircfg = options.aircfg
		self.dnspwn = options.dnspwn

		if os.geteuid() != 0:
			sys.exit("[-] AirPwn plugin requires root privileges")

		if not self.mon_interface:
			sys.exit("[-] AirPwn plugin requires --miface argument")

		try:
			self.aircfg = ConfigObj("./config/airpwn.cfg")
			#Here we compile the regexes for faster performance when injecting packets
			for rule in self.aircfg.items():
				rule[1]['match'] = re.compile(r'%s' % rule[1]['match'])
				if 'ignore' in rule[1].keys():
					rule[1]['ignore'] = re.compile(r'%s' % rule[1]['ignore'])

		except Exception, e:
			sys.exit("[-] Error parsing airpwn config file: %s" % e) 
Example #5
Source File: PhoneClassifier.py    From Ossian with Apache License 2.0 6 votes vote down vote up
def load(self):
        self.target_nodes = self.config.get('target_nodes', '//token')
        self.input_attribute = self.config.get('input_attribute', 'text')

        ## osw: For now, store phone class info to voice, not corpus.
        #self.phone_class_file = os.path.join(self.voice_resources.get_path(c.LANG), self.config['phone_classes'])
        self.phone_class_file = os.path.join(self.get_location(), self.config['phone_classes'])
        
        self.trained = False
        if os.path.isfile(self.phone_class_file):
            self.trained = True
        self.phone_classes = ConfigObj(self.phone_class_file, encoding='utf8')
        #if self.trained == False:
        #    self.phone_classes.clear()
        
        pass 
Example #6
Source File: configuration_manager.py    From panoptes with Apache License 2.0 6 votes vote down vote up
def _get_zookeeper_servers(self, config):
        """
        This method parses and stores the ZooKeeper servers to be used by the system

        Args:
            config (ConfigObj): The ConfigObj that holds the configuration

        Returns:
            None
        """
        zookeeper_servers = set()
        for zookeeper_server_id in config[u'zookeeper'][u'servers']:
            zookeeper_server = config[u'zookeeper'][u'servers'][zookeeper_server_id][u'host'] + u':' + \
                               str(config[u'zookeeper'][u'servers'][zookeeper_server_id][u'port'])
            zookeeper_servers.add(zookeeper_server)

        self._zookeeper_servers = zookeeper_servers 
Example #7
Source File: configuration_manager.py    From panoptes with Apache License 2.0 6 votes vote down vote up
def _get_kafka_brokers(self, config):
        """
        This method parses and stores the Kafka brokers to be used by the system

        Args:
            config (ConfigObj): The ConfigObj that holds the configuration

        Returns:
            None
        """
        kafka_brokers = set()
        for kafka_broker_id in config[u'kafka'][u'brokers']:
            kafka_broker = config[u'kafka'][u'brokers'][kafka_broker_id][u'host'] + u':' + \
                           str(config[u'kafka'][u'brokers'][kafka_broker_id][u'port'])

            kafka_brokers.add(kafka_broker)

        self._kafka_brokers = kafka_brokers 
Example #8
Source File: main.py    From pgcli with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_service_info(service):
    service = service or os.getenv("PGSERVICE")
    service_file = os.getenv("PGSERVICEFILE")
    if not service_file:
        # try ~/.pg_service.conf (if that exists)
        if platform.system() == "Windows":
            service_file = os.getenv("PGSYSCONFDIR") + "\\pg_service.conf"
        elif os.getenv("PGSYSCONFDIR"):
            service_file = os.path.join(os.getenv("PGSYSCONFDIR"), ".pg_service.conf")
        else:
            service_file = expanduser("~/.pg_service.conf")
    if not service:
        # nothing to do
        return None, service_file
    service_file_config = ConfigObj(service_file)
    if service not in service_file_config:
        return None, service_file
    service_conf = service_file_config.get(service)
    return service_conf, service_file 
Example #9
Source File: voices.py    From picochess with GNU General Public License v3.0 6 votes vote down vote up
def write_voice_ini():
    """Read the voices folder and write the result to voices.ini."""
    def get_immediate_subdirectories(a_dir):
        """Return the immediate subdirs."""
        return [name for name in os.listdir(a_dir)
                if os.path.isdir(os.path.join(a_dir, name))]

    program_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    voices_path = program_path + os.sep + 'talker' + os.sep + 'voices'
    config = ConfigObj(voices_path + os.sep + 'voices.ini', indent_type='    ')

    lang_list = get_immediate_subdirectories(voices_path)
    for lang_dir_name in lang_list:
        print(lang_dir_name)
        config[lang_dir_name] = {}
        speak_list = get_immediate_subdirectories(voices_path + os.sep + lang_dir_name)
        for speak_dir_name in speak_list:
            config[lang_dir_name][speak_dir_name] = {}
            config[lang_dir_name][speak_dir_name]['small'] = speak_dir_name[:6]
            config[lang_dir_name][speak_dir_name]['medium'] = speak_dir_name[:8].title()
            config[lang_dir_name][speak_dir_name]['large'] = speak_dir_name[:11].title()
    config.write() 
Example #10
Source File: config.py    From aws-deployments with MIT License 6 votes vote down vote up
def __init__(self):
    # our basic program variables
    self.config = ConfigObj('./conf/config.ini')
    
    # get user supplied variables
    self.config.merge(ConfigObj(os.path.expanduser(self.config['global_vars'])))

    # check that we got everything we need
    for v in self.config['required_vars']:
      if not v in self.config:
        raise Exception(
          'Required variable "{}" not found in {}'.format(v, self.config['global_vars']))
        
    self.config['vars_path'] = os.path.expanduser(
      '~/vars/{}'.format(self.config['prog']))
    self.config['env_path'] = self.config['vars_path'] + '/env'
    self.config['bin_path'] = self.config['install_path'] + '/bin'

    # make the /env/ directory if it does not exist
    try:
      os.makedirs(self.config['env_path'])
    except OSError:
      pass 
Example #11
Source File: vault.py    From datasploit with GNU General Public License v3.0 6 votes vote down vote up
def get_key(a):
    config_path = os.path.dirname(__file__)
    config_file = "%s/config.py" % config_path
    if os.path.exists(config_file):
      config = ConfigObj(config_file)
      try:
          if config[a] != '':
              return config[a]
          else:
              msg = "[-] " + a + " not configured"
              print colored(msg, 'yellow')
              return None
      except:
          msg = "[!] " + a + " not present"
          print colored(msg, 'yellow')
          return None
    else:
      print colored("[-] Error opening config file", 'yellow') 
Example #12
Source File: test_infos.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_validate_profile_infos(tmpdir, false_plugin_with_holder):
    """Test validating a profile.

    """
    i = ProfileInfos(path=PROFILE_PATH, plugin=false_plugin_with_holder)
    r, msg = validate_profile_infos(i)
    assert r

    for p, m in [(os.path.join(str(tmpdir), 'd_%s.instr.ini' % m), m)
                 for m in ('id', 'model_id', 'connections', 'settings')]:
        c = ConfigObj(PROFILE_PATH)
        del c[m]
        with open(p, 'wb') as f:
            c.write(f)
        i = ProfileInfos(path=p, plugin=false_plugin_with_holder)
        r, msg = validate_profile_infos(i)
        assert not r
        assert m in msg 
Example #13
Source File: module.py    From counterblock with MIT License 6 votes vote down vote up
def toggle(mod, enabled=True):
    try:
        imp.find_module(mod)
    except:
        print(("Unable to find module %s" % mod))
        return
    mod_config_path = os.path.join(config.config_dir, CONFIG_FILENAME % config.net_path_part)
    module_conf = ConfigObj(mod_config_path)
    try:
        try:
            if module_conf['LoadModule'][mod][0] in ['True', 'False']:
                module_conf['LoadModule'][mod][0] = enabled
            else:
                module_conf['LoadModule'][mod][1] = enabled
        except:
            module_conf['LoadModule'][mod].insert(0, enabled)
    except:
        if not "LoadModule" in module_conf:
            module_conf['LoadModule'] = {}
        module_conf['LoadModule'][mod] = enabled
    module_conf.write()
    print(("%s Module %s" % ("Enabled" if enabled else "Disabled", mod))) 
Example #14
Source File: config.py    From marvin-python-toolbox with Apache License 2.0 6 votes vote down vote up
def load_conf_from_file(path=None, section='marvin'):
    data = {}
    config_path = path  # try to get config path from args
    if not config_path:  # try to get config file from env
        config_path = os.getenv('MARVIN_CONFIG_FILE') or os.getenv('CONFIG_FILE')
    if not config_path:  # use default file
        config_path = os.getenv("DEFAULT_CONFIG_PATH")
    logger.info('Loading configuration values from "{path}"...'.format(path=config_path))
    config_parser = ConfigObj(config_path)
    try:
        data = config_parser[section]
    except NoSectionError:
        logger.warn('Couldn\'t find "{section}" section in "{path}"'.format(
            section=section, path=config_path
        ))

    return data 
Example #15
Source File: templates.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_template(path, data, doc):
    """ Save a template to a file

    Parameters
    ----------
        path : unicode
            Path of the file to which save the template
        data : dict
            Dictionnary containing the tempate parameters
        doc : unicode
            The template doc

    """
    # Create an empty ConfigObj and set filename after so that the data are
    # not loaded. Otherwise merge might lead to corrupted data.
    config = ConfigObj(indent_type='    ', encoding='utf-8')
    config.filename = path
    config.merge(data)
    config.initial_comment = wrap(doc, 79)

    config.write() 
Example #16
Source File: infos.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_blank(cls, plugin):
        """Create a new blank ProfileInfos.

        """
        c = ConfigObj(encoding='utf-8', indent_type='    ', )
        c['id'] = ''
        c['model_id'] = ''
        c['connections'] = {}
        c['settings'] = {}
        return cls(plugin=plugin, _config=c)

    # =========================================================================
    # --- Private API ---------------------------------------------------------
    # =========================================================================

    #: ConfigObj object associated to the profile. 
Example #17
Source File: plugin.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_preferences(self, path=None):
        """Collect and save preferences for all registered plugins.

        Parameters
        ----------
        path : unicode, optional
            Path of the file in which save the preferences. In its absence
            the default file is used.

        """
        if path is None:
            path = os.path.join(self.app_directory, 'preferences',
                                'default.ini')

        prefs = ConfigObj(path, encoding='utf-8', indent_type='    ')
        for plugin_id in self._pref_decls:
            plugin = self.workbench.get_plugin(plugin_id)
            decl = self._pref_decls[plugin_id]
            save_method = getattr(plugin, decl.saving_method)
            prefs[plugin_id] = save_method()

        prefs.write() 
Example #18
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_load3(pref_workbench, app_dir):
    """Test loading preferences from non-default file for started plugin.

    """
    pref_workbench.register(PreferencesManifest())
    c_man = PrefContributor()
    pref_workbench.register(c_man)
    contrib = pref_workbench.get_plugin(c_man.id)

    path = os.path.join(app_dir, 'preferences', 'custom.ini')
    conf = ConfigObj(path)
    conf[c_man.id] = {}
    conf[c_man.id]['string'] = 'test'
    conf.write()

    assert contrib.string == ''

    core = pref_workbench.get_plugin('enaml.workbench.core')
    core.invoke_command('exopy.app.preferences.load',
                        {'path': path}, pref_workbench)

    assert contrib.string == 'test' 
Example #19
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_load1(pref_workbench, app_dir):
    """Test loading default preferences for unstarted plugin.

    """
    # Register and start preferences plugin
    pref_workbench.register(PreferencesManifest())
    pref_workbench.get_plugin(PLUGIN_ID)

    c_man = PrefContributor()
    pref_workbench.register(c_man)

    path = os.path.join(app_dir, 'preferences', 'default.ini')
    conf = ConfigObj(path)
    conf[c_man.id] = {}
    conf[c_man.id]['string'] = 'test'
    conf.write()

    core = pref_workbench.get_plugin('enaml.workbench.core')
    core.invoke_command('exopy.app.preferences.load', {})
    assert pref_workbench.get_plugin(c_man.id, False) is None
    contrib = pref_workbench.get_plugin(c_man.id)

    assert contrib.string == 'test' 
Example #20
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_save1(pref_workbench, app_dir):
    """Test saving to the default file.

    """
    pref_workbench.register(PreferencesManifest())
    c_man = PrefContributor()
    pref_workbench.register(c_man)

    contrib = pref_workbench.get_plugin(c_man.id)
    contrib.string = 'test_save'

    core = pref_workbench.get_plugin('enaml.workbench.core')
    core.invoke_command('exopy.app.preferences.save', {}, pref_workbench)

    path = os.path.join(app_dir, 'preferences', 'default.ini')
    ref = {c_man.id: {'string': 'test_save', 'auto': ''}}
    assert os.path.isfile(path)
    assert ConfigObj(path).dict() == ref 
Example #21
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_auto_sync(pref_workbench, app_dir):
    """Check that auito_sync members are correctly handled.

    """
    pref_workbench.register(PreferencesManifest())
    c_man = PrefContributor()
    pref_workbench.register(c_man)

    contrib = pref_workbench.get_plugin(c_man.id)
    contrib.auto = 'test_auto'

    ref = {c_man.id: {'auto': 'test_auto'}}
    path = os.path.join(app_dir, 'preferences', 'default.ini')
    assert os.path.isfile(path)
    assert ConfigObj(path).dict() == ref

    contrib.auto = 'test'

    ref = {c_man.id: {'auto': 'test'}}
    path = os.path.join(app_dir, 'preferences', 'default.ini')
    assert os.path.isfile(path)
    assert ConfigObj(path).dict() == ref 
Example #22
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_load_defaultini(pref_workbench, app_dir):
    """Test that a default.ini file found in the app folder under prefs
    is loaded on startup.

    """
    prefs_path = os.path.join(app_dir, 'preferences')
    os.mkdir(os.path.join(app_dir, 'preferences'))

    conf = ConfigObj(os.path.join(prefs_path, 'default.ini'))
    c_man = PrefContributor()
    conf[c_man.id] = {}
    conf[c_man.id]['string'] = 'This is a test'
    conf.write()

    pref_man = PreferencesManifest()
    pref_workbench.register(pref_man)
    pref_workbench.register(c_man)

    c_pl = pref_workbench.get_plugin(c_man.id)

    assert c_pl.string == 'This is a test' 
Example #23
Source File: test_prefs.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_app_startup3(pref_workbench, tmpdir, exopy_qtbot):
    """Test app start-up when a preference file already exists.

    """
    manifest = PreferencesManifest()
    pref_workbench.register(manifest)

    # Create a trash app_directory.ini file. The global fixture ensure
    # that it cannot be a user file. Don't use app_dir fixture as I need to
    # test directory creation.
    app_pref = os.path.join(exopy_path(), APP_PREFERENCES, APP_DIR_CONFIG)
    app_dir = str(tmpdir.join('exopy'))
    conf = ConfigObj(encoding='utf8')
    conf.filename = app_pref
    conf['app_path'] = app_dir
    conf.write()

    assert not os.path.isdir(app_dir)

    # Start the app and fake a user answer.
    app = pref_workbench.get_plugin('exopy.app')

    app.run_app_startup(object())

    assert os.path.isdir(app_dir) 
Example #24
Source File: test_instr_view.py    From exopy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def instr_task_workbench(instr_workbench):
    """Workbench with instrument and task support and patched task.

    """
    w = instr_workbench
    w.register(InstrContributor())
    c = ConfigObj(PROFILE_PATH, encoding='utf-8')
    add_profile(instr_workbench, c, ['fp1', 'fp2', 'fp3', 'fp4'])
    w.register(TasksManagerManifest())
    p = w.get_plugin('exopy.tasks')
    infos = TaskInfos(cls=InterInstrTask,
                      instruments=['tasks.test.FalseDriver'])
    infos.interfaces = \
        {'test.I': InterfaceInfos(cls=TaskInterface, parent=infos,
                                  instruments=['tasks.test.FalseDriver4',
                                               'tasks.test.FalseDriver2']
                                  )
         }
    p._tasks.contributions['exopy.InstrumentTask'] = infos

    return w 
Example #25
Source File: naive_util.py    From Ossian with Apache License 2.0 5 votes vote down vote up
def config_list(value):
    '''
    ConfigObj handles reading of string lists without validation, but in the case of 
    1-item lists, strings will of course be returned. This function does type checking
    and conversion for this case.
    '''
    if isinstance(value, list):
        return value
    else:
        return [ value ] 
Example #26
Source File: procexp.py    From procexp with GNU General Public License v3.0 5 votes vote down vote up
def saveSettings():
  '''save settings to ~.procexp directory, in file "settings"
  '''
  widths = []
  for headerSection in range(g_mainUi.processTreeWidget.header().count()):
    widths.append(g_mainUi.processTreeWidget.header().sectionSize(headerSection))
  g_settings["columnWidths"] = widths
  
  settingsPath = os.path.expanduser("~/.procexp")
  if not(os.path.exists(settingsPath)):
    os.makedirs(settingsPath)
  f = file(settingsPath + "/settings","wb")
  cfg = configobj.ConfigObj(g_settings)
  cfg.write(f)
  f.close() 
Example #27
Source File: naive_util.py    From Ossian with Apache License 2.0 5 votes vote down vote up
def section_to_config(section):
    """
    Take a section from a ConfigObj and make it into new ConfigObj
    """
    new_conf = ConfigObj()
    for (key, value) in section.items():
        new_conf[key] = value
    return new_conf 
Example #28
Source File: measurement.py    From exopy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save(self, path):
        """Save the measurement as a ConfigObj object.

        Parameters
        ----------
        path : unicode
            Path of the file to which save the measurement.

        """
        config = ConfigObj(indent_type='    ', encoding='utf-8')
        config.update(self.preferences_from_members())

        # First save the task.
        core = self.plugin.workbench.get_plugin(u'enaml.workbench.core')
        cmd = u'exopy.tasks.save'
        task_prefs = core.invoke_command(cmd, {'task': self.root_task,
                                               'mode': 'config'}, self)
        config['root_task'] = {}
        include_configobj(config['root_task'], task_prefs)

        # Save the state of each monitor, pre-hook, post-hook.
        for kind in ('monitors', 'pre_hooks', 'post_hooks'):
            config[kind] = {}
            for id, obj in getattr(self, kind).items():
                state = obj.get_state()
                config[kind][id] = {}
                include_configobj(config[kind][id], state)

        with open(path, 'wb') as f:
            config.write(f)

        self.path = path 
Example #29
Source File: Syllabifier.py    From Ossian with Apache License 2.0 5 votes vote down vote up
def load(self):
        self.parent_node_type =  self.config.get('parent_node_type', '//token')
        self.target_nodes = self.config.get('target_nodes', "//token[@token_class='word']/descendant::segment")
        ## read phonetic classes, either unsupervised or human produced
        self.phoneclass_filename = os.path.join(self.get_location()+"/../phonetic_classifier", self.config['phone_classes'])
        #filename = os.path.join(self.voice_resources.get_path(c.LANG), self.config['phone_classes'])
        if os.path.isfile(self.phoneclass_filename):
            self.phones = ConfigObj(self.phoneclass_filename, encoding='utf8')
            # culcurate legexprs on init
            
            self.regexps = self._compile_syllable_regexps()
            self.trained = True
        else:
            self.trained = False 
Example #30
Source File: naive_util.py    From Ossian with Apache License 2.0 5 votes vote down vote up
def reformat_ini_to_htk(infile, outfile):
    config = ConfigObj(infile, encoding='UTF8', interpolation="Template")
    config = ['%s = %s'%(key,value) for (key,value) in config]
    config = '\n'.join(config) + '\n'
    writelist(config, outfile, uni=True)

##-----------------------------------