def get_config_param(param): """ Parse config file and find source dir in it """ curdir = os.curdir if '__file__' in globals(): curdir = os.path.dirname(os.path.abspath(__file__)) config = None for loc in curdir, os.curdir, os.path.expanduser('~'): try: with open(os.path.join(loc, 'precommit1c.ini')) as source: if sys.version_info < (3, 0, 0): from ConfigParser import ConfigParser # @NoMove @UnusedImport else: from configparser import ConfigParser config = ConfigParser() config.read_file(source) break except IOError: pass if config is not None and config.has_option('default', param): value = config.get('default', param) return value return None
def load_conf(path=None, file=None): """Load configuration in global var CONF :param path: The path to the configuration file :param file: If provided read instead the file like object """ conf = ConfigParser() if file: try: conf.read_file(file, path) except AttributeError: # read_file only exists in Python3 conf.readfp(file) return conf confpath = None if not path: try: confpath = os.environ['DULWICH_SWIFT_CFG'] except KeyError: raise Exception("You need to specify a configuration file") else: confpath = path if not os.path.isfile(confpath): raise Exception("Unable to read configuration file %s" % confpath) conf.read(confpath) return conf
def get_config_param(param): """ Parse config file and find source dir in it """ curdir = os.curdir if '__file__' in globals(): curdir = os.path.dirname(os.path.abspath(__file__)) config = None for loc in curdir, os.curdir, os.path.expanduser('~'): try: with open(os.path.join(loc, 'precommit1c.ini')) as source: if sys.version_info < (3, 0, 0): from ConfigParser import ConfigParser # @NoMove @UnusedImport else: from configparser import ConfigParser config = ConfigParser() config.read_file(source) break except IOError: pass if config is not None and config.has_option('default', param): value = config.get('default', param) return value return None
def get_config_param(param): ''' parse config file and find in them source dir ''' curdir = os.curdir if '__file__' in globals(): curdir = os.path.dirname(os.path.abspath(__file__)) config = None for loc in curdir, os.curdir, os.path.expanduser("~"): try: with open(os.path.join(loc, "precommit1c.ini")) as source: if sys.version_info < (3, 0, 0): from ConfigParser import ConfigParser else: from configparser import ConfigParser config = ConfigParser() config.read_file(source) break except IOError: pass if not config is None and config.has_option("DEFAULT", param): value = config.get("DEFAULT", param) return value return None
def load_conf(path=None, file=None): """Load configuration in global var CONF :param path: The path to the configuration file :param file: If provided read instead the file like object """ conf = ConfigParser() if file: try: conf.read_file(file, path) except AttributeError: # read_file only exists in Python3 conf.readfp(file) return conf confpath = None if not path: try: confpath = os.environ['DULWICH_SWIFT_CFG'] except KeyError: raise Exception("You need to specify a configuration file") else: confpath = path if not os.path.isfile(confpath): raise Exception("Unable to read configuration file %s" % confpath) conf.read(confpath) return conf
def load_ini(stream): if 'ExtendedInterpolation' in globals(): parser = ConfigParser(interpolation=ExtendedInterpolation()) else: parser = ConfigParser() parser.read_file(stream) return { section: OrderedDict(parser.items(section)) for section in parser.sections() }
def values_from_file(docopt_dict, config_option, settable, booleans, repeatable): """Parse config file and read settable values. Can be overridden by both command line arguments and environment variables. :raise DocoptcfgError: If `config_option` isn't found in docstring. :raise DocoptcfgFileError: On any error while trying to read and parse config file. :param dict docopt_dict: Dictionary from docopt with environment variable defaults merged in by docoptcfg(). :param str config_option: Config option long name with file path as its value. :param iter settable: Option long names available to set by config file. :param iter booleans: Option long names of boolean/flag types. :param iter repeatable: Option long names of repeatable options. :return: Settable values. :rtype: dict """ section = docopt.DocoptExit.usage.split()[1] settable = set(o for o in settable if o != config_option) config = ConfigParser() defaults = dict() # Sanity checks. if config_option not in docopt_dict: raise DocoptcfgError if docopt_dict[config_option] is None or not settable: return defaults # Read config file. path = DocoptcfgFileError.FILE_PATH = docopt_dict[config_option] try: with open(path) as handle: if hasattr(config, 'read_file'): config.read_file(handle) else: getattr(config, 'readfp')(handle) except Error as exc: raise DocoptcfgFileError('Unable to parse config file.', str(exc)) except IOError as exc: raise DocoptcfgFileError('Unable to read config file.', str(exc)) # Make sure section is in config file. if not config.has_section(section): raise DocoptcfgFileError( 'Section [{0}] not in config file.'.format(section)) # Parse config file. for key in settable: if config.has_option(section, key[2:]): defaults[key] = get_opt(key, config, section, booleans, repeatable) return defaults
def values_from_file(docopt_dict, config_option, settable, booleans, repeatable): """Parse config file and read settable values. Can be overridden by both command line arguments and environment variables. :raise DocoptcfgError: If `config_option` isn't found in docstring. :raise DocoptcfgFileError: On any error while trying to read and parse config file. :param dict docopt_dict: Dictionary from docopt with environment variable defaults merged in by docoptcfg(). :param str config_option: Config option long name with file path as its value. :param iter settable: Option long names available to set by config file. :param iter booleans: Option long names of boolean/flag types. :param iter repeatable: Option long names of repeatable options. :return: Settable values. :rtype: dict """ section = docopt.DocoptExit.usage.split()[1] settable = set(o for o in settable if o != config_option) config = ConfigParser() defaults = dict() # Sanity checks. if config_option not in docopt_dict: raise DocoptcfgError if docopt_dict[config_option] is None or not settable: return defaults # Read config file. path = DocoptcfgFileError.FILE_PATH = docopt_dict[config_option] try: with open(path) as handle: if hasattr(config, 'read_file'): config.read_file(handle) else: getattr(config, 'readfp')(handle) except Error as exc: raise DocoptcfgFileError('Unable to parse config file.', str(exc)) except IOError as exc: raise DocoptcfgFileError('Unable to read config file.', str(exc)) # Make sure section is in config file. if not config.has_section(section): raise DocoptcfgFileError('Section [{0}] not in config file.'.format(section)) # Parse config file. for key in settable: if config.has_option(section, key[2:]): defaults[key] = get_opt(key, config, section, booleans, repeatable) return defaults
def from_file(cls, in_file): if not hasattr(in_file, 'read'): in_file = open(in_file, 'r') config_parser = ConfigParser() try: # Python 3 config_parser.read_file(in_file) except AttributeError: # Python 2 config_parser.readfp(in_file) return ToxConfig(envlist=cls._get_envlist_from_tox(in_file.name), commands=config_parser.get('testenv', 'commands'))
def ssid_password(source='/etc/NetworkConnections/system-connections', ext=''): if isinstance(source, ConfigParser): ssid = source.get('wifi', 'ssid') if source.has_option('wifi', 'ssid') else None psk = source.get('wifi-security', 'psk') if source.has_option('wifi-security', 'psk') else None return (ssid or os.path.basename(source), psk or '') elif os.path.isdir(source): return dict([ssid_password(meta['path']) for meta in find_files(source, ext=ext)]) elif os.path.isfile(source) or callable(getattr(source, 'read', None)): config = ConfigParser() if hasattr(source, 'read'): config.read_file(source) else: config.read(source) return ssid_password(config) elif isinstance(source, basestring): return ssid_password(StringIO(source))
def get_config(cls, arguments): config_file = arguments.config if not os.path.isfile(config_file): raise ConfigurationError('configuration file not found: %s' % config_file) cls.logger.debug('using config file: %s' % config_file) config_parser = ConfigParser() with codecs.open(config_file, "r", "utf8") as config_fp: if hasattr(config_parser, 'read_file'): config_parser.read_file(config_fp) else: config_parser.readfp(config_fp) return Configuration(arguments, config_parser)
def from_file(self, in_file): if not hasattr(in_file, 'read'): in_file = open(in_file, 'r') config_parser = ConfigParser() try: # Python 3 config_parser.read_file(in_file) except AttributeError: # Python 2 config_parser.readfp(in_file) return ToxConfig( envlist=list(map(str.strip, config_parser.get('tox', 'envlist').split(','))), commands=config_parser.get('testenv', 'commands') )
def from_file(self, in_file): if not hasattr(in_file, 'read'): in_file = open(in_file, 'r') config_parser = ConfigParser() try: # Python 3 config_parser.read_file(in_file) except AttributeError: # Python 2 config_parser.readfp(in_file) return ToxConfig(envlist=list( map(str.strip, config_parser.get('tox', 'envlist').split(','))), commands=config_parser.get('testenv', 'commands'))
def set_from_config_file(self, filename): """ Loads lint config from a ini-style config file """ if not os.path.exists(filename): raise LintConfigError(u"Invalid file path: {0}".format(filename)) self._config_path = os.path.realpath(filename) try: parser = ConfigParser() with io.open(filename, encoding=DEFAULT_ENCODING) as config_file: parser.read_file(config_file) for section_name in parser.sections(): for option_name, option_value in parser.items(section_name): self.set_option(section_name, option_name, ustr(option_value)) except ConfigParserError as e: raise LintConfigError(ustr(e))
def from_file(cls, in_file): if not hasattr(in_file, 'read'): in_file = open(in_file, 'r') config_parser = ConfigParser() try: # Python 3 config_parser.read_file(in_file) except AttributeError: # Python 2 config_parser.readfp(in_file) return ToxConfig( envlist=cls._get_envlist_from_tox(in_file.name), commands=config_parser.get('testenv', 'commands') )
def cfgread(config_file): """ ConfigFile reader, register sections to global `CONF` instance. """ cfg = ConfigParser() if not hasattr(cfg, 'read_file'): cfg.read_file = cfg.readfp try: cfp = open(config_file) cfg.read_file(cfp) cfp.close() except: raise ConfigError("cannot open/read configfile, {0}".format(excinst())) for _cs in cfg.sections(): CONF.regisiter_opts(_cs, dict(zip( [ c[0] for c in cfg.items(_cs) ], [ c[1].strip('\'').strip('"') for c in cfg.items(_cs) ]))) return CONF
def get_defined_stops(): app_root = path.split(sys.path[0])[0] backend_root = path.join(app_root, 'backend') cp = ConfigParser() with open(path.join(backend_root, 'stops.ini'), 'r', encoding='cp65001') as config_file: # windows utf-8 if PY2: cp.readfp(config_file) else: cp.read_file(config_file) stops = [] for section_name in cp.sections(): stop_configuration = cp.items(section_name) stop_data = {} for key, value in stop_configuration: if key in ('name', 'pane'): stop_data[key] = value else: stop_data[key] = int(value) stops.append(stop_data) return stops
def from_stream(stream): """Retrieves AWS settings from a stream in INI format. Example: >>> from io import StringIO >>> stream = StringIO(''' ... ... [credentials] ... aws_access_key_id=KEY_ID ... aws_secret_access_key=SECRET ... ... [defaults] ... region=eu-west-1 ... ... ''') >>> settings = from_stream(stream) >>> settings['aws_access_key_id'] == 'KEY_ID' True >>> settings['aws_secret_access_key'] == 'SECRET' True >>> settings['region'] == 'eu-west-1' True >>> stream = StringIO(''' ... ... [credentials] ... aws_access_key_id=KEY_ID ... aws_secret_access_key=SECRET ... ... ''') >>> settings = from_stream(stream) >>> settings['aws_access_key_id'] == 'KEY_ID' True >>> settings['aws_secret_access_key'] == 'SECRET' True :param stream: of chars in INI format. :type stream: stream. :rtype: dict ..note:: some fields may be None. """ config = ConfigParser(allow_no_value=True) if hasattr(config, "read_file"): config.read_file(stream) else: config.readfp(stream) # deprecated name settings = {} if config.has_section("credentials"): settings.update({ "aws_access_key_id": config.get("credentials", "aws_access_key_id"), "aws_secret_access_key": config.get("credentials", "aws_secret_access_key"), }) if config.has_section("defaults"): settings["region"] = config.get("defaults", "region") return settings
class Config: """This class represents the frabit configuration. Default configuration files are /etc/frabit/frabit.conf """ CONFIG_FILES = ['/etc/frabit/frabit.conf'] _QUOTE_RE = re.compile(r"""^(["'])(.*)\1$""") def __init__(self, filename=None): # In Python 3 ConfigParser has changed to be strict by default. self._config = ConfigParser(strict=False) # 如果提供自定义配置文件,优先使用 if filename: if hasattr(filename, 'read'): self._config.read_file(filename) else: # check for the existence of the user defined file if not os.path.exists(filename): sys.exit("Configuration file '{}' does not exist".format(filename)) self._config.read(os.path.expanduser(filename)) # 没有指定配置文件的话,使用默认的配置文件(强烈建议使用默认配置文件) else: # Check for the presence of configuration files inside default directories for path in self.CONFIG_FILES: full_path = os.path.expanduser(path) if os.path.exists(full_path) and full_path in self._config.read(full_path): filename = full_path break else: sys.exit("Could not find any configuration file at default locations.\n" "Check Frabit's documentation for more help.") self.config_file = filename self._servers = None self.servers_msg_list = [] self._parse_global_config() def get(self, section, option, defaults=None, none_value=None): """ Method to get the value from a given section from Frabit configuration """ if not self._config.has_section(section): return None try: value = self._config.get(section, option, raw=False, vars=defaults) if value.lower() == 'none': value = none_value if value is not None: value = self._QUOTE_RE.sub(lambda m: m.group(2), value) return value except NoOptionError: return None def _parse_global_config(self): """ This method parses the global [frabit] section """ self.frabit_home = self.get('frabit', 'frabit_home') self.frabit_lock_directory = self.get('frabit', 'frabit_lock_directory') or self.frabit_home self.user = self.get('frabit', 'frabit_user') or DEFAULT_USER self.log_file = self.get('frabit', 'log_file') self.log_format = self.get('frabit', 'log_format') or DEFAULT_LOG_FORMAT self.log_level = self.get('frabit', 'log_level') or DEFAULT_LOG_LEVEL # save the raw frabit section to be compared later in _is_global_config_changed() method self._global_config = set(self._config.items('frabit')) def _is_global_config_changed(self): """Return true if something has changed in global configuration""" return self._global_config != set(self._config.items('frabit')) def load_configuration_files_directory(self): """ Read the "configuration_files_directory" option and load all the configuration files with the .conf suffix that lie in that folder """ config_files_directory = self.get('frabit', 'configuration_files_directory') if not config_files_directory: return if not os.path.isdir(os.path.expanduser(config_files_directory)): _logger.warn('Ignoring the "configuration_files_directory" option as "{}" is not a directory'.format( config_files_directory)) return for cfile in sorted(iglob(os.path.join(os.path.expanduser(config_files_directory), '*.conf'))): filename = os.path.basename(cfile) if os.path.isfile(cfile): # Load a file _logger.debug('Including configuration file: {}'.format(filename)) self._config.read(cfile) if self._is_global_config_changed(): msg = "The configuration file {} contains a not empty [frabit] section".format(filename) _logger.fatal(msg) raise SystemExit("FATAL: {}".format(msg)) else: # Add an info that a file has been discarded _logger.warn('Discarding configuration file: {} (not a file)'.format(filename)) def _populate_servers(self): """ Populate server list from config file Also check for paths errors in config. If two or more paths overlap in a single server,that server is disabled. If two or more directory paths overlap between different servers an error is raised. """ # Populate servers if self._servers is not None: return self._servers = {} # Cycle all the available configurations sections for section in self._config.sections(): if section == 'frabit': # skip global settings continue # Exit if the section has a reserved name if section in FORBIDDEN_SERVER_NAMES: msg = "The reserved word '{}' is not allowed as server name,Please rename it.".format(section) _logger.fatal(msg) raise SystemExit("FATAL: {}".format(msg)) # Create a ServerConfig object self._servers[section] = ServerConfig(self, section) # Check for conflicting paths in Frabit configuration self._check_conflicting_paths() def _check_conflicting_paths(self): """ Look for conflicting paths intra-server and inter-server """ # All paths in configuration servers_paths = {} # Global errors list self.servers_msg_list = [] # Cycle all the available configurations sections for section in sorted(self._config.sections()): if section == 'frabit': # skip global settings continue # Paths map section_conf = self._servers[section] config_paths = { 'backup_directory': section_conf.backup_directory, 'basebackups_directory': section_conf.basebackups_directory, 'errors_directory': section_conf.errors_directory, 'incoming_wals_directory': section_conf.incoming_wals_directory, 'streaming_wals_directory': section_conf.streaming_wals_directory, 'wals_directory': section_conf.wals_directory } # Check for path errors for label, path in sorted(config_paths.items()): # If the path does not conflict with the others, add it to the paths map real_path = os.path.realpath(path) if real_path not in servers_paths: servers_paths[real_path] = PathConflict(label, section) else: if section == servers_paths[real_path].server: # Internal path error. # Insert the error message into the server.msg_list if real_path == path: self._servers[section].msg_list.append("Conflicting path: {label}={path} conflicts with " "'{real_lable}' for server '{real_path}'".format(label=label, path=path, real_lable=servers_paths[real_path].label, real_server=servers_paths[real_path].server ) ) else: # Symbolic link self._servers[section].msg_list.append( "Conflicting path: {label}={path} (symlink to: {real_path}) " "conflicts with '{real_label}' for server '{real_server}'".format( label=label, path=path, real_path=real_path, real_label=servers_paths[real_path].label, real_server=servers_paths[real_path].server) ) # Disable the server self._servers[section].disabled = True else: # Global path error. # Insert the error message into the global msg_list if real_path == path: self.servers_msg_list.append( "Conflicting path: {label}={path} for server '{section}' conflicts with " "'{real_label}' for server '{real_server}'".format( label=label, path=path, section=section, real_label=servers_paths[real_path].label, real_server=servers_paths[real_path].server) ) else: # Symbolic link self.servers_msg_list.append( "Conflicting path: {label}={path} (symlink to: {real_path}) for server '{section}' " "conflicts with '{real_label}' for server '{real_server}'".format( label=label, path=path, real_path=real_path, section=section, real_label=servers_paths[real_path].label, real_server=servers_paths[real_path].server) ) def server_names(self): """This method returns a list of server names""" self._populate_servers() return self._servers.keys() def servers(self): """This method returns a list of server parameters""" self._populate_servers() return self._servers.values() def get_server(self, name): """ Get the configuration of the specified server :param str name: the server name """ self._populate_servers() return self._servers.get(name, None) def validate_global_config(self): """ Validate global configuration parameters """ # Check for the existence of unexpected parameters in the global section of the configuration file keys = ['frabit_home', 'frabit_lock_directory', 'frabit_user', 'log_file', 'log_level', 'configuration_files_directory'] keys.extend(ServerConfig.KEYS) self._validate_with_keys(self._global_config, keys, 'frabit') def validate_server_config(self, server): """ Validate configuration parameters for a specified server :param str server: the server name """ # Check for the existence of unexpected parameters in the # server section of the configuration file self._validate_with_keys(self._config.items(server), ServerConfig.KEYS, server) @staticmethod def _validate_with_keys(config_items, allowed_keys, section): """ Check every config parameter against a list of allowed keys :param config_items: list of tuples containing provided parameters along with their values :param allowed_keys: list of allowed keys :param section: source section (for error reporting) """ for parameter in config_items: # if the parameter name is not in the list of allowed values, then output a warning name = parameter[0] if name not in allowed_keys: output.warning('Invalid configuration option "{name}" in [{section}] section.'.format(name=name, section=section ))
def from_stream(stream): """Retrieves AWS settings from a stream in INI format. Example: >>> from io import StringIO >>> stream = StringIO(''' ... ... [credentials] ... aws_access_key_id=KEY_ID ... aws_secret_access_key=SECRET ... ... [defaults] ... region=eu-west-1 ... ... ''') >>> settings = from_stream(stream) >>> settings['aws_access_key_id'] == 'KEY_ID' True >>> settings['aws_secret_access_key'] == 'SECRET' True >>> settings['region'] == 'eu-west-1' True >>> stream = StringIO(''' ... ... [credentials] ... aws_access_key_id=KEY_ID ... aws_secret_access_key=SECRET ... ... ''') >>> settings = from_stream(stream) >>> settings['aws_access_key_id'] == 'KEY_ID' True >>> settings['aws_secret_access_key'] == 'SECRET' True :param stream: of chars in INI format. :type stream: stream. :rtype: dict ..note:: some fields may be None. """ config = ConfigParser(allow_no_value=True) if hasattr(config, 'read_file'): config.read_file(stream) else: config.readfp(stream) # deprecated name settings = {} if config.has_section('credentials'): settings.update({ 'aws_access_key_id': config.get('credentials', 'aws_access_key_id'), 'aws_secret_access_key': config.get('credentials', 'aws_secret_access_key') }) if config.has_section('defaults'): settings['region'] = config.get('defaults', 'region') return settings
def configure(self): defaults = {} defaults["message"] = "UNCLASSIFIED" defaults["fgcolor"] = "#FFFFFF" defaults["bgcolor"] = "#007A33" defaults["face"] = "liberation-sans" defaults["size"] = "small" defaults["weight"] = "bold" defaults["show_top"] = True defaults["show_bottom"] = True defaults["hres"] = 0 defaults["vres"] = 0 defaults["sys_info"] = False defaults["opacity"] = 0.75 defaults["esc"] = True defaults["spanning"] = False # Check if a configuration file was passed in from the command line default_heading = DEFAULTSECT conf_parser = ArgumentParser( formatter_class=RawDescriptionHelpFormatter, add_help=False) conf_parser.add_argument("-c", "--config", help="Specify the configuration file", metavar="FILE") conf_parser.add_argument("--heading", help="Specify the config. section to use.", default=default_heading) options, args = conf_parser.parse_known_args() config_file = None if options.config: config_file = os.path.abspath(options.config) if not os.path.isfile(config_file): print("ERROR: Specified configuration file does not exist.") sys.exit(1) else: config_file = os.path.abspath(CONF_FILE) if not os.path.isfile(config_file): config_file = None # In order to maintain backwards compatibility with the way the # previous configuration file format, a dummy section may need # to be added to the configuration file. If this is the case, # a temporary file is used in order to avoid overwriting the # user's configuration. config = ConfigParser() if config_file is not None: fp = open(config_file, "r") while True: try: if sys.hexversion >= 0x03000000: config.read_file(fp, source=config_file) else: config.readfp(fp, config_file) break except MissingSectionHeaderError: # Recreate the file, adding a default section. fp.close() fp = TemporaryFile() with open(config_file) as original: fp.write("[%s]\n" % default_heading + original.read()) fp.seek(0) fp.close() # If this was a temporary file it will now be deleted. # ConfigParser treats everything as strings and any quotation # marks in a setting are explicitly added to the string. # One way to fix this is to add everything to the defaults and # then strip the quotation marks off of everything. defaults.update(dict(config.items(options.heading))) for key, val in defaults.items(): if config.has_option(options.heading, key): defaults[key] = val.strip("\"'") # TODO: This coercion section is hacky and should be fixed. for key in ["show_top", "show_bottom", "sys_info", "esc", "spanning"]: if config.has_option(options.heading, key): defaults[key] = config.getboolean(options.heading, key) for key in ["hres", "vres"]: if config.has_option(options.heading, key): defaults[key] = config.getint(options.heading, key) for key in ["opacity"]: if config.has_option(options.heading, key): defaults[key] = config.getfloat(options.heading, key) # Use the global config to set defaults for command line options parser = ArgumentParser(parents=[conf_parser]) parser.add_argument("-m", "--message", default=defaults["message"], help="Set the Classification message") parser.add_argument("-f", "--fgcolor", default=defaults["fgcolor"], help="Set the Foreground (text) color") parser.add_argument("-b", "--bgcolor", default=defaults["bgcolor"], help="Set the Background color") parser.add_argument("-x", "--hres", default=defaults["hres"], type=int, help="Set the Horizontal Screen Resolution") parser.add_argument("-y", "--vres", default=defaults["vres"], type=int, help="Set the Vertical Screen Resolution") parser.add_argument("-o", "--opacity", default=defaults["opacity"], type=float, dest="opacity", help="Set the window opacity for composted window managers") parser.add_argument("--face", default=defaults["face"], help="Font face") parser.add_argument("--size", default=defaults["size"], help="Font size") parser.add_argument("--weight", default=defaults["weight"], help="Set the Font weight") parser.add_argument("--disable-esc", default=defaults["esc"], dest="esc", action="store_false", help="Disable the 'ESC to hide' feature and don't show the banner message") parser.add_argument("--hide-top", default=defaults["show_top"], dest="show_top", action="store_false", help="Disable the top banner") parser.add_argument("--hide-bottom", default=defaults["show_bottom"], dest="show_bottom", action="store_false", help="Disable the bottom banner") parser.add_argument("--system-info", default=defaults["sys_info"], dest="sys_info", action="store_true", help="Show user and hostname in the top banner") parser.add_argument("--enable-spanning", default=defaults["spanning"], dest="spanning", action="store_true", help="Enable banner(s) to span across screens as a single banner") options = parser.parse_args() return options
class LifeGameModel(QObject): oncellStatusChanged = pyqtSignal(int, int, int) onFpsChanged = pyqtSignal(int) onStepChanged = pyqtSignal(int) onPlayStateChanged = pyqtSignal(bool) onSizeChanged = pyqtSignal(int, int, np.ndarray) def __init__(self): super(LifeGameModel, self).__init__() self.data_path = "Configs" self.parser = ConfigParser() self.icon_path = "icons" self.settings_file = "settings.json" self.currentSizeIndex = 0 self.grid_sizes = [[20, 10], [40, 20], [60, 30], [80, 40]] self.current_settings = self.load_settings(self.settings_file) self.rows, self.cols = self.current_settings.cells_h, self.current_settings.cells_w if not self.isSizeAccepted(): self.currentSizeIndex = 0 self.cols = self.grid_sizes[0][0] self.rows = self.grid_sizes[0][1] self.fps = self.current_settings.current_fps self.icons_dataset = self.load_icons() self.config_ext = ".cells" self.configurations = self.load_configs(self.data_path) self.cells = self.build_cells() self.step = 0 self.evolutioner = Evolutioner() self.evolutioner.set_fps(self.fps) self.onFpsChanged.connect(self.evolutioner.set_fps) self.evolutioner.onStepTrieggered.connect(self.step_life) self.is_playing = False self.last_step_ts = -1 self.base_config = self.cells.copy() def isSizeAccepted(self): for i, (w, h) in enumerate(self.grid_sizes): if w == self.cols and h == self.rows: self.currentSizeIndex = i return True return False def build_cells(self): return np.zeros((self.rows, self.cols)) def setPlaystate(self, playing: bool): self.is_playing = playing self.onPlayStateChanged.emit(playing) def load_configs(self, confs_dir): confs = os.listdir(confs_dir) return [conf[:-len(self.config_ext)] for conf in confs if conf.endswith(self.config_ext)] def put_data_in_grid(self, data, noemit=False): start_i, start_j = int(self.rows / 2) - int(data.shape[0] / 2), int(self.cols / 2) - int(data.shape[1] / 2) # if start_i >= 0 and start_j >= 0: for i in range(data.shape[0]): for j in range(data.shape[1]): r, c = start_i + i, start_j + j if 0 <= r <= self.rows - 1 and 0 <= c <= self.cols - 1: if noemit: self.cells[r, c] = int(data[i, j]) else: self.changeCellStatus(r, c, int(data[i, j])) def load_config(self, conf): if conf in self.configurations: data = self.parser.read_file(os.path.join(self.data_path, conf + self.config_ext)) self.resetCells() self.put_data_in_grid(data) def save_config(self, complete_path: str): if not complete_path.endswith(self.config_ext): complete_path += self.config_ext self.parser.write_file(complete_path, self.cells) def resetCells(self): for i in range(self.rows): for j in range(self.cols): self.changeCellStatus(i, j, 0) def save_settings(self, filename: str, s: LifeSettings): with open(filename, 'w') as out_file: out_file.write(json.dumps(s.__dict__)) def load_settings(self, filename, exit_if_error=False): if not os.path.exists(filename): sett = LifeSettings() self.save_settings(filename, sett) settings = LifeSettings() try: with open(filename, 'r') as in_file: sett_dict = json.load(in_file) settings.__dict__ = sett_dict except: if exit_if_error: exit(-1) print("Error loading settings file. Writing temporany settings file.") self.load_settings("settings_tmp.json", True) return settings def load_icons(self): icons_dts = {} icons_dts['play'] = "Play.png" icons_dts['pause'] = "Pause.png" icons_dts['step'] = "End.png" icons_dts['stop'] = "Stop.png" icons_dts['clear'] = "clear-button.png" icons_dts['open'] = "Folder-Open-icon.png" icons_dts['save'] = "Save-icon.png" return icons_dts def load_data(self): pass def getIconPath(self, icon_name): if icon_name in self.icons_dataset.keys(): path = os.path.join(self.icon_path, self.icons_dataset[icon_name]) else: path = os.path.join(self.icon_path, "Error-Delete-Icon.png") return path def changeCellStatus(self, i, j, new_state): if self.cells[i, j] != new_state: self.cells[i, j] = new_state self.oncellStatusChanged.emit(i, j, new_state) def step_life(self): """ Stepping one generation :return: """ tmp_cells = np.zeros((self.rows, self.cols)) for i in range(self.rows): for j in range(self.cols): val = self.cells[i, j] new_state = self.check_step_cell(i, j) tmp_cells[i, j] = new_state if new_state != val else 6 for i in range(self.rows): for j in range(self.cols): if tmp_cells[i, j] != 6: self.changeCellStatus(i, j, tmp_cells[i, j]) self.setStep(self.step + 1) def check_step_cell(self, i, j): """ Executes a step on a given cell and returns next state :param i: row :param j: column :return: """ if i == 1 and j == 1: a = 4 mat = self.cells[max(0, i - 1): min(self.rows - 1, i + 2), max(0, j - 1):min(self.cols - 1, j + 2)] s = np.sum(mat, dtype=np.int32) - self.cells[i, j] if s <= 1: return 0 # loneliness elif s >= 4: return 0 # overpopulation else: if self.cells[i, j] == 1: return 1 # Survives else: return 1 if s == 3 else 0 def changeFps(self, framerate: float): self.fps = framerate self.current_settings.current_fps = framerate self.save_settings(self.settings_file, self.current_settings) self.onFpsChanged.emit(framerate) def changeGridSize(self, value): data_backup = np.copy(self.cells) self.currentSizeIndex = value self.current_settings.cells_w = self.cols = self.grid_sizes[value][0] self.current_settings.cells_h = self.rows = self.grid_sizes[value][1] self.save_settings(self.settings_file, self.current_settings) self.cells = self.build_cells() self.put_data_in_grid(data_backup, noemit=True) self.onSizeChanged.emit(self.rows, self.cols, self.cells) def setStep(self, new_step: int): self.step = new_step self.onStepChanged.emit(new_step) def stop(self): self.evolutioner.stop() self.setStep(0) self.resetCells() self.put_data_in_grid(self.base_config) self.setPlaystate(False) def start(self): self.base_config = self.cells.copy() self.evolutioner.start() self.setPlaystate(True) def pause(self): self.evolutioner.stop() self.setPlaystate(False)
class Config(object): """This class represents the barman configuration. Default configuration files are /etc/barman.conf, /etc/barman/barman.conf and ~/.barman.conf for a per-user configuration """ CONFIG_FILES = [ '~/.barman.conf', '/etc/barman.conf', '/etc/barman/barman.conf', ] _QUOTE_RE = re.compile(r"""^(["'])(.*)\1$""") def __init__(self, filename=None): # In Python 3 ConfigParser has changed to be strict by default. # Barman wants to preserve the Python 2 behavior, so we are # explicitly building it passing strict=False. try: # Python 3.x self._config = ConfigParser(strict=False) except TypeError: # Python 2.x self._config = ConfigParser() if filename: if hasattr(filename, 'read'): try: # Python 3.x self._config.read_file(filename) except AttributeError: # Python 2.x self._config.readfp(filename) else: # check for the existence of the user defined file if not os.path.exists(filename): sys.exit("Configuration file '%s' does not exist" % filename) self._config.read(os.path.expanduser(filename)) else: # Check for the presence of configuration files # inside default directories for path in self.CONFIG_FILES: full_path = os.path.expanduser(path) if os.path.exists(full_path) \ and full_path in self._config.read(full_path): filename = full_path break else: sys.exit("Could not find any configuration file at " "default locations.\n" "Check Barman's documentation for more help.") self.config_file = filename self._servers = None self.servers_msg_list = [] self._parse_global_config() def get(self, section, option, defaults=None, none_value=None): """Method to get the value from a given section from Barman configuration """ if not self._config.has_section(section): return None try: value = self._config.get(section, option, raw=False, vars=defaults) if value.lower() == 'none': value = none_value if value is not None: value = self._QUOTE_RE.sub(lambda m: m.group(2), value) return value except NoOptionError: return None def _parse_global_config(self): """ This method parses the global [barman] section """ self.barman_home = self.get('barman', 'barman_home') self.barman_lock_directory = self.get( 'barman', 'barman_lock_directory') or self.barman_home self.user = self.get('barman', 'barman_user') or DEFAULT_USER self.log_file = self.get('barman', 'log_file') self.log_format = self.get('barman', 'log_format') or DEFAULT_LOG_FORMAT self.log_level = self.get('barman', 'log_level') or DEFAULT_LOG_LEVEL # save the raw barman section to be compared later in # _is_global_config_changed() method self._global_config = set(self._config.items('barman')) def _is_global_config_changed(self): """Return true if something has changed in global configuration""" return self._global_config != set(self._config.items('barman')) def load_configuration_files_directory(self): """ Read the "configuration_files_directory" option and load all the configuration files with the .conf suffix that lie in that folder """ config_files_directory = self.get('barman', 'configuration_files_directory') if not config_files_directory: return if not os.path.isdir(os.path.expanduser(config_files_directory)): _logger.warn( 'Ignoring the "configuration_files_directory" option as "%s" ' 'is not a directory', config_files_directory) return for cfile in sorted( iglob( os.path.join(os.path.expanduser(config_files_directory), '*.conf'))): filename = os.path.basename(cfile) if os.path.isfile(cfile): # Load a file _logger.debug('Including configuration file: %s', filename) self._config.read(cfile) if self._is_global_config_changed(): msg = "the configuration file %s contains a not empty [" \ "barman] section" % filename _logger.fatal(msg) raise SystemExit("FATAL: %s" % msg) else: # Add an info that a file has been discarded _logger.warn('Discarding configuration file: %s (not a file)', filename) def _populate_servers(self): """ Populate server list from configuration file Also check for paths errors in configuration. If two or more paths overlap in a single server, that server is disabled. If two or more directory paths overlap between different servers an error is raised. """ # Populate servers if self._servers is not None: return self._servers = {} # Cycle all the available configurations sections for section in self._config.sections(): if section == 'barman': # skip global settings continue # Exit if the section has a reserved name if section in FORBIDDEN_SERVER_NAMES: msg = "the reserved word '%s' is not allowed as server name." \ "Please rename it." % section _logger.fatal(msg) raise SystemExit("FATAL: %s" % msg) # Create a ServerConfig object self._servers[section] = ServerConfig(self, section) # Check for conflicting paths in Barman configuration self._check_conflicting_paths() def _check_conflicting_paths(self): """ Look for conflicting paths intra-server and inter-server """ # All paths in configuration servers_paths = {} # Global errors list self.servers_msg_list = [] # Cycle all the available configurations sections for section in sorted(self._config.sections()): if section == 'barman': # skip global settings continue # Paths map section_conf = self._servers[section] config_paths = { 'backup_directory': section_conf.backup_directory, 'basebackups_directory': section_conf.basebackups_directory, 'errors_directory': section_conf.errors_directory, 'incoming_wals_directory': section_conf.incoming_wals_directory, 'streaming_wals_directory': section_conf.streaming_wals_directory, 'wals_directory': section_conf.wals_directory, } # Check for path errors for label, path in sorted(config_paths.items()): # If the path does not conflict with the others, add it to the # paths map real_path = os.path.realpath(path) if real_path not in servers_paths: servers_paths[real_path] = PathConflict(label, section) else: if section == servers_paths[real_path].server: # Internal path error. # Insert the error message into the server.msg_list if real_path == path: self._servers[section].msg_list.append( "Conflicting path: %s=%s conflicts with " "'%s' for server '%s'" % (label, path, servers_paths[real_path].label, servers_paths[real_path].server)) else: # Symbolic link self._servers[section].msg_list.append( "Conflicting path: %s=%s (symlink to: %s) " "conflicts with '%s' for server '%s'" % (label, path, real_path, servers_paths[real_path].label, servers_paths[real_path].server)) # Disable the server self._servers[section].disabled = True else: # Global path error. # Insert the error message into the global msg_list if real_path == path: self.servers_msg_list.append( "Conflicting path: " "%s=%s for server '%s' conflicts with " "'%s' for server '%s'" % (label, path, section, servers_paths[real_path].label, servers_paths[real_path].server)) else: # Symbolic link self.servers_msg_list.append( "Conflicting path: " "%s=%s (symlink to: %s) for server '%s' " "conflicts with '%s' for server '%s'" % (label, path, real_path, section, servers_paths[real_path].label, servers_paths[real_path].server)) def server_names(self): """This method returns a list of server names""" self._populate_servers() return self._servers.keys() def servers(self): """This method returns a list of server parameters""" self._populate_servers() return self._servers.values() def get_server(self, name): """ Get the configuration of the specified server :param str name: the server name """ self._populate_servers() return self._servers.get(name, None) def validate_global_config(self): """ Validate global configuration parameters """ # Check for the existence of unexpected parameters in the # global section of the configuration file keys = [ 'barman_home', 'barman_lock_directory', 'barman_user', 'log_file', 'log_level', 'configuration_files_directory' ] keys.extend(ServerConfig.KEYS) self._validate_with_keys(self._global_config, keys, 'barman') def validate_server_config(self, server): """ Validate configuration parameters for a specified server :param str server: the server name """ # Check for the existence of unexpected parameters in the # server section of the configuration file self._validate_with_keys(self._config.items(server), ServerConfig.KEYS, server) @staticmethod def _validate_with_keys(config_items, allowed_keys, section): """ Check every config parameter against a list of allowed keys :param config_items: list of tuples containing provided parameters along with their values :param allowed_keys: list of allowed keys :param section: source section (for error reporting) """ for parameter in config_items: # if the parameter name is not in the list of allowed values, # then output a warning name = parameter[0] if name not in allowed_keys: output.warning( 'Invalid configuration option "%s" in [%s] ' 'section.', name, section)
log) if clean_intermediates: from summary import cleanup_intermediate_folders log.readout('Removing intermediate files and folders') cleanup_intermediate_folders(output_dir, parser_dict, target) log.readout('Completed summary of %s', target) log.removeFilter(summary) return args = parseArgs() pipeline_config = args['pipeline_config'] parser = Parser() if int(sys.version[0]) == 3: try: parser.read_file( open(pipeline_config)) #reading in the config file except IOError as e: print('Missing config file!') sys.exit() try: parser.read_file(open(parser.get('i/o', 'static_config'))) except configparser.NoOptionError: pass elif int(sys.version[0]) == 2: try: parser.readfp(open(pipeline_config)) except IOError as e: print('Missing config file!') sys.exit() try: parser.readfp(open(parser.get('i/o', 'static_config')))