def get_config_values(config_path, defaults):
    """Get the script's configuration values and return them in a dict

    If a config file exists, merge its values with the defaults. If no config
    file exists, just return the defaults.
    """

    try:
        # For Python 2
        from ConfigParser import SafeConfigParser as ConfigParser
    except ImportError:
        # For Python 3.0 and later
        from configparser import ConfigParser

    config = ConfigParser(defaults)
    config_values = config.defaults()

    config_paths = [
        config_path,
        os.path.join(os.path.expanduser('~'), '.trailers.cfg'),
    ]

    config_file_found = False
    for path in config_paths:
        if os.path.exists(path):
            config_file_found = True
            config.read(path)
            config_values = config.defaults()
            break

    if not config_file_found:
        logging.info('Config file not found. Using default values.')

    return config_values
Пример #2
0
def getConfigValues(configPath, defaults):
    """Get the script's configuration values and return them in a dict
    
    If a config file exists, merge its values with the defaults. If no config
    file exists, just return the defaults.
    """
    from ConfigParser import SafeConfigParser

    config = SafeConfigParser(defaults)
    configValues = config.defaults()

    configPaths = [
        configPath,
        os.path.join(os.path.expanduser('~'), '.trailers.cfg'),
    ]

    configFileFound = False
    for path in configPaths:
        if os.path.exists(path):
            configFileFound = True
            config.read(path)
            configValues = config.defaults()
            break

    if not configFileFound:
        print 'Config file not found.  Using default values.'

    return configValues
def usage6():
    # Option Search Path
    # If the option name appears in the vars dictionary passed to get(), the value from vars is returned.
    # If the option name appears in the specified section, the value from that section is returned.
    # If the option name appears in the DEFAULT section, that value is returned.
    # If the option name appears in the defaults dictionary passed to the constructor, that value is returned.

    # Define the names of the options
    option_names = [
        'from-default',
        'from-section', 'section-only',
        'file-only', 'init-only', 'init-and-file',
        'from-vars',
        ]

    # Initialize the parser with some defaults
    parser = SafeConfigParser(
        defaults={'from-default':'value from defaults passed to init',
                  'init-only':'value from defaults passed to init',
                  'init-and-file':'value from defaults passed to init',
                  'from-section':'value from defaults passed to init',
                  'from-vars':'value from defaults passed to init',
                  })

    print 'Defaults before loading file:'
    defaults = parser.defaults()
    for name in option_names:
        if name in defaults:
            print '  %-15s = %r' % (name, defaults[name])

    # Load the configuration file
    parser.read('searchpath.config')

    print '\nDefaults after loading file:'
    defaults = parser.defaults()
    for name in option_names:
        if name in defaults:
            print '  %-15s = %r' % (name, defaults[name])

    # Define some local overrides
    vars = {'from-vars':'value from vars'}

    # Show the values of all of the options
    print '\nOption lookup:'
    for name in option_names:
        value = parser.get('sect', name, vars=vars)
        print '  %-15s = %r' % (name, value)

    # Show error messages for options that do not exist
    print '\nError cases:'
    try:
        print 'No such option :', parser.get('sect', 'no-option')
    except Exception as err:
        print str(err)

    try:
        print 'No such section:', parser.get('no-sect', 'no-option')
    except Exception as err:
        print str(err)
Пример #4
0
def alert(subject, message):
	configp = SafeConfigParser()
	configp.read(config_paths)

	smtp_addr = configp.defaults()['smtp_server']
	smtp_port = configp.defaults()['smtp_port']
	alert_email = configp.defaults()['alert_email']
	alert_password = configp.defaults()['alert_password']

	mailer.mail(smtp_addr, smtp_port, alert_email, alert_password, 
				'*****@*****.**', subject, message)
Пример #5
0
    def load_converters(self, path):
        """Loads converters from conv files in path and and populates
        ``self.converters`` and ``self.converter_map`` structures.

        :param path: a glob-bable path like ``/foo/bar/baz/*.conv``
        """
        platform = app.config.get(prefs.APP_PLATFORM)
        groups = glob(path)
        groups.sort()
        for group_definition in groups:
            parser = SafeConfigParser()
            definition_file = open(group_definition)
            try:
                parser.readfp(definition_file)
                defaults = parser.defaults()
                sections = parser.sections()
                group_converters = list()
                for section in sections:
                    converter_info = ConverterInfo(section, parser)
                    if ((converter_info.platforms is None
                         or platform in converter_info.platforms)):
                        ident = converter_info.identifier
                        self.converter_map[ident] = converter_info
                        group_converters.append(converter_info)
                group_converters.sort(key=lambda x: x.name)
                self.converters.append((defaults['name'], group_converters))
            finally:
                definition_file.close()
Пример #6
0
    def load_converters(self, path):
        """Loads converters from conv files in path and and populates
        ``self.converters`` and ``self.converter_map`` structures.

        :param path: a glob-bable path like ``/foo/bar/baz/*.conv``
        """
        platform = app.config.get(prefs.APP_PLATFORM)
        groups = glob(path)
        groups.sort()
        for group_definition in groups:
            parser = SafeConfigParser()
            definition_file = open(group_definition)
            try:
                parser.readfp(definition_file)
                defaults = parser.defaults()
                sections = parser.sections()
                group_converters = list()
                for section in sections:
                    converter_info = ConverterInfo(section, parser)
                    if ((converter_info.platforms is None
                         or platform in converter_info.platforms)):
                        ident = converter_info.identifier
                        self.converter_map[ident] = converter_info
                        group_converters.append(converter_info)
                group_converters.sort(key=lambda x: x.name)
                self.converters.append((defaults['name'], group_converters))
            finally:
                definition_file.close()
        messages.ConverterList(self.converters).send_to_frontend()
Пример #7
0
    def _parse_config_files(self):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser(self.defaults)
        parser.read(self.config_files)
        self.config = dict(parser.defaults())

        if parser.has_section(self.command):
            self.config.update(dict(parser.items(self.command, raw=True)))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser.items(section, raw=True)))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist."
                                     % section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [ self.config['filter'] ]
        else:
            self.config['filter'] = []
