Пример #1
0
    def read_config_file(path):
        config = SafeConfigParser()
        cfp = open(path, 'r')
        config.readfp(cfp)
        cfp.close()

        return config
Пример #2
0
    def read_config_file(path):
        config = SafeConfigParser()
        cfp = open(path, 'r')
        config.readfp(cfp)
        cfp.close()

        return config
Пример #3
0
    def auth_config(cls, email, password, hostname, namespace, dir_file):
        parser = SafeConfigParser()

        def try_section(section, email, password, filename):
            if parser.has_section(section) or section == DEFAULTSECT:
                # look at options which look like EMAIL@DOMAIN = PASSWORD
                # We might want other per-domain options later
                candidates = [(e,p) for e, p in parser.items(section)
                              if '@' in e and e.startswith(email)]
                if len(candidates) == 1:
                    email, password = candidates[0]
                elif len(candidates) > 1:
                    raise ValueError(
                        "Email prefix <{0}> is ambiguous in [{1}] of '{2}'".format(
                            email, section, filename))
            return (email, password)

        if not email:
            # Do not provide default credentials. It's too dangerous.
            return (None, None)

        dir, file = dir_file
        if dir:
            # TODO: check that file is not world-readable
            filename = os.path.join(dir, file)
            parser.read(filename)
            (email, password) = try_section(hostname, email, password, filename)
            if not (email and password):
                (email, password) = try_section(DEFAULTSECT, email, password, filename)
        return (email, password)
Пример #4
0
    def from_config(cls, path, **kwargs):
        """
        Create a LastFM instance from a config file in the following format:

            [lastfm]
            api_key = myapikey
            api_secret = myapisecret
            username = thesquelched
            password = plaintext_password

            # Can be 'password' or 'hashed_password'
            auth_method = password

        You can also override config values with keyword arguments.
        """
        config = SafeConfigParser()
        config.add_section('lastfm')
        config.read(os.path.expanduser(os.path.expandvars(path)))

        return LastFM(
            cls._getoption(config, kwargs, 'api_key'),
            cls._getoption(config, kwargs, 'api_secret'),
            username=cls._getoption(config, kwargs, 'username', None),
            password=cls._getoption(config, kwargs, 'password', None),
            auth_method=cls._getoption(config, kwargs, 'auth_method', None),
            session_key=cls._getoption(config, kwargs, 'session_key', None),
            url=cls._getoption(config, kwargs, 'url', None),
        )
Пример #5
0
    def __init__(self, plugin_dir=None):
        self.config = SafeConfigParser()
        config_path = save_config_path(self.APP_NAME)
        self.config_file = os.path.join(config_path, self.APP_NAME + ".conf")
        self.config.read(self.config_file)

        this_dir = os.path.abspath(os.path.dirname(__file__))
        self.plugin_dir = plugin_dir or os.path.join(this_dir,
                                                     self._default_dir)
        places = [
            self.plugin_dir,
        ]
        [
            places.append(os.path.join(path, self.APP_NAME, "evaluators"))
            for path in xdg_data_dirs
        ]

        PluginManagerSingleton.setBehaviour([
            ConfigurablePluginManager,
            VersionedPluginManager,
        ])

        self.manager = PluginManagerSingleton.get()
        self.manager.setConfigParser(self.config, self.write_config)
        self.manager.setPluginInfoExtension("expr-plugin")
        self.manager.setPluginPlaces(places)
        self.manager.collectPlugins()
Пример #6
0
class ConfigHelper(object):
    NONE_VALUE = 'None'
    DEFAULT_HOST = "127.0.0.1"
    DEFAULT_PORT = 5696
    DEFAULT_CERTFILE = os.path.normpath(os.path.join(
        FILE_PATH, '../demos/certs/server.crt'))
    DEFAULT_KEYFILE = os.path.normpath(os.path.join(
        FILE_PATH, '../demos/certs/server.key'))
    DEFAULT_CA_CERTS = os.path.normpath(os.path.join(
        FILE_PATH, '../demos/certs/server.crt'))
    DEFAULT_SSL_VERSION = 'PROTOCOL_SSLv23'
    DEFAULT_USERNAME = None
    DEFAULT_PASSWORD = None

    def __init__(self):
        self.logger = logging.getLogger(__name__)

        self.conf = SafeConfigParser()
        if self.conf.read(CONFIG_FILE):
            self.logger.debug("Using config file at {0}".format(CONFIG_FILE))
        else:
            self.logger.warning(
                "Config file {0} not found".format(CONFIG_FILE))

    def get_valid_value(self, direct_value, config_section,
                        config_option_name, default_value):
        """Returns a value that can be used as a parameter in client or
        server. If a direct_value is given, that value will be returned
        instead of the value from the config file. If the appropriate config
        file option is not found, the default_value is returned.

        :param direct_value: represents a direct value that should be used.
                             supercedes values from config files
        :param config_section: which section of the config file to use
        :param config_option_name: name of config option value
        :param default_value: default value to be used if other options not
                              found
        :returns: a value that can be used as a parameter
        """
        ARG_MSG = "Using given value '{0}' for {1}"
        CONF_MSG = "Using value '{0}' from configuration file {1} for {2}"
        DEFAULT_MSG = "Using default value '{0}' for {1}"
        if direct_value:
            return_value = direct_value
            self.logger.debug(ARG_MSG.format(direct_value, config_option_name))
        else:
            try:
                return_value = self.conf.get(config_section,
                                             config_option_name)
                self.logger.debug(CONF_MSG.format(return_value,
                                                  CONFIG_FILE,
                                                  config_option_name))
            except:
                return_value = default_value
                self.logger.debug(DEFAULT_MSG.format(default_value,
                                                     config_option_name))
        if return_value == self.NONE_VALUE:
            return None
        else:
            return return_value
