Пример #1
0
def load_parameters(file_name, config=None, term_conds=None,
                    dynamics=None, optim=None, pulsegen=None):
    """
    Import parameters for the optimisation objects
    Will throw a ValueError if file_name does not exist
    """
    parser = SafeConfigParser()
    readFiles = parser.read(file_name)
    if len(readFiles) == 0:
        raise ValueError("Parameter file '{}' not found".format(file_name))

    if config is not None:
        s = 'optimconfig'
        try:
            attr_names = parser.options(s)
            for a in attr_names:
                set_param(parser, s, a, config, a)
        except Exception as e:
            logger.warn("Unable to load {} parameters:({}) {}".format(
                s, type(e).__name__, e))

    if term_conds is not None:
        s = 'termconds'
        try:
            attr_names = parser.options(s)
            for a in attr_names:
                set_param(parser, s, a, term_conds, a)
        except Exception as e:
            logger.warn("Unable to load {} parameters:({}) {}".format(
                s, type(e).__name__, e))

    if dynamics is not None:
        s = 'dynamics'
        try:
            attr_names = parser.options(s)
            for a in attr_names:
                set_param(parser, s, a, dynamics, a)
        except Exception as e:
            logger.warn("Unable to load {} parameters:({}) {}".format(
                s, type(e).__name__, e))

    if optim is not None:
        s = 'optimizer'
        try:
            attr_names = parser.options(s)
            for a in attr_names:
                set_param(parser, s, a, optim, a)
        except Exception as e:
            logger.warn("Unable to load {} parameters:({}) {}".format(
                s, type(e).__name__, e))

    if pulsegen is not None:
        s = 'pulsegen'
        try:
            attr_names = parser.options(s)
            for a in attr_names:
                set_param(parser, s, a, pulsegen, a)
        except Exception as e:
            logger.warn("Unable to load {} parameters:({}) {}".format(
                s, type(e).__name__, e))
Пример #2
0
    def load(self, filename):
        """Loads the text map data and deserializes entities"""
        self.map_name = filename
        file_path = self.get_folder() + sep + filename
        config = SafeConfigParser()
        if not config.read(file_path):
            raise IOError("Map filename '%s' doesn't exist" % file_path)

         # Get the save header
        s = self.__cfg_header
        version = float(config.get(s, "version"))
        if version != WORLD_VERSION:
            self.__log.warning("Version mismatch, save is %s, and internal version is %s" % (version, WORLD_VERSION))

         # Deserialize and load each entity
        s = self.__cfg_entities
        for uid in config.options(s):
            data = chr_bytes(config.get(s, uid))
            self.entity_manager.deserialize_entity(int(uid), data)

        s = self.__cfg_map
        self.map = []
        for str_x in config.options(s):
            x = int(str_x)
            while len(self.map) <= x:
                self.map.append(None)  # Fill the unnallocated space
            y = config.get(s, str_x)
            self.map[x] = y

        for x, line in enumerate(self.map):
            if line == None:
                raise ParsingError("Missing map line definition at line %s" % x)
        self.redraw_map = True
        self.redraw_minimap = True
        self.__log.info("Loaded world %s" % filename)
Пример #3
0
def createDictFromConfig():
    """ Read the configuration from scipion/config/scipionbox.conf.
     A dictionary will be created where each key will be a section starting
     by MICROSCOPE:, all variables that are in the GLOBAL section will be
     inherited by default.
    """
    # Read from users' config file.
    confGlobalDict = OrderedDict()
    confDict = OrderedDict()
    cp = SafeConfigParser()

    cp.optionxform = str  # keep case (stackoverflow.com/questions/1611799)

    confFile = pw.getConfigPath("scipionbox.conf")

    print "Reading conf file: ", confFile
    cp.read(confFile)

    GLOBAL = "GLOBAL"
    MICROSCOPE = "MICROSCOPE:"

    if not GLOBAL in cp.sections():
        raise Exception("Missing section %s in %s" % (GLOBAL, confFile))
    else:
        for opt in cp.options(GLOBAL):
            confGlobalDict[opt] = cp.get(GLOBAL, opt)

    for section in cp.sections():
        if section != GLOBAL and section.startswith(MICROSCOPE):
            sectionDict = OrderedDict(confGlobalDict)
            for opt in cp.options(section):
                sectionDict[opt] = cp.get(section, opt)
            confDict[section.replace(MICROSCOPE, '')] = sectionDict

    return confDict
def _config_from_config_profile(config_file, profile, app):
    """
    Configures ec2stack app based on configuration profile.

    @param config_file: current config file configuration.
    @param profile: the profile to set the attribute in.
    """
    config = SafeConfigParser()
    config.read(config_file)

    if not config.has_section(profile):
        sys.exit('No profile matching ' + profile +
                 ' found in configuration, please run ec2stack-configure -p ' + profile)

    for attribute in config.options(profile):
        app.config[attribute.upper()] = config.get(profile, attribute)

    instance_type_map = {}
    instance_section = profile + "instancemap"
    if config.has_section(instance_section):
        for attribute in config.options(instance_section):
            instance_type_map[attribute] = config.get(
                instance_section, attribute)

    app.config['INSTANCE_TYPE_MAP'] = instance_type_map

    resource_type_map = {}
    resource_section = profile + "resourcemap"
    if config.has_section(resource_section):
        for attribute in config.options(resource_section):
            resource_type_map[attribute] = config.get(
                resource_section, attribute)

    app.config['RESOURCE_TYPE_MAP '] = resource_type_map
Пример #5
0
    def check(self, filename):
        """ Check the settings currently set against default.cfg """
        default = Parser()
        default.readfp(open(os.path.join(self.config_location, "default.cfg")))
        local = Parser()
        local.readfp(open(filename))

        local_ok = True
        diff = set(local.sections()) - set(default.sections())
        for section in diff:
            logging.warning("Section {} does not exist in {}".format(
                section, "default.cfg"))
            local_ok = False
        for section in local.sections():
            if not default.has_section(section):
                continue
            diff = set(local.options(section)) - set(default.options(section))
            for option in diff:
                logging.warning(
                    "Option {} in section {} does not exist in {}".format(
                        option, section, "default.cfg"))
                local_ok = False
        if local_ok:
            logging.info("{} is OK".format(filename))
        else:
            logging.warning("{} contains errors.".format(filename))
        return local_ok
