Beispiel #1
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info(u'Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = []
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                cos = ConfigurationOptionSetting()
                cos._namespace = misc.to_unicode(section)
                cos._option_name = misc.to_unicode(option)
                cos._value = misc.to_unicode(value)
                option_settings.append(cos) 
        
        log.debug(u'Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error(u'Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return []
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
Beispiel #2
0
def load_env_option_setting_file(location, option_settings = None, quiet = False):
    log.info('Reading environment option settings from file at "{0}".'.format(location))
    
    if option_settings is None:
        option_settings = dict()
    
    try:
        parser = SectionedConfigParser()
        parser.read(location)
        
        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value) 
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value 
        
        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))
        
        check_access_permission(location, True)
        return option_settings
    
    except BaseException as ex:
        log.error('Failed to load environment option setting file, because: "{0}"'.format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(OptionSettingFileErrorMessage.ReadError.format(location))        
            raise
def load_env_option_setting_file(location, option_settings=None, quiet=False):
    log.info('Reading environment option settings from file at "{0}".'.format(
        location))

    if option_settings is None:
        option_settings = dict()

    try:
        parser = SectionedConfigParser()
        parser.read(location)

        for section in parser.sections():
            for option, value in parser.items(section):
                section = misc.to_unicode(section)
                option = misc.to_unicode(option)
                value = misc.to_unicode(value)
                if section not in option_settings:
                    option_settings[section] = dict()
                option_settings[section][option] = value

        log.debug('Option settings read from file include: "{0}".'.\
                  format(misc.collection_to_string(option_settings)))

        check_access_permission(location, True)
        return option_settings

    except BaseException as ex:
        log.error(
            'Failed to load environment option setting file, because: "{0}"'.
            format(ex))
        if quiet:
            return option_settings
        else:
            prompt.error(
                OptionSettingFileErrorMessage.ReadError.format(location))
            raise
Beispiel #4
0
def load_eb_config_file(location, parameter_pool, quiet = False):
    log.info(u'Reading EB configuration from file: "{0}"'.format(location))
    try:
        try: 
            parser = SectionedConfigParser()
            parser.read(location)

            log.debug(u'Found a sectioned config file.')
            extra_config = dict()
            extra_config[EbConfigFile.RootSectionName] = dict()
            branches = dict()
            
            for section in parser.sections():
                log.debug(u'Reading section "{0}" from config file.'.format(section))                
                # Known sections
                if section in EbConfigFile.KnownSections.keys(): 
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            parameter_pool.put(Parameter(key, value, ParameterSource.ConfigFile))
                        else:
                            extra_config[EbConfigFile.RootSectionName][key] = value
                            
                elif section == EbConfigFile.BranchSectionName:
                    #branch section
                    branch_mapping = dict()
                    for key, value in parser.items(section):
                        branch_mapping[key] = value
                    parameter_pool.put(Parameter(ParameterName.BranchMapping, 
                                                 branch_mapping, 
                                                 ParameterSource.ConfigFile))
                    
                elif section.startswith(EbConfigFile.BranchSectionPrefix):
                    #branch environment session
                    parsed = section.split(EbConfigFile.SectionNameDelimiter)
                    if len(parsed) != 2 or misc.is_blank_string(parsed[1]):
                        continue    # skip if no environment name
                    branch_name = parsed[1]
                    branches[branch_name] = dict()
                    for key, value in parser.items(section):
                        if ConfigFileParameters.has_key(key):
                            from_file, _ = ConfigFileParameters[key]
                            if from_file:
                                value = from_file(value)
                            branches[branch_name][key] = value
                        else:
                            if not extra_config.has_key(section):
                                extra_config[section] = dict()
                            extra_config[section][key] = value
                
                else:
                    # unknown sections
                    new_section = dict()
                    for key, value in parser.items(section):
                        new_section[key] = value 
                    extra_config[section] = new_section

            parameter_pool.put(Parameter(ParameterName.ConfigFileExtra, 
                                         extra_config, 
                                         ParameterSource.ConfigFile))
            parameter_pool.put(Parameter(ParameterName.Branches, 
                                         branches, 
                                         ParameterSource.ConfigFile))     
            
        except MissingSectionHeaderError as ex:
            # old format: sectionless config file
            log.debug(u'Found a section-less config file.')
            nosect_parser = NoSectionConfigParser()
            nosect_parser.read(location)

            for name, (from_file, _) in ConfigFileParameters.iteritems():
                if nosect_parser.has_option(name):
                    value = nosect_parser.get(name)
                    if from_file is not None:
                        value = from_file(value)
                    parameter_pool.put(Parameter(name, value, ParameterSource.ConfigFile))
        
        # Add original parameter infos
        for name, ori_name in EbConfigFile.BranchResetParameters.iteritems():
            if parameter_pool.has(name):
                parameter_pool.put(Parameter(ori_name, 
                                             parameter_pool.get_value(name), 
                                             ParameterSource.ConfigFile))

        log.info(u'Finished reading from EB configuration file.')
   
    except BaseException as ex:
        log.error(u'Failed to parse EB configuration from file, because: "{0}"'.format(ex))
        if not quiet:
            if (isinstance(ex, OSError) or isinstance(ex, IOError)) and\
                ex.errno == FileErrorConstant.FileNotFoundErrorCode:
                raise EBConfigFileNotExistError(ex)
            else:
                msg = ConfigFileErrorMessage.ReadError.format(location)
                prompt.error(msg)
                raise EBConfigFileNotExistError(msg)
        else:    
            pass # if failed, just skip