def __getattr__(self, name):
        if name in self.config:
            return self.config[name]

        if name in FALLBACK_TABLE:
            return self.config[FALLBACK_TABLE[name]]

        raise error.SettingsException('Value for {0} option is not '
                                      'configured'.format(name))
    def populate_default_settings(self, source, destination):
        """Puts default configuration file to a user's home directory."""

        try:
            dst_dir = os.path.dirname(destination)

            if not os.path.exists(dst_dir):
                os.makedirs(dst_dir, 0o700)

            shutil.copy(source, destination)
            os.chmod(destination, 0o600)
        except (IOError, OSError):
            msg = ('Could not save settings to {0}. Please make sure the '
                   'directory is writable')
            raise error.SettingsException(msg.format(dst_dir))
示例#3
0
    def __init__(self):
        settings_files = []

        user_conf_dir = os.getenv('XDG_CONFIG_HOME',
                                  os.path.expanduser('~/.config/'))

        # Look up for a default file distributed with the source code
        default_settings = pkg_resources.resource_filename(
            'fuelclient', 'fuel_client.yaml')

        self.user_settings = os.path.join(user_conf_dir, 'fuel',
                                          'fuel_client.yaml')
        custom_settings = os.getenv('FUELCLIENT_CUSTOM_SETTINGS')

        if not os.path.exists(self.user_settings) and not custom_settings:
            self.populate_default_settings(default_settings,
                                           self.user_settings)
            six.print_('Settings for Fuel Client have been saved to {0}.\n'
                       'Consider changing default values to the ones which '
                       'are appropriate for you.'.format(self.user_settings))

        self._add_file_if_exists(default_settings, settings_files)
        self._add_file_if_exists(self.user_settings, settings_files)

        # Add a custom settings file specified by user
        self._add_file_if_exists(custom_settings, settings_files)

        self.config = {}
        for sf in settings_files:
            try:
                self._update_from_file(sf)
            except Exception as e:
                msg = ('Error while reading config file '
                       '%(file)s: %(err)s') % {
                           'file': sf,
                           'err': str(e)
                       }

                raise error.SettingsException(msg)

        self._update_from_env()
        self._check_deprecated()
    def __init__(self):
        settings_files = []

        # Look up for a default file distributed with the source code
        project_path = os.path.dirname(__file__)
        project_settings_file = os.path.join(project_path,
                                             'fuelclient_settings.yaml')
        settings_files.append(project_settings_file)

        # Check whether a user specified a custom settings file
        test_config = os.environ.get('FUELCLIENT_CUSTOM_SETTINGS')
        if test_config:
            settings_files.append(test_config)

        self.config = {}
        for sf in settings_files:
            try:
                self._update_from_file(sf)
            except Exception as e:
                msg = ('Error while reading config file '
                       '%(file)s: %(err)s') % {'file': sf, 'err': str(e)}

                raise error.SettingsException(msg)