Пример #8
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {}
        for option in [
                "timeout", "host", "port", "nick", "channel", "info",
                "heartbeat", "password", "username", "realname", "ssl"
        ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")

        factory = factory(options)
        optionAsBoolean = {
            "": False,
            "False": False,
            "false": False,
            "no": False,
            "True": True,
            "true": True,
            "yes": True
        }
        sentinel = object()
        ssl = options.get(option['ssl'], sentinel)
        if sentinel == ssl:
            raise TypeError("Cannot convert '{}' to boolean.".format(ssl))
        elif ssl:
            reactor.connectSSL(options['host'], int(options['port']), factory,
                               ClientContextFactory(), int(options['timeout']))
        else:
            reactor.connectTCP(options['host'], int(options['port']), factory,
                               int(options['timeout']))

    reactor.callWhenRunning(signal, SIGINT, handler)
def getConfigValues():
    """Get the script's configuration values and return them in a dict
    
    If a config file exists, merge its values with the defaults. If no config
    file exists, just return the defaults. Validates the settings in the
    config file and raises a ValueError exception if any of the given settings
    are invalid.
    
    """
    from ConfigParser import SafeConfigParser

    scriptDir = os.path.abspath(os.path.dirname(__file__))
    configPath = "%s/settings.cfg" % scriptDir
 
    config = SafeConfigParser(
        defaults = {
            'resolution': '720',
            'download_dir': scriptDir
        }
    )
    configValues = config.defaults()

    if (not os.path.exists(configPath)):
        print 'No config file found.  Using default values.'
        print "    Resolution: %sp" % configValues['resolution']
        print "    Download Directory: %s" % configValues['download_dir']
    else:
        config.read(configPath)

        configValues = config.defaults()
        validResolutions = ['480', '720', '1080']

        # Validate the config options
        if configValues['resolution'] not in validResolutions:
            resString = ', '.join(validResolutions)
            raise ValueError("Invalid resolution. Valid values: %s" % resString)

        if (len(configValues['download_dir']) < 1) or (not os.path.exists(configValues['download_dir'])):
            raise ValueError('The download directory must be a valid path')


    if (configValues['download_dir'][-1] != '/'):
        configValues['download_dir'] = "%s/" % configValues['download_dir']

    return configValues
Пример #10
0
def read_config():
    global USER_WORK_DIR, USER_SAVE_DIR

    parser = SafeConfigParser(defaults=defaults)
    parser.read(USER_CFG_PATH)

    settings = parser.defaults()
    USER_WORK_DIR = settings["user_work_dir"]
    USER_SAVE_DIR = settings["user_save_dir"]
Пример #11
0
    def parse_config_files(self, git_treeish=None):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser()
        # Fill in the built in values
        self.config = dict(self.__class__.defaults)
        # Update with the values from the defaults section. This is needed
        # in case the config file doesn't have a [<command>] section at all
        config_files = self.get_config_files()
        try:
            repo = GitRepository(".")
        except GitRepositoryError:
            repo = None
        # Read all config files
        for filename in config_files:
            self._read_config_file(parser, repo, filename, git_treeish)
        self.config.update(dict(parser.defaults()))

        # Make sure we read any legacy sections prior to the real subcommands
        # section i.e. read [gbp-pull] prior to [pull]
        if (self.command.startswith('gbp-')
                or self.command.startswith('git-')):
            oldcmd = self.command
            if parser.has_section(oldcmd):
                self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command[4:]
        else:
            for prefix in ['gbp', 'git']:
                oldcmd = '%s-%s' % (prefix, self.command)
                if parser.has_section(oldcmd):
                    self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command

        # Update with command specific settings
        if parser.has_section(cmd):
            # Don't use items() until we got rid of the compat sections
            # since this pulls in the defaults again
            self.config.update(dict(parser._sections[cmd].items()))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser._sections[section].items()))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist." %
                                     section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [self.config['filter']]
        else:
            self.config['filter'] = []
Пример #12
0
  def __init__(self):
    '''
    The constructor of this plugin.
    '''
    if Plugin.DESCRIPTION is None:
      raise ValueError("Plugin.DESCRIPTION must be set in your plugin code")
    super( Plugin, self ).__init__(description=Plugin.DESCRIPTION)
    self.add_argument( '--xml', action='store_true', dest='xml', default=False, help='show xml description of parameters (default: FALSE)' )
    self.add_argument( '--configuration', action='store', dest='configuration', default="", help='custom userconfiguration in JSON format (default: "")' )
    self.add_argument( '--icon', action='store_true', dest='icon', default=False, help='show the description of this plugin (default: FALSE)' )
    self.add_argument( '--description', action='store_true', dest='description', default=False, help='show the icon path of this plugin (default: FALSE)' )
    self.add_argument( '--output', action='store', dest='output', help='the output directory' )

    #chris-related environment variables
    if 'ENV_CHRISRUN_DIR' in os.environ:
      self.chrisRunDir = os.environ['ENV_CHRISRUN_DIR']
    else:
      self.chrisRunDir = ''
    if 'ENV_CLUSTERTYPE' in os.environ:
      self.clusterType = os.environ['ENV_CLUSTERTYPE']
    else:
      self.clusterType = ''  
    if 'ENV_REMOTEHOST' in os.environ:
      self.remoteHost = os.environ['ENV_REMOTEHOST']
    else:
      self.remoteHost = ''
    if 'ENV_REMOTEUSER' in os.environ:
      self.remoteUser = os.environ['ENV_REMOTEUSER']
    else:
      self.remoteUser = '' 
    if 'ENV_REMOTEUSERIDENTITY' in os.environ:
      self.remoteUserIdentity = os.environ['ENV_REMOTEUSERIDENTITY']
    else:
      self.remoteUserIdentity = '' 
    
    #read plugin configuration file containing the requiered plugin environment variables
    config = SafeConfigParser()
    config.optionxform = str
    current_dir = os.path.dirname(os.path.abspath(__file__))
    config.read(os.path.join(current_dir, 'plugin.conf'))
    self.envVars = config.defaults()
    
    # the custom parameter list
    self.__panels = []
    self.__parameters = []
    self.options = []

    # the initial status
    self.status = 0
    
    # the initial memory
    self.memory = 512

    # is it an inteactive plugin
    self.interactive = False
Пример #13
0
	def get_configs(self):
		"""Reads all the deployment configs from deploy.conf into memory."""
		cfgfile = SafeConfigParser()
		cfgfile.read('deploy.conf')
		defaults = cfgfile.defaults()
		self.configs = {}
		for section in cfgfile.sections():
			secvals = defaults.copy()
			secvals.update(cfgfile.items(section))
			self.configs[section] = secvals
		return self.configs
Пример #14
0
def main(log_to_stdout=True):
    # At least warning because the database initialization logs progress at
    # warning. Standard output to provide feedback on the terminal.
    if log_to_stdout:
        logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s",
                            level=logging.INFO, stream=sys.stdout)

    parser = SafeConfigParser()
    parser.read("database.config")
    config = parser.defaults()

    return db.Database(config)
Пример #15
0
 def read_conf(self):
     config = SafeConfigParser()
     config.read(self.conf_file)
     for k, v in config.defaults().items():
         if k == 'local_path':
             self.local_path = v
         elif k == 'remote_ip':
             self.remote_ip = v
         elif k == 'remote_path':
             self.remote_path = v
         elif 'except_path' in k:
             self.except_paths.add(v)
Пример #16
0
 def __parse_config_files(self):
     """parse the possible config files and set appropriate values default values"""
     parser = SafeConfigParser(self.defaults)
     parser.read(self.config_files)
     self.config = dict(parser.defaults())
     if parser.has_section(self.command):
         self.config.update(dict(parser.items(self.command, raw=True)))
     # filter can be either a list or a string, always build a list:
     if self.config['filter']:
         if self.config['filter'].startswith('['):
             self.config['filter'] = eval(self.config['filter'])
         else:
             self.config['filter'] = [ self.config['filter'] ]
Пример #17
0
def parse_config():
    with open(CONFIG_FILE) as cfg_handle:
        parser = SafeConfigParser()
        parser.readfp(cfg_handle)
        defaults = parser.defaults()
        d = {}
        for key, val in defaults.items():
            vals = val.split(',')
            if len(vals) > 1:
                d[key] = [x.strip() for x in vals if len(x.strip()) > 0]
            else:
                d[key] = val
        return d
Пример #18
0
def main(log_to_stdout=True):
    # At least warning because the database initialization logs progress at
    # warning. Standard output to provide feedback on the terminal.
    if log_to_stdout:
        logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s",
                            level=logging.INFO,
                            stream=sys.stdout)

    parser = SafeConfigParser()
    parser.read("database.config")
    config = parser.defaults()

    return db.Database(config)
Пример #19
0
def getversioncfg():
    import os
    from ConfigParser import SafeConfigParser
    cp = SafeConfigParser()
    cp.read(versioncfgfile)
    if not os.path.isdir('.git'):  return cp
    d = cp.defaults()
    g = gitinfo()
    if g['commit'] != d.get('commit'):
        cp.set('DEFAULT', 'version', g['version'])
        cp.set('DEFAULT', 'commit', g['commit'])
        cp.set('DEFAULT', 'date', g['date'])
        cp.write(open(versioncfgfile, 'w'))
    return cp
