Пример #1
0
    def __init__(self, logger=logging, instance_config_metadata=None):
        """Constructor.

    Inherit from the ConfigManager class. Read the template for instance
    defaults and write new sections and options. This prevents package
    updates from overriding user set defaults.

    Args:
      logger: logger object, used to write to SysLog and serial port.
      instance_config_metadata: string, a config file specified in metadata.
    """
        self.logger = logger
        self.instance_config_metadata = instance_config_metadata
        self.instance_config_header %= (self.instance_config_script,
                                        self.instance_config_template)
        # User provided instance configs should always take precedence.
        super(InstanceConfig,
              self).__init__(config_file=self.instance_config_template,
                             config_header=self.instance_config_header)

        # Use the instance config settings from metadata if specified. Then use
        # settings in an instance config file if one exists. If a config
        # file does not already exist, try to use the distro provided defaults. If
        # no file exists, use the default configuration settings.
        config_files = [self.instance_config, self.instance_config_distro]
        config_defaults = []
        if self.instance_config_metadata:
            config = parser.SafeConfigParser()
            try:
                config.readfp(stringio.StringIO(self.instance_config_metadata))
            except parser.Error as e:
                self.logger.error('Error parsing metadata configs: %s', str(e))
            else:
                config_defaults.append(
                    dict(
                        (s, dict(config.items(s))) for s in config.sections()))
        for config_file in config_files:
            if os.path.exists(config_file):
                config = parser.SafeConfigParser()
                try:
                    config.read(config_file)
                except parser.Error as e:
                    self.logger.error('Error parsing config file: %s', str(e))
                else:
                    config_defaults.append(
                        dict((s, dict(config.items(s)))
                             for s in config.sections()))
        config_defaults.append(self.instance_config_options)

        for defaults in config_defaults:
            for section, options in sorted(defaults.items()):
                for option, value in sorted(options.items()):
                    super(InstanceConfig, self).SetOption(section,
                                                          option,
                                                          value,
                                                          overwrite=False)
Пример #2
0
  def __init__(self):
    """Constructor.

    Inherit from the ConfigManager class. Read the template for instance
    defaults and write new sections and options. This prevents package
    updates from overriding user set defaults.
    """
    self.instance_config_header %= (
        self.instance_config_script, self.instance_config_template)
    # User provided instance configs should always take precedence.
    super(InstanceConfig, self).__init__(
        config_file=self.instance_config_template,
        config_header=self.instance_config_header)

    # Use the settings in an instance config file if one exists. If a config
    # file does not already exist, try to use the distro provided defaults. If
    # no file exists, use the default configuration settings.
    config_files = [self.instance_config, self.instance_config_distro]
    config_defaults = []
    for config_file in config_files:
      if os.path.exists(config_file):
        config = parser.SafeConfigParser()
        config.read(config_file)
        config_defaults.append(
            dict((s, dict(config.items(s))) for s in config.sections()))
    config_defaults.append(self.instance_config_options)

    for defaults in config_defaults:
      for section, options in sorted(defaults.items()):
        for option, value in sorted(options.items()):
          super(InstanceConfig, self).SetOption(
              section, option, value, overwrite=False)
Пример #3
0
    def __init__(self):
        """Constructor.

    Inherit from the ConfigManager class. Read the template for instance
    defaults and write new sections and options. This prevents package
    updates from overriding user set defaults.
    """
        self.instance_config_header %= (self.instance_config_script,
                                        self.instance_config_template)
        super(InstanceConfig,
              self).__init__(config_file=self.instance_config_template,
                             config_header=self.instance_config_header)

        if os.path.exists(self.instance_config):
            config = parser.SafeConfigParser()
            config.read(self.instance_config)
            defaults = dict(
                (s, dict(config.items(s))) for s in config.sections())
        else:
            defaults = self.instance_config_options

        for section, options in sorted(defaults.items()):
            for option, value in sorted(options.items()):
                super(InstanceConfig, self).SetOption(section,
                                                      option,
                                                      value,
                                                      overwrite=False)
Пример #4
0
    def __init__(self, config_file=None, config_header=None):
        """Constructor.

    Args:
      config_file: string, the location of the config file.
      config_header: string, the message to write at the top of the config.
    """
        self.config_file = config_file or CONFIG
        self.config_header = config_header
        self.config = parser.SafeConfigParser()
        self.config.read(self.config_file)