Пример #6
0
def _config_from_config_profile(config_file, profile, app):
    """
    Configures ec2stack app based on configuration profile.

    @param config_file: current config file configuration.
    @param profile: the profile to set the attribute in.
    """
    config = SafeConfigParser()
    config.read(config_file)

    if not config.has_section(profile):
        sys.exit('No profile matching ' + profile +
                 ' found in configuration, please run ec2stack-configure -p ' +
                 profile)

    for attribute in config.options(profile):
        app.config[attribute.upper()] = config.get(profile, attribute)

    instance_type_map = {}
    instance_section = profile + "instancemap"
    if config.has_section(instance_section):
        for attribute in config.options(instance_section):
            instance_type_map[attribute] = config.get(instance_section,
                                                      attribute)

    app.config['INSTANCE_TYPE_MAP'] = instance_type_map

    resource_type_map = {}
    resource_section = profile + "resourcemap"
    if config.has_section(resource_section):
        for attribute in config.options(resource_section):
            resource_type_map[attribute] = config.get(resource_section,
                                                      attribute)

    app.config['RESOURCE_TYPE_MAP '] = resource_type_map
Пример #7
0
def load_config(cfgfile):
    s = SafeConfigParser()
    if not s.read(cfgfile):
        raise Exception("Can't read configfile {0}".format(cfgfile))

    conf = {
        "encoding": sys.getfilesystemencoding(),
        "sleeptime": "60",
    }
    conf.update((k, s.get('tilde', k)) for k in s.options('tilde'))

    conf['sleeptime'] = int(conf['sleeptime'])
    enc = conf['encoding']

    conf['servers'] = srv = {}

    conf['defaults'] = defaults = {'commands': None}
    if s.has_section('defaults'):
        defaults.update((k, s.get('defaults', k).decode(enc))
                        for k in s.options('defaults'))

    conf['commands'] = cmds = {"ubuntu": commands.ubuntu}
    for section in [
            section for section in s.sections()
            if section.startswith("commands:")
    ]:
        name = section.replace("commands:", "", 1).decode(enc)
        opts = dict(
            (k, s.get(section, k).decode(enc)) for k in s.options(section))

        inherit = opts.pop("__inherit__", None)

        c = cmds[inherit].copy() if inherit else commands.Commands()
        for k, v in opts.iteritems():
            setattr(c, k, v)

        cmds[name] = c

    for section in [
            section for section in s.sections()
            if section.startswith("server:")
    ]:
        name = section.replace("server:", "", 1).decode(enc)
        opts = defaults.copy()
        opts.update(
            (k, s.get(section, k).decode(enc)) for k in s.options(section))

        if opts["commands"]:
            opts["commands"] = cmds[opts["commands"]]

        opts.setdefault("hostname", name)

        if "port" in opts:
            try:
                opts["port"] = int(opts["port"])
            except ValueError, e:
                # This may be a port name, which could be handled fine
                pass

        srv[name] = Server(**opts)
Пример #8
0
class ConfigIni():
    def __init__(self, path):

        self.parser = SafeConfigParser()
        self.path = path

    # Read the value from the option inside the session, output: value (string)

    def read_value(self, section, option):
        self.parser.read('%s' % self.path)
        if section in self.parser.sections():
            if option in self.parser.options(section):
                return self.parser.get('%s' % section, '%s' % option)
        else:
            return None

    # Read all values and all options inside the session, output: values list(string), options list(string)

    def read_all_values(self, section):
        self.parser.read('%s' % self.path)
        if section in self.parser.sections():
            options = self.parser.options(section)
            if options:
                values = []
                for value in self.parser.items(section):
                    values.append(value[1])
                return options, values
        else:
            return None

    # Write or change the session information, if the section exist a new session is added

    def write_section(self, section, options, values):
        '''
        Write or change the session information, if the section exist a new session is added
        :param section: Init section
        :param options: Must be a list
        :param values: Must be a list
        :return:
        '''
        self.parser.read('%s' % self.path)
        if section not in self.parser.sections():
            self.parser.add_section('%s' % section)
        for option, value in zip(options, values):
            self.parser.set('%s' % section, '%s' % option, '%s' % value)
        cfgfile = open('%s' % self.path, 'w')
        self.parser.write(cfgfile)
        cfgfile.close()

    # Read all sections, output: sections list(string)

    def read_sections(self):
        self.parser.read('%s' % self.path)
        sections = self.parser.sections()
        return sections
Пример #9
0
    def initialise_righteous(self):
        config = SafeConfigParser()
        config.read('righteous.config')
        if not config.has_section('auth'):
            raise Exception('Please create a righteous.config file with appropriate credentials')

        self.auth = dict((key,config.get('auth', key)) for key in config.options('auth'))
        self.server = dict((key,config.get('server-defaults', key)) for key in config.options('server-defaults'))
        righteous.init(self.auth['username'], self.auth['password'], self.auth['account_id'], **self.server)

        if not righteous.config.settings.cookies:
            righteous.login()
Пример #10
0
 def loadStatus(self, name="./conf"):
     config = SafeConfigParser()
     config.read(name)
     try:
         if config.sections() != []:
             if config.has_section("GAME"):
                 self._quest = config.getint("GAME", "quest")
                 self.setState(config.getint("GAME", "state"))
                 self._secState = config.get("GAME", "secState")
                 if self._secState != "None":
                     self._secState = int(self._secState)
             if config.has_section("PLAYABLE_AGENTS"):
                 self.agentManager.reset()
                 for l in config.options("PLAYABLE_AGENTS"):
                     name = "{:s}:{:s}".format(l.split('_')[0].upper(), l.split('_')[1])
                     a = self.agentManager.getAgentByName(name)
                     params = config.get("PLAYABLE_AGENTS", l).split(';')
                     if a != None:
                         a.health = int(params[0])
                         a.magic = int(params[1])
                         l = a.agent.getLocation()
                         l.setLayerCoordinates(fife.ModelCoordinate(*(int(float(params[2])), int(float(params[3])))))
                         a.agent.setLocation(l)
                         a._mode = int(params[4])
                     self.agentManager.addNewPlayableAgent(name)
             if config.has_section("BEES"):
                 for l in config.options("BEES"):
                     name = "{:s}:{:s}:{:s}".format(l.split('_')[0].upper(), l.split('_')[1], l.split('_')[2])
                     b = self.agentManager.getAgentByName(name)
                     params = config.get("BEES", l).split(';')
                     if b != None:
                         l = fife.Location(self.world.map.getLayer('TechdemoMapGroundObjectLayer'))
                         l.setLayerCoordinates(fife.ModelCoordinate(*(int(float(params[0])), int(float(params[1])))))
                         b.agent.setLocation(l)
                         b.state = int(params[2])
                         b.mode = int(params[3])
                         b.start()
             self.dialog.hide_exit_menu()
             self.dialog.hide_load_menu()
     except AttributeError as e:
         print "###################################################################################"
         print "Unexpected error:", sys.exc_info()[0]
         print e.message
         print "--- Configuration file malformed. Deleted. ---"
         print "--- Please, restart again the game ---"
         print "###################################################################################"
         self.deleteStatus()
         self.exit()