Пример #20
0
def read_cfg(cfg_file, include_default=True, only_default=False):
    """
    Reads a WARP configuration file.

    Parameters
    ----------
    cfg_file : str
        File name of the configuration file.
    include_default : bool, optional
        Include default items. Default: True
    only_default : bool, optional
        Return only default items. Default: False

    Returns
    -------
    ds : dict
        Dictionary generated from the items and values in the config file.
    """
    if not os.path.isfile(cfg_file):
        raise IOError("Configuration file does not exist {:}".format(cfg_file))

    config = SafeConfigParser()
    config.read(cfg_file)

    cfg = {}

    if only_default:
        for item, value in config.defaults().iteritems():
            cfg[item] = eval_param(item, value)
    else:
        for section in config.sections():
            cfg[section] = {}
            for item, value in config.items(section):
                if include_default or item not in config.defaults().keys():
                    cfg[section][item] = value

    return cfg
Пример #21
0
def read_cfg(cfg_file, include_default=True, only_default=False):
    """
    Reads a WARP configuration file.

    Parameters
    ----------
    cfg_file : str
        File name of the configuration file.
    include_default : bool, optional
        Include default items. Default: True
    only_default : bool, optional
        Return only default items. Default: False

    Returns
    -------
    ds : dict
        Dictionary generated from the items and values in the config file.
    """
    if not os.path.isfile(cfg_file):
        raise IOError("Configuration file does not exist {:}".format(cfg_file))

    config = SafeConfigParser()
    config.read(cfg_file)

    cfg = {}

    if only_default:
        for item, value in config.defaults().iteritems():
            cfg[item] = eval_param(item, value)
    else:
        for section in config.sections():
            cfg[section] = {}
            for item, value in config.items(section):
                if include_default or item not in config.defaults().keys():
                    cfg[section][item] = value

    return cfg
Пример #22
0
    def parse_config_files(self):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser()
        # Fill in the built in values
        self.config = dict(self.__class__.defaults)
        # Update with the values from the defaults section. This is needed
        # in case the config file doesn't have a [<command>] section at all
        parser.read(self.config_files)
        self.config.update(dict(parser.defaults()))

        # Make sure we read any legacy sections prior to the real subcommands
        # section i.e. read [gbp-pull] prior to [pull]
        if (self.command.startswith('gbp-') or
            self.command.startswith('git-')):
            oldcmd = self.command
            if parser.has_section(oldcmd):
                self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command[4:]
        else:
            for prefix in ['gbp', 'git']:
                oldcmd = '%s-%s' % (prefix, self.command)
                if parser.has_section(oldcmd):
                    self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command

        # Update with command specific settings
        if parser.has_section(cmd):
            # Don't use items() until we got rid of the compat sections
            # since this pulls in the defaults again
            self.config.update(dict(parser._sections[cmd].items()))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser._sections[section].items()))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist."
                                     % section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [ self.config['filter'] ]
        else:
            self.config['filter'] = []
Пример #23
0
def getversioncfg(config=versioncfgfile):
    from ConfigParser import SafeConfigParser
    cp = SafeConfigParser()
    cp.read(config)
    gitdir = os.path.join(MYDIR, '.git')
    if not os.path.exists(gitdir):  return cp
    d = cp.defaults()
    g = gitinfo()
    if g['version'] != d.get('version') or g['commit'] != d.get('commit'):
        cp.set('DEFAULT', 'version', g['version'])
        cp.set('DEFAULT', 'commit', g['commit'])
        cp.set('DEFAULT', 'date', g['date'])
        cp.set('DEFAULT', 'timestamp', g['timestamp'])
        cp.write(open(config, 'w'))
    return cp
Пример #24
0
def getversioncfg(config=versioncfgfile):
    from ConfigParser import SafeConfigParser
    cp = SafeConfigParser()
    cp.read(config)
    gitdir = os.path.join(MYDIR, '.git')
    if not os.path.exists(gitdir): return cp
    d = cp.defaults()
    g = gitinfo()
    if g['version'] != d.get('version') or g['commit'] != d.get('commit'):
        cp.set('DEFAULT', 'version', g['version'])
        cp.set('DEFAULT', 'commit', g['commit'])
        cp.set('DEFAULT', 'date', g['date'])
        cp.set('DEFAULT', 'timestamp', g['timestamp'])
        cp.write(open(config, 'w'))
    return cp
Пример #25
0
    def read_ini(cls, path, section=None):
        """read preferences from an .ini file"""

        parser = ConfigParser()
        parser.read(path)

        if section:
            if section not in parser.sections():
                raise PreferencesReadError("No section '%s' in %s" % (section, path))
            retval = parser.items(section, raw=True)
        else:
            retval = parser.defaults().items()

        # cast the preferences since .ini is just strings
        return [(i, cls.cast(j)) for i, j in retval]
Пример #26
0
    def read_ini(cls, path, section=None):
        """read preferences from an .ini file"""

        parser = ConfigParser()
        parser.readfp(mozfile.load(path))

        if section:
            if section not in parser.sections():
                raise PreferencesReadError("No section '%s' in %s" % (section, path))
            retval = parser.items(section, raw=True)
        else:
            retval = parser.defaults().items()

        # cast the preferences since .ini is just strings
        return [(i, cls.cast(j)) for i, j in retval]
Пример #27
0
    def __init__(self, filename = None, section = None, **kwds):
        if not filename:
            filename = os.path.join(os.path.dirname(__file__),'wt.cfg')

        scp = SafeConfigParser()
        res = scp.read(filename)

        cfg = dict(scp.defaults())
        cfg.update(kwds)

        if section:
            cfg.update(scp.items(section))

        self.__dict__.update(cfg)
        return
Пример #28
0
def persistent_search_paths():
    """ Return a list of persistent registered (prefix, path) pairs
    """

    global_settings_dir = Orange.misc.environ.install_dir
    user_settings_dir = Orange.misc.environ.orange_settings_dir
    parser = SafeConfigParser()
    parser.read([os.path.join(global_settings_dir, "orange-search-paths.cfg"),
                 os.path.join(user_settings_dir, "orange-search-paths.cfg")])
    try:
        items = parser.items("paths")
        defaults = parser.defaults().items()
        items = [i for i in items if i not in defaults]
    except ConfigParser.NoSectionError:
        items = []
    return items