Пример #7
0
 def read(cfgfile):
     if not os.path.exists(cfgfile):
         ex = IOError if six.PY2 else FileNotFoundError
         raise ex('File {name} does not exist.'.format(name=cfgfile))
     data = SafeConfigParser()
     data.read(cfgfile)
     return data
Пример #8
0
 def from_sections(cls, sections):
     config = SafeConfigParser()
     for section, keyvalues in sections.items():
         config.add_section(section)
         for key, value in keyvalues.items():
             config.set(section, key, value)
     return cls(config)
Пример #9
0
def load_from_config(config_file, args, values):
    """
    Load config from user's home folder, and load into config object. If values are given during runtime that conflict
    with values in config file, the config file values are overwritten.

    :param config_file: Name of an existing config file
    :param args: Array of values containing argument names from main
    :param values: Array of values containing values from arguments from main
    :return: key/value pairs of args/values
    """
    # TODO: Handle args not existing in user config file and passed args
    config = dict()
    if config_file:
        load_config = os.path.join(BASE_CONFIG_DIR, config_file)
        if os.path.exists(load_config):
            parser = SafeConfigParser()
            parser.read(load_config)
            for _var in CONFIG_VARS:
                config[_var['var']] = parser.get('settings', _var['var'])

    items = values.items()
    for k, v in items:
        if k in args and v is not None:
            config[k] = v

    for _var in CONFIG_VARS:
        if _var['var'] in args and values[_var['var']] is not None:
            config[_var['var']] = values[_var['var']]
        if _var["var"] not in config:
            click.echo(_var['error'])

    return namedtuple('GenericDict', config.keys())(**config)
Пример #10
0
    def read(self):
        """Override base class version to avoid parsing error with the first
        'RANDFILE = ...' part of the openssl file.  Also, reformat _sections
        to allow for the style of SSL config files where section headings can
        have spaces either side of the brackets e.g.
        [ sectionName ]

        and comments can occur on the same line as an option e.g.
        option = blah # This is option blah

        Reformat _sections to """
        try:
            config_file = open(self._filePath)
            fileTxt = config_file.read()
        except Exception as e:
            raise OpenSSLConfigError('Reading OpenSSL config file "%s": %s' %
                                                    (self._filePath, e))

        idx = re.search('\[\s*\w*\s*\]', fileTxt).span()[0]
        config_file.seek(idx)
        SafeConfigParser.readfp(self, config_file)

        # Filter section names and remove comments from options
        for section, val in list(self._sections.items()):
            newSection = section
            self._sections[newSection.strip()] = \
                                    dict([(opt, self._filtOptVal(optVal))
                                          for opt, optVal in list(val.items())])
            del self._sections[section]

        self._set_required_dn_params()
Пример #11
0
def load(f):
    p = SafeConfigParser()
    p.read(f)
    if not p.has_section('oauth2'):
        p.add_section('oauth2')
    if not p.has_section('oauth2-state'):
        p.add_section('oauth2-state')
    return p
Пример #12
0
    def __init__(self):
        self.logger = logging.getLogger(__name__)

        self.conf = SafeConfigParser()
        if self.conf.read(CONFIG_FILE[1]):
            self.logger.debug("Using config file at {0}".format(CONFIG_FILE))
        else:
            self.logger.warning(
                "Config file {0} not found".format(CONFIG_FILE))
Пример #13
0
def get_config(use_closest=True):
    """Get Scrapy config file as a SafeConfigParser"""
    # 获取资源
    sources = get_sources(use_closest)

    # 解析配置
    cfg = SafeConfigParser()
    cfg.read(sources)
    return cfg
Пример #14
0
 def __init__(self):
     # noinspection PyUnresolvedReferences
     from six.moves.configparser import SafeConfigParser
     self.package_cfg = os.path.expanduser('~/Documents/site-packages/.pypi_packages')
     if not os.path.isfile(self.package_cfg):
         print('Creating package file')
         with open(self.package_cfg, 'w') as outs:
             outs.close()
     self.parser = SafeConfigParser()
     self.parser.read(self.package_cfg)
Пример #15
0
    def _init_config_parser(self, sections=None):
        parser = SafeConfigParser(allow_no_value=True)
        if sections:
            for section in sections:
                parser.add_section(section)
                for key, value in sections[section].items():
                    parser.set(section, key,
                               self._value_converter.to_strings(value))

        return parser
