コード例 #1
0
def read_config(filename):
    ''' Read in the user configuation file.'''
    user_config = {
        'user_call' : 'HORUS_RX',
        'ozi_udp_port' : 55683,
        'summary_port' : 55672,
        'station_lat' : 0.0,
        'station_lon' : 0.0,
        'radio_comment' : "",
        'antenna_comment' : ""
    }

    try:
        config = RawConfigParser()
        config.read(filename)

        user_config['user_call'] = config.get('user', 'callsign')
        user_config['station_lat'] = config.getfloat('user', 'station_lat')
        user_config['station_lon'] = config.getfloat('user', 'station_lon')
        user_config['radio_comment'] = config.get('user', 'radio_comment')
        user_config['antenna_comment'] = config.get('user', 'antenna_comment')
        user_config['ozi_udp_port'] = config.getint('horus_udp', 'ozimux_port')
        user_config['summary_port'] = config.getint('horus_udp', 'summary_port')

        return user_config

    except:
        traceback.print_exc()
        logging.error("Could not parse config file, exiting. Have you copied user.cfg.example to user.cfg?")
        return None
コード例 #2
0
class ReadConfig:
    """读取配置文件"""
    def __init__(self):
        self.config = RawConfigParser()
        self.config.read(contants.global_conf_file,
                         encoding='utf-8')  #先读取global.conf文件
        switch = self.config.getboolean(
            'switch',
            'on')  #global.conf文件中的switch如果是true就去读取online.conf,相反就去读取test.conf
        if switch:
            self.config.read(contants.online_conf_file, encoding='utf-8')
        else:
            self.config.read(contants.test_conf_file, encoding='utf-8')

    def get_str(self, section, option):
        return self.config.get(section, option)

    def get_int(self, section, option):
        return self.config.getint(section, option)

    def get_float(self, section, option):
        return self.config.getfloat(section, option)

    def get_bool(self, section, option):
        return self.config.getboolean(section, option)

    def write(self, section, option, value):
        self.config.set(section, option, value)
        with open(contants.test_conf_file, 'w') as f:
            self.config.write(f)
コード例 #3
0
    def test_default_realfloat(self):
        """
        Test if default float variable is returned if given as string

        Note:
        In the configuration file, a float can be given as string,
        so the string should be converted to a float. For the wrapper
        a string value has to work as well.
        """
        config = RawConfigParser()

        configtext = """
        [test]
        """
        config.read_string(configtext)

        with self.assertRaises(NoOptionError):
            _ = config.getfloat("test", "var1")

        wrap = ConfigWrapper(
            config, {
                "var1": {
                    "default": "1.1",
                    "description": "this is just a test for float var1"
                }
            })

        var1 = wrap.getfloat("test", "var1")
        print(f"From ConfigWrapper -> var1: {var1}")
        self.assertIsInstance(var1, float)
        self.assertEqual(var1, 1.1)
コード例 #4
0
    def test_existing_float(self):
        """Test if float variable given in config file is correctly returned"""
        config = RawConfigParser()

        configtext = """
        [test]
        var1 = 10.1
        """
        config.read_string(configtext)

        var1 = config.getfloat("test", "var1")
        print(f"From RawConfigParser -> var1: {var1}")
        self.assertEqual(var1, 10.1)

        wrap = ConfigWrapper(
            config, {
                "var1": {
                    "default": -1.1,
                    "description": "this is just a test for float var1"
                }
            })

        var1 = wrap.getfloat("test", "var1")
        print(f"From ConfigWrapper -> var1: {var1}")
        self.assertIsInstance(var1, float)
        self.assertEqual(var1, 10.1)
コード例 #5
0
ファイル: lib_losoto.py プロジェクト: tammojan/losoto
 def getfloat(self, s, v, default=None):
     if self.has_option(s, v):
         return RawConfigParser.getfloat(self, s, v)
     elif default is None:
         logging.error('Section: %s - Values: %s: required (expected float).' % (s, v))
     else:
         return default
コード例 #6
0
ファイル: config.py プロジェクト: whoswq/PKUAutoElective
class BaseConfig(object, metaclass=Singleton):

    CONFIG_FILE = ""
    ALLOW_NO_VALUE = True

    def __init__(self):
        if self.__class__ is __class__:
            raise NotImplementedError
        file = os.path.normpath(os.path.abspath(self.__class__.CONFIG_FILE))
        if not os.path.exists(file):
            raise FileNotFoundError("config file was not found: %s" % file)
        self._config = RawConfigParser(
            allow_no_value=self.__class__.ALLOW_NO_VALUE)
        self._config.read(file, encoding="utf-8-sig")  # 必须显示指明 encoding

    def get(self, section, key):
        return self._config.get(section, key)

    def getint(self, section, key):
        return self._config.getint(section, key)

    def getfloat(self, section, key):
        return self._config.getfloat(section, key)

    def getboolean(self, section, key):
        return self._config.getboolean(section, key)
コード例 #7
0
ファイル: config.py プロジェクト: unseenmagik/nestwatcher
    def __init__(self, config_path="config/config.ini"):
        config_file = RawConfigParser()
        config_file.read(config_path)

        self.hours_since_change = 3
        self.auto_time = config_file.getboolean("Config", "auto_time", fallback=True)
        self.use_events = config_file.getboolean("Config", "events", fallback=True)
        self.hemisphere = config_file.get("Config", "hemisphere", fallback="all")
        self.less_queries = config_file.getboolean("Config", "less_queries", fallback=False)
        self.submitted_by = config_file.get("Config", "bot_name", fallback=None)
        if not self.submitted_by:
            self.submitted_by = None
        self.pokestop_pokemon = config_file.getboolean("Config", "pokestop_pokemon")
        self.in_meganest = config_file.getboolean("Config", "i_scan_berlin", fallback=False)
        self.poracle = config_file.get("Config", "poracle_endpoint", fallback=False)
        self.workers = 5

        self.scanner = config_file.get("Scanner DB", "scanner")
        self.db_name = config_file.get("Scanner DB", "name")
        self.db_user = config_file.get("Scanner DB", "user")
        self.db_password = config_file.get("Scanner DB", "password")
        self.db_host = config_file.get("Scanner DB", "host")
        self.db_port = config_file.getint("Scanner DB", "port")
        self.custom_pokemon = config_file.get("Scanner DB", "custom_pokemon_table", fallback="pokemon")

        self.nest_db_name = config_file.get("Nest DB", "name")
        self.nest_db_user = config_file.get("Nest DB", "user")
        self.nest_db_password = config_file.get("Nest DB", "password")
        self.nest_db_host = config_file.get("Nest DB", "host")
        self.nest_db_port = config_file.getint("Nest DB", "port")

        self.default_park_name = config_file.get("Geojson", "default_park_name")
        self.json_path = config_file.get("Geojson", "path")
        self.json_stroke = config_file.get("Geojson", "stroke")
        self.json_stroke_width = config_file.getint("Geojson", "stroke_width")
        self.json_stroke_opacity = config_file.getfloat("Geojson", "stroke_opacity")
        self.json_fill = config_file.get("Geojson", "fill")
        self.json_fill_opacity = config_file.getfloat("Geojson", "fill_opacity")

        self.discord_token = config_file.get("Discord", "token")
        self.language = config_file.get("Discord", "language")
        self.static_url = config_file.get("Discord", "tileserver_url")
        self.icon_repo = config_file.get("Discord", "icon_repo")
        self.time_format = config_file.get("Discord", "time_format", fallback="%d.%m. %H:%M")
コード例 #8
0
class IniConfig(object):
    """This class is used to access/read config file, if it exists.

        :param config_file: the config file name
        :type config_file: str or None
    """
    def __init__(self, filename=None):
        self.config_file = filename

        self.parser = RawConfigParser()
        self.load()

    def load(self, encoding='utf-8'):
        """Load a config file from the list of paths, if it exists."""
        config_file = self.config_file
        if os.path.isfile(config_file) and os.path.getsize(config_file) > 0:
            try:
                if is_py3:
                    self.parser.read(config_file, encoding=encoding)
                else:
                    self.parser.read(config_file)
                # print(_("DEBUG: Read configuration file %s") % config_file)
            except UnicodeDecodeError as e:
                print(
                    _("Error: Cannot decode configuration file '{0}': {1}").
                    format(config_file, e))
                sys.exit(1)

    def items(self, section):
        """Return the items list of a section."""
        return self.parser.items(section)

    def has_section(self, section):
        """Return info about the existence of a section."""
        return self.parser.has_section(section)

    def get_option(self, section, option):
        """Get the float value of an option, if it exists."""
        try:
            value = self.parser.getfloat(section, option)
        except NoOptionError:
            return
        else:
            return value

    def get_raw_option(self, section, option):
        """Get the raw value of an option, if it exists."""
        try:
            value = self.parser.get(section, option)
        except NoOptionError:
            return
        else:
            return value
コード例 #9
0
class IniConfig(object):

    """This class is used to access/read config file, if it exists.

        :param config_file: the config file name
        :type config_file: str or None
    """

    def __init__(self, filename=None):
        self.config_file = filename

        self.parser = RawConfigParser()
        self.load()

    def load(self, encoding="utf-8"):
        """Load a config file from the list of paths, if it exists."""
        config_file = self.config_file
        if os.path.isfile(config_file) and os.path.getsize(config_file) > 0:
            try:
                if is_py3:
                    self.parser.read(config_file, encoding=encoding)
                else:
                    self.parser.read(config_file)
                # print(_("DEBUG: Read configuration file %s") % config_file)
            except UnicodeDecodeError as e:
                print (_("Error: Cannot decode configuration file '{0}': {1}").format(config_file, e))
                sys.exit(1)

    def items(self, section):
        """Return the items list of a section."""
        return self.parser.items(section)

    def has_section(self, section):
        """Return info about the existence of a section."""
        return self.parser.has_section(section)

    def get_option(self, section, option):
        """Get the float value of an option, if it exists."""
        try:
            value = self.parser.getfloat(section, option)
        except NoOptionError:
            return
        else:
            return value

    def get_raw_option(self, section, option):
        """Get the raw value of an option, if it exists."""
        try:
            value = self.parser.get(section, option)
        except NoOptionError:
            return
        else:
            return value