Пример #29
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {}
        for option in [ "timeout", "host", "port", "nick", "channel", "info", "heartbeat", "password", "username", "realname", "ssl" ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")

        factory = factory(options)
        optionAsBoolean = { "": False, "False": False, "false": False, "no": False, "True": True, "true": True, "yes": True }
        sentinel = object()
        ssl = options.get(option['ssl'], sentinel)
        if sentinel == ssl:
            raise TypeError("Cannot convert '{}' to boolean.".format(ssl))
        elif ssl:
            reactor.connectSSL(options['host'], int(options['port']), factory, ClientContextFactory(), int(options['timeout']))
        else:
            reactor.connectTCP(options['host'], int(options['port']), factory, int(options['timeout']))

    reactor.callWhenRunning(signal, SIGINT, handler)
Пример #30
0
def persistent_search_paths():
    """ Return a list of persistent registered (prefix, path) pairs
    """

    global_settings_dir = Orange.misc.environ.install_dir
    user_settings_dir = Orange.misc.environ.orange_settings_dir
    parser = SafeConfigParser()
    parser.read([
        os.path.join(global_settings_dir, "orange-search-paths.cfg"),
        os.path.join(user_settings_dir, "orange-search-paths.cfg")
    ])
    try:
        items = parser.items("paths")
        defaults = parser.defaults().items()
        items = [i for i in items if i not in defaults]
    except ConfigParser.NoSectionError:
        items = []
    return items
Пример #31
0
 def _read_credentials(cls):
     here = os.path.dirname(os.path.abspath(__file__))
     credentials_path = os.path.join(here, 'credentials.txt')
     try:
         from ConfigParser import SafeConfigParser
     except ImportError:
         # Python3
         from configparser import SafeConfigParser
     parser = SafeConfigParser()
     parser.read(credentials_path)
     parser.defaults()['driver'] = 'Firefox'
     cls.ac_driver = parser.get('ac_server', 'driver')
     cls.base_url = parser.get('ac_server', 'base_url')
     cls.login = parser.get('ac_server', 'login')
     cls.password = parser.get('ac_server', 'password')
     cls.firefox_path = None
     if parser.has_option('ac_server', 'firefox_path'):
         cls.firefox_path = parser.get('ac_server', 'firefox_path')
Пример #32
0
def getversioncfg():
    from ConfigParser import SafeConfigParser
    cp = SafeConfigParser()
    cp.read(versioncfgfile)
    gitdir = os.path.join(MYDIR, '.git')
    if not os.path.isdir(gitdir):  return cp
    try:
        g = gitinfo()
    except OSError:
        return cp
    d = cp.defaults()
    if g['version'] != d.get('version') or g['commit'] != d.get('commit'):
        cp.set('DEFAULT', 'version', g['version'])
        cp.set('DEFAULT', 'commit', g['commit'])
        cp.set('DEFAULT', 'date', g['date'])
        cp.set('DEFAULT', 'timestamp', g['timestamp'])
        cp.write(open(versioncfgfile, 'w'))
    return cp
Пример #33
0
def getversioncfg():
    from ConfigParser import SafeConfigParser
    cp = SafeConfigParser()
    cp.read(versioncfgfile)
    gitdir = os.path.join(MYDIR, '.git')
    if not os.path.isdir(gitdir): return cp
    try:
        g = gitinfo()
    except OSError:
        return cp
    d = cp.defaults()
    if g['version'] != d.get('version') or g['commit'] != d.get('commit'):
        cp.set('DEFAULT', 'version', g['version'])
        cp.set('DEFAULT', 'commit', g['commit'])
        cp.set('DEFAULT', 'date', g['date'])
        cp.set('DEFAULT', 'timestamp', g['timestamp'])
        cp.write(open(versioncfgfile, 'w'))
    return cp
Пример #34
0
def parse_config_file(current_dir):
    """
	This function finds and parses a configuration file, which contains various constants 
	"""
    cfg_files = [
        os.path.join(current_dir, f) for f in os.listdir(current_dir)
        if f.endswith('cfg')
    ]
    if len(cfg_files) == 1:
        logging.info('Parsing report configuration file at %s' % cfg_files[0])
        parser = SafeConfigParser()
        parser.read(cfg_files[0])
        return parser.defaults()
    else:
        logging.error(
            'There were %d config (*cfg) files found in %s.  Need exactly 1.  Correct this.'
            % (len(cfg_files), current_dir))
        sys.exit(1)
Пример #35
0
def persistent_search_paths():
    """Return a list of persistent registered (prefix, path) pairs.
    """

    global_settings_dir = Orange.utils.environ.install_dir
    user_settings_dir = Orange.utils.environ.orange_settings_dir
    parser = SafeConfigParser()
    parser.read([os.path.join(global_settings_dir, "orange-search-paths.cfg"),
                 os.path.join(user_settings_dir, "orange-search-paths.cfg")])
    try:
        items = parser.items("paths")
        defaults = parser.defaults().items()
        items = [i for i in items if i not in defaults]
    except ConfigParser.NoSectionError:
        items = []
    # Replace "__default__" prefix with ""
    items = [item if item[0] != "__default__" else ("", item[1]) \
             for item in items]
    return items
Пример #36
0
def _load_lsg_credentials():
    """Read Life Science Grid Portal credentials from file and store in os.environ variables."""
    #Get path to credential file
    from divergence import resource_filename
    lsgp_credentials_file = resource_filename(__name__, 'credentials/lsg-portal.cfg')

    # Copy template config file to actual search path when file can not be found
    if not os.path.exists(lsgp_credentials_file):
        shutil.copy(lsgp_credentials_file + '.sample', lsgp_credentials_file)
        logging.info('Copied .sample file to %s', lsgp_credentials_file)

    logging.info('Credentials not found on path: Reading credentials from %s', lsgp_credentials_file)

    #Parse credential file
    from ConfigParser import SafeConfigParser
    parser = SafeConfigParser()
    parser.read(lsgp_credentials_file)
    os.environ['lsg_username'] = parser.defaults()['lsg_username']
    os.environ['lsg_password'] = parser.defaults()['lsg_password']
Пример #37
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {}
        for option in [
                "timeout", "host", "port", "nick", "channel", "info",
                "heartbeat", "password", "username", "realname"
        ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "SilentJoinPart":
            factory = SilentJoinPartFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")

        factory = factory(options)
        reactor.connectSSL(options['host'], int(options['port']), factory,
                           ssl.ClientContextFactory(), int(options['timeout']))

    reactor.callWhenRunning(signal, SIGINT, handler)
def persistent_search_paths():
    """Return a list of persistent registered (prefix, path) pairs.
    """

    global_settings_dir = Orange.utils.environ.install_dir
    user_settings_dir = Orange.utils.environ.orange_settings_dir
    parser = SafeConfigParser()
    parser.read([os.path.join(global_settings_dir, "orange-search-paths.cfg"),
                 os.path.join(user_settings_dir, "orange-search-paths.cfg")])
    try:
        items = parser.items("paths")
        defaults = parser.defaults().items()
        items = [i for i in items if i not in defaults]
    except ConfigParser.NoSectionError:
        items = []
    # Replace "__default__" prefix with ""
    items = [item if item[0] != "__default__" else ("", item[1]) \
             for item in items]
    return items
Пример #39
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {}
        for option in ["timeout", "host", "port", "nick", "channel", "channels", "links", "info", "heartbeat",
                       "password",
                       "username", "realname"]:
            options[option] = get(option)

        mode = get("mode")

        # Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        # (ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "SilentJoinPart":
            factory = SilentJoinPartFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")

        factory = factory(options)
        reactor.connectSSL(options['host'], int(options['port']), factory, ssl.ClientContextFactory(),
                           int(options['timeout']))

    reactor.callWhenRunning(signal, SIGINT, handler)
Пример #40
0
    def _parse_config_files(self):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser()
        # Fill in the built in values
        self.config = dict(self.__class__.defaults)
        # Update with the values from the defaults section. This is needed
        # in case the config file doesn't have a [<command>] section at all
        parser.read(self.config_files)
        self.config.update(dict(parser.defaults()))

        if not (self.command.startswith('gbp-') or
                self.command.startswith('git-')):
            # Invoked as gbp <command> syntax, so parse the old sections
            # of {gbp.git}-<command> for backward compatibility:
            for prefix in ['gbp', 'git']:
                oldcmd = '%s-%s' % (prefix, self.command)
                if parser.has_section(oldcmd):
                    self.config.update(dict(parser.items(oldcmd, raw=True)))

        # Update with command specific settings
        if parser.has_section(self.command):
            self.config.update(dict(parser.items(self.command, raw=True)))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser.items(section, raw=True)))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist."
                                     % section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [ self.config['filter'] ]
        else:
            self.config['filter'] = []