Пример #16
0
    def __init__(self,
                 client_id,
                 client_secret,
                 access_token=None,
                 refresh_token=None):
        self.client_id = client_id
        self.client_secret = client_secret
        self.access_token = access_token
        self.refresh_token = refresh_token
        self.expires_in = None

        parser = SafeConfigParser()
        parser.read(os.path.dirname(os.path.abspath(__file__)) + '/config.ini')

        self._requests = requests.Session()
        try:
            self.SSL_VERSION = parser.get('config', 'ssl_version')
            self._requests.mount(
                'https://',
                SSLAdapter(ssl_version=getattr(ssl, self.SSL_VERSION)))
        except:
            self._requests = requests

        self.API_ROOT_URL = parser.get('config', 'api_root_url')
        self.SDK_VERSION = parser.get('config', 'sdk_version')
        self.AUTH_URL = parser.get('config', 'auth_url')
        self.OAUTH_URL = parser.get('config', 'oauth_url')
Пример #17
0
class Config(object):

    def __init__(self):
        self.config = SafeConfigParser()
        self.name = ''

    def parse(self, fname, override):
        if override:
            override = [x for x in csv.reader(
                ' '.join(override).split(','), delimiter='.')]

        logger.info('Reading configuration file: {}'.format(fname))
        if not os.path.isfile(fname):
            logger.interrupt('File doesn\'t exist: {}'.format(fname))
        self.config.optionxform = str
        self.config.read(fname)
        for section, option, value in override:
            if not self.config.has_section(section):
                self.config.add_section(section)
            self.config.set(section, option, value)

        basename = os.path.basename(fname)
        self.name = os.path.splitext(basename)[0]

    @safe
    def _get_options_as_dict(self, section):
        if section in self.config.sections():
            return {p: v for p, v in self.config.items(section)}
        else:
            return {}
Пример #18
0
def getManifest(fp, format, defaults=None):
    """Read the manifest from the given open file pointer according to the
    given ManifestFormat. Pass a dict as ``defaults`` to override the defaults
    from the manifest format.
    """

    if defaults is None:
        defaults = format.defaults

    parser = SafeConfigParser()
    if six.PY2:
        parser.readfp(fp)
    else:
        data = fp.read()
        if isinstance(data, six.binary_type):
            data = data.decode()
        parser.read_string(data)

    results = {}
    for key in format.keys:
        if parser.has_option(format.resourceType, key):
            results[key] = parser.get(format.resourceType, key)
        else:
            results[key] = defaults.get(key, None)

    for key in format.parameterSections:
        sectionName = "%s:%s" % (format.resourceType, key,)
        if parser.has_section(sectionName):
            results[key] = dict(parser.items(sectionName))
        else:
            results[key] = {}

    return results
Пример #19
0
    def save(self):
        """Save the modules to @_path. If @modules is an empty
        dict, then @_path should be removed.
        """
        root_logger.debug("Saving StateFile to '%s'", self._path)

        for module in list(self.modules):
            if len(self.modules[module]) == 0:
                del self.modules[module]

        if len(self.modules) == 0:
            root_logger.debug("  -> no modules, removing file")
            if os.path.exists(self._path):
                os.remove(self._path)
            return

        p = SafeConfigParser()
        p.optionxform = str

        for module in self.modules:
            p.add_section(module)
            for (key, value) in self.modules[module].items():
                p.set(module, key, str(value))

        with open(self._path, "w") as f:
            p.write(f)
Пример #20
0
class Config(object):
    """A ConfigParser wrapper to support defaults when calling instance
    methods, and also tied to a single section"""

    SECTION = 'scrapydartx'

    def __init__(self, values=None, extra_sources=()):
        if values is None:
            sources = self._getsources()
            default_config = get_data(__package__,
                                      'default_scrapyd.conf').decode('utf8')
            self.cp = SafeConfigParser()
            self.cp.readfp(StringIO(default_config))
            self.cp.read(sources)
            for fp in extra_sources:
                self.cp.readfp(fp)
        else:
            self.cp = SafeConfigParser(values)
            self.cp.add_section(self.SECTION)

    def _getsources(self):
        sources = [
            '/etc/scrapydartx/scrapydartx.conf', r'c:\scrapyd\scrapyd.conf'
        ]
        sources += sorted(glob.glob('/etc/scrapydartx/conf.d/*'))
        sources += ['scrapydartx.conf']
        sources += [expanduser('~/.scrapydartx.conf')]
        scrapy_cfg = closest_scrapy_cfg()
        if scrapy_cfg:
            sources.append(scrapy_cfg)
        return sources

    def _getany(self, method, option, default):
        try:
            return method(self.SECTION, option)
        except (NoSectionError, NoOptionError):
            if default is not None:
                return default
            raise

    def get(self, option, default=None):
        return self._getany(self.cp.get, option, default)

    def getint(self, option, default=None):
        return self._getany(self.cp.getint, option, default)

    def getfloat(self, option, default=None):
        return self._getany(self.cp.getfloat, option, default)

    def getboolean(self, option, default=None):
        return self._getany(self.cp.getboolean, option, default)

    def items(self, section, default=None):
        try:
            return self.cp.items(section)
        except (NoSectionError, NoOptionError):
            if default is not None:
                return default
            raise
