Пример #1
0
class Config(object):
    """use singleton avoid global variables"""
    __metaclass__ = Singleton

    DEFAULT_CFILE = abspath(join(dirname(__file__), '../../conf/config.ini'))
    ACTUAL_CFILE = None
    SECTION_NAME = 'main'

    def __init__(self):
        self.load_config()

    def load_config(self):
        config_file = self.__class__.ACTUAL_CFILE or self.__class__.DEFAULT_CFILE
        self._cfg = SafeConfigParser()
        self._cfg.read([
            config_file,
        ])

    def get(self, option, section=None, value_type=str):
        return self._cfg._get(section or self.__class__.SECTION_NAME,
                              value_type, option)

    def __getattr__(self, option):
        try:
            return self.get(option)
        except NoOptionError:
            log.debug("Got an unknown config: %s" % option)
            return None
Пример #2
0
class Config(object):
    """use singleton avoid global variables"""
    __metaclass__ = Singleton

    SECTION_NAME = 'main'
    ACTUAL_CONFIG_FILE = None
    DEFAULT_CONFIG_FILE = abspath(join(dirname(__file__),
                                       '../conf/config.ini'))
    def __init__(self):
        self.load_config()

    def load_config(self):
        config_file = self.__class__.ACTUAL_CONFIG_FILE or self.__class__.DEFAULT_CONFIG_FILE
        self._cfg = SafeConfigParser()
        self._cfg.read([config_file, ])

    def get(self, option, section=None, value_type=str):
        return self._cfg._get(section or self.__class__.SECTION_NAME, value_type, option)

    def __getattr__(self, option):
        try:
            return self.get(option)
        except NoOptionError as e:
            print str(e)
            return None
Пример #3
0
class Config(object):
    """use singleton avoid global variables"""
    __metaclass__ = Singleton

    DEFAULT_CONFIG_FILE = None
    ACTUAL_CONFIG_FILE = None
    SECTION_NAME = 'main'

    def __init__(self, config_name):
        # 原来是这里弄错了,应该是config
        self.__class__.DEFAULT_CONFIG_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../config/{0}.ini'.format(config_name)))
        self.load_config()

    def load_config(self):
        config_file = self.__class__.ACTUAL_CONFIG_FILE or self.__class__.DEFAULT_CONFIG_FILE
        self._cfg = SafeConfigParser()
        self._cfg.read([config_file, ])

    def get(self, option, section=None, value_type=str):
        return self._cfg._get(section or self.__class__.SECTION_NAME, value_type, option)

    def __getattr__(self, option):
        try:
            return self.get(option)
        except NoOptionError as e:
            return None
Пример #4
0
    def getAsDType(self, section, option, dtype, default=None):
        """Convenience method to query options with a custom default and type

        This method simply wraps the base class method, but adds a `default`
        keyword argument. The value of `default` is returned whenever the
        config parser does not have the requested option and/or section.

        In addition, the returned value is converted into the specified `dtype`.
        """
        if not self.has_option(section, option):
            return default

        return SafeConfigParser._get(self, section, dtype, option)
Пример #5
0
    def get_as_dtype(self, section, option, dtype, default=None):
        """Convenience method to query options with a custom default and type

        This method simply wraps the base class method, but adds a `default`
        keyword argument. The value of `default` is returned whenever the
        config parser does not have the requested option and/or section.

        In addition, the returned value is converted into the specified `dtype`.
        """
        if not self.has_option(section, option):
            return default
        try:
            return SafeConfigParser._get(self, section, dtype, option)
        except ValueError as e:
            # provide somewhat descriptive error
            raise ValueError(
                "Failed to obtain value from configuration for %s.%s. "
                "Original exception was: %s" % (section, option, e))
Пример #6
0
    def get_as_dtype(self, section, option, dtype, default=None):
        """Convenience method to query options with a custom default and type

        This method simply wraps the base class method, but adds a `default`
        keyword argument. The value of `default` is returned whenever the
        config parser does not have the requested option and/or section.

        In addition, the returned value is converted into the specified `dtype`.
        """
        if not self.has_option(section, option):
            return default
        try:
            return SafeConfigParser._get(self, section, dtype, option)
        except ValueError, e:
            # provide somewhat descriptive error
            raise ValueError, \
                  "Failed to obtain value from configuration for %s.%s. " \
                  "Original exception was: %s" % (section, option, e)