コード例 #10
0
class BaseConfig(object):
    def __init__(self, config_file=None):
        if self.__class__ is __class__:
            raise NotImplementedError
        file = os.path.normpath(os.path.abspath(config_file))
        if not os.path.exists(file):
            raise FileNotFoundError("Config file was not found: %s" % file)
        self._config = RawConfigParser()
        self._config.read(file, encoding="utf-8-sig")

    def get(self, section, key):
        return self._config.get(section, key)

    def getint(self, section, key):
        return self._config.getint(section, key)

    def getfloat(self, section, key):
        return self._config.getfloat(section, key)

    def getboolean(self, section, key):
        return self._config.getboolean(section, key)

    def getdict(self, section, options):
        assert isinstance(options, (list, tuple, set))
        d = dict(self._config.items(section))
        if not all(k in d for k in options):
            raise UserInputException(
                "Incomplete course in section %r, %s must all exist." %
                (section, options))
        return d

    def getlist(self, section, option, *args, **kwargs):
        v = self.get(section, option, *args, **kwargs)
        return _reCommaSep.split(v)

    def ns_sections(self, ns):
        ns = ns.strip()
        ns_sects = OrderedDict()  # { id: str(section) }
        for s in self._config.sections():
            mat = _reNamespacedSection.match(s)
            if mat is None:
                continue
            if mat.group('ns') != ns:
                continue
            id_ = mat.group('id')
            if id_ in ns_sects:
                raise DuplicateSectionError("%s:%s" % (ns, id_))
            ns_sects[id_] = s
        return [(id_, s)
                for id_, s in ns_sects.items()]  # [ (id, str(section)) ]
コード例 #11
0
 def read_file(self):
     here = "config.read_file"
     config_read = RawConfigParser()
     config_read.read(self.config_filename)
     section = "Scan"
     self.scan_delay = float(config_read.get(section, 'scan_delay'))
     self.max_scans = float(config_read.get(section, 'max_scans'))
     section = "Log"
     self.log_directory = config_read.get(section, 'log_directory')
     self.local_dir_www = config_read.get(section, 'local_dir_www')
     self.log_buffer_flag = config_read.getboolean(section,
                                                   'log_buffer_flag')
     self.text_buffer_length = int(
         config_read.get(section, 'text_buffer_length'))
     section = "Ftp"
     self.ftp_creds_filename = config_read.get(section,
                                               'ftp_creds_filename')
     self.ftp_log_max_count = config_read.getfloat(section,
                                                   'ftp_log_max_count')
     self.ftp_timeout = config_read.getfloat(section, 'ftp_timeout')
     self.ftplog = config_read.getfloat(section, 'ftplog')
     self.ftp_debug = config_read.getboolean(section, 'ftp_debug')
     section = "Sauna"
     self.max_temp = float(config_read.get(section, 'max_temp'))
     self.min_temp = float(config_read.get(section, 'min_temp'))
     self.min_speed = float(config_read.get(section, 'min_speed'))
     self.max_speed = float(config_read.get(section, 'max_speed'))
     self.min_freq = float(config_read.get(section, 'min_freq'))
     self.max_freq = float(config_read.get(section, 'max_freq'))
     self.sauna_GPIO_port = int(config_read.get(section, 'sauna_GPIO_port'))
     self.sensor4readings = str(config_read.get(section, 'sensor4readings'))
     section = "Lamps_and_logging"
     self.lamps_control = config_read.getboolean(section, 'lamps_control')
     self.latitude = float(config_read.get(section, 'latitude'))
     self.longitude = float(config_read.get(section, 'longitude'))
     return
コード例 #12
0
    def test_nodefault_exception_float(self):
        """Test if undefined or value without default raises exception"""
        config = RawConfigParser()

        configtext = """
        [test]
        """
        config.read_string(configtext)

        with self.assertRaises(NoOptionError):
            _ = config.getfloat("test", "var1")
        with self.assertRaises(NoOptionError):
            _ = config.getfloat("test", "var2")

        wrap = ConfigWrapper(
            config,
            {"var1": {
                "description": "this is just a test for string var1"
            }})

        with self.assertRaises(NoOptionError):
            _ = wrap.getfloat("test", "var2")
        with self.assertRaises(NoOptionError):
            _ = wrap.getfloat("test", "var1")
コード例 #13
0
ファイル: build_site.py プロジェクト: ryanvilbrandt/comic_git
def get_option(comic_info: RawConfigParser, section: str, option: str, option_type: type=str, default: Any=None) \
        -> Union[str, int, float, bool]:
    if comic_info.has_section(section):
        if comic_info.has_option(section, option):
            if option_type == str:
                return comic_info.get(section, option)
            elif option_type == int:
                return comic_info.getint(section, option)
            elif option_type == float:
                return comic_info.getfloat(section, option)
            elif option_type == bool:
                return comic_info.getboolean(section, option)
            else:
                raise ValueError(f"Invalid option type: {option_type}")
    return default
コード例 #14
0
ファイル: config.py プロジェクト: 5l1v3r1/easywall
class Config(object):
    """This class is a wrapper class around configparser"""

    def __init__(self, configpath: str):
        self.configpath = configpath
        self.configlib = RawConfigParser()
        self.configlib.read(self.configpath)

    def get_value(self, section: str, key: str) -> any:
        """Returns a value in a given section from the configuration file.
        Returns String, Float, Integer, Boolean"""
        value = ""
        try:
            value = self.configlib[section][key]
        except KeyError:
            error("Could not find key {} in section {}".format(key, section))
            info("Valid sections are: ")
            info("{}".format(self.get_sections()))
        if value in ["yes", "no", "true", "false", "on", "off"]:
            return self.configlib.getboolean(section, key)
        if is_int(value):
            return self.configlib.getint(section, key)
        if is_float(value):
            return self.configlib.getfloat(section, key)
        return value

    def get_sections(self) -> list:
        """Return a list of section names, excluding [DEFAULT]"""
        return self.configlib.sections()

    def set_value(self, section: str, key: str, value: str) -> bool:
        """Writes a key, value pair into memory configuration and writes it to config file"""
        result = True
        try:
            self.configlib[section][key] = value
        except Exception as exc:
            error(
                "Error while writing {} into key {} in section {}: {}".format(
                    value, key, section, exc))
            info("Valid sections are: ")
            info("{}".format(self.get_sections()))
            result = False

        if result:
            with open(self.configpath, 'w') as configfile:
                self.configlib.write(configfile)

        return result
コード例 #15
0
class BaseConfig(object):
    def __init__(self, config_file=None):
        self._config = RawConfigParser()
        self._config.read(config_file, encoding="utf-8-sig")

    def get(self, section, key):
        return self._config.get(section, key)

    def getint(self, section, key):
        return self._config.getint(section, key)

    def getfloat(self, section, key):
        return self._config.getfloat(section, key)

    def getboolean(self, section, key):
        return self._config.getboolean(section, key)
コード例 #16
0
class ConfigHandler:
    def __init__(self):
        self.config = {}
        self.parser = RawConfigParser()

    def add(self, key, value):
        self.config[key] = value

    def havekey(self, key):
        if key in self.config.keys():
            return True
        else:
            return False

    def get(self, section, option):
        if option == "table_prefix":
            return os.environ['TABLE_PREFIX']
        return self.parser.get(section, option)

    def getfloat(self, section, option):
        return self.parser.getfloat(section, option)

    def getint(self, section, option):
        return self.parser.getint(section, option)

    def getvalue(self, key):
        return self.config[key]

    def read(self, site_config_path):
        self.parser.read(site_config_path)

    def read_config_section(self, section_name):

        print("From Config {}, Reading Section {}.".format(
            self.config_path, section_name))

        config = dict()
        return config

    def read_config_key(self, site_config_path, section_name, key_name):
        self.parser.read(site_config_path)
        print("From Config {}, Reading Key {} from Section {}.".format(
            self.config_path, key_name, section_name))

        return value
コード例 #17
0
class read_con:
    #_____________________________________________________________________________
    def __init__(self, config):

        self.con = RawConfigParser(inline_comment_prefixes=(";","#"))
        self.con.read(config)

    #_____________________________________________________________________________
    def __call__(self, par):

        return self.con.getfloat("main", par)

    #_____________________________________________________________________________
    def str(self, par):

        return self.con.get("main", par).strip("\"'")

    #_____________________________________________________________________________
    def int(self, par):

        return self.con.getint("main", par)
コード例 #18
0
ファイル: config.py プロジェクト: zmd971202/PKUAutoElective
class BaseConfig(object):
    def __init__(self, config_file=None, allow_no_value=True):
        if self.__class__ is __class__:
            raise NotImplementedError
        file = os.path.normpath(os.path.abspath(config_file))
        if not os.path.exists(file):
            raise FileNotFoundError("config file was not found: %s" % file)
        self._config = RawConfigParser(allow_no_value=allow_no_value)
        self._config.read(file, encoding="utf-8-sig")  # 必须显示指明 encoding

    def get(self, section, key):
        return self._config.get(section, key)

    def getint(self, section, key):
        return self._config.getint(section, key)

    def getfloat(self, section, key):
        return self._config.getfloat(section, key)

    def getboolean(self, section, key):
        return self._config.getboolean(section, key)
コード例 #19
0
class ReadConfig:
    """读取配置文件"""
    def __init__(self):
        """初始化函数中先读取global.conf文件,如果on为ture读取online.conf,如果on为false读取test.conf"""
        self.config = RawConfigParser()
        self.config.read(contants.global_file,encoding='utf-8')
        switch = self.config.getboolean('switch','on')
        if switch:
            self.config.read(contants.online_file,encoding='utf-8')
        else:
            self.config.read(contants.test_file,encoding='utf-8')

    def get_str(self,section,option):
        return self.config.get(section,option)

    def get_int(self,section,option):
        return self.config.getint(section,option)

    def get_float(self,section,option):
        return self.config.getfloat(section,option)

    def get_bool(self,section,option):
        return self.config.getboolean(section,option)