Пример #21
0
    def __init__(self):
        """
        DEFAULT VALUES
        """
        self._basescript = None
        self.recentvaults = []
        self.pwlength = 10
        self.search_notes = False
        self.search_passwd = False
        self.use_pwgen = False
        # self.alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
        self.alphabet = "abcdefghikmnopqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ_"
        self.avoid_bigrams = "cl mn nm nn rn vv VV"

        self._fname = self.get_config_filename()
        print("Using config %s" % self._fname)
        self._parser = SafeConfigParser()

        if os.path.exists(self._fname):
            self._parser.read(self._fname)

        if not self._parser.has_section("base"):
            self._parser.add_section("base")

        for num in range(10):
            if (not self._parser.has_option("base",
                                            "recentvaults" + str(num))):
                break
            self.recentvaults.append(
                self._parser.get("base", "recentvaults" + str(num)))

        if self._parser.has_option("base", "pwlength"):
            self.pwlength = int(self._parser.get("base", "pwlength"))

        if self._parser.has_option("base", "search_notes"):
            if self._parser.get("base", "search_notes") == "True":
                self.search_notes = True

        if self._parser.has_option("base", "search_passwd"):
            if self._parser.get("base", "search_passwd") == "True":
                self.search_passwd = True

        if self._parser.has_option("base", "use_pwgen"):
            if self._parser.get("base", "use_pwgen") == "True":
                self.use_pwgen = True

        if self._parser.has_option("base", "alphabet"):
            self.alphabet = self._parser.get("base", "alphabet")

        if self._parser.has_option("base", "avoid_bigrams"):
            self.avoid_bigrams = self._parser.get("base", "avoid_bigrams")

        if not os.path.exists(self._fname):
            self.save()
Пример #22
0
def load(f):
    p = SafeConfigParser()
    p.read(f)
    if not p.has_section('oauth2'):
        p.add_section('oauth2')
    if not p.has_section('oauth2-state'):
        p.add_section('oauth2-state')
    return p
Пример #23
0
def read_config_file():
    if not os.path.isfile(config_file_path):
        download_config_file()

    config = SafeConfigParser()
    config.optionxform = str
    list_of_successfully_parsed_files = config.read(config_file_path)
    if config_file_path not in list_of_successfully_parsed_files:
        raise Exception(
            'Could not read {0} succesfully.'.format(config_file_path)
        )
    return config
Пример #24
0
def init_ini_file(file=args.ini_file):
    cp=SafeConfigParser()
    fp=open(file)
    cp.optionxform = str
    cp.readfp(fp)
    fp.close()

    cp.set('condor','lalsuite-install',lalinf_prefix)
    cp.set('analysis','engine',args.engine)
    cp.remove_option('analysis','nparallel')

    return cp
Пример #25
0
 def __init__(self, values=None, extra_sources=()):
     if values is None:
         sources = self._getsources()
         default_config = get_data(__package__, 'default_scrapyd.conf').decode('utf8')
         self.cp = SafeConfigParser()
         self.cp.readfp(StringIO(default_config))
         self.cp.read(sources)
         for fp in extra_sources:
             self.cp.readfp(fp)
     else:
         self.cp = SafeConfigParser(values)
         self.cp.add_section(self.SECTION)
Пример #26
0
 def logging_config(self, name):
     if name != 'main':
         raise KeyError
     parser = SafeConfigParser()
     parser.read(self.path)
     for section_name in ('loggers', 'handlers', 'formatters'):
         if not parser.has_section(section_name):
             raise KeyError
     loggers = convert_loggers(parser)
     handlers = convert_handlers(parser)
     formatters = convert_formatters(parser)
     return combine(loggers, handlers, formatters)
Пример #27
0
 def __init__(self, values=None, extra_sources=()):
     if values is None:
         sources = self._getsources()
         self.cp = ConfigParser()
         if __package__:
             default_config = ensure_str(get_data(__package__, 'default.conf'))
             self._load_config_file(StringIO(default_config))
         for source in sources:
             if os.path.exists(source):
                 self._load_config_file(open(source))
     else:
         self.cp = SafeConfigParser(values)
         self.cp.add_section(self.SECTION)
Пример #28
0
 def __init__(self, values=None, extra_sources=()):
     if values is None:
         sources = self._getsources()
         default_config = get_data(__package__,
                                   'default_engine.conf').decode('utf8')
         self.cp = SafeConfigParser()
         self.cp.readfp(StringIO(default_config))
         self.cp.read(sources)
         for fp in extra_sources:
             self.cp.readfp(fp)
     else:
         self.cp = SafeConfigParser(values)
         self.cp.add_section(self.SECTION)
Пример #29
0
    def __init__(self, path=None):
        self.logger = logging.getLogger(__name__)

        self.conf = SafeConfigParser()

        filenames = path
        if not path:
            filenames = CONFIG_FILE

        if self.conf.read(filenames):
            self.logger.debug("Using config file at {0}".format(filenames))
        else:
            self.logger.warning("Config file {0} not found".format(filenames))