Пример #41
0
def _load_lsg_credentials():
    """Read Life Science Grid Portal credentials from file and store in os.environ variables."""
    #Get path to credential file
    from divergence import resource_filename
    lsgp_credentials_file = resource_filename(__name__,
                                              'credentials/lsg-portal.cfg')

    # Copy template config file to actual search path when file can not be found
    if not os.path.exists(lsgp_credentials_file):
        shutil.copy(lsgp_credentials_file + '.sample', lsgp_credentials_file)
        logging.info('Copied .sample file to %s', lsgp_credentials_file)

    logging.info('Credentials not found on path: Reading credentials from %s',
                 lsgp_credentials_file)

    #Parse credential file
    from ConfigParser import SafeConfigParser
    parser = SafeConfigParser()
    parser.read(lsgp_credentials_file)
    os.environ['lsg_username'] = parser.defaults()['lsg_username']
    os.environ['lsg_password'] = parser.defaults()['lsg_password']
Пример #42
0
def parse_config():
    """
	This function finds and parses a configuration file, which contains various parameters/constants to run this pipeline
	"""
    current_dir = os.path.dirname(
        os.path.realpath(__file__))  # this gets the directory of this script
    cfg_files = [
        os.path.join(current_dir, f) for f in os.listdir(current_dir)
        if f.endswith('cfg')
    ]
    if len(cfg_files) == 1:
        parser = SafeConfigParser()
        parser.read(cfg_files[0])
        config_params = parser.defaults()
        config_params = {
            k: parse_vals(config_params[k])
            for k in config_params.keys()
        }
        return config_params
    else:
        sys.exit(1)
Пример #43
0
def main():
	config = SafeConfigParser()
	#Case-sensitive to set args attributes correctly.
	config.optionxform = str
	config.read("probe.config")
	defaults = config.defaults()

	def get(option):
		return config.get("OVERRIDE", option) or defaults[option]

	args = Arguments()
	for arg in defaults.keys():
		setattr(args, arg, get(arg))

	#Convert integer options
	for arg in [ "numThreads", "port", "probeWait", "hopsToLive" ]:
		setattr(args, arg, int(getattr(args, arg)))

	#Convert floating point options.
	for arg in [ "timeout" ]:
		setattr(args, arg, float(getattr(args, arg)))

	#Convert types list to list
	args.types = split(args.types, ",")

	logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s", level=getattr(logging, args.verbosity), filename=args.logFile)
	logging.info("Starting up.")

	#Ensure the database holds the required tables, columns, and indicies.
	init_database(sqlite3.connect(args.databaseFile))

	if args.numThreads < 1:
		print("Cannot run fewer than one thread.")
		exit(1)

	reactor.callWhenRunning(signal, SIGINT, sigint_handler)
	reactor.callWhenRunning(signal, SIGTERM, sigint_handler)
	reactor.connectTCP(args.host, args.port, FCPReconnectingFactory(args))
Пример #44
0
def main():
    config_parser = SafeConfigParser()
    # Case-sensitive option names.
    config_parser.optionxform = str
    config_parser.read("probe.config")
    config = config_parser.defaults()

    for arg, value in config_parser.items('OVERRIDE'):
        config[arg] = value

    #Convert integer options
    for arg in ["port", "hopsToLive", "probeRate"]:
        config[arg] = int(config[arg])

    #Convert floating point options.
    for arg in ["timeout", "databaseTimeout"]:
        config[arg] = float(config[arg])

    #Convert types list to list
    config['types'] = split(config['types'], ",")

    # Compute probe period. Rate is easier to think about, so it's used in the
    # config file. probeRate is probes/minute. Period is seconds/probe.
    # 60 seconds   1 minute           seconds
    # ---------- * ---------------- = -------
    # 1 minute     probeRate probes   probe
    config['probePeriod'] = 60 / config['probeRate']

    logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s",
                        level=getattr(logging, config['verbosity']),
                        filename=config['logFile'])
    logging.info("Starting up.")

    conn = update_db.main(log_to_stdout=False).add

    return internet.TCPClient(config['host'], config['port'],
                              FCPReconnectingFactory(config, conn))
Пример #45
0
def main():
    config_parser = SafeConfigParser()
    # Case-sensitive option names.
    config_parser.optionxform = str
    config_parser.read("probe.config")
    config = config_parser.defaults()

    for arg, value in config_parser.items('OVERRIDE'):
        config[arg] = value

    #Convert integer options
    for arg in ["port", "hopsToLive", "probeRate"]:
        config[arg] = int(config[arg])

    #Convert floating point options.
    for arg in ["timeout", "databaseTimeout"]:
        config[arg] = float(config[arg])

    #Convert types list to list
    config['types'] = split(config['types'], ",")

    # Compute probe period. Rate is easier to think about, so it's used in the
    # config file. probeRate is probes/minute. Period is seconds/probe.
    # 60 seconds   1 minute           seconds
    # ---------- * ---------------- = -------
    # 1 minute     probeRate probes   probe
    config['probePeriod'] = 60 / config['probeRate']

    logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s",
                        level=getattr(logging, config['verbosity']),
                        filename=config['logFile'])
    logging.info("Starting up.")

    conn = update_db.main(log_to_stdout=False).add

    return internet.TCPClient(config['host'], config['port'],
                              FCPReconnectingFactory(config, conn))
Пример #46
0
	, 'init-and-file'
	, 'from-vars'
	]

# Initialize the parser with some defaults
parser = ConfigParser.SafeConfigParser(
	defaults={ 'from-default':'value from defaults passed to init'
	         , 'init-only':'value from defaults passed to init'
	         , 'init-and-file':'value from defaults passed to init'
	         , 'from-section':'value from defaults passed to init'
	         , 'from-vars':'value from detaults passed to init'
	         }	
	         )

print('Defaults before loading file:')
defaults = parser.defaults()
for name in option_names:
	if name in defaults:
		print('  %-15s = %r' %(name, defaults[name]))

# LOAD THE CONFIGURATION FILE
parser.read('with-defaults.ini')

print('\nDefaults after loading file:')
defaults = parser.defaults()
for name in option_names:
	if name in defaults:
		print('  %-15s = %r' %(name, defaults[name]))