コード例 #20
0
    def test_default_float(self):
        """Test if float variable given in config file is correctly returned"""
        config = RawConfigParser()

        configtext = """
        [test]
        """
        config.read_string(configtext)

        with self.assertRaises(NoOptionError):
            _ = config.getfloat("test", "var1")

        wrap = ConfigWrapper(
            config, {
                "var1": {
                    "default": -1.1,
                    "description": "this is just a test for integer var1"
                }
            })

        var1 = wrap.getfloat("test", "var1")
        print(f"From ConfigWrapper -> var1: {var1}")
        self.assertIsInstance(var1, float)
        self.assertEqual(var1, -1.1)
コード例 #21
0
    def test_manual_fallback_float(self):
        """Test if explicit fallback float has precedence"""
        config = RawConfigParser()

        configtext = """
        [test]
        """
        config.read_string(configtext)

        with self.assertRaises(NoOptionError):
            _ = config.getfloat("test", "var1")

        wrap = ConfigWrapper(
            config, {
                "var1": {
                    "default": -1.1,
                    "description": "this is just a test for float var1"
                }
            })

        var1 = wrap.getfloat("test", "var1", fallback=9.99)
        print(f"From ConfigWrapper -> var1: {var1}")
        self.assertEqual(var1, 9.99)
        self.assertIsInstance(var1, float)
コード例 #22
0
class Config(object):

    """This class is used to access/read config file, if it exists.

    :param location: the custom path to search for config file
    :type location: str or None
    """

    def __init__(self, location=None):
        self.location = location

        self.config_filename = 'glances.conf'

        self.parser = RawConfigParser()

        self._loaded_config_file = None
        self.load()

    def load(self):
        """Load a config file from the list of paths, if it exists."""
        for config_file in self.get_config_paths():
            if os.path.isfile(config_file) and os.path.getsize(config_file) > 0:
                try:
                    if is_py3:
                        self.parser.read(config_file, encoding='utf-8')
                    else:
                        self.parser.read(config_file)
                    logger.info("Read configuration file '{0}'".format(config_file))
                except UnicodeDecodeError as e:
                    logger.error("Cannot decode configuration file '{0}': {1}".format(config_file, e))
                    sys.exit(1)
                # Save the loaded configuration file path (issue #374)
                self._loaded_config_file = config_file
                break

    def get_loaded_config_file(self):
        """Return the loaded configuration file"""
        return self._loaded_config_file

    def get_config_paths(self):
        r"""Get a list of config file paths.

        The list is built taking into account of the OS, priority and location.

        * running from source: /path/to/glances/conf
        * per-user install: ~/.local/etc/glances (Unix-like only)
        * Linux: ~/.config/glances, /etc/glances
        * BSD: ~/.config/glances, /usr/local/etc/glances
        * Mac: ~/Library/Application Support/glances, /usr/local/etc/glances
        * Windows: %APPDATA%\glances

        The config file will be searched in the following order of priority:
            * /path/to/file (via -C flag)
            * /path/to/glances/conf
            * user's local directory (per-user install settings)
            * user's home directory (per-user settings)
            * {/usr/local,}/etc directory (system-wide settings)
        """
        paths = []
        conf_path = os.path.realpath(
            os.path.join(work_path, '..', '..', 'conf'))

        if self.location is not None:
            paths.append(self.location)

        if os.path.exists(conf_path):
            paths.append(os.path.join(conf_path, self.config_filename))

        if not is_windows:
            paths.append(os.path.join(os.path.expanduser('~/.local'), 'etc', appname, self.config_filename))

        if is_linux or is_bsd:
            paths.append(os.path.join(
                os.environ.get('XDG_CONFIG_HOME') or os.path.expanduser(
                    '~/.config'),
                appname, self.config_filename))
            if hasattr(sys, 'real_prefix') or is_bsd:
                paths.append(
                    os.path.join(sys.prefix, 'etc', appname, self.config_filename))
            else:
                paths.append(
                    os.path.join('/etc', appname, self.config_filename))
        elif is_mac:
            paths.append(os.path.join(
                os.path.expanduser('~/Library/Application Support/'),
                appname, self.config_filename))
            paths.append(os.path.join(
                sys_prefix, 'etc', appname, self.config_filename))
        elif is_windows:
            paths.append(os.path.join(
                os.environ.get('APPDATA'), appname, self.config_filename))

        return paths

    def items(self, section):
        """Return the items list of a section."""
        return self.parser.items(section)

    def has_section(self, section):
        """Return info about the existence of a section."""
        return self.parser.has_section(section)

    def get_option(self, section, option):
        """Get the float value of an option, if it exists."""
        try:
            value = self.parser.getfloat(section, option)
        except NoOptionError:
            return
        else:
            return value

    def get_raw_option(self, section, option):
        """Get the raw value of an option, if it exists."""
        try:
            value = self.parser.get(section, option)
        except NoOptionError:
            return
        else:
            return value