Пример #30
0
    def setUp(self):
        confdata = six.StringIO("""[broker]
urls = amqps://broker1.example.com:5671 amqps://broker2.example.com:5671
cert = /etc/koji-hub/plugins/client.pem
cacert = /etc/koji-hub/plugins/ca.pem
topic_prefix = koji
connect_timeout = 10
send_timeout = 60
""")
        conf = SafeConfigParser()
        conf.readfp(confdata)
        self.handler = protonmsg.TimeoutHandler(
            'amqps://broker1.example.com:5671', [], conf)
Пример #31
0
def get_config(use_closest=True):
    '''
    sources = ['/etc/scrapy.cfg',
         'c:\\scrapy\\scrapy.cfg',
         'C:\\Users\\Yingchao.wang/.config/scrapy.cfg',
         'C:\\Users\\Yingchao.wang/.scrapy.cfg',
         'C:\\Users\\Yingchao.wang\\Desktop\\product\\ScrapyTest\\scrapy.cfg']
    '''

    sources = get_sources(use_closest)
    cfg = SafeConfigParser()
    cfg.read(sources)
    return cfg
Пример #32
0
class Config(object):
    """A ConfigParser wrapper to support defaults when calling instance
    methods, and also tied to a single section"""

    SECTION = 'scrapyd'

    def __init__(self, values=None, extra_sources=()):
        if values is None:
            sources = self._getsources()
            default_config = get_data(__package__, 'default_scrapyd.conf').decode('utf8')
            self.cp = SafeConfigParser()
            self.cp.readfp(StringIO(default_config))
            self.cp.read(sources)
            for fp in extra_sources:
                self.cp.readfp(fp)
        else:
            self.cp = SafeConfigParser(values)
            self.cp.add_section(self.SECTION)

    def _getsources(self):
        sources = ['/etc/scrapyd/scrapyd.conf', r'c:\scrapyd\scrapyd.conf']
        sources += sorted(glob.glob('/etc/scrapyd/conf.d/*'))
        sources += ['scrapyd.conf']
        sources += [expanduser('~/.scrapyd.conf')]
        scrapy_cfg = closest_scrapy_cfg()
        if scrapy_cfg:
            sources.append(scrapy_cfg)
        return sources

    def _getany(self, method, option, default):
        try:
            return method(self.SECTION, option)
        except (NoSectionError, NoOptionError):
            if default is not None:
                return default
            raise

    def get(self, option, default=None):
        return self._getany(self.cp.get, option, default)

    def getint(self, option, default=None):
        return self._getany(self.cp.getint, option, default)

    def getfloat(self, option, default=None):
        return self._getany(self.cp.getfloat, option, default)

    def getboolean(self, option, default=None):
        return self._getany(self.cp.getboolean, option, default)

    def items(self, section, default=None):
        try:
            return self.cp.items(section)
        except (NoSectionError, NoOptionError):
            if default is not None:
                return default
            raise
Пример #33
0
def get_config(app_dir=None, platform=None):
    """
    Load app configuration and return as config instance.
    """
    config = ConfigParser()
    path = os.environ.get("KNOWHOW_CONF")
    if not path:
        if not app_dir:
            app_dir = get_app_dir(platform=platform)
        assert app_dir
        path = os.path.join(app_dir, "knowhow.ini")
    if os.path.exists(path):
        config.read(path)
    return config
Пример #34
0
class Evaluator(object):

    APP_NAME = 'expression-evaluators'
    _default_dir = 'evaluators'

    def __init__(self, plugin_dir=None):
        self.config = SafeConfigParser()
        config_path = save_config_path(self.APP_NAME)
        self.config_file = os.path.join(config_path, self.APP_NAME + ".conf")
        self.config.read(self.config_file)

        this_dir = os.path.abspath(os.path.dirname(__file__))
        self.plugin_dir = plugin_dir or os.path.join(this_dir,
                                                     self._default_dir)
        places = [
            self.plugin_dir,
        ]
        [
            places.append(os.path.join(path, self.APP_NAME, "evaluators"))
            for path in xdg_data_dirs
        ]

        PluginManagerSingleton.setBehaviour([
            ConfigurablePluginManager,
            VersionedPluginManager,
        ])

        self.manager = PluginManagerSingleton.get()
        self.manager.setConfigParser(self.config, self.write_config)
        self.manager.setPluginInfoExtension("expr-plugin")
        self.manager.setPluginPlaces(places)
        self.manager.collectPlugins()

    def _get_all_evaluators(self):
        return self.manager.getAllPlugins()

    def _get_evaluator(self, name):
        pl = self.manager.getPluginByName(name)
        if not pl:
            raise Exception('No expression evaluator %s' % name)
        return pl.plugin_object

    def write_config(self):
        f = open(self.config_file, "w")
        self.config.write(f)
        f.close()

    def evaluate(self, lang, expression, *args, **kwargs):
        pl = self._get_evaluator(lang)
        return pl.evaluate(expression, *args, **kwargs)
 def get_vrouter_tor_agent_name(self, conf_file):
     tor_agent_name = None
     if conf_file:
         try:
             data = StringIO('\n'.join(line.strip()
                             for line in open(conf_file)))
             Config = SafeConfigParser()
             Config.readfp(data)
         except Exception as err:
             self.msg_log("Error reading file : " + conf_file + " Error : " + str(err),
                          SandeshLevel.SYS_ERR)
             return tor_agent_name
         tor_agent_name = Config.get("DEFAULT", "agent_name")
     return tor_agent_name