Пример #11
0
def main():
    if len(sys.argv) < 3:
        sys.exit('Usage: AddUser.py <username> <password> \n Set password to "-" to delete a user')
        
    parser = SafeConfigParser()
    parser.read('users.ini')
    
    if not parser.has_section('users'):
        parser.add_section('users')

    try:
        if (sys.argv[2] == "-"):
            parser.remove_option('users', sys.argv[1])
        else:
            parser.set('users', sys.argv[1], hashlib.md5(sys.argv[2].strip()).hexdigest() )
    except:
        pass

    # prevent an empty file -- always have a default user if there is no other user
    if len(parser.options('users')) == 0:
        parser.set('users', 'default', hashlib.md5('default').hexdigest() )
    
    file = open('users.ini', 'w')
    file.write('#This is the list of users/encrypted passwords for the Linux CNC Web Server\n\n')
    file.write('#Use the included python script AddUser to add users\n')
    file.write('#Users can be removed by deleting the entries in this file with their user name\n')
    file.write('#This file will be auto-generated and overwritten by the AddUser program\n\n')
    parser.write(file)
    file.close()
Пример #12
0
def main(inifile = 'example.ini', verbose = 0):
    """Parse the config file and perform actions it specifies."""
    parser = SafeConfigParser()
    try: parser.read(inifile.name)
    except: parser.read(inifile)
    if verbose > 0: 
        print 'verbose output'
        print parser.sections()
    for section_name in parser.sections():
        print section_name
        if verbose > 0: 
            print 'Section:', section_name
            print '  Options:', parser.options(section_name)
            for name, value in parser.items(section_name):
                print '  %s = %s' % (name, value)
        try: 
            act = parser.get(section_name, 'action')
        except Exception as e:
            raise e
        if act == 'unzip': 
            unzipper(parser.get(section_name, 'input'), 
                parser.get(section_name, 'output'),
                verbose)
        elif act == 'gzip':
            gzipper(parser.get(section_name, 'input'), 
                parser.get(section_name, 'output'),
                verbose)
        else:
            raise Exception('how do I {0}?'.format(act))
Пример #13
0
    def from_inifile(self, filename, silent=False):
        """
        Reads settings from an ini file.
        Settings are added to the configuration dictionary in
        the following form. All section and option keys are
        changed to uppercase.

        <SECTION_NAME>_<OPTION_NAME>

        However, all options in the special section "namake"
        are added to the dictionary without the section name 
        as follows.

        <OPTION_NAME>
        """
        from ConfigParser import SafeConfigParser

        filename = os.path.join(self.root_path, filename)
        config = SafeConfigParser()
        with open(filename, 'rb') as cfgfile:
            config.readfp(cfgfile)
        for section in config.sections():
            for option in config.options(section):
                # Try to coerce the value from the ini file into
                # native python objects automatically.
                for method in ['getint', 'getfloat', 'getboolean', 'get']:
                    try:
                        value = getattr(config, method)(section, option)
                        if section.upper() == 'NAMAKE':
                            self[option.upper()] = value
                        else:
                            self[section.upper() + '_' + option.upper()] = value
                        break
                    except ValueError:
                        pass
Пример #14
0
	def setDriver(self,fname="") :

		### File check
		if not os.path.exists(fname) :
			raise IOError(fname)

		parser			 = SafeConfigParser()
		parser.read(fname)

		## ConfigParserを使って連想配列に変換する
		config = {}
		for sect in parser.sections() :
			config[sect] = {}
			for opt in parser.options(sect) :
				config[sect][opt] = parser.get(sect, opt)

		self.__driver	= config['DataAccess']['driver']

		## DriverXxxxx の形式に変換。
		class_name		= 'Driver' + self.__driver.capitalize()

		## インポートを動的に実行。private変数の__instanceにドライバーの
		## インスタンスを格納する。
		module_name			= __import__('driver.' + self.__driver, globals(), locals(), [class_name], -1)
		class_constructor	= getattr(module_name, class_name)
		self.__instance		= class_constructor()

		## ドライバーにconfigオブジェクトを渡す
		self.__instance.setConfig(config)
		return self
def parse_ini():
    parser = SafeConfigParser()
    parser.read("danboorudownloader.ini")
    for sect in parser.sections():
        print(sect, parser.options(sect))
        for name, value in parser.items(sect):
            print(name, value)
Пример #16
0
    def _loadconfig(self, arg=[]):
        # manage services
        for service, value in self.services.iteritems():
            if self.name not in app['services'][service].services:
                app['services'][service].services = {}
            app['services'][service].services[self.name] = value

        # add command line args to the program options + build default configuration data
        defaultConf = '[' + self.name + ']\n'
        group = OptionGroup(app['parser'],
                            self.name.capitalize() + " Options", self.desc)
        if self.options.__class__ is dict:
            tmp = []
            for option, value in self.options.items():
                tmp.append({option: value})
            self.options = tmp
        #for option, value in self.options.items():
        for option in self.options:
            option, value = option.items()[0]
            if len(value) == 3:
                help = value[0] + " " + value[2] + ""
                defaultConf += '; ' + value[0] + ' - choices: ' + value[
                    2] + '\n'
            else:
                help = value[0]
                defaultConf += '; ' + value[0] + '\n'
            defaultConf += ';' + option + '=' + str(value[1]) + '\n\n'
            if option != 'start':
                if self.name == 'main':
                    group.add_option('--' + option,
                                     '--' + self.name + '.' + option,
                                     type='str',
                                     help=help,
                                     metavar=str(value[1]))
                else:
                    group.add_option('--' + self.name + '.' + option,
                                     type='str',
                                     help=help,
                                     metavar=str(value[1]))
            self.conf[option] = value[1]
        app['parser'].add_option_group(group)

        # create default config if none
        userConfFile = app['path']['conf'] + self.nameconf
        if not os.path.exists(userConfFile):
            if not os.path.exists(app['path']['conf']):
                os.mkdir(app['path']['conf'])
            fp = open(userConfFile, 'w')
            fp.write(defaultConf)
            fp.close()

        # read user config
        fileconf = SafeConfigParser()
        fileconf.read(userConfFile)

        # set user config
        for option in fileconf.options(self.name):
            self.conf[option] = fileconf.get(self.name, option)

        self.pLoadconfig()