コード例 #23
0
ファイル: config.py プロジェクト: zsau/quodlibet
class Config(object):
    """A wrapper around RawConfigParser.

    Provides a ``defaults`` attribute of the same type which can be used
    to set default values.
    """

    def __init__(self, version=None, _defaults=True):
        """Use read() to read in an existing config file.

        version should be an int starting with 0 that gets incremented if you
        want to register a new upgrade function. If None, upgrade is disabled.
        """

        self._config = ConfigParser(dict_type=_sorted_dict)
        self.defaults = None
        if _defaults:
            self.defaults = Config(_defaults=False)
        self._version = version
        self._loaded_version = None
        self._upgrade_funcs = []

    def _do_upgrade(self, func):
        assert self._loaded_version is not None
        assert self._version is not None

        old_version = self._loaded_version
        new_version = self._version
        if old_version != new_version:
            print_d("Config upgrade: %d->%d (%r)" % (
                old_version, new_version, func))
            func(self, old_version, new_version)

    def get_version(self):
        """Get the version of the loaded config file (for testing only)

        Raises Error if no file was loaded or versioning is disabled.
        """

        if self._version is None:
            raise Error("Versioning disabled")

        if self._loaded_version is None:
            raise Error("No file loaded")

        return self._loaded_version

    def register_upgrade_function(self, function):
        """Register an upgrade function that gets called at each read()
        if the current config version and the loaded version don't match.

        Can also be registered after read was called.

        function(config, old_version: int, new_version: int) -> None
        """

        if self._version is None:
            raise Error("Versioning disabled")

        self._upgrade_funcs.append(function)
        # after read(), so upgrade now
        if self._loaded_version is not None:
            self._do_upgrade(function)
        return function

    def reset(self, section, option):
        """Reset the value to the default state"""

        assert self.defaults is not None

        try:
            self._config.remove_option(section, option)
        except NoSectionError:
            pass

    def options(self, section):
        """Returns a list of options available in the specified section."""

        try:
            options = self._config.options(section)
        except NoSectionError:
            if self.defaults:
                return self.defaults.options(section)
            raise
        else:
            if self.defaults:
                try:
                    options.extend(self.defaults.options(section))
                    options = list_unique(options)
                except NoSectionError:
                    pass
            return options

    def get(self, section, option, default=_DEFAULT):
        """get(section, option[, default]) -> str

        If default is not given or set, raises Error in case of an error
        """

        try:
            return self._config.get(section, option)
        except Error:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.get(section, option)
                    except Error:
                        pass
                raise
            return default

    def gettext(self, *args, **kwargs):
        value = self.get(*args, **kwargs)
        # make sure there are no surrogates
        value.encode("utf-8")
        return value

    def getbytes(self, section, option, default=_DEFAULT):
        try:
            value = self._config.get(section, option)
            value = value.encode("utf-8", "surrogateescape")
            return value
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getbytes(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getboolean(self, section, option, default=_DEFAULT):
        """getboolean(section, option[, default]) -> bool

        If default is not given or set, raises Error in case of an error
        """

        try:
            return self._config.getboolean(section, option)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getboolean(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getint(self, section, option, default=_DEFAULT):
        """getint(section, option[, default]) -> int

        If default is not give or set, raises Error in case of an error
        """

        try:
            return int(self._config.getfloat(section, option))
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getint(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getfloat(self, section, option, default=_DEFAULT):
        """getfloat(section, option[, default]) -> float

        If default is not give or set, raises Error in case of an error
        """

        try:
            return self._config.getfloat(section, option)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getfloat(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getstringlist(self, section, option, default=_DEFAULT):
        """getstringlist(section, option[, default]) -> list

        If default is not given or set, raises Error in case of an error.
        Gets a list of strings, using CSV to parse and delimit.
        """

        try:
            value = self._config.get(section, option)

            parser = csv.reader(
                [value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
            try:
                vals = next(parser)
            except (csv.Error, ValueError) as e:
                raise Error(e)
            return vals
        except Error as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getstringlist(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def setstringlist(self, section, option, values):
        """Saves a list of unicode strings using the csv module"""

        sw = StringIO()
        values = [str(v) for v in values]

        writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(values)
        self.set(section, option, sw.getvalue())

    def setlist(self, section, option, values, sep=","):
        """Saves a list of str using ',' as a separator and \\ for escaping"""

        values = [str(v) for v in values]
        joined = join_escape(values, sep)
        self.set(section, option, joined)

    def getlist(self, section, option, default=_DEFAULT, sep=","):
        """Returns a str list saved with setlist()"""

        try:
            value = self._config.get(section, option)
            return split_escape(value, sep)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getlist(section, option, sep=sep)
                    except Error:
                        pass
                raise Error(e)
            return default

    def set(self, section, option, value):
        """Saves the string representation for the passed value

        Don't pass unicode, encode first.
        """

        if isinstance(value, bytes):
            raise TypeError("use setbytes")

        # RawConfigParser only allows string values but doesn't
        # scream if they are not (and it only fails before the
        # first config save..)
        if not isinstance(value, str):
            value = str(value)

        try:
            self._config.set(section, option, value)
        except NoSectionError:
            if self.defaults and self.defaults.has_section(section):
                self._config.add_section(section)
                self._config.set(section, option, value)
            else:
                raise

    def settext(self, section, option, value):
        value = str(value)

        # make sure there are no surrogates
        value.encode("utf-8")

        self.set(section, option, value)

    def setbytes(self, section, option, value):
        assert isinstance(value, bytes)

        value = value.decode("utf-8", "surrogateescape")

        self.set(section, option, value)

    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb") as fileobj:
                temp = StringIO()
                self._config.write(temp)
                data = temp.getvalue().encode("utf-8", "surrogateescape")
                fileobj.write(data)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)

    def clear(self):
        """Remove all sections."""

        for section in self._config.sections():
            self._config.remove_section(section)

    def is_empty(self):
        """Whether the config has any sections"""

        return not self._config.sections()

    def read(self, filename):
        """Reads the config from `filename` if the file exists,
        otherwise does nothing

        Can raise EnvironmentError, Error.
        """

        try:
            with open(filename, "rb") as fileobj:
                fileobj = StringIO(
                    fileobj.read().decode("utf-8", "surrogateescape"))
                self._config.readfp(fileobj, filename)
        except (IOError, OSError):
            return

        # don't upgrade if we just created a new config
        if self._version is not None:
            self._loaded_version = self.getint("__config__", "version", -1)
            for func in self._upgrade_funcs:
                self._do_upgrade(func)

    def has_option(self, section, option):
        """If the given section exists, and contains the given option"""

        return self._config.has_option(section, option) or (
            self.defaults and self.defaults.has_option(section, option))

    def has_section(self, section):
        """If the given section exists"""

        return self._config.has_section(section) or (
            self.defaults and self.defaults.has_section(section))

    def remove_option(self, section, option):
        """Remove the specified option from the specified section

        Can raise Error.
        """

        return self._config.remove_option(section, option)

    def add_section(self, section):
        """Add a section named section to the instance if it not already
        exists."""

        if not self._config.has_section(section):
            self._config.add_section(section)
コード例 #24
0
ファイル: config.py プロジェクト: Perdu/poezio
 def getfloat(self, option, section=DEFSECTION):
     """
     get a value and returns it as a float
     """
     return RawConfigParser.getfloat(self, section, option)
コード例 #25
0
class Config(object):

    """This class is used to access/read config file, if it exists.

    :param location: the custom path to search for config file
    :type location: str or None
    """

    def __init__(self, location=None):
        self.location = location
        self.config_filename = appname + '.conf'

        self.parser = RawConfigParser()
        self.load()

    def load(self, encoding='utf-8'):
        """Load a config file from the list of paths, if it exists."""
        for config_file in self.get_config_paths():
            if os.path.isfile(config_file) and os.path.getsize(config_file) > 0:
                try:
                    if is_py3:
                        self.parser.read(config_file, encoding=encoding)
                    else:
                        self.parser.read(config_file)
                    # print(_("DEBUG: Read configuration file %s") % config_file)
                except UnicodeDecodeError as e:
                    print(_("Error: Cannot decode configuration file '{0}': {1}").format(config_file, e))
                    sys.exit(1)
                break

    def get_config_paths(self):
        r"""Get a list of config file paths. """
        paths = []
        conf_path = os.path.realpath(os.path.join(work_path, '..', '..', 'conf'))

        if self.location is not None:
            #paths.append(self.location)
            paths.append(os.path.join(self.location, self.config_filename))

        if os.path.exists(conf_path):
            paths.append(os.path.join(conf_path, self.config_filename))

        if is_linux or is_bsd:
            paths.append(os.path.join(
                os.environ.get('XDG_CONFIG_HOME') or os.path.expanduser('~/.config'),
                appname, self.config_filename))
            if hasattr(sys, 'real_prefix') or is_bsd:
                paths.append(os.path.join(sys.prefix, 'etc', appname, self.config_filename))
            else:
                paths.append(os.path.join('/etc', appname, self.config_filename))
        elif is_mac:
            paths.append(os.path.join(
                os.path.expanduser('~/Library/Application Support/'),
                appname, self.config_filename))
            paths.append(os.path.join(
                sys_prefix, 'etc', appname, self.config_filename))
        elif is_windows:
            paths.append(os.path.join(
                os.environ.get('APPDATA'), appname, self.config_filename))

        return paths

    def items(self, section):
        """Return the items list of a section."""
        return self.parser.items(section)

    def has_section(self, section):
        """Return info about the existence of a section."""
        return self.parser.has_section(section)

    def get_option(self, section, option):
        """Get the float value of an option, if it exists."""
        try:
            value = self.parser.getfloat(section, option)
        except NoOptionError:
            return
        else:
            return value

    def get_raw_option(self, section, option):
        """Get the raw value of an option, if it exists."""
        try:
            value = self.parser.get(section, option)
        except NoOptionError:
            return
        else:
            return value
コード例 #26
0
class TwitterBot(object):
    def __init__(self, config_filename=None):
        self.config_filename = config_filename
        self.config = RawConfigParser(
            defaults={
                # DEFAULTS
                # general
                'robust': 'False',
                # logging
                'loglevel': 'DEBUG',
                'logformat':
                '[%(asctime)s] %(levelname)s %(name)s: %(message)s',
                'dateformat': '%Y-%m-%dT%H:%M:%S',
                # irc
                'use_ssl': 'False',
                'nick': 'twitterbot',
                'password': '',
                'msg_prefix': '',
                'notification_command': 'NOTICE',
                # twitter
                'poll_interval': '60.0',
                'skip_old_tweets_on_start': 'True',
                'query': '',
                # urls
                'surround_urls_with_space': 'True',
                'use_expanded_urls': 'False',
                'follow_redirects': 'False',
                'follow_only_domains': '',
                'detect_urls_by_regex': 'False',
            })
        self.config.read(self.config_filename)

        # setup general
        self.be_robust = (self.config.has_section('general')
                          and self.config.getboolean('general', 'robust'))

        # setup logging
        self.log = logging.getLogger('twitterbot')
        self.log.setLevel(
            getattr(logging,
                    self.config.get('logging', 'loglevel').upper()))
        log_stream = logging.StreamHandler()
        log_stream.setFormatter(
            logging.Formatter(self.config.get('logging', 'logformat'),
                              self.config.get('logging', 'dateformat')))
        self.log.addHandler(log_stream)

        # setup twitter
        self.twitter = twitter.Api(
            consumer_key=self.config.get('oauth', 'consumer_key'),
            consumer_secret=self.config.get('oauth', 'consumer_secret'),
            access_token_key=self.config.get('oauth', 'access_token_key'),
            access_token_secret=self.config.get('oauth',
                                                'access_token_secret'))
        self.query = self.config.get('twitter', 'query')
        self.seen_tweets = set()  # set of ids

        # get url expansion preferences
        self.surround_urls_with_space = \
                self.config.getboolean('urls', 'surround_urls_with_space')
        self.use_expanded_urls = \
                self.config.getboolean('urls', 'use_expanded_urls')
        self.follow_redirects = \
                self.config.getboolean('urls', 'follow_redirects')
        self.follow_only_domains = [
            domain.strip() for domain in self.config.get(
                'urls', 'follow_only_domains').strip().split(',')
            if domain.strip()
        ]
        self.detect_urls_by_regex = \
                self.config.getboolean('urls', 'detect_urls_by_regex')

        # setup irc
        self.irc = irc.client.Reactor() if hasattr(
            irc.client, 'Reactor') else irc.client.IRC()
        self.irc.add_global_handler('privmsg', self.handle_privmsg)
        self.irc_server = self.irc.server()
        self.irc_server_name = self.config.get('irc', 'server')
        self.irc_use_ssl = self.config.getboolean('irc', 'use_ssl')
        self.irc_server_port = (self.config.getint(
            'irc', 'port') if self.config.has_option('irc', 'port') else None)
        self.nick = self.config.get('irc', 'nick')
        self.irc_password = self.config.get('irc', 'password')
        self.channels = [
            channel.strip() for channel in self.config.get(
                'irc', 'channels').strip().split(',') if channel.strip()
        ]

        # setup scheduled tasks
        self.scheduler = Scheduler([
            ScheduledTask(self.process_irc_events, delta=0.25),
            ScheduledTask(self.process_twitter,
                          delta=self.config.getfloat('twitter',
                                                     'poll_interval'))
        ])

    def handle_privmsg(self, connection, event):
        pass  #TODO:
        """
        help
        follow foo
        add-to-query foo
        alter query
        search foo
        # add notification in the channels
        """

    def process_irc_events(self):
        self.irc.process_once()

    def _get_tweets(self):
        """Returns: new twitter status objects."""
        tweets = list(reversed(self.twitter.GetHomeTimeline()))
        if self.query:
            tweets += list(reversed(self.twitter.GetSearch(self.query)))
        seen_tweets = set(tweet.id for tweet in tweets)

        tweets = [
            tweet for tweet in tweets if tweet.id not in self.seen_tweets
        ]
        if len(self.seen_tweets) < 100000:
            self.seen_tweets.update(seen_tweets)
        else:
            self.seen_tweets = seen_tweets
        return tweets

    def notify_channels(self, message):
        command = '%s %s :' % (self.config.get(
            'irc', 'notification_command'), ','.join(self.channels))

        for chunk in split_utf8_at_space(message.encode('utf8'),
                                         IRC_LINE_LIMIT - len(command)):
            self.irc_server.send_raw(command + chunk.decode('utf8'))

    def _follow_url(self, url):
        """Follows urls and returns the result."""
        self.log.debug('[following] %r', url)
        try:
            if self.follow_only_domains:
                if urllib.parse.urlparse(url).hostname not in \
                        self.follow_only_domains:
                    return url

            r = requests.head(url, allow_redirects=True)
            r.raise_for_status()
            result = r.url
            self.log.debug('[following] %r -> %r', url, result)
            return result

        except requests.ConnectionError as e:
            self.log.debug('[following] %r - connection error: %s', url, e)
        except Exception as e:
            self.log.exception('[following] %r error: %s', url, e, exc_info=e)
        return url

    def _handle_url_expansion(self, message, tweet_urls, max_link_length=None):
        """
        :param message: the tweet as text
        :param tweet_url: {short_url: expanded_url}
        :param max_link_length: if expanded urls shorten if final url is longer
        """
        if not (self.surround_urls_with_space, self.use_expanded_urls,
                self.follow_redirects):
            return message

        tweet_urls = (tweet_urls or {}).copy()

        self.log.debug("[expanding urls] (using urls %r) %r", tweet_urls,
                       message)

        if self.detect_urls_by_regex:
            non_urled = reduce(lambda s, url: s.replace(url, ' '),
                               sorted(tweet_urls, key=len, reverse=True),
                               message)
            detected_urls = re.findall("(https?://[^ )]+)", non_urled)
            tweet_urls.update((url, url) for url in detected_urls)

        if not self.use_expanded_urls:
            tweet_urls = dict((url, url) for url in tweet_urls)

        if self.follow_redirects:
            tweet_urls = dict((url, self._follow_url(value))
                              for (url, value) in tweet_urls.items())

        if tweet_urls:
            url_replace = lambda url: (
                (" ?%s ?"
                 if self.surround_urls_with_space else "%s") % re.escape(url))

            def get_final_url(matched_text):
                res = tweet_urls.get(matched_text.strip(), matched_text)
                if len(res) > max_link_length:
                    return matched_text
                else:
                    return res

            message = re.sub(
                '|'.join(
                    map(url_replace, sorted(tweet_urls, key=len,
                                            reverse=True))),
                lambda m: "{sep}{url}{sep}".format(
                    sep=' ' if self.surround_urls_with_space else '',
                    url=tweet_urls.get(m.group(0).strip(), m.group(0))),
                message)

        return message.strip()

    def _urls_to_dict(self, url_list):
        """Extracts urls from tweet status url list to {url: expanded}."""
        url_list = url_list or []
        return dict((u.url, u.expanded_url) for u in url_list)

    def _ircfy_tweet(self, tweet):
        """Takes a twitter status and outputs irc message."""
        message = tweet.text
        urls = tweet.urls
        if tweet.retweeted_status:
            #HACK: because iPhone sucks and does not correctly handle RT
            message = "RT @{0}: {1}".format(
                tweet.retweeted_status.user.screen_name,
                tweet.retweeted_status.text)
            urls = tweet.urls
        try:
            message = HTMLParser().unescape(message)
        except:
            self.log.exception("Unable to escape message %r", message)

        message = "{surround}{screen_name}{surround}: {message}".format(
            surround=IRC_BOLD,
            screen_name=tweet.user.screen_name,
            message=message)
        message = message.replace('\r', '').replace('\n', '  ')
        urls = self._urls_to_dict(urls)
        message = self._handle_url_expansion(message, urls, 440)
        return message

    def process_twitter(self):
        tweets = self._get_tweets()
        self.log.debug("Fetched %d new tweets", len(tweets))

        for tweet in tweets:
            message = self._ircfy_tweet(tweet)
            self.log.debug("[notifying] %r", message)
            self.notify_channels(message)

    def die(self):
        self.log.debug("Quiting...")
        self.irc_server.send_raw('quit')

    def run(self):
        self._connect_irc()

        if self.config.getboolean('twitter', 'skip_old_tweets_on_start'):
            self._get_tweets()

        while True:
            try:
                self.scheduler.run_forever()
            except KeyboardInterrupt:
                self.die()
                break
            except twitter.TwitterError as e:
                # these are mostly harmless - twitter being down or rate
                # limit exceeded
                self.log.info("Twitter error: %s", e)
            except http.client.BadStatusLine as e:
                # for whatever reason twitter sucks
                self.log.info("BadStatusLine (probably twitter error): %s", e)
            except irc.client.ServerNotConnectedError:
                self.log.debug("Not connected to irc server. "
                               "Trying to reconnect...")
                self._connect_irc()
            except UnicodeDecodeError:
                self.log.exception("Unicode exception: %s", e, exc_info=e)
                pass  # not good
            except Exception as e:
                self.log.exception("Unhandled exception: %s", e)
                if not self.be_robust:
                    raise

    def _connect_irc(self):
        self.log.debug('Connecting to %s...', self.irc_server_name)
        self.irc_server.connect(
            self.irc_server_name,
            self.irc_server_port or (self.irc_use_ssl and 6697 or 6667),
            self.nick,
            self.irc_password,
            connect_factory=(
                self.irc_use_ssl
                and irc.connection.Factory(wrapper=ssl.wrap_socket)
                # FIXME: there is no ssl verification
                or irc.connection.Factory()))
        self.log.debug('Connected to %s.', self.irc_server_name)
        for channel in self.channels:
            self.irc_server.join(channel)
        self.log.debug('Joined channels %s .', ','.join(self.channels))
コード例 #27
0
class Config(object):
    """This class is used to access/read config file, if it exists.

    :param location: the custom path to search for config file
    :type location: str or None
    """
    def __init__(self, location=None):
        self.location = location

        self.config_filename = 'glances.conf'

        self.parser = RawConfigParser()

        self._loaded_config_file = None
        self.load()

    def load(self):
        """Load a config file from the list of paths, if it exists."""
        for config_file in self.get_config_paths():
            if os.path.isfile(
                    config_file) and os.path.getsize(config_file) > 0:
                try:
                    if is_py3:
                        self.parser.read(config_file, encoding='utf-8')
                    else:
                        self.parser.read(config_file)
                    logger.info(
                        "Read configuration file '{0}'".format(config_file))
                except UnicodeDecodeError as e:
                    logger.error(
                        "Cannot decode configuration file '{0}': {1}".format(
                            config_file, e))
                    sys.exit(1)
                # Save the loaded configuration file path (issue #374)
                self._loaded_config_file = config_file
                break

    def get_loaded_config_file(self):
        """Return the loaded configuration file"""
        return self._loaded_config_file

    def get_config_paths(self):
        r"""Get a list of config file paths.

        The list is built taking into account of the OS, priority and location.

        * running from source: /path/to/glances/conf
        * per-user install: ~/.local/etc/glances (Unix-like only)
        * Linux: ~/.config/glances, /etc/glances
        * BSD: ~/.config/glances, /usr/local/etc/glances
        * Mac: ~/Library/Application Support/glances, /usr/local/etc/glances
        * Windows: %APPDATA%\glances

        The config file will be searched in the following order of priority:
            * /path/to/file (via -C flag)
            * /path/to/glances/conf
            * user's local directory (per-user install settings)
            * user's home directory (per-user settings)
            * {/usr/local,}/etc directory (system-wide settings)
        """
        paths = []
        conf_path = os.path.realpath(
            os.path.join(work_path, '..', '..', 'conf'))

        if self.location is not None:
            paths.append(self.location)

        if os.path.exists(conf_path):
            paths.append(os.path.join(conf_path, self.config_filename))

        if not is_windows:
            paths.append(
                os.path.join(os.path.expanduser('~/.local'), 'etc', appname,
                             self.config_filename))

        if is_linux or is_bsd:
            paths.append(
                os.path.join(
                    os.environ.get('XDG_CONFIG_HOME')
                    or os.path.expanduser('~/.config'), appname,
                    self.config_filename))
            if hasattr(sys, 'real_prefix') or is_bsd:
                paths.append(
                    os.path.join(sys.prefix, 'etc', appname,
                                 self.config_filename))
            else:
                paths.append(
                    os.path.join('/etc', appname, self.config_filename))
        elif is_mac:
            paths.append(
                os.path.join(
                    os.path.expanduser('~/Library/Application Support/'),
                    appname, self.config_filename))
            paths.append(
                os.path.join(sys_prefix, 'etc', appname, self.config_filename))
        elif is_windows:
            paths.append(
                os.path.join(os.environ.get('APPDATA'), appname,
                             self.config_filename))

        return paths

    def items(self, section):
        """Return the items list of a section."""
        return self.parser.items(section)

    def has_section(self, section):
        """Return info about the existence of a section."""
        return self.parser.has_section(section)

    def get_option(self, section, option):
        """Get the float value of an option, if it exists."""
        try:
            value = self.parser.getfloat(section, option)
        except NoOptionError:
            return
        else:
            return value

    def get_raw_option(self, section, option):
        """Get the raw value of an option, if it exists."""
        try:
            value = self.parser.get(section, option)
        except NoOptionError:
            return
        else:
            return value
コード例 #28
0
class Settings:
    def __init__(self):
        self.__config = RawConfigParser()
        self.items = self.__config.items
        if os.name == 'posix':
            aphom = expanduser("~/.config")
            if isdir(aphom):
                self.__app_home = join(aphom, "XRCEA")
            else:
                self.__app_home = expanduser("~/.XRCEA")
        elif os.name == 'nt':
            if isdir(expanduser("~/Application Data")):
                self.__app_home = expanduser("~/Application Data/XRCEA")
            else:
                self.__app_home = expanduser("~/XRCEA")
        else:
            self.__app_home = normpath(expanduser("~/XRCEA"))
        if isfile(self.__app_home):
            os.remove(self.__app_home)
        if not isdir(self.__app_home):
            os.mkdir(self.__app_home, 0o755)
        self.__config.read(self.get_home("XRCEA.cfg"))
        self.declare_section("PALETTE")
        self.__default_colors = {}

    def declare_section(self, section):
        if not self.__config.has_section(section):
            self.__config.add_section(section)

    def get(self, name, default=None, section='DEFAULT'):
        if not self.__config.has_option(section, name):
            return default
        if default is not None:
            deft = type(default)
        else:
            deft = str
        try:
            if deft is float:
                return self.__config.getfloat(section, name)
            if deft is int:
                return self.__config.getint(section, name)
            if deft is bool:
                return self.__config.getboolean(section, name)
            return deft(self.__config.get(section, name))
        except ValueError:
            print("Warning: cannot convert {} into {}".format(
                repr(self.__config.get(section, name)), deft.__name__))
            return default

    def get_color(self, name):
        if name is None:
            return
        if not self.__config.has_option("PALETTE", name):
            return self.__default_colors.get(name)
        return self.__config.get("PALETTE", name)

    def set(self, name, val, section='DEFAULT'):
        if not isinstance(val, str):
            val = repr(val)
        self.__config.set(section, name, val)

    def set_color(self, name, val):
        self.__config.set("PALETTE", name, val)

    def add_default_colors(self, colors):
        self.__default_colors.update(colors)

    def get_home(self, name=''):
        if name:
            return join(self.__app_home, name)
        return self.__app_home

    def save(self):
        with open(self.get_home("XRCEA.cfg"), "w") as fobj:
            self.__config.write(fobj)
コード例 #29
0
ファイル: c.py プロジェクト: Toqozz/yarn
gap = parser.getint('lemonbar', 'gap')
direction = parser.get('lemonbar', 'direction')
background = parser.get('lemonbar', 'background')
foreground = parser.get('lemonbar', 'foreground')
width = parser.getint('lemonbar', 'width')
height = parser.getint('lemonbar', 'height')
offset = parser.getint('lemonbar', 'offset')
max_notify = parser.getint('lemonbar', 'max_notify')

#[scroll]
scroll = parser.getboolean('scroll', 'scroll')
half_scroll = parser.getboolean('scroll', 'half_scroll')
scroll_space_side = parser.get('scroll', 'scroll_space_side')
scroll_space = parser.getint('scroll', 'scroll_space')
scroll_len = parser.getint('scroll', 'scroll_len')
scroll_interval = parser.getfloat('scroll', 'scroll_interval')
scroll_reverse = parser.getboolean('scroll', 'scroll_reverse')
scroll_bounce = parser.getboolean('scroll', 'scroll_bounce')
bounce_pause = parser.getint('scroll', 'bounce_pause')
loopback_amount = parser.getint('scroll', 'loopback_amount')
loopback_distance = parser.getint('scroll', 'loopback_distance')
ellipse = parser.getboolean('scroll', 'ellipse')

#[style]
summary_font = parser.get('style', 'summary_font')
body_font = parser.get('style', 'body_font')
summary_chars = parser.getint('style', 'summary_chars')

#[other]
timeout = parser.getint('other', 'timeout')
sleep = parser.getint('other', 'sleep')
コード例 #30
0
ファイル: config.py プロジェクト: stressrelief/poezio
 def getfloat(self, option, section=DEFSECTION):
     """
     get a value and returns it as a float
     """
     return RawConfigParser.getfloat(self, section, option)
コード例 #31
0
ファイル: classify.py プロジェクト: adambloniarz/CAGe
# hardcoded thresholds determining 'low' vs 'high' complexity regions
# thresholds determined via manual inspection of clusters (see R code)

# thresholds for the human chr20 data
#COV_MIN = 0.45
#COV_MAX = 0.7
#SEQ_ERR_MAX = 0.02
#MUT_MAX = 0.003

# thresholds for the Venter chr20 subset data
config = RawConfigParser()
config_file = sys.argv[4]
config.read(config_file)

COV_MIN = config.getfloat("cutoffs", "cov_min")
COV_MAX = config.getfloat("cutoffs", "cov_max")
ERR_MAX = config.getfloat("cutoffs", "err_max")
MUT_MAX = config.getfloat("cutoffs", "mut_max")
INDEL_MAX = config.getfloat("cutoffs", "indel_max")
ZETA_MAX = config.getfloat("cutoffs", "zeta_max")

def add_region(all_regions, start_pos, end_pos):
  if len(all_regions) > 0:
    end_pos_last = all_regions[-1][1]
    if end_pos_last + 1 == start_pos: # regions on either side of changepoint have same designation 
      all_regions[-1][1] = end_pos
    else:
      all_regions.append([start_pos, end_pos])
  else:
    all_regions.append([start_pos, end_pos])
コード例 #32
0
 def getfloat(self, section, option):
     if not self.has_option(section, option):
         return self.DEF_FLOAT
     return RawConfigParser.getfloat(self, section, option)
コード例 #33
0
ファイル: jd_OpenCard.py プロジェクト: zing555/JD-Script
try:
    configinfo = RawConfigParser()
    try:
        configinfo.read(pwd + "OpenCardConfig.ini", encoding="UTF-8")
    except Exception as e:
        with open(pwd + "OpenCardConfig.ini", "r", encoding="UTF-8") as config:
            getConfig = config.read().encode('utf-8').decode('utf-8-sig')
        with open(pwd + "OpenCardConfig.ini", "w", encoding="UTF-8") as config:
            config.write(getConfig)
        try:
            configinfo.read(pwd + "OpenCardConfig.ini", encoding="UTF-8")
        except:
            configinfo.read(pwd + "OpenCardConfig.ini", encoding="gbk")
    cookies = configinfo.get('main', 'JD_COOKIE')
    openCardBean = configinfo.getint('main', 'openCardBean')
    sleepNum = configinfo.getfloat('main', 'sleepNum')
    record = configinfo.getboolean('main', 'record')
    onlyRecord = configinfo.getboolean('main', 'onlyRecord')
    memory = configinfo.getboolean('main', 'memory')
    printlog = configinfo.getboolean('main', 'printlog')
    isRemoteSid = configinfo.getboolean('main', 'isRemoteSid')
    TG_BOT_TOKEN = configinfo.get('main', 'TG_BOT_TOKEN')
    TG_USER_ID = configinfo.get('main', 'TG_USER_ID')
    PUSH_PLUS_TOKEN = configinfo.get('main', 'PUSH_PLUS_TOKEN')
    TG_PROXY_IP = configinfo.get('main', 'TG_PROXY_IP')
    TG_PROXY_PORT = configinfo.get('main', 'TG_PROXY_PORT')
    TG_API_HOST = configinfo.get('main', 'TG_API_HOST')
    QYWX_AM = configinfo.get('main', 'QYWX_AM')
    Concurrent = configinfo.getboolean('main', 'Concurrent')
    DoubleThread = configinfo.getboolean('main', 'DoubleThread')
    BARK = configinfo.get('main', 'BARK')
コード例 #34
0
ファイル: config.py プロジェクト: mandymak/easywall
class Config(object):
    """
    This class is a generic class for configuration
    It is a wrapper around the default configparser and contains basic functionality.

    [Methods]
    get_value: retrieve a value from a config file
    set_value: set a value in the configuration and write the config file to disk
    get_sections: get a list of all possible config sections

    [Raises]
    FileNotFoundError: When the configuration file was not found a exception is thrown.
    Exception: when the configparser failed to read the config file a exception is thrown.
    """

    def __init__(self, config_file_path: str) -> None:
        if not file_exists(config_file_path):
            raise FileNotFoundError("config file '{}' not found".format(config_file_path))
        self.config_file_path = config_file_path
        self.configlib = RawConfigParser()
        try:
            self.configlib.read(self.config_file_path)
        except ParsingError as exc:
            raise ParsingError(
                "{} is not readable by RawConfigParser. \n inner exception: {}".format(
                    config_file_path, format_exception(exc)))

    def get_value(self, section: str, key: str) -> Union[bool, int, float, str]:
        """
        Returns a value from a given section of the configuration.

        [Data Types] String, Float, Integer, Boolean
        """
        value = ""
        try:
            value = self.configlib[section][key]
        except KeyError:
            error("Could not find key {} in section {}".format(key, section))
            info("Valid sections are: ")
            info("{}".format(self.get_sections()))
        if value in ["yes", "no", "true", "false", "on", "off"]:
            return self.configlib.getboolean(section, key)
        if is_int(value):
            return self.configlib.getint(section, key)
        if is_float(value):
            return self.configlib.getfloat(section, key)
        return value

    def set_value(self, section: str, key: str, value: str) -> bool:
        """
        Writes a key, value pair into memory configuration and writes it to config file

        [Data Types] bool
        """
        result = True
        try:
            self.configlib[section][key] = value
        except KeyError as exc:
            message = "Failed to write data to configuration: \n " + \
                "section: '{}' \n key: '{}' \n value: '{}' \n " + \
                "valid sections are: \n {} \n inner error: \n {}"
            error(message.format(section, key, value, self.get_sections(), format_exception(exc)))
            result = False

        if result:
            with open(self.config_file_path, 'w') as configfile:
                self.configlib.write(configfile)

        return result

    def get_sections(self) -> list:
        """
        Return a list of the configuration section names/keys
        [WARNING] The name [DEFAULT] is excluded here if used!

        [Data Types] list
        """
        return self.configlib.sections()
コード例 #35
0
    parsed_value = ast.literal_eval(value)
    if isinstance(parsed_value, list):
        options[key] = parsed_value
    else:
        options[key] = [parsed_value]


# overwrite default values for options
if config.has_section(config_name):
    options['train_log_dir'] = config.get(config_name, 'train_log_dir')
    options['output_dir'] = config.get(config_name, 'output_dir')
    options['training_file'] = config.get(config_name, 'training_file')
    options['noise_dims'] = config.getint(config_name, 'noise_dims')
    options['latent_dims'] = config.getint(config_name, 'latent_dims')
    options['batch_size'] = config.getint(config_name, 'batch_size')
    options['gen_lr'] = config.getfloat(config_name, 'gen_lr')
    options['dis_lr'] = config.getfloat(config_name, 'dis_lr')
    options['lambda'] = config.getfloat(config_name, 'lambda')
    options['epochs'] = config.get(config_name, 'epochs')
    options['type_latent'] = config.get(config_name, 'type_latent')
    options['g_weight_decay_gen'] = config.get(config_name,
                                               'g_weight_decay_gen')
    options['d_weight_decay_dis'] = config.get(config_name,
                                               'd_weight_decay_dis')

parse_range('epochs')

# Set up the input
input_data = pickle.load(open(options['training_file'], 'rb'),
                         encoding='latin1')
rectangles = np.array(list(map(lambda x: x[0], input_data['data'])),
コード例 #36
0
ファイル: proc_gimms_modis_ndvi.py プロジェクト: zzwei1/nuwrf
def main():

    logging.info("Start NDVI processing")

    # First, check for command line flags
    logging.info('Reading command line')
    parser = optparse.OptionParser(usage="%prog [--help] [-D] [-S]")
    parser.add_option("-D",
                      "--download",
                      action="store_true",
                      dest="downloadFiles",
                      default=False,
                      help="Download files from remote GIMMS server")
    parser.add_option("-S",
                      "--save",
                      action="store_true",
                      dest="saveDownloadedFiles",
                      default=False,
                      help="Save downloaded files after processing")
    (options, args) = parser.parse_args()

    # Next, read config file
    logging.info('Reading config file')
    try:
        try:
            config = ConfigParser.RawConfigParser()
        except:
            config = RawConfigParser()
        config.read('gimms_modis.cfg')
        satellite = config.get(SECTION_NAME, 'satellite')
        startYear = config.getint(SECTION_NAME, 'start_year')
        startMonth = config.getint(SECTION_NAME, 'start_month')
        startDay = config.getint(SECTION_NAME, 'start_day')
        endYear = config.getint(SECTION_NAME, 'end_year')
        endMonth = config.getint(SECTION_NAME, 'end_month')
        endDay = config.getint(SECTION_NAME, 'end_day')
        ndviBareness4WrfPath = \
            config.get(SECTION_NAME,'ndvi_bareness_4_wrf_path')
        topWorkDir = config.get(SECTION_NAME, 'top_work_dir')
        if not options.downloadFiles:
            topLocalArchiveDir = \
                config.get(SECTION_NAME,'top_local_archive_dir')
        lowerLeftLat = config.getfloat(SECTION_NAME, 'lower_left_lat')
        lowerLeftLon = config.getfloat(SECTION_NAME, 'lower_left_lon')
        upperRightLat = config.getfloat(SECTION_NAME, 'upper_right_lat')
        upperRightLon = config.getfloat(SECTION_NAME, 'upper_right_lon')
        ndviBarenessThreshold1 = \
            config.getfloat(SECTION_NAME,'ndvi_bareness_threshold_1')
        ndviBarenessThreshold2 = \
            config.getfloat(SECTION_NAME,'ndvi_bareness_threshold_2')
    except ConfigParser.Error:
        logging.critical("Problem with the config file!")
        raise

    # Next, sanity check config file options
    logging.info("Sanity checking config file settings")
    satellite = satellite.upper()
    if satellite not in ["TERRA", "AQUA"]:
        fatal("Invalid satellite %s selected for MODIS NDVI!" \
                  %(satellite))
    try:
        startdate = datetime.date(startYear, startMonth, startDay)
    except ValueError:
        logging.critical("Invalid start date!")
        raise
    try:
        enddate = datetime.date(endYear, endMonth, endDay)
    except ValueError:
        logging.critical("Invalid end date!")
        raise

    if enddate < startdate:
        fatal("Start date is after end date!")
    if startdate.year < 2000:
        fatal("Start date year is before 2000, no NDVI data available!")
    if not os.path.exists(ndviBareness4WrfPath):
        fatal("%s does not exist!" % (ndviBareness4WrfPath))
    if not os.access(ndviBareness4WrfPath, os.X_OK):
        fatal("%s is not executable!" % (ndviBareness4WrfPath))
    if not os.path.exists(topWorkDir):
        fatal("%s does not exist!" % (topWorkDir))
    if not os.access(topWorkDir, os.R_OK | os.W_OK | os.X_OK):
        fatal("%s does not have read/write/exec permission!" % (topWorkDir))
    if not options.downloadFiles:
        if not os.path.exists(topLocalArchiveDir):
            fatal("%s does not exist!" % (topLocalArchiveDir))
        if not os.access(topLocalArchiveDir, os.R_OK | os.X_OK):
            fatal("%s does not have read/exec permission!" \
                      %(topLocalArchiveDir))
    if lowerLeftLat < -60. or lowerLeftLat > 80.:
        fatal("Invalid lower left latitude!")
    if lowerLeftLon < -180. or lowerLeftLon > 180.:
        fatal("Invalid lower left longtitude!")
    if upperRightLat < -60. or upperRightLat > 80.:
        fatal("Invalid upper right latitude!")
    if upperRightLon < -180. or upperRightLon > 180.:
        fatal("Invalid upper right longtitude!")
    if lowerLeftLat > upperRightLat:
        fatal("Invalid latitude bounds!")
    if lowerLeftLon > upperRightLon:
        fatal("Invalid longitude bounds!")
    if ndviBarenessThreshold1 < 0 or ndviBarenessThreshold1 > 1:
        fatal("Invalid value for ndvi_bareness_threshold_1!")
    if ndviBarenessThreshold2 < 0 or ndviBarenessThreshold2 > 1:
        fatal("Invalid value for ndvi_bareness_threshold_2!")

    # Loop through the dates.  We'll download (if necessary) and write to WPS
    # format.
    foundData = False
    curdate = startdate
    while curdate <= enddate:
        year = curdate.year
        jday = curdate.timetuple()[7]

        # Search for pre-existing local files, unless downloading is
        # specified at command line.
        if not options.downloadFiles:
            tmpArchiveDir = "%s/%4.4d" % (topLocalArchiveDir, year)
            if not os.path.exists(tmpArchiveDir):
                fatal("Cannot access %s" % (tmpArchiveDir))
            tmpArchiveDir = "%s/%4.4d/%3.3d" % (topLocalArchiveDir, year, jday)
            if not os.path.exists(tmpArchiveDir):
                logging.warning("No %s MODIS NDVI data available for %s" \
                                    %(satellite,curdate))
                curdate += datetime.timedelta(days=1)
                continue
            files = glob.glob("%s/%s*" %
                              (tmpArchiveDir, tiff_prefix[satellite]))
            if len(files) == 0:
                logging.warning("No %s MODIS NDVI data available for %s" \
                                    %(satellite,curdate))
                curdate += datetime.timedelta(days=1)
                continue
            foundData = True

        else:

            # Open anonymous FTP connection
            try:
                ftp = ftplib.FTP(SERVER_HOST)
                ftp.login()
            except ftplib.all_errors:
                logging.critical("Problem logging into GIMMS FTP server!")
                raise

            # See if requested year is available
            dirname = top_dir[satellite] + "/%4.4d" % (year)
            try:
                ftp.cwd(dirname)
            except ftplib.all_errors:
                logging.critical( \
                    "Cannot access directory on GIMMS FTP server!" %(dirname))
                raise

            # See if requested day of year is available
            try:
                jdays = ftp.nlst()
            except ftplib.all_errors:
                logging.critical( \
                    "Cannot get directory contents on GIMMS FTP server!")
                raise
            s_jday = "%3.3d" % (jday)
            if s_jday not in jdays:
                logging.warning("No NDVI data available for %s" % (curdate))
                try:
                    ftp.quit()
                except ftplib.all_errors:
                    logging.critical( \
                        "Cannot close connection to GIMMS FTP server!")
                    raise
                curdate += datetime.timedelta(days=1)
                continue
            dirname = "%3.3d" % (jday)
            try:
                ftp.cwd(dirname)
            except ftplib.all_errors:
                logging.critical(\
                    "Cannot access directory on GIMMS FTP server!")
                raise

            # Create local output directory
            tmpDownloadDir = "%s/download/%4.4d/%3.3d" % (topWorkDir, year,
                                                          jday)
            if not os.path.exists(tmpDownloadDir):
                try:
                    os.makedirs(tmpDownloadDir)
                except os.error:
                    logging.critical("Problem creating directory %s" \
                                         %(tmpDownloadDir))
                    raise
            try:
                os.chdir(tmpDownloadDir)
            except os.error:
                logging.critical("Problem accessing directory %s" \
                                     %(tmpDownloadDir))
                raise

            # Loop through the files and download the GZIPPED GeoTIFF files.
            # Only select the tiles needed to fill the requested lat/lon
            # bounds.
            try:
                remotefiles_all = ftp.nlst()
            except ftplib.all_errors:
                logging.critical("Problem listing files on GIMMS FTP server!")
                raise
            remotefiles = []
            prefix = "%s.A%4.4d%3.3d.08d.latlon." \
                %(tiff_prefix[satellite],year,jday)
            west_x = int(math.floor((180 + lowerLeftLon) / 9))
            south_y = int(math.floor((90 - lowerLeftLat) / 9))
            east_x = int(math.floor((180 + upperRightLon) / 9))
            north_y = int(math.floor((90 - upperRightLat) / 9))
            for x in range(west_x, east_x + 1):
                for y in range(north_y, south_y + 1):
                    remotefile = prefix + \
                        "x%2.2dy%2.2d.6v1.NDVI.tif.gz" %(x,y)
                    if remotefile not in remotefiles_all:
                        logging.warning("%s is not on remote server" \
                                            %(remotefile))
                        continue
                    remotefiles.append(remotefile)
                    try:
                        fhandle = open(remotefile, 'wb')
                    except IOError:
                        logging.critical("Cannot open %s!" % (remotefile))
                        raise
                    logging.info("Fetching %s" % (remotefile))
                    try:
                        ftp.retrbinary('RETR %s' % (remotefile), fhandle.write)
                    except ftplib.all_errors:
                        logging.critical("Cannot download %s!" % (remotefile))
                        raise
                    fhandle.close()

            # Close the FTP connection.
            try:
                ftp.quit()
            except ftplib.all_errors:
                logging.critical(\
                    "Problem closing connection to GIMMS FTP server")
                raise

            # Clean up for this day.
            del remotefiles_all
            if len(remotefiles) == 0:
                logging.warning("No NDVI files found for %s " % (curdate))
                curdate += datetime.timedelta(days=1)
                continue
            del remotefiles
            foundData = True

            # Now uncompress the files
            logging.info("Running gunzip...")
            cmd = 'gunzip *.tif.gz'
            try:
                status = os.system(cmd)
                if status != 0:
                    fatal("Problem running gunzip on GeoTIFF files!")
            except os.error:
                logging.critical("Problem running gunzip on GeoTIFF files!")
                raise

        # Now create namelist file for ndviBareness4Wrf
        try:
            os.chdir(topWorkDir)
        except os.error:
            logging.critical("Cannot access work directory %s!" % (topWorkDir))
            raise
        logging.info("Creating namelist.ndviBareness4Wrf")
        try:
            fd = open("namelist.ndviBareness4Wrf", "w")
            fd.write("\n &settings\n")
            if satellite == "AQUA":
                fd.write("   ndviFormat = 10\n")
            elif satellite == "TERRA":
                fd.write("   ndviFormat = 11\n")
            fd.write("   lowerLeftLat = %f\n" % (lowerLeftLat))
            fd.write("   lowerLeftLon = %f\n" % (lowerLeftLon))
            fd.write("   upperRightLat = %f\n" % (upperRightLat))
            fd.write("   upperRightLon = %f\n" % (upperRightLon))
            fd.write("   ndviBarenessThreshold1 = %f\n" \
                         %(ndviBarenessThreshold1))
            fd.write("   ndviBarenessThreshold2 = %f\n" \
                         %(ndviBarenessThreshold2))
            if not options.downloadFiles:
                fd.write("   inputDirectory = '%s'\n" % (topLocalArchiveDir))
            else:
                fd.write("   inputDirectory = '%s/download'\n" % (topWorkDir))
            fd.write("   year = %d\n" % (curdate.year))
            fd.write("   month = %d\n" % (curdate.month))
            fd.write("   dayOfMonth = %d\n" % (curdate.day))
            day_of_year = datetime.date(curdate.year, curdate.month,
                                        curdate.day)
            doy = day_of_year.strftime('%j')
            fd.write("   dayOfYear = %s\n" % (doy))
            fd.write("   outputDirectory = './'\n")
            fd.write("   outputFilePrefix = 'GIMMS_MODIS'\n")
            fd.write("   /\n")
            fd.close()
        except IOError:
            logging.critical("Problem writing to namelist.ndviBareness4Wrf!")
            raise

        # Now run ndviBareness4Wrf
        logging.info("Calling ndviBareness4Wrf...")
        try:
            status = os.system(ndviBareness4WrfPath)
            if status != 0:
                fatal("Problem running ndviBareness4Wrf!")
        except os.error:
            logging.critical("Problem running ndviBareness4Wrf!")
            raise

        # Optionally clean up downloaded files.  Pre-existing files will not
        # be affected.
        if options.downloadFiles:
            if not options.saveDownloadedFiles:
                logging.debug("Removing %s/download" % (topWorkDir))
                try:
                    shutil.rmtree("%s/download" % (topWorkDir))
                except shutil.Error:
                    logging.critical("Problem deleting %s/download!" \
                                         %(topWorkDir))
                    raise
            else:
                logging.debug("Saving %s" % (tmpDownloadDir))

        # Now move to next day
        curdate += datetime.timedelta(days=1)

    # The end
    logging.info("End NDVI processing")
    if not foundData:
        fatal("No %s MODIS NDVI files were processed!" % (satellite))
コード例 #37
0
ファイル: config.py プロジェクト: weblate/quodlibet
class Config:
    """A wrapper around RawConfigParser.

    Provides a ``defaults`` attribute of the same type which can be used
    to set default values.
    """
    def __init__(self, version=None, _defaults=True):
        """Use read() to read in an existing config file.

        version should be an int starting with 0 that gets incremented if you
        want to register a new upgrade function. If None, upgrade is disabled.
        """

        self._config = ConfigParser(dict_type=_sorted_dict)
        self.defaults = None
        if _defaults:
            self.defaults = Config(_defaults=False)
        self._version = version
        self._loaded_version = None
        self._upgrade_funcs = []

    def _do_upgrade(self, func):
        assert self._loaded_version is not None
        assert self._version is not None

        old_version = self._loaded_version
        new_version = self._version
        if old_version != new_version:
            print_d("Config upgrade: %d->%d (%r)" %
                    (old_version, new_version, func))
            func(self, old_version, new_version)

    def get_version(self):
        """Get the version of the loaded config file (for testing only)

        Raises Error if no file was loaded or versioning is disabled.
        """

        if self._version is None:
            raise Error("Versioning disabled")

        if self._loaded_version is None:
            raise Error("No file loaded")

        return self._loaded_version

    def register_upgrade_function(self, function):
        """Register an upgrade function that gets called at each read()
        if the current config version and the loaded version don't match.

        Can also be registered after read was called.

        function(config, old_version: int, new_version: int) -> None
        """

        if self._version is None:
            raise Error("Versioning disabled")

        self._upgrade_funcs.append(function)
        # after read(), so upgrade now
        if self._loaded_version is not None:
            self._do_upgrade(function)
        return function

    def reset(self, section, option):
        """Reset the value to the default state"""

        assert self.defaults is not None

        try:
            self._config.remove_option(section, option)
        except NoSectionError:
            pass

    def options(self, section):
        """Returns a list of options available in the specified section."""

        try:
            options = self._config.options(section)
        except NoSectionError:
            if self.defaults:
                return self.defaults.options(section)
            raise
        else:
            if self.defaults:
                try:
                    options.extend(self.defaults.options(section))
                    options = list_unique(options)
                except NoSectionError:
                    pass
            return options

    def get(self, section, option, default=_DEFAULT):
        """get(section, option[, default]) -> str

        If default is not given or set, raises Error in case of an error
        """

        try:
            return self._config.get(section, option)
        except Error:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.get(section, option)
                    except Error:
                        pass
                raise
            return default

    def gettext(self, *args, **kwargs):
        value = self.get(*args, **kwargs)
        # make sure there are no surrogates
        value.encode("utf-8")
        return value

    def getbytes(self, section, option, default=_DEFAULT):
        try:
            value = self._config.get(section, option)
            value = value.encode("utf-8", "surrogateescape")
            return value
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getbytes(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getboolean(self, section, option, default=_DEFAULT):
        """getboolean(section, option[, default]) -> bool

        If default is not given or set, raises Error in case of an error
        """

        try:
            return self._config.getboolean(section, option)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getboolean(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getint(self, section, option, default=_DEFAULT):
        """getint(section, option[, default]) -> int

        If default is not give or set, raises Error in case of an error
        """

        try:
            return int(self._config.getfloat(section, option))
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getint(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getfloat(self, section, option, default=_DEFAULT):
        """getfloat(section, option[, default]) -> float

        If default is not give or set, raises Error in case of an error
        """

        try:
            return self._config.getfloat(section, option)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getfloat(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def getstringlist(self, section, option, default=_DEFAULT):
        """getstringlist(section, option[, default]) -> list

        If default is not given or set, raises Error in case of an error.
        Gets a list of strings, using CSV to parse and delimit.
        """

        try:
            value = self._config.get(section, option)

            parser = csv.reader([value],
                                lineterminator='\n',
                                quoting=csv.QUOTE_MINIMAL)
            try:
                vals = next(parser)
            except (csv.Error, ValueError) as e:
                raise Error(e)
            return vals
        except Error as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getstringlist(section, option)
                    except Error:
                        pass
                raise Error(e)
            return default

    def setstringlist(self, section, option, values):
        """Saves a list of unicode strings using the csv module"""

        sw = StringIO()
        values = [str(v) for v in values]

        writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(values)
        self.set(section, option, sw.getvalue())

    def setlist(self, section, option, values, sep=","):
        """Saves a list of str using ',' as a separator and \\ for escaping"""

        values = [str(v) for v in values]
        joined = join_escape(values, sep)
        self.set(section, option, joined)

    def getlist(self, section, option, default=_DEFAULT, sep=","):
        """Returns a str list saved with setlist()"""

        try:
            value = self._config.get(section, option)
            return split_escape(value, sep)
        except (Error, ValueError) as e:
            if default is _DEFAULT:
                if self.defaults is not None:
                    try:
                        return self.defaults.getlist(section, option, sep=sep)
                    except Error:
                        pass
                raise Error(e)
            return default

    def set(self, section, option, value):
        """Saves the string representation for the passed value

        Don't pass unicode, encode first.
        """

        if isinstance(value, bytes):
            raise TypeError("use setbytes")

        # RawConfigParser only allows string values but doesn't
        # scream if they are not (and it only fails before the
        # first config save..)
        if not isinstance(value, str):
            value = str(value)

        try:
            self._config.set(section, option, value)
        except NoSectionError:
            if self.defaults and self.defaults.has_section(section):
                self._config.add_section(section)
                self._config.set(section, option, value)
            else:
                raise

    def settext(self, section, option, value):
        value = str(value)

        # make sure there are no surrogates
        value.encode("utf-8")

        self.set(section, option, value)

    def setbytes(self, section, option, value):
        assert isinstance(value, bytes)

        value = value.decode("utf-8", "surrogateescape")

        self.set(section, option, value)

    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb") as fileobj:
                temp = StringIO()
                self._config.write(temp)
                data = temp.getvalue().encode("utf-8", "surrogateescape")
                fileobj.write(data)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)

    def clear(self):
        """Remove all sections."""

        for section in self._config.sections():
            self._config.remove_section(section)

    def is_empty(self):
        """Whether the config has any sections"""

        return not self._config.sections()

    def read(self, filename):
        """Reads the config from `filename` if the file exists,
        otherwise does nothing

        Can raise EnvironmentError, Error.
        """

        try:
            with open(filename, "rb") as fileobj:
                fileobj = StringIO(fileobj.read().decode(
                    "utf-8", "surrogateescape"))
                self._config.readfp(fileobj, filename)
        except (IOError, OSError):
            return

        # don't upgrade if we just created a new config
        if self._version is not None:
            self._loaded_version = self.getint("__config__", "version", -1)
            for func in self._upgrade_funcs:
                self._do_upgrade(func)

    def has_option(self, section, option):
        """If the given section exists, and contains the given option"""

        return self._config.has_option(
            section, option) or (self.defaults
                                 and self.defaults.has_option(section, option))

    def has_section(self, section):
        """If the given section exists"""

        return self._config.has_section(section) or (
            self.defaults and self.defaults.has_section(section))

    def remove_option(self, section, option):
        """Remove the specified option from the specified section

        Can raise Error.
        """

        return self._config.remove_option(section, option)

    def add_section(self, section):
        """Add a section named section to the instance if it not already
        exists."""

        if not self._config.has_section(section):
            self._config.add_section(section)
コード例 #38
0
            filename = "out_%s.svg" % counter
    else:
        filename = args[0]
        if os.path.exists(filename):
            sys.stdout.write("File %s exists\n" % filename)
            sys.exit(1)

    cfgfile = arg_dict.get('-c',"default.cfg")

    outfile = open(filename,"w")
    svg_version=1.1

    config = RawConfigParser()
    config.read(cfgfile)

    coaster_radius = config.getfloat('Coaster', 'radius')
    coaster_boarder = config.getfloat('Coaster', 'boarder')
    min_bubble_gap = config.getfloat('Bubbles', 'min_gap')
    small_bubble_stroke = config.get('Bubbles', 'small_bubble.stroke')
    small_bubble_fill = config.get('Bubbles', 'small_bubble.fill')
    small_bubble = {
        "mean" : config.getfloat('Bubbles', 'small_bubble.mean'),
        "deviation" : config.getfloat('Bubbles', 'small_bubble.deviation'),
        "stroke" : small_bubble_stroke,
        "fill" : small_bubble_fill
    }
    large_bubble_stroke = config.get('Bubbles', 'large_bubble.stroke')
    large_bubble_fill = config.get('Bubbles', 'large_bubble.fill')
    large_bubble = {
        "mean" : config.getfloat('Bubbles', 'large_bubble.mean'),
        "deviation" : config.getfloat('Bubbles', 'large_bubble.deviation'),