Пример #36
0
def get_config(app_dir=None, platform=None):
    """
    Load app configuration and return as config instance.
    """
    config = ConfigParser()
    path = os.environ.get("KNOWHOW_CONF")
    if not path:
        if not app_dir:
            app_dir = get_app_dir(platform=platform)
        assert app_dir
        path = os.path.join(app_dir, "knowhow.ini")
    if os.path.exists(path):
        config.read(path)
    return config
Пример #37
0
def getManifest(fp, format, defaults=None):
    """Read the manifest from the given open file pointer according to the
    given ManifestFormat. Pass a dict as ``defaults`` to override the defaults
    from the manifest format.
    """

    if defaults is None:
        defaults = format.defaults

    parser = SafeConfigParser()
    parser.readfp(fp)

    results = {}
    for key in format.keys:
        if parser.has_option(format.resourceType, key):
            results[key] = parser.get(format.resourceType, key)
        else:
            results[key] = defaults.get(key, None)

    for key in format.parameterSections:
        sectionName = "%s:%s" % (format.resourceType, key,)
        if parser.has_section(sectionName):
            results[key] = dict(parser.items(sectionName))
        else:
            results[key] = {}

    return results
 def parse(self):
     parser = SafeConfigParser()
     parser.readfp(StringIO(self.obj.content))
     for section in parser.values():
         if section.name == 'options':
             options = 'install_requires', 'setup_requires', 'test_require'
             for name in options:
                 content = section.get(name)
                 if not content:
                     continue
                 self._parse_content(content)
         elif section.name == 'options.extras_require':
             for content in section.values():
                 self._parse_content(content)
Пример #39
0
    def save(self):
        """Save the modules to @_path. If @modules is an empty
        dict, then @_path should be removed.
        """
        root_logger.debug("Saving StateFile to '%s'", self._path)

        for module in list(self.modules.keys()):
            if len(self.modules[module]) == 0:
                del self.modules[module]

        if len(self.modules) == 0:
            root_logger.debug("  -> no modules, removing file")
            if os.path.exists(self._path):
                os.remove(self._path)
            return

        p = SafeConfigParser()
        p.optionxform = str

        for module in self.modules.keys():
            p.add_section(module)
            for (key, value) in self.modules[module].items():
                p.set(module, key, str(value))

        with open(self._path, "w") as f:
            p.write(f)
Пример #40
0
def _is_telemetry_enabled(cfg_file):
    config = SafeConfigParser()

    if not config.read(cfg_file):
        return False

    try:
        telemetry_enabled = config.getboolean("build", "telemetry")
    except NoOptionError:
        return False

    if not telemetry_enabled:
        return False

    return True
def find_scrapy_project(project):
    project_config_path = closest_scrapy_cfg()
    if not project_config_path:
        raise RuntimeError('Cannot find scrapy.cfg file')
    project_config = SafeConfigParser()
    project_config.read(project_config_path)
    try:
        project_settings = project_config.get('settings', project)
    except (NoSectionError, NoOptionError) as e:
        raise RuntimeError(e.message)
    if not project_settings:
        raise RuntimeError('Cannot find scrapy project settings')
    project_location = os.path.dirname(project_config_path)
    sys.path.append(project_location)
    return project_settings
Пример #42
0
def load_config(config_files=(), overrides=()):
    cp = SafeConfigParser()
    cp.optionxform = str  # make parsing case-sensitive
    cp.read(FILENAMES + config_files)

    for section, option, value in overrides:
        if value is not None:
            if isinstance(value, bool):
                value = int(value)
            cp.set(section, option, str(value))

    return {section: dict(cp.items(section)) for section in cp.sections()}
Пример #43
0
    def _update_gridinit_rawx(self, port):
        grid = SafeConfigParser()
        grid.read(GRID_CONF)

        section = "Service.%s-%s" % (self.ns, self.name)
        val = grid.get(section, "Group").split(",")

        val[3] = val[3].split(':')[0] + ':' + str(port)
        grid.set(section, "Group", ",".join(val))

        with open(GRID_CONF, "w") as fp:
            grid.write(fp)
Пример #44
0
def get_config(use_closest=True):
    """Get Scrapy config file as a SafeConfigParser"""

    ## 获取 Scrapy 配置文件可能的路径列表: ['/etc/scrapy.cfg', ...]
    sources = get_sources(use_closest)
    ## 实例化一个配置解析器
    ## 该模块是用来解析配置文件的,配置文件中的内容可以包含一个或多个节(section)
    ## 若传入的参数是多个配置文件的列表,会从左往右依次读取,后面的值会覆盖前面的值
    ## 每个节可以有多个参数(键=值)或(键:值)
    ## 使用配置文件的好处是使程序更加灵活,将变化抽离出来,单独管理
    ## Python 3.2 之后,该模块被更名为 ConfigParser
    cfg = SafeConfigParser()
    ## 读取 sources 列表中的所有配置文件(以 .cfg 结尾),将文件中的配置按一定的
    ## 格式存储到配置解析器实例 cfg 的相关属性中
    cfg.read(sources)
    return cfg