def parse_config_file(filename):
    """
    Parses the config file passed into this script
    NOTE: note the conversion to unicode
    """

    triggers={}
    parser = SafeConfigParser()
    parser.read(filename)

    for s in parser.sections():
      if s != 'settings':
        triggers[s]={}
        triggers[s]['rules']=[]
        for o in parser.options(s):
          stuff = o.lstrip("regex_")
          if stuff != o:
            triggers[s]['rules'].append(stuff)
            triggers[s]['rules'].append(unicode(parser.get(s,o), "utf-8"))
          else:
            triggers[s][o]=unicode(parser.get(s,o), "utf-8").strip("'")

    return (unicode(parser.get("settings", "rabbitmqusername"), "utf-8"),
            unicode(parser.get("settings", "rabbitmqpassword"), "utf-8"),
            unicode(parser.get("settings", "cbserverip"), "utf-8"),
            unicode(parser.get("settings", "cbtoken"), "utf-8"),
            unicode(parser.get("settings", "epserverip"), "utf-8"),
            unicode(parser.get("settings", "eptoken"), "utf-8"),
            triggers)
Пример #18
0
def getOptions(cfgFile, section, args):
    cfgParser = SafeConfigParser()
    cfgParser.read(cfgFile)
    argParser = OptionParser()

    masterSection=section
    masterOptions = cfgParser.options(masterSection);
    for dest in masterOptions:
        line=cfgParser.get(masterSection,dest)
        line=line.strip()
        if not line: continue
        short, long, help, meta, type, default = line.split(";")
        if default=="True":
            argParser.add_option(short, long, dest=dest, action="store_false", help=help)
        elif default=="False":
            argParser.add_option(short, long, dest=dest, action="store_true", help=help)
        else:
            argParser.add_option(short, long, dest=dest, help=help, default=default, type=type, metavar=meta)

    options, additionals = argParser.parse_args(args)
    
    # '+' args are for TGE
    for i in range(len(additionals)):
        additionals[i] = additionals[i].replace('+', '-')
    
    return options, additionals
Пример #19
0
def read_config(file_config="dagon.ini", section=None):
    """
    Reads the configuration file specified

    :param file_config: path to the configuration file
    :type file_config: str

    :param section: section of the file to read
    :type section: str

    :return: dictionary with the configuration
    :rtype: dict(str, dict)
    """
    config = SafeConfigParser()
    config.read(file_config)
    if section is not None:
        try:
            return dict(config.items(section))
        except:
            return None
    else:
        dictionary = defaultdict(dict)
        for section in config.sections():
            dictionary[section] = {}
            for option in config.options(section):
                # print option
                dictionary[section][option] = config.get(section,
                                                         option,
                                                         raw=True)
        return dictionary
Пример #20
0
def get_section(configfile_name, section_name):
    parser = SafeConfigParser()
    dictionary = {}
    
    for folder in FOLDERS:
        config_file_path = folder + "/" + configfile_name
        log.debug(config_file_path)
        
        if os.path.exists(config_file_path):
            parser.read(config_file_path)
            
            try:
                for key in parser.options(section_name):
                    value = parser.get(section_name, key)
                    
                    if "," in value:
                        value = value.split(",")
                    
                    dictionary[key] = value
            
            except NoSectionError:
                continue
    
        log.debug(dictionary)        
        return dictionary
Пример #21
0
def configParse(file_path) :

    ### File check
    if not os.path.exists(file_path) :
        raise IOError(file_path)

    parser = SafeConfigParser()
    parser.read(file_path)

    ### Convert to dictionary
    config = {}
    for sect in parser.sections() :
        config[sect] = {}
        for opt in parser.options(sect) :
            config[sect][opt] = parser.get(sect, opt)

    ### Data check and convert from type
    for sect in config_settings.keys() :

#        if not sect in config :
#            raise KeyError(sect)

        for opt_attr in config_settings[sect] :
            if opt_attr['required'] and (not opt_attr['name'] in config[sect]) :
                raise KeyError(opt_attr['name'])

            if config[sect][opt_attr['name']] == 'None' :
                config[sect][opt_attr['name']] = None
            else :
                config[sect][opt_attr['name']] = opt_attr['type'](config[sect][opt_attr['name']])

    return config
Пример #22
0
def ReadAllConf(configfile,sections,DefaultConfDict=None):
    confDict = {}
    scp = SafeConfigParser()
    with open(configfile,'r') as f:
        f.seek(0)
        try:
            scp.readfp(f)
        except:
            confDict = {}
        else:
            for section in scp.sections():
                optionDict = {}
                options = scp.options(section)
                #print options
                for key in options:
                    #print key,scp.get(section,key)
                    try:
                        value = scp.get(section,key)
                    except:
                        value = ""
                    optionDict[key] = value
                    confDict[section] = optionDict
    if len(confDict) == 0:
        confDict = DefaultConfDict
    return confDict
Пример #23
0
class ConfigManager(object):
    class Section:
        def __init__(self, name, parser):
            self.__dict__["name"] = name
            self.__dict__["parser"] = parser

        def __setattr__(self, option, value):
            self.__dict__[option] = str(value)
            self.parser.set(self.name, option, str(value))

    def __init__(self, file_name):
        self.parser = SafeConfigParser()
        self.parser.read(file_name)
        self.file_name = file_name

        for section in self.parser.sections():
            setattr(self, section, self.Section(section, self.parser))
            for option in self.parser.options(section):
                setattr(getattr(self, section), option, self.parser.get(section, option))

    def __getattr__(self, section):
        self.parser.add_section(section)
        setattr(self, section, self.Section(section, self.parser))
        return getattr(self, section)

    def save_config(self):
        with file(self.file_name, "w") as fb:
            self.parser.write(fb)
Пример #24
0
def get_workflow_actions_from_text(wf_txt, is_error_wf=False):
    """Parse workflow actions in a text snippet
    Note that no section header [ticket-workflow_xxx] must be provided"""

    def get_line_txt(txt):
        try:
            msg = txt.split(']:')[1]
        except IndexError:
            msg = txt

        return msg

    error_txt = ""
    config = SafeConfigParser()
    try:
        config.readfp(
            io.BytesIO("[ticket-workflow]\n" + wf_txt.encode('utf-8')))
        raw_actions = [(key, config.get('ticket-workflow', key)) for key in
                       config.options('ticket-workflow')]
    except ParsingError, err:
        error_txt = u"Parsing error: %s" % get_line_txt(
            unicode(err).replace('\\n', '').replace('<???>', ''))

        if not is_error_wf:  # prevent recursion
            actions, tmp = get_workflow_actions_for_error()
        else:
            actions = []
        return actions, error_txt
Пример #25
0
    def _load_from_file(self, *args):
        config = SafeConfigParser()
        config.read(args)

        for section in config.sections():
            for option in config.options(section):
                self.data[unicode(option)] = config.get(section, option)
