def build_mysql_config(mysql_config): """Given a standard Holland [mysql:client] section build an in-memory config that represents the auth parameters, including those merged in from *defaults-extra-files* :param mysql_config: required. This should be a dict object with the zero or more of the following keys: user (string) password (string) host (string) socket (string) port (integer) defaults-extra-file (list) :type mysql_config: dict """ defaults_config = ConfigObj() defaults_config['client'] = {} for config in mysql_config['defaults-extra-file']: LOG.info("Loading %s [%s]", config, os.path.expanduser(config)) _my_config = load_options(config) defaults_config.update(_my_config) for key in ('user', 'password', 'socket', 'host', 'port'): if key in mysql_config and mysql_config[key]: defaults_config['client'][key] = mysql_config[key] return defaults_config
def write_options(config, filename): quoted_config = ConfigObj(list_values=False) quoted_config.update(config) if isinstance(filename, basestring): filename = codecs.open(filename, 'w', 'utf8') quoted_config.write(filename) filename.close()
def load_options(path): """Load mysql option file from filename""" path = os.path.abspath(os.path.expanduser(path)) cfg = ConfigObj(list_values=False) cfg.filename = path try: cfg.reload() except ConfigObjError, exc: LOG.debug("Skipping unparsable lines") for _exc in exc.errors: LOG.debug("Ignored line %d: %s", _exc.lineno, _exc.line.rstrip())
def merge_options(path, *defaults_files, **kwargs): defaults_config = ConfigObj(list_values=False) defaults_config['client'] = {} for config in defaults_files: _my_config = load_options(config) defaults_config.update(_my_config) for key in ('user', 'password', 'socket', 'host', 'port'): if kwargs.get(key) is not None: defaults_config['client'][key] = kwargs[key] write_options(defaults_config, path)
def client_keys(config): """Create a copy of option_section with non-authentication options stripped out. Authentication options supported are: user, password, host, port, and socket """ clean_namespace = ConfigObj(list_values=False) clean_namespace.update(config) valid_keys = ['user', 'password', 'host', 'port', 'socket'] for key in config: if key not in valid_keys: del clean_namespace[key] else: clean_namespace[key] = unquote(config[key]) return clean_namespace