Пример #45
0
    def __init__(self, *args, **kwargs):

        self.fpath = os.path.expanduser(self.strip_kwarg(kwargs, 'fpath'))
        self.section = self.strip_kwarg(kwargs, 'section')
        initvals = self.strip_kwarg(kwargs, 'initvals')
        self.header = self.strip_kwarg(kwargs, 'header')

        SafeConfigParser.__init__(self, *args, **kwargs)

        self.add_section(self.section)

        for option in initvals:
            self.set(self.section, option, initvals[option])

        self.read(self.fpath)
        self.save()
Пример #46
0
 def __init__(self, values=None, extra_sources=()):
     if values is None:
         sources = self._getsources()
         default_config = get_data(__package__, 'default_scrapyd.conf').decode('utf8')
         self.cp = SafeConfigParser()
         self.cp.readfp(io.StringIO(default_config))
         sources.extend(extra_sources)
         for fname in sources:
             try:
                 with io.open(fname) as fp:
                     self.cp.readfp(fp)
             except (IOError, OSError):
                 pass
     else:
         self.cp = SafeConfigParser(values)
         self.cp.add_section(self.SECTION)
Пример #47
0
    def setUp(self):
        super(FunctionalTestBase, self).setUp()
        if not os.path.exists(TEST_CFG):
            raise Exception("Unable to run the write tests without a test.ini in that defines an access_token with write privs.")

        cfg = SafeConfigParser()
        with open(TEST_CFG) as fp:
            cfg.readfp(fp, 'test.ini')
            access_token = cfg.get('write_tests', 'access_token')
            try:
                activity_id = cfg.get('activity_tests', 'activity_id')
            except NoOptionError:
                activity_id = None

        self.client = Client(access_token=access_token)
        self.activity_id = activity_id
Пример #48
0
    def __init__(self, *args, **kwargs):

        self.fpath = os.path.expanduser(self.strip_kwarg(kwargs, 'fpath'))
        self.section = self.strip_kwarg(kwargs, 'section')
        initvals = self.strip_kwarg(kwargs, 'initvals')
        self.header = self.strip_kwarg(kwargs, 'header')

        SafeConfigParser.__init__(self, *args, **kwargs)

        self.add_section(self.section)

        for option in initvals:
            self.set(self.section, option, initvals[option])

        self.read(self.fpath)
        self.save()
Пример #49
0
def read(config_path):
    config_path = os.path.abspath(config_path)
    config_root = os.path.split(config_path)[0]
    parser = SafeConfigParser()
    success = parser.read(config_path)
    assert config_path in success, success

    subns = {"pwd": os.path.abspath(os.path.curdir)}

    rv = OrderedDict()
    for section in parser.sections():
        rv[section] = ConfigDict(config_root)
        for key in parser.options(section):
            rv[section][key] = parser.get(section, key, False, subns)

    return rv
Пример #50
0
class Evaluator(object):

    APP_NAME = 'expression-evaluators'
    _default_dir = 'evaluators'

    def __init__(self, plugin_dir=None):
        self.config = SafeConfigParser()
        config_path = save_config_path(self.APP_NAME)
        self.config_file = os.path.join(config_path, self.APP_NAME + ".conf")
        self.config.read(self.config_file)

        this_dir = os.path.abspath(os.path.dirname(__file__))
        self.plugin_dir = plugin_dir or os.path.join(
            this_dir, self._default_dir)
        places = [self.plugin_dir, ]
        [places.append(os.path.join(path, self.APP_NAME, "evaluators")) for
         path in xdg_data_dirs]

        PluginManagerSingleton.setBehaviour([
            ConfigurablePluginManager,
            VersionedPluginManager,
        ])

        self.manager = PluginManagerSingleton.get()
        self.manager.setConfigParser(self.config, self.write_config)
        self.manager.setPluginInfoExtension("expr-plugin")
        self.manager.setPluginPlaces(places)
        self.manager.collectPlugins()

    def _get_all_evaluators(self):
        return self.manager.getAllPlugins()

    def _get_evaluator(self, name):
        pl = self.manager.getPluginByName(name)
        if not pl:
            raise Exception('No expression evaluator %s' % name)
        return pl.plugin_object

    def write_config(self):
        f = open(self.config_file, "w")
        self.config.write(f)
        f.close()

    def evaluate(self, lang, expression, *args, **kwargs):
        pl = self._get_evaluator(lang)
        return pl.evaluate(expression, *args, **kwargs)
Пример #51
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        config_file = os.path.join(os.environ["HOME"], ".zuliprc")
        if not os.path.exists(config_file):
            raise RuntimeError("No ~/.zuliprc found")
        config = SafeConfigParser()
        with open(config_file, 'r') as f:
            config.readfp(f, config_file)
        api_key = config.get("api", "key")
        email = config.get("api", "email")

        try:
            user_profile = get_user_profile_by_email(email)
            user_profile.api_key = api_key
            user_profile.save(update_fields=["api_key"])
        except UserProfile.DoesNotExist:
            print("User %s does not exist; not syncing API key" % (email,))
Пример #52
0
    def __init__(self):
        here = os.path.dirname(__file__)
        self.filename = os.path.join(here, self._filename)

        parser = SafeConfigParser()
        parser.getlist = lambda s, o: parser.get(s, o).split()
        parser.getlines = lambda s, o: [l.strip() for l in parser.get(s, o).splitlines() if l.strip()]
        found = parser.read(self.filename)
        if not found:
            raise RuntimeError('failed to read app config %r' % self.filename)

        getters = {}
        for attr, options in self._getters.items():
            getters.update(dict.fromkeys(options, getattr(parser, attr)))

        def items(section):
            for o in parser.options(section):
                yield o, getters.get(o, parser.get)(section, o)

        kwargs = [dict([('name', section)] + list(items(section)))
            for section in parser.sections()]
        apps = [App(**kw) for kw in kwargs]

        # some consistency checks: names and ports must be unique to make it
        # possible to deploy each app on each server.
        names = [app.name for app in apps]
        ports = [app.port for app in apps]
        assert len(names) == len(set(names))
        assert len(ports) == len(set(ports))

        super(Config, self).__init__((app.name, app) for app in apps)
Пример #53
0
    def __init__(self):
        self.logger = logging.getLogger(__name__)

        self.conf = SafeConfigParser()
        if self.conf.read(CONFIG_FILE):
            self.logger.debug("Using config file at {0}".format(CONFIG_FILE))
        else:
            self.logger.warning(
                "Config file {0} not found".format(CONFIG_FILE))
Пример #54
0
def start_master(options, plugins):
    cfgparser = SafeConfigParser()
    cfgparser.read(options.config)
    cfg = Configuration(cfgparser)

    pidfile = None
    user, group, umask = None, None, None

    def process_init():
        system.drop_privileges(user, group, umask)
        time.sleep(0.1)

    master = WorkerMaster(process_callback=process_init)

    cfg.configure_logging()
    cfg.configure_amqp()
    cfg.configure_mysql()

    for plugin in plugins:
        for entry_point in iter_entry_points('provoke.workers', plugin):
            logger.debug('Loading plugin: name=%s', entry_point.name)
            register = entry_point.load()
            register(master, cfgparser)
            break
        else:
            raise BadPlugin(plugin)

    for res, limits in cfg.get_rlimits():
        resource.setrlimit(res, limits)

    if options.daemon:
        logger.debug('Daemonizing master process')
        pidfile = cfg.get_pidfile()
        stdout, stderr, stdin = cfg.get_stdio_redirects()
        user, group, umask = cfg.get_worker_privileges()
        if stdout or stderr or stdin:
            system.redirect_stdio(stdout, stderr, stdin)
        system.daemonize()
    with system.PidFile(pidfile):
        handle_signals()
        try:
            master.run()
        finally:
            master.wait()
Пример #55
0
def parse_config_file(filename):
    """Parse INI files containing IMAP connection details.

    Used by livetest.py and interact.py
    """

    parser = SafeConfigParser(get_string_config_defaults())
    with open(filename, 'r') as fh:
        parser.readfp(fh)

    conf = _read_config_section(parser, "DEFAULT")
    if conf.expect_failure:
        raise ValueError("expect_failure should not be set for the DEFAULT section")

    conf.alternates = {}
    for section in parser.sections():
        conf.alternates[section] = _read_config_section(parser, section)

    return conf
Пример #56
0
    def __init__(self, filePath=None, caDir=None):
        """Initial OpenSSL configuration optionally setting a file path to
        read from

        @type filePath: string
        @param filePath: path to OpenSSL configuration file

        @type caDir: string
        @param caDir: directory for SimpleCA.  This is substituted for $dir
        in OpenSSL config file where present.  caDir can be left out in
        which case the substitution is not done"""

        SafeConfigParser.__init__(self)

        self._reqDN = None
        self._setFilePath(filePath)

        # Set-up CA directory
        self.setCADir(caDir)
Пример #57
0
 def __init__(self):
     # noinspection PyUnresolvedReferences
     from six.moves.configparser import SafeConfigParser
     self.package_cfg = os.path.expanduser('~/Documents/site-packages/.pypi_packages')
     if not os.path.isfile(self.package_cfg):
         print('Creating package file')
         with open(self.package_cfg, 'w') as outs:
             outs.close()
     self.parser = SafeConfigParser()
     self.parser.read(self.package_cfg)
Пример #58
0
def _update_settings_from_file(section, settings):
    tries = 0
    current_directory = os.path.normpath(os.getcwd())
    config_file = None
    while current_directory and tries < MAX_CONFIG_SEARCH_DEPTH:
        potential_path = os.path.join(current_directory, 'setup.cfg')
        if os.path.exists(potential_path):
            config_file = potential_path
            break

        new_directory = os.path.split(current_directory)[0]
        if current_directory == new_directory:
            break
        current_directory = new_directory
        tries += 1

    if config_file and os.path.exists(config_file):
        with open(config_file, 'rU') as fp:
            config = SafeConfigParser()
            config.readfp(fp)
        if config.has_section('tool:multilint'):
            settings.update(sanitize(config.items('tool:multilint')))