# DEFINE SOME LOCAL OVERRIDES
vars = {'from-vars':'value from vars'}
Пример #47
0
class XYZBathyBank(object):
    """Bank of XYZ bathymetry infos

    .. seealso::

        :class:`XYZBathyBankClient`

    :Usage:

    >>> xyzb = XYZBathyBank()

    """
    def __init__(self, cfgfile=None):

        # Guess file name
        if cfgfile is None:
            f = ConfigParser()
            f.read(get_config_value(__name__, 'cfgfile_xyz'))
            if f.has_option('banks', 'main'):
                cfgfile = f.get('banks', 'main')
            #else:
            #    raise Exception, 'Cannot read config file to guess bank file name'
        # Read file
        self.cfg = SafeConfigParser()
        self.cfgfile = cfgfile
        if cfgfile and os.path.exists(cfgfile): self.cfg.read(cfgfile)
        self._clients = {}
        for id in self.cfg.sections():
            self._clients[id] = XYZBathyBankClient(self, id)
        self._order = None

    def ids(self):
        """Return the list of bathy ids"""
        return self.cfg.sections()

    def __str__(self):
        if not len(self):
            return 'No bathymetry'
        infos = []
        for id in self.ids():
            infos.append(str(self._clients[id]))
        return '\n'.join(infos)

    def __len__(self):
        return len(self.cfg.sections())

    def add(self, xyzfile, id=None, force=False, **kwargs):
        """Add a bathy to the bank

        - **xyzfile**: xyz file.
        - *id*: Id of the bank. If ``None``, it is guessed from the file name:
           ``xyzfile="/path/toto.xyz"`` ->  ``id="toto"``
        """
        if id is None:
            id = os.path.basename(xyzfile)
            if id.endswith('.xyz'): id = id[:-4]
        if self.cfg.has_section(id):
            if force:
                self.cfg.remove_section(id)
            else:
                raise KeyError, 'Bathymetry %s already exists' % id
        self.cfg.add_section(id)
        self.cfg.set(id, 'xyzfile', xyzfile)
        self._clients[id] = XYZBathyBankClient(self, id, **kwargs)
        #       self._save_()
        self._update_order_()

    def __iadd__(self, d):
        self.add(d)
        return self

    def remove(self, id):
        """Remove a bathy from the bank

        *id*: A bathy id or :class:`XYZBathyBankClient` instance.
        """
        if isinstance(id, XYZBathyBankClient):
            id = id.id
        if not self.cfg.has_section(id):
            warn('No such bathymetry %s' % id)
            return
        self.cfg.remove_section(id)
        del self._clients[id]
        self._update_order_()
#       self._save_()

    def __isub__(self, d):
        self.remove(id)
        return self

    def copy(self, id1, id2):
        """Copy bathy infos to another"""
        assert self.cfg.has_section(id1), 'No such bathymetry %s' % id1
        if not self.cfg.has_section(id2):
            self.cfg.add_section(id2)
        for opt in self.cfg.options(id1):
            self.cfg.set(id2, self.cfg.get(id1, opt))
        self._clients[id2] = XYZBathyBankClient(self, id2)
        self._update_order_()


##      self._save_()

    def __getitem__(self, id):
        assert self.cfg.has_section(id), 'No such bathymetry %s' % id
        return self._clients[id]

    def __setitem__(self, id, xyzfile):
        self.add(id, xyzfile)

    def __deltiem__(self, id):
        self.remove(id)

    def load(self, id):
        """Load a bathymetry as a :class:`XYZBathy` instance"""
        assert self.cfg.has_section(id), 'No such bathymetry ' + id
        return self._clients[id].load()

    def list(self):
        """Get all clients of the bank as a list"""
        return self._clients.values()

    def select(self,
               xmin=None,
               xmax=None,
               ymin=None,
               ymax=None,
               load=True,
               margin=None,
               ordered=None):
        """Return a list of bathymetries relevant to the specified region

        - *margin*: Margin for loading xyz bathies:

            - if None, the whole bathy is loaded from the bank,
            - else it should be a value relative to the approximative resolution (see :meth:`XYZBathy.resol`)
        """
        res = []
        for id in self.ids():
            b = self._clients[id]
            if   (None not in (xmin, b.xmax) and xmin > b.xmax) or \
                (None not in (xmax, b.xmin) and xmax < b.xmin) or \
                (None not in (ymin, b.ymax) and ymin > b.ymax) or \
                (None not in (ymax, b.ymin) and ymax > b.ymin):
                continue
            if load:
                b = b.load(zone=(xmin, ymin, xmax, ymax), margin=margin)
            res.append(b)
        if self.get_order() is not None and ordered != False:
            ids = [b.id for b in res]
            order = [id for id in self.get_order() if id in ids]
            res.sort(cmp=lambda b1, b2: cmp(self.get_order().index(b1),
                                            self.get_order().index(b2)))
        return res

    def set_order(self, order):
        """Set a list of ids to define the order"""
        if order is None:
            del self.cfg.defaults()['order']
        else:
            self.cfg.defaults()['order'] = str(
                [id for id in order if id in self.ids()])

    def get_order(self, ):
        """Get a list of ids that define the order"""
        return eval(self.cfg.defaults().get('order', 'None'))

    def _update_order_(self):
        if self.get_order() is None: return
        self.cfg.defaults()['order'] = str(
            [id for id in self.get_order() if id in self.ids()])

    def save(self, cfgfile=None):
        if cfgfile is None:
            cfgfile = self.cfgfile
        else:
            self.cfgfile = cfgfile
        if cfgfile is None:
            raise VACUMMError('You must provide a valid file name')
        f = open(cfgfile, 'w')
        self.cfg.write(f)
        f.close()

    def merger(self, **kwargs):
        """Return a :class:`XYZBathyMerger` instance using the current bank

        Keywords are passed to :meth:`select`
        """
        merger = XYZBathyMerger()
        merger += self.select(**kwargs)
        return merger

    def plot(self, **kwargs):
        """Plot current bathies using a :class:`XYZBathyMerger` instance"""

        return self.merger(**kwfilter(kwargs, 'merger')).plot(**kwargs)
Пример #48
0
    "cwd": os.getcwd(),
    "start_time": start_time,
})
config.read(config_file)
config.set('DEFAULT', 'runtime_directory', runtime_directory)
config.set('DEFAULT', 'default_working_directory', working_directory)

# Extract input file list from parset
to_process = input_parset.getStringVector(
    'ObsSW.Observation.DataProducts.measurementSets')

# Read config file to establish location of parset directory to use
parset_directory = config.get("layout", "parset_directory")
create_directory(parset_directory)

# For each task (currently only ndppp), extract and write parset
tasks = ConfigParser(config.defaults())
tasks.read(string_to_list(config.get("DEFAULT", "task_files")))
ndppp_parset_location = tasks.get("ndppp", "parset")
input_parset.makeSubset(
    "ObsSW.Observation.ObservationControl.PythonControl.DPPP.").writeFile(
        ndppp_parset_location)

# Run pipeline & wait for result
subprocess.check_call([
    'python', pipeline_definition, '-j', tree_id, '-d', '--config',
    config_file, '--runtime-directory', runtime_directory,
    '--default-working-directory', working_directory, '--start-time',
    start_time
])
Пример #49
0
    help='If specified plots uptime distribution over the last recency period.'
)
parser.add_argument(
    '--bulk-reject',
    dest='bulkReject',
    default=False,
    action='store_true',
    help=
    'If specified plots bulk reject distribution over the last recency period.'
)

args = parser.parse_args()

parser = SafeConfigParser()
parser.read("database.config")
config = parser.defaults()

# Set default locale for number formatting.
locale.setlocale(locale.LC_ALL, '')


def log(msg):
    if not args.quiet:
        print("{0}: {1}".format(datetime.datetime.now(), msg))


# Store the time the script started so that all database queries can cover
# the same time span by only including up to this point in time.
# This allows times other than the current one.
# If a time period for RRD includes this start time it is considered
# incomplete and not computed.
Пример #50
0
def main():
    config = SafeConfigParser()
    config.read("relaybot.config")
    defaults = config.defaults()

    # Webhook stuff
    webhooks = resource.Resource()
    pool = client.HTTPConnectionPool(reactor)
    agent = client.Agent(reactor, pool=pool)

    for section in config.sections():

        def get(option):
            if option in defaults or config.has_option(section, option):
                return config.get(section, option) or defaults[option]
            else:
                return None

        options = {'servername': section}
        for option in [
                "timeout", "host", "port", "nick", "channel", "channels",
                "heartbeat", "password", "username", "realname", "mode", "ssl",
                "fingerprint", "nickcolor", "topicsync"
        ]:
            options[option] = get(option)

        mode = get("mode")

        #Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
        #(ReconnectingClientFactory equivalent for endpoints.)
        factory = None
        if mode == "Default":
            factory = RelayFactory
        elif mode == "FLIP":
            factory = FLIPFactory
        elif mode == "NickServ":
            factory = NickServFactory
            options["nickServPassword"] = get("nickServPassword")
        elif mode == "ReadOnly":
            factory = ReadOnlyFactory
            options["nickServPassword"] = get("nickServPassword")
        # RelayByCommand: only messages with <nickname>: will be relayed.
        elif mode == "RelayByCommand":
            factory = CommandFactory
        elif mode == "Webhooks":
            options['webhookNonce'] = get('webhookNonce')
            options['outgoingWebhook'] = get('outgoingWebhook')
            webhooks.putChild(options['webhookNonce'], Webhook(agent, options))
            continue

        factory = factory(options)
        if options['ssl'] == "True":
            if options['fingerprint']:
                ctx = certoptions(fingerprint=options['fingerprint'],
                                  verifyDepth=0)
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ctx, int(options['timeout']))
            else:
                reactor.connectSSL(options['host'], int(options['port']),
                                   factory, ssl.ClientContextFactory(),
                                   int(options['timeout']))
        else:
            reactor.connectTCP(options['host'], int(options['port']), factory,
                               int(options['timeout']))

    # Start incoming webhook server
    if 'webhook' in defaults:
        serverFromString(reactor,
                         defaults['webhook']).listen(server.Site(webhooks))

    reactor.callWhenRunning(signal, SIGINT, handler)
Пример #51
0
class ConfigSection(object):
    """
    Wraps SafeConfigParser with static section handling

    :param defaults: dict-like containing default keys/values
    :param section: name of section to initially bind to

    :note: Not an exact interface reproduction, some functionality
           left out!
    """
    def __init__(self, defaults, section):
        self._section = section
        # SafeConfigParser is old-style, and we're changing method parameters
        self._scp = SafeConfigParser(defaults)
        self._scp.add_section(self._section)

    def defaults(self):
        """
        Returns dictionary of default options
        """
        return self._scp.defaults()

    def sections(self):
        """
        Returns a list containing this instances section-name
        """
        return [self._section]

    def add_section(self, section):
        """
        Not written, do not use!

        :raises NotImplementedError: DO NOT USE!
        """
        raise NotImplementedError()

    def has_section(self, section):
        """
        Returns True if instance-section == ``section``

        :param section: Name of section to check.
        :returns: True/False if section exists.
        """
        return section == self._section

    def options(self):
        """
        Returns dictionary of all options keys/values
        """
        return self._scp.options(self._section)

    def has_option(self, option):
        """
        Returns True if key-named ``option`` exists

        :param option: Name of the option (key) to check.
        :returns: True/False if option (key) exists.
        """
        return self._scp.has_option(self._section, option)

    # Private method doesn't need docstring
    def _prune_sections(self):  # pylint: disable=C0111
        for section in self._scp.sections():
            if section != self._section:
                self._scp.remove_section(section)

    def read(self, filenames):
        """
        Replace current contents with content from filename(s)/list

        :param filenames: Same as for ``SafeConfigParser.read`` method
        :return: List of successfully parsed filenames.
        """
        result = self._scp.read(filenames)  # Changes self._scp
        self._prune_sections()
        return result

    # Short name 'fp' mirrors use in ConfigParser module
    def readfp(self, fp, filename=None):  # pylint: disable=C0103
        """
        Replace current contents with content from file

        :param fp: Same as for ``SafeConfigParser.readfp`` method
        :param filename: Same as for ``SafeConfigParser.readfp`` method
        :return:  Same as for ``SafeConfigParser.readfp`` method
        """
        result = self._scp.readfp(fp, filename)  # Changes self._scp
        self._prune_sections()
        return result

    def get(self, option):
        """
        Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :returns: The value assigned to ``option``
        """
        return self._scp.get(self._section, option)

    def getint(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: Value assigned to ``option`` converted to an integer.
        """
        return self._scp.getint(self._section, option)

    def getfloat(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: Value assigned to ``option`` converted to a float.
        """
        return self._scp.getfloat(self._section, option)

    def getboolean(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: ``True``: if value is ``yes``, ``true``. ``False`` if ``no``
                           or ``false``.
        """
        try:
            value = self._scp.get(self._section, option).lower().strip()
            positives = ("yes", "true")
            negatives = ("no", "false")
            if value in positives:
                return True
            if value in negatives:
                return False
            # try regular way
        except AttributeError:
            pass  # try regular way
        return self._scp.getboolean(self._section, option)

    def set(self, option, value):
        """
        Set value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to set.
        :param value: Content to assign to ``option``.
        :return: Same as for ``SafeConfigParser.set`` method.
        """
        return self._scp.set(self._section, option, str(value))

    def write(self, fileobject):
        """
        Overwrite current contents of ``fileobject.name``
        """
        return self._scp.write(open(fileobject.name, "wb"))

    def merge_write(self, fileobject):
        """
        Update section contents of ``fileobject.name`` by section only.
        """
        scp = SafeConfigParser()
        # Safe if file doesn't exist
        scp.read(fileobject.name)
        # N/B: This won't work with DEFAULTS
        if not scp.has_section(self._section):
            scp.add_section(self._section)
        for key, value in self.items():
            scp.set(self._section, key, value)
        scp.write(open(fileobject.name, "w+b"))  # truncates file first

    def remove_option(self, option):
        """
        Remove option-key ``option``
        """
        return self._scp.remove_option(self._section, option)

    def remove_section(self):
        """
        Not implemented, do not use!

        :raises NotImplementedError: DO NOT USE!
        """
        raise NotImplementedError()

    def items(self):
        """
        Return list of ``key``/``value`` tuples for contents
        """
        return self._scp.items(self._section)
Пример #52
0
def merge_configs(cfgs, forprint=True, raw=True):
    """Merge configurations

    Options of the last config overrides options of the other ones,
    except for config file names (values then end with .ini or .cfg)
    that are store as a tuple.

    .. warning::

        This makes output config incompatible with
        :meth:`~ConfigParser.ConfigParser.getint`, etc.
    """
    # Init
    if not isinstance(cfgs, (list, tuple)):
        cfgs = [cfgs]
    cfg = SafeConfigParser()

    # Loop on configs
    kwget = get_dir_dict()
    kwget['mod_dir'] = '%%(mod_dir)s'
    pending = {}
    for cfgi in cfgs:
        #        print '-'*50
        #        S = StringIO.StringIO()
        #        cfgi.write(S)
        #        s = S.getvalue()
        #        S.close()
        #        print s

        if not isinstance(cfgi, SafeConfigParser):
            cfgfiles = cfgi
            cfgi = SafeConfigParser()
            cfgi.read(cfgfiles)

        cfg._defaults.update(cfgi.defaults())

        for section in cfgi.sections():

            if section not in cfg.sections():
                cfg.add_section(section)
            pending.setdefault(section, {})

            for option, rvali in cfgi.items(section, raw=raw):

                if option in pending[
                        section]:  # already identified as an option of config files
                    if rvali not in pending[section][option]:
                        pending[section][option] = tuple(
                            pending[section][option]) + (rvali, )
                elif _re_cfgfile(cfgi.get(section, option, vars=kwget)):
                    pending[section][option] = rvali,
                else:
                    cfg.set(section, option, rvali)

    # Convert pending options
    for section, options in pending.items():
        for option, value in options.items():
            if forprint:
                if len(value) == 1:
                    value = value[0]
                else:
                    value = '(%s)' % ', '.join(value)
            else:
                value = '(%s)' % '|'.join(value)


#            print section, option, value
            cfg.set(section, option, value)

    return cfg
Пример #53
0
# On charge le fichier
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
import os
print os.getcwd()
config.read('misc.io.config.in.ini')

# List des sections
print config.sections()

# On récupère les unités pour la SST
print config.get('sst', 'units')
#  -> m/s

# La latitude max de la section par défaut
print config.defaults()['lat_max']
#  -> 50.0
# Latitude max de la sst = celle par défaut
print config.getfloat('sst', 'lat_max') + 1
#  -> 51.0

# Subsitutions
# - contenu substitué
print config.get('wind', 'name')
#  -> Wind on Iroise
# - contenu brut
print config.get('wind', 'name', raw=True)
# -> Wind on %(zone)s
# - contenu substitué par la force
print config.get('wind', 'name', vars=dict(zone='for Britanny'))
#  -> Wind on Britanny
Пример #54
0
class ConfigSection(object):

    """
    Wraps SafeConfigParser with static section handling

    :param defaults: dict-like containing default keys/values
    :param section: name of section to initially bind to

    :note: Not an exact interface reproduction, some functionality
           left out!
    """

    def __init__(self, defaults, section):
        self._section = section
        # SafeConfigParser is old-style, and we're changing method parameters
        self._scp = SafeConfigParser(defaults)
        self._scp.add_section(self._section)

    def defaults(self):
        """
        Returns dictionary of default options
        """
        return self._scp.defaults()

    def sections(self):
        """
        Returns a list containing this instances section-name
        """
        return [self._section]

    def add_section(self, section):
        """
        Not written, do not use!

        :raises NotImplementedError: DO NOT USE!
        """
        raise NotImplementedError()

    def has_section(self, section):
        """
        Returns True if instance-section == ``section``

        :param section: Name of section to check.
        :returns: True/False if section exists.
        """
        return section == self._section

    def options(self):
        """
        Returns dictionary of all options keys/values
        """
        return self._scp.options(self._section)

    def has_option(self, option):
        """
        Returns True if key-named ``option`` exists

        :param option: Name of the option (key) to check.
        :returns: True/False if option (key) exists.
        """
        return self._scp.has_option(self._section, option)

    # Private method doesn't need docstring
    def _prune_sections(self):  # pylint: disable=C0111
        for section in self._scp.sections():
            if section != self._section:
                self._scp.remove_section(section)

    def read(self, filenames):
        """
        Replace current contents with content from filename(s)/list

        :param filenames: Same as for ``SafeConfigParser.read`` method
        :return: List of successfully parsed filenames.
        """
        result = self._scp.read(filenames)  # Changes self._scp
        self._prune_sections()
        return result

    # Short name 'fp' mirrors use in ConfigParser module
    def readfp(self, fp, filename=None):  # pylint: disable=C0103
        """
        Replace current contents with content from file

        :param fp: Same as for ``SafeConfigParser.readfp`` method
        :param filename: Same as for ``SafeConfigParser.readfp`` method
        :return:  Same as for ``SafeConfigParser.readfp`` method
        """
        result = self._scp.readfp(fp, filename)  # Changes self._scp
        self._prune_sections()
        return result

    def get(self, option):
        """
        Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :returns: The value assigned to ``option``
        """
        return self._scp.get(self._section, option)

    def getint(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: Value assigned to ``option`` converted to an integer.
        """
        return self._scp.getint(self._section, option)

    def getfloat(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: Value assigned to ``option`` converted to a float.
        """
        return self._scp.getfloat(self._section, option)

    def getboolean(self, option):
        """
        Convert/Return value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to check.
        :return: ``True``: if value is ``yes``, ``true``. ``False`` if ``no``
                           or ``false``.
        """
        try:
            value = self._scp.get(self._section, option).lower().strip()
            positives = ("yes", "true")
            negatives = ("no", "false")
            if value in positives:
                return True
            if value in negatives:
                return False
            # try regular way
        except AttributeError:
            pass  # try regular way
        return self._scp.getboolean(self._section, option)

    def set(self, option, value):
        """
        Set value assigned to key named ``option``

        :param option: Name of the ``option`` (key) to set.
        :param value: Content to assign to ``option``.
        :return: Same as for ``SafeConfigParser.set`` method.
        """
        return self._scp.set(self._section, option, str(value))

    def write(self, fileobject):
        """
        Overwrite current contents of ``fileobject.name``
        """
        return self._scp.write(open(fileobject.name, "wb"))

    def merge_write(self, fileobject):
        """
        Update section contents of ``fileobject.name`` by section only.
        """
        scp = SafeConfigParser()
        # Safe if file doesn't exist
        scp.read(fileobject.name)
        # N/B: This won't work with DEFAULTS
        if not scp.has_section(self._section):
            scp.add_section(self._section)
        for key, value in self.items():
            scp.set(self._section, key, value)
        scp.write(open(fileobject.name, "w+b"))  # truncates file first

    def remove_option(self, option):
        """
        Remove option-key ``option``
        """
        return self._scp.remove_option(self._section, option)

    def remove_section(self):
        """
        Not implemented, do not use!

        :raises NotImplementedError: DO NOT USE!
        """
        raise NotImplementedError()

    def items(self):
        """
        Return list of ``key``/``value`` tuples for contents
        """
        return self._scp.items(self._section)
Пример #55
0
# Extract runtime, working, results directories from input parset
runtime_directory = input_parset.getString("ObsSW.Observation.ObservationControl.PythonControl.runtimeDirectory")
working_directory = input_parset.getString("ObsSW.Observation.ObservationControl.PythonControl.workingDirectory")
results_directory = input_parset.getString("ObsSW.Observation.ObservationControl.PythonControl.resultDirectory")

# Set up configuration for later processing stages
config = ConfigParser({
    "job_name": tree_id,
    "cwd": os.getcwd(),
    "start_time": start_time,
})
config.read(config_file)
config.set('DEFAULT', 'runtime_directory', runtime_directory)
config.set('DEFAULT', 'default_working_directory', working_directory)

# Extract input file list from parset
to_process = input_parset.getStringVector('ObsSW.Observation.DataProducts.measurementSets')

# Read config file to establish location of parset directory to use
parset_directory = config.get("layout", "parset_directory")
create_directory(parset_directory)

# For each task (currently only ndppp), extract and write parset
tasks = ConfigParser(config.defaults())
tasks.read(string_to_list(config.get("DEFAULT", "task_files")))
ndppp_parset_location = tasks.get("ndppp", "parset")
input_parset.makeSubset("ObsSW.Observation.ObservationControl.PythonControl.DPPP.").writeFile(ndppp_parset_location)

# Run pipeline & wait for result
subprocess.check_call(['python', pipeline_definition, '-j', tree_id, '-d', '--config', config_file, '--runtime-directory', runtime_directory, '--default-working-directory', working_directory, '--start-time', start_time])
Пример #56
0
def parse_config():
    with open(CONFIG_FILE) as cfg_handle:
        parser = SafeConfigParser()
        parser.readfp(cfg_handle)
        return parser.defaults()
Пример #57
0
from ConfigParser import SafeConfigParser
import os, errno

pwd = os.path.dirname(os.path.realpath(__file__))
print pwd
parser = SafeConfigParser()
parser.read("config.ini")
for f in parser.defaults():
    nf = parser.get('targets',f)
    of = "%s/%s"%(pwd,f)
    print "Symlink %s -> %s"%(of,nf)
    try:
        os.symlink(of,nf)
    except OSError, e:
        if e.errno == errno.EEXIST:
            os.remove(nf)
            os.symlink(of,nf)