Пример #26
0
class ConfigManager(object):
    class Section:
        def __init__(self, name, parser):
            self.__dict__['name'] = name
            self.__dict__['parser'] = parser

        def __setattr__(self, option, value):
            self.__dict__[option] = str(value)
            self.parser.set(self.name, option, str(value))

    def __init__(self, file_name):
        self.parser = SafeConfigParser()
        self.parser.read(file_name)
        self.file_name = file_name
        for section in self.parser.sections():
            setattr(self, section, self.Section(section, self.parser))
            for option in self.parser.options(section):
                setattr(getattr(self, section), option,
                        self.parser.get(section, option))

    def __getattr__(self, option):
        self.parser.add_section(option)
        setattr(self, option, Section(option, self.parser))
        return getattr(self, option)

    def save_config(self):
        f = open(self.file_name, 'w')
        self.parser.write(f)
        f.close()
def get_oauth_token(tribe_url, secrets_location):
    secrets_file = SafeConfigParser()
    secrets_file.read(secrets_location)

    if not secrets_file.has_section('Tribe secrets'):
        logger.error('Secrets file has no "Tribe secrets" section, which is'
                     ' required to save the processed genesets to Tribe.')
        sys.exit(1)

    required_secrets = set(
        ['tribe_id', 'tribe_secret', 'username', 'password'])
    secrets_set = set(secrets_file.options('Tribe secrets'))

    if not required_secrets.issubset(secrets_set):
        logger.error('Tribe secrets section must contain TRIBE_ID, '
                     'TRIBE_SECRET, USERNAME, and PASSWORD to be able to save'
                     ' processed genesets to Tribe.')
        sys.exit(1)

    tribe_id = secrets_file.get('Tribe secrets', 'TRIBE_ID')
    tribe_secret = secrets_file.get('Tribe secrets', 'TRIBE_SECRET')
    username = secrets_file.get('Tribe secrets', 'USERNAME')
    password = secrets_file.get('Tribe secrets', 'PASSWORD')

    access_token_url = tribe_url + '/oauth2/token/'
    access_token = obtain_token_using_credentials(username, password, tribe_id,
                                                  tribe_secret,
                                                  access_token_url)
    return access_token, username
Пример #28
0
def read_config(config_filepath):
    """
    Return a dictionary with subdictionaries of all configFile options/values

    Parameters
    ----------
    config_filepath : str
        Filepath to configparser / ini style formatted configuration file

    Returns
    -------
    config_dict : collections.OrderedDict
        OrderedDict of parsed configuration file value

    """

    if not os.path.isfile(config_filepath):
        raise RasmLibIOError('Configuration File ({0}) does not '
                             'exist'.format(config_filepath))

    config = SafeConfigParser()
    config.optionxform = str
    config.read(config_filepath)
    sections = config.sections()
    config_dict = OrderedDict()
    for section in sections:
        options = config.options(section)
        dict2 = OrderedDict()
        for option in options:
            dict2[option] = config_type(config.get(section, option))
        config_dict[section] = dict2
    return config_dict
Пример #29
0
def load_local_config(config_file, required=[], optional=[]):
    """Loads config from ~/.cloudconnect and from os.environ, and merges values into current globals()"""
    from ConfigParser import SafeConfigParser

    config = SafeConfigParser()
    config.optionxform = str
    new_config = {}

    if os.path.exists(config_file):
        if "DEBUG_SETTINGS" in os.environ:
            print "Reading '%s'" % config_file
        config.read(config_file)

        env = determine_env()
        if env in config.sections():
            for key in config.options(env):
                val = config.get(env, key)
                if key in os.environ:
                    val = os.environ[key]
                val = cast_env_vars(val)
                if "DEBUG_SETTINGS" in os.environ:
                    print "Setting %s to %s" % (key, str(val))
                new_config[key] = val

    for key in required:
        if key in os.environ:
            new_config[key] = cast_env_vars(os.environ[key])

    for key in optional:
        if key in os.environ:
            new_config[key] = cast_env_vars(os.environ[key])
    return new_config
Пример #30
0
def getSensorName(sensor):
    parser = SafeConfigParser()
    parser.read(SETTINGS_FILE)
    for t_sensor in parser.options('temp sensors'):
        if sensor == int(t_sensor):
            return parser.get('temp sensors', t_sensor)
    return 'Unknown'
Пример #31
0
 def _configure(self, *args, **kwargs):
   """"""
   
   config = SafeConfigParser()
   if args:
     old_config = args[0]
     for section in old_config.sections():
       config.add_section(section)
       for option in old_config.options(section):
         config.set(section, option, old_config.get(section, option))
   else:
     config_files = [os.path.join('config', 'defaults.cfg'),
                     kwargs.pop('config_file', '')]
     config.read(config_files)
   # argparse syntax should be --section_name option1=value1 option2=value2
   # kwarg syntax should be section_name={option1: value1, option2: value2}
   # or option1=value1, option2=value2
   config_sections = {section.lower().replace(' ', '_'): section for section in config.sections()}
   config_sections['default'] = 'DEFAULT'
   config_options = set()
   for section in config.sections():
     config_options.update(config.options(section))
   for kw, arg in kwargs.iteritems():
     if isinstance(arg, dict):
       section = config_sections[kw]
       for option, value in arg.iteritems():
         assert option in config_options
         config.set(section, option, str(value))
     else:
       option, value = kw, arg
       section = re.sub('\B([A-Z][a-z])', r' \1', self.__class__.__name__)
       section = re.sub('([a-z])([A-Z])', r'\1 \2', section)
       assert option in config_options
       config.set(section, option, str(value))
   return config
def AccDataDownloader():
    '''Download Acceleration data to disk
    '''

    time.clock()

    scp = SafeConfigParser()
    scp.read('Config.ini')

    server = scp.get('General', 'server')
    db = scp.get('General', 'db')
    uid = scp.get('General', 'uid')
    pwd = scp.get('General', 'pwd')
    path = scp.get('General', 'path')

    dtStart = scp.get('AcceCollect', 'StartDatetime')
    dtEnd = scp.get('AcceCollect', 'EndDatetime')
    chStart = int( scp.get('AcceCollect', 'StartChannel') )
    chEnd = int( scp.get('AcceCollect', 'EndChannel') )

    paras = {}
    chs = scp.options('AcceSensitivity')

    for ch in chs:
        paras[ch] = float(scp.get('AcceSensitivity', ch))

    t = AcceData(server, db, uid, pwd, paras, path, 'Acce')
    t.DTInit(dtStart, dtEnd)
    # cProfile.run('t.GetData(chStart, chEnd)')
    t.GetData(chStart, chEnd)

    print 'Finished in %6.3fs' % time.clock()
Пример #33
0
class Configs:
    """ Grab data from configs """

    def __init__(self):
        """ Setup """
        CONFIGFILE = 'settings.conf'
        self.settings = SafeConfigParser()
        curdir = os.path.dirname(os.path.realpath(__file__))
        configfile = "{0}/{1}".format(curdir, CONFIGFILE)
        self.settings.read(configfile)

    def get_creds(self):
        """ returns a dict of username, token """
        username = self.settings.options('username')[0]
        token = self.settings.get('username', username)
        return {'username': username, 'token': token}

    def github(self):
        """ Kinda lame that this only returns github host """
        return {'host': self.settings.get('github', 'host')}

    def git_user(self):
        """ returns just the github user in config"""
        # this is added because the auth user may not be the same as git user
        return {'user': self.settings.get('github', 'user')}
Пример #34
0
def config_parser(config_file):
    """
    Parse data from configuration files.
    :param config_file: Configuration file with currencies or exchanges data.
    """
    res = []

    # Parse and read configuration file.
    cparser = SafeConfigParser()
    cparser.read(config_file)

    for section in cparser.sections():
        tup = ()

        for option in cparser.options(section):
            value = cparser.get(section, option)
            # String 'True' or 'False' values to boolean
            if value == 'True':
                value = True
            elif value == 'False':
                value = False
            tup += (value, )
        res.append(tup)

    return res
Пример #35
0
def set_env_vars(config_file):
    parser = SafeConfigParser()
    parser.read(config_file)

    env_base = "CONFIG_PARSER"

    final_env = {}

    for s in parser.sections():
        sec = s.upper()
        for o in parser.options(s):
            final_env["___".join([env_base, sec,
                                  o.upper()])] = parser.get(s, o)


#final_string = " ".join(["%(env)s=%(val)s" % {"env":k,"val":final_env[k]} for k in final_env.keys()])

#print final_string

    final_strings = [
        "%(env)s=%(val)s" % {
            "env": k,
            "val": final_env[k]
        } for k in final_env.keys()
    ]

    for k in final_env.keys():
        print k
        if len(sys.argv) > 2:
            del os.environ[k]
        else:
            os.environ[k] = str(final_env[k])
Пример #36
0
    def showVersion(self, module):
	from ConfigParser import SafeConfigParser
	from StringIO import StringIO
	import datetime
	import time
	file = open(FILE_VERSION_PATH,'r')
	f = StringIO(file.read())
	f.seek(0)
	iniPaser = SafeConfigParser()
	iniPaser.readfp(f)
	sections = iniPaser.sections()
	number = None
	try:
	    options = iniPaser.options(module)
 	    for opt in options:
	       	value = iniPaser.get(module, opt)
		if number is None:
		    number = str(value)
	        else:
		    number = number + "." + str(value)
	    self.version = module + "_" + number
	    self.version = self.version + "_" + datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d')
	    self.buildLog(self.version)
	except:
	    self.buildLog(module + " is no version number.")
	file.close()
Пример #37
0
    def _add_default(self, app):

        for id in ['alameter.conf', 'alameter.conf.user']:
            row = self.get(id)
            if row:
                # configuration exists
                continue

            # load configuration
            if id == 'alameter.conf':
                path = "%s/api/etc/%s.%s" % (HOME_DEFAULT, id, app.lower())
            else:
                path = "%s/api/etc/%s" % (HOME_DEFAULT, id)
            if not os.path.isfile(path):
                continue

            conf_parser = SafeConfigParser()
            conf_parser.read(path)

            conf = {}
            for s in conf_parser.sections():
                conf[s] = {}
                for o in conf_parser.options(s):
                    conf[s][o] = conf_parser.get(s, o)

            # insert to database
            row = self.get(id)
            if not row:
                # check again to prevent concurrent insert
                self.insert({'id': id, 'config': json.dumps(conf)})
Пример #38
0
def configParse(file_path):

    ### File check
    if not os.path.exists(file_path):
        raise IOError(file_path)

    parser = SafeConfigParser()
    parser.read(file_path)

    ### Convert to dictionary
    config = {}
    for sect in parser.sections():
        config[sect] = {}
        for opt in parser.options(sect):
            config[sect][opt] = parser.get(sect, opt)

    ### Data check and convert from type
    for sect in config_settings.keys():

        #        if not sect in config :
        #            raise KeyError(sect)

        for opt_attr in config_settings[sect]:
            if opt_attr['required'] and (not opt_attr['name'] in config[sect]):
                raise KeyError(opt_attr['name'])

            if config[sect][opt_attr['name']] == 'None':
                config[sect][opt_attr['name']] = None
            else:
                config[sect][opt_attr['name']] = opt_attr['type'](
                    config[sect][opt_attr['name']])

    return config
Пример #39
0
def configure_parameters():
    global portID
    global logginglevelName
    global loggingLevel
    global logFile
    global MQTT_brokerHost
    global MQTT_QOS
    global MQTT_retention
    global MQTT_mapString
    global MQTT_mapping
    global MQTT_topic

    config = SafeConfigParser()
    config.readfp(open('w800.conf'))

    try:
        sections = config.sections()
        print "sections:", sections
        for section in sections:
            options = config.options(section)
            print "section, options:", section, ":", options
            for option in options:
                option_value = config.get(section, option)
                print "section:options:  ", section + ": " + option + ": " + option_value
                if section == 'ports':
                    print "  ports: option", option, config.get(
                        section, option)
                    if option == 'portid':
                        portID = config.get(section, option)
                    else:
                        print "Unexpected option in ports: " + option

                elif section == 'Logging':
                    print "  logging: option", option, config.get(
                        section, option)
                    if option == 'logginglevel':
                        loggingLevelName = config.get(section, option)
                        print "logginglevelName", loggingLevelName
                        loggingLevel = loggingLevels[loggingLevelName]
                    elif option == 'logFile':
                        logFile = config.get(section, option)
                    else:
                        print "Unexpected option in logging: " + option

                elif section == 'MQTT':
                    print "  mqtt: option", option, config.get(section, option)
                    if option == 'mqtt_brokerhost':
                        MQTT_brokerHost = config.get(section, option)
                    elif option == 'mqtt_qos':
                        MQTT_QOS = config.getint(section, option)
                    elif option == 'mqtt_topic':
                        MQTT_topic = config.get(section, option)
                    elif option == 'mqtt_mapping':
                        MQTT_mapString = config.get(section, option)
                    else:
                        print "Unexpected option in mqtt: " + option
                else:
                    print "Unexpected section: " + section
    except:
        print "The value for option '" + option + "' in section '" + section + "' isn't right"
Пример #40
0
def configme(pkg_name):
    '''Given a pkg name, can I get the two possible locations to 
       gather config
    '''
    confd = {}
    well_known_dir = os.path.join('/usr/local/etc/', pkg_name)
    well_known_location = os.path.join(well_known_dir, 'conf.ini')
    confp = SafeConfigParser()
    try:
        confp.read(well_known_location)
    except:
        #: no config at all ????
        #: need a default
        return confd
    
    for section in confp.sections():
        #[IAmASection]
        print section
        if section not in confd:
            confd[section] = {}
        options = confp.options(section)
        for option in options:
            try:
                print option
                print confp.get(section, option)
                confd[section][option] = confp.get(section, option)
            except Exception, e:
                
                logging.error("Failed to add config to confd %s %s %s", e, section, option)
Пример #41
0
	def getIncludes(resource, seen = []):
		"""
		Given 1 config resource returns list of included files
		(recursively) with the original one as well
		Simple loops are taken care about
		"""
		
		
		SCPWI = SafeConfigParserWithIncludes
		
		parser = SafeConfigParser()
		parser.read(resource)
		
		resourceDir = os.path.dirname(resource)
		newFiles = [ ('before', []), ('after', []) ]
		if SCPWI.SECTION_NAME in parser.sections():
			for option_name, option_list in newFiles:
				if option_name in parser.options(SCPWI.SECTION_NAME):
					newResources = parser.get(SCPWI.SECTION_NAME, option_name)
					for newResource in newResources.split('\n'):
						if os.path.isabs(newResource):
							r = newResource
						else:
							r = "%s/%s" % (resourceDir, newResource)
						if r in seen:
							continue
						s = seen + [resource]
						option_list += SCPWI.getIncludes(r, s)
		
		return newFiles[0][1] + [resource] + newFiles[1][1]
Пример #42
0
def getSensorName(sensor):
    parser = SafeConfigParser()
    parser.read(SETTINGS_FILE)
    for t_sensor in parser.options('temp sensors'):
        if sensor == int(t_sensor):
            return parser.get('temp sensors',t_sensor)
    return 'Unknown'
Пример #43
0
def load_config(config_file):
    config = SafeConfigParser()
    config.read(config_file)
    sections = ['Server', 'Application', 'MongoDB']
    the_config = {}
    for section in sections:
        options = config.options(section)
        for option in options:
            try:
                the_config[option] = config.get(section, option)
                try_next = True
                if try_next:
                    try:
                        the_config[option] = str2bool(the_config[option])
                        try_next = False
                    except:
                        try_next = True
                if try_next:
                    try:
                        the_config[option] = float(the_config[option])
                        try_next = False
                    except:
                        try_next = True
            except:
                print "Config exception on %s!" % option
                the_config[option] = None

    return the_config
Пример #44
0
    def from_filename(filename, allow_empty=False):
        from ConfigParser import SafeConfigParser, NoOptionError
        parser = SafeConfigParser()
        parser.read(filename)

        obj = OMPEnv()

        # Consistency check. Note that we only check if the option name is correct, 
        # we do not check whether the value is correct or not.
        if "openmp" not in parser.sections():
            if not allow_empty:
                raise ValueError("%s does not contain any [openmp] section" % filename) 
            return obj

        err_msg = ""
        for key in parser.options("openmp"):
            if key.upper() not in OMPEnv._keys:
                err_msg += "unknown option %s, maybe a typo" % key

        if err_msg: 
            raise ValueError(err_msg)

        for key in OMPEnv._keys:
            try:
                obj[key] = str(parser.get("openmp", key))
            except NoOptionError:
                try:
                    obj[key] = str(parser.get("openmp", key.lower()))
                except NoOptionError:
                    pass

        if not allow_empty and not obj:
            raise ValueError("Refusing to return with an empty dict") 

        return obj
Пример #45
0
def _read_config_files(paths):
    """
    Read configuration data from INI-style file(s).

    Data loaded from the given files is aggregated into a nested 2-level Python
    mapping, where 1st-level keys are config section names (as read from the
    files), and corresponding items are again key/value mappings (configuration
    item name and value).

    :param paths: list of filesystem paths of files to read
    """
    # read given config files
    configparser = SafeConfigParser()
    configparser.read(paths)

    # temporarily modify environment to allow both `${...}` and `%(...)s`
    # variable substitution in config values
    defaults = _make_defaults_dict()
    config = {}
    with environment(**defaults):
        for section in configparser.sections():
            config[section] = {}
            for key in configparser.options(section):
                # `configparser.get()` performs the `%(...)s` substitutions
                value = configparser.get(section, key, vars=defaults)
                # `expandvars()` performs the `${...}` substitutions
                config[section][key] = expandvars(value)

    return config
Пример #46
0
class ConfigManager(object):
    class Section:
        def __init__(self, name, parser):
            self.__dict__['name'] = name
            self.__dict__['parser'] = parser
            #reason for using __dict__ is that __setattr__ is called to initiate all local class attrs and so
            #declaring the below would cause __setattr__ to be called and obviously neither name nor parser exist yet
            #self.name = name
            #self.parser = parser

        def __setattr__(self, option, value):
            self.__dict__[option] = str(value)
            self.parser.set(self.name, option, str(value))

    def __init__(self, file_name):
        self.parser = SafeConfigParser()
        self.parser.read(file_name)
        self.file_name = file_name
        for section in self.parser.sections():
            setattr(self, section, self.Section(section, self.parser))
            for option in self.parser.options(section):
                setattr(getattr(self, section), option,
                        self.parser.get(section, option))

    def __getattr__(self, option):
        self.parser.add_section(option)
        setattr(self, option, Section(option, self.parser))
        return getattr(self, option)

    def save_config(self):
        f = open(self.file_name, 'w')
        self.parser.write(f)
        f.close()
Пример #47
0
def _read_config(suite):
    """
    Read the config ini file for the test suite `suite`
    (identified as a group in the config)

    Returns a dictionary of configuration values that will
    be passed as environment variables to the test suite.
    """
    if not os.path.isfile(CONFIG_PATH):
        msg = """
            Could not find config file at '{0}'.
            Please set the CONFIG_PATH environment variable
        """.format(CONFIG_PATH)
        _abort(msg)

    config = SafeConfigParser()
    config.read(CONFIG_PATH)

    result = {
        key: config.get(suite, key) for key in config.options(suite)
    }

    # Validate the required keys
    for key in ['protocol', 'test_host']:
        if key not in result:
            _abort("Missing '{0}' in config file.".format(key))

    return result
Пример #48
0
def _read_config_files(paths):
    """
    Read configuration data from INI-style file(s).

    Data loaded from the given files is aggregated into a nested 2-level Python
    mapping, where 1st-level keys are config section names (as read from the
    files), and corresponding items are again key/value mappings (configuration
    item name and value).

    :param paths: list of filesystem paths of files to read
    """
    # read given config files
    configparser = SafeConfigParser()
    configparser.read(paths)

    # temporarily modify environment to allow both `${...}` and `%(...)s`
    # variable substitution in config values
    defaults = _make_defaults_dict()
    config = {}
    with environment(**defaults):
        for section in configparser.sections():
            config[section] = {}
            for key in configparser.options(section):
                # `configparser.get()` performs the `%(...)s` substitutions
                value = configparser.get(section, key, vars=defaults)
                # `expandvars()` performs the `${...}` substitutions
                config[section][key] = expandvars(value)

    return config
Пример #49
0
def get_section(configfile_name, section_name):
    parser = SafeConfigParser()
    dictionary = {}

    for folder in FOLDERS:
        config_file_path = folder + "/" + configfile_name
        log.debug(config_file_path)

        if os.path.exists(config_file_path):
            parser.read(config_file_path)

            try:
                for key in parser.options(section_name):
                    value = parser.get(section_name, key)

                    if "," in value:
                        value = value.split(",")

                    dictionary[key] = value

            except NoSectionError:
                continue

        log.debug(dictionary)
        return dictionary
Пример #50
0
def read_conf(file):
    """parse the config file and return a list of dict, return empty dict with wrong config"""
    res=[]
    need_options=set(['cut_interval', 'expect_time', 'accept_time', 'servernames', 'pattern'])
    number_options=('cut_interval','expect_time','accept_time')
    parser=SafeConfigParser()
    parser.read(file)
    for fname in parser.sections():
        options=set(parser.options(fname))
        if need_options <= options:
            parser_dict=dict(parser.items(fname))
            d={'logfile':fname}
            #d.update(dict((k,float(v) if '.' in v else int(v) ) for k,v in  parser.items(fname)))
            #d.update({k:float(v) if '.' in v else int(v)  for k,v in  parser.items(fname)})
            d.update({}.fromkeys(options))
            for op in number_options:
                d[op]=float(parser_dict[op]) if '.' in parser_dict[op] else int(parser_dict[op])
            d['servernames']=tuple(s.strip() for s in parser_dict['servernames'].split(','))
            d['pattern']= parser.get(fname, 'pattern')
            res.append(d)
        else :
            print 'Wrong config with {0}'.format(fname)
            continue
    #if res and 'zabbix_server' in parser.defaults():
    #    res.append(parser.defaults()['zabbix_server'])
    return res
Пример #51
0
 def config(self,file):
   config=SafeConfigParser()
   config.read(file)
   for section in config.sections():
     for option in config.options(section):
       value=config.get(section,option)
       setattr(self,option,value)
Пример #52
0
	def getIncludes(resource, seen = []):
		"""
		Given 1 config resource returns list of included files
		(recursively) with the original one as well
		Simple loops are taken care about
		"""
		
		# Use a short class name ;)
		SCPWI = SafeConfigParserWithIncludes
		
		parser = SafeConfigParser()
		parser.read(resource)
		
		resourceDir = os.path.dirname(resource)

		newFiles = [ ('before', []), ('after', []) ]
		if SCPWI.SECTION_NAME in parser.sections():
			for option_name, option_list in newFiles:
				if option_name in parser.options(SCPWI.SECTION_NAME):
					newResources = parser.get(SCPWI.SECTION_NAME, option_name)
					for newResource in newResources.split('\n'):
						if os.path.isabs(newResource):
							r = newResource
						else:
							r = os.path.join(resourceDir, newResource)
						if r in seen:
							continue
						s = seen + [resource]
						option_list += SCPWI.getIncludes(r, s)
		# combine lists
		return newFiles[0][1] + [resource] + newFiles[1][1]
Пример #53
0
    def from_file(cls, fname, allow_empty=False):
        """Initialize the object from file."""
        parser = SafeConfigParser()
        parser.read(fname)

        inst = OMPEnvironment()

        # Consistency check. Note that we only check if the option name is correct,
        # we do not check whether the value is correct or not.
        if "openmp" not in parser.sections():
            if not allow_empty:
                raise ValueError("%s does not contain any [openmp] section" %
                                 fname)
            return inst

        err_msg = ""
        for key in parser.options("openmp"):
            if key.upper() not in OMPEnvironment._keys:
                err_msg += "unknown option %s, maybe a typo" % key
        if err_msg:
            raise ValueError(err_msg)

        for key in OMPEnvironment._keys:
            try:
                inst[key] = str(parser.get("openmp", key))
            except NoOptionError:
                try:
                    inst[key] = str(parser.get("openmp", key.lower()))
                except NoOptionError:
                    pass

        if not allow_empty and not inst:
            raise ValueError("Refusing to return with an empty dict")

        return inst
Пример #54
0
def set_env_vars(config_file):
    parser = SafeConfigParser()
    parser.read(config_file)

    env_base = "CONFIG_PARSER"

    final_env = {}

    for s in parser.sections():
        sec = s.upper()
        for o in parser.options(s):
            final_env["___".join([env_base, sec, o.upper()])] = parser.get(s, o)

    # final_string = " ".join(["%(env)s=%(val)s" % {"env":k,"val":final_env[k]} for k in final_env.keys()])

    # print final_string

    final_strings = ["%(env)s=%(val)s" % {"env": k, "val": final_env[k]} for k in final_env.keys()]

    for k in final_env.keys():
        print k
        if len(sys.argv) > 2:
            del os.environ[k]
        else:
            os.environ[k] = str(final_env[k])
Пример #55
0
class Config:
    """
        Initialize the config class and parse the config file.
    """
    def __init__(self, path=os.path.normpath(os.getcwd()), filename="config.ini"):
        # Default config file: <current directory of script>/config.ini
        self.file = "%s/%s" % (path, filename)
        
        # Initialize the ini parser class
        self.parser = SafeConfigParser()
        
        # Read and parse the ini file
        self.parser.read(self.file)
        
        # We initially have an empty config to deal with
        self.config = {}
        
        # So we don't initialize this every...damn...time.
        val = ""
        
        # Loop through each section in the ini file
        for section in self.parser.sections():
            # Create an empty dictionary entry for each entry (otherwise: exceptions)
            self.config.update({section : dict()})
            
            # Loop through each option in the section
            for option in self.parser.options(section):
                # Get the value of the option (just done for clarity)
                val = self.parser.get(section, option)
                
                # Store the new 'option' : 'option val' dictionary entry into the section dictionary
                self.config[section].update({ option : val })
    
    """
        Retrieve an item from the configuration.
    """
    def __getitem__(self, section, option=None):
        # First we MUST have a section name given, and only continue if it's valid
        if self.config.has_key(section):
            # If an option key was given and it exists, return the value
            if option != None and self.config[section].has_key(option):
                return self.config[section][option]
            else if option == None:
                # No option was given, user wants just the section data
                return self.config[section]
            else:
                # No option was found, even tho it was requested, return empty string
                return ""
        else:
            return {'' : ''}
    
    """
        Simply returns a dump of the config file.  Primarily for debugging purposes.
    """
    def dump(self):
        print self.config
    
    def file(self):
        return self.file