<s> """<STR_LIT>""" <EOL> import copy <EOL> import logging <EOL> import string <EOL> from compass . db . api import adapter as adapter_api <EOL> from compass . db . api import database <EOL> from compass . db . api import utils <EOL> from compass . db import callback as metadata_callback <EOL> from compass . db import exception <EOL> from compass . db import models <EOL> from compass . db import validator as metadata_validator <EOL> from compass . utils import setting_wrapper as setting <EOL> from compass . utils import util <EOL> OS_FIELDS = None <EOL> PACKAGE_FIELDS = None <EOL> FLAVOR_FIELDS = None <EOL> OSES_METADATA = None <EOL> PACKAGES_METADATA = None <EOL> FLAVORS_METADATA = None <EOL> OSES_METADATA_UI_CONVERTERS = None <EOL> FLAVORS_METADATA_UI_CONVERTERS = None <EOL> def _get_field_from_configuration ( configs ) : <EOL> """<STR_LIT>""" <EOL> fields = { } <EOL> for config in configs : <EOL> if not isinstance ( config , dict ) : <EOL> raise exception . InvalidParameter ( <EOL> '<STR_LIT>' % config <EOL> ) <EOL> field_name = config [ '<STR_LIT>' ] <EOL> fields [ field_name ] = { <EOL> '<STR_LIT:name>' : field_name , <EOL> '<STR_LIT:id>' : field_name , <EOL> '<STR_LIT>' : config . get ( '<STR_LIT>' , basestring ) , <EOL> '<STR_LIT>' : config . get ( '<STR_LIT>' , '<STR_LIT:text>' ) , <EOL> '<STR_LIT>' : config . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : config . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:description>' : config . get ( '<STR_LIT>' , field_name ) <EOL> } <EOL> return fields <EOL> def _get_os_fields_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . OS_FIELD_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> return _get_field_from_configuration ( <EOL> configs <EOL> ) <EOL> def _get_package_fields_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . PACKAGE_FIELD_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> return _get_field_from_configuration ( <EOL> configs <EOL> ) <EOL> def _get_flavor_fields_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . FLAVOR_FIELD_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> return _get_field_from_configuration ( <EOL> configs <EOL> ) <EOL> def _get_metadata_from_configuration ( <EOL> path , name , config , <EOL> fields , ** kwargs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( config , dict ) : <EOL> raise exception . InvalidParameter ( <EOL> '<STR_LIT>' % ( path , config ) <EOL> ) <EOL> metadata_self = config . get ( '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in metadata_self : <EOL> field_name = metadata_self [ '<STR_LIT>' ] <EOL> field = fields [ field_name ] <EOL> else : <EOL> field = { } <EOL> mapping_to_template = metadata_self . get ( '<STR_LIT>' , None ) <EOL> if mapping_to_template : <EOL> mapping_to = string . Template ( <EOL> mapping_to_template <EOL> ) . safe_substitute ( <EOL> ** kwargs <EOL> ) <EOL> else : <EOL> mapping_to = None <EOL> self_metadata = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , name ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' , dict ) , <EOL> '<STR_LIT>' : field . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:description>' : metadata_self . get ( <EOL> '<STR_LIT:description>' , field . get ( '<STR_LIT:description>' , None ) <EOL> ) , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : mapping_to , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , field . get ( '<STR_LIT>' , None ) <EOL> ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , field . get ( '<STR_LIT>' , None ) <EOL> ) , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : metadata_self . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : metadata_self . get ( <EOL> '<STR_LIT>' , False ) <EOL> } <EOL> self_metadata . update ( kwargs ) <EOL> metadata = { '<STR_LIT>' : self_metadata } <EOL> key_extensions = metadata_self . get ( '<STR_LIT>' , { } ) <EOL> general_keys = [ ] <EOL> for key , value in config . items ( ) : <EOL> if key . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> if key in key_extensions : <EOL> if not key . startswith ( '<STR_LIT:$>' ) : <EOL> raise exception . InvalidParameter ( <EOL> '<STR_LIT>' % ( <EOL> path , key <EOL> ) <EOL> ) <EOL> extended_keys = key_extensions [ key ] <EOL> for extended_key in extended_keys : <EOL> if extended_key . startswith ( '<STR_LIT:$>' ) : <EOL> raise exception . InvalidParameter ( <EOL> '<STR_LIT>' % ( <EOL> path , extended_key <EOL> ) <EOL> ) <EOL> sub_kwargs = dict ( kwargs ) <EOL> sub_kwargs [ key [ <NUM_LIT:1> : ] ] = extended_key <EOL> metadata [ extended_key ] = _get_metadata_from_configuration ( <EOL> '<STR_LIT>' % ( path , extended_key ) , extended_key , value , <EOL> fields , ** sub_kwargs <EOL> ) <EOL> else : <EOL> if key . startswith ( '<STR_LIT:$>' ) : <EOL> general_keys . append ( key ) <EOL> metadata [ key ] = _get_metadata_from_configuration ( <EOL> '<STR_LIT>' % ( path , key ) , key , value , <EOL> fields , ** kwargs <EOL> ) <EOL> if len ( general_keys ) > <NUM_LIT:1> : <EOL> raise exception . InvalidParameter ( <EOL> '<STR_LIT>' % ( <EOL> path , general_keys <EOL> ) <EOL> ) <EOL> return metadata <EOL> def _get_oses_metadata_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> oses_metadata = { } <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . OS_METADATA_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> for config in configs : <EOL> os_name = config [ '<STR_LIT>' ] <EOL> os_metadata = oses_metadata . setdefault ( os_name , { } ) <EOL> for key , value in config [ '<STR_LIT>' ] . items ( ) : <EOL> os_metadata [ key ] = _get_metadata_from_configuration ( <EOL> key , key , value , OS_FIELDS <EOL> ) <EOL> oses = adapter_api . OSES <EOL> parents = { } <EOL> for os_name , os in oses . items ( ) : <EOL> parent = os . get ( '<STR_LIT>' , None ) <EOL> parents [ os_name ] = parent <EOL> for os_name , os in oses . items ( ) : <EOL> oses_metadata [ os_name ] = util . recursive_merge_dict ( <EOL> os_name , oses_metadata , parents <EOL> ) <EOL> return oses_metadata <EOL> def _get_packages_metadata_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> packages_metadata = { } <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . PACKAGE_METADATA_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> for config in configs : <EOL> adapter_name = config [ '<STR_LIT>' ] <EOL> package_metadata = packages_metadata . setdefault ( adapter_name , { } ) <EOL> for key , value in config [ '<STR_LIT>' ] . items ( ) : <EOL> package_metadata [ key ] = _get_metadata_from_configuration ( <EOL> key , key , value , PACKAGE_FIELDS <EOL> ) <EOL> adapters = adapter_api . ADAPTERS <EOL> parents = { } <EOL> for adapter_name , adapter in adapters . items ( ) : <EOL> parent = adapter . get ( '<STR_LIT>' , None ) <EOL> parents [ adapter_name ] = parent <EOL> for adapter_name , adapter in adapters . items ( ) : <EOL> packages_metadata [ adapter_name ] = util . recursive_merge_dict ( <EOL> adapter_name , packages_metadata , parents <EOL> ) <EOL> return packages_metadata <EOL> def _get_flavors_metadata_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> flavors_metadata = { } <EOL> env_locals = { } <EOL> env_locals . update ( metadata_validator . VALIDATOR_LOCALS ) <EOL> env_locals . update ( metadata_callback . CALLBACK_LOCALS ) <EOL> configs = util . load_configs ( <EOL> setting . FLAVOR_METADATA_DIR , <EOL> env_locals = env_locals <EOL> ) <EOL> for config in configs : <EOL> adapter_name = config [ '<STR_LIT>' ] <EOL> flavor_name = config [ '<STR_LIT>' ] <EOL> flavor_metadata = flavors_metadata . setdefault ( <EOL> adapter_name , { } <EOL> ) . setdefault ( flavor_name , { } ) <EOL> for key , value in config [ '<STR_LIT>' ] . items ( ) : <EOL> flavor_metadata [ key ] = _get_metadata_from_configuration ( <EOL> key , key , value , FLAVOR_FIELDS <EOL> ) <EOL> packages_metadata = PACKAGES_METADATA <EOL> adapters_flavors = adapter_api . ADAPTERS_FLAVORS <EOL> for adapter_name , adapter_flavors in adapters_flavors . items ( ) : <EOL> package_metadata = packages_metadata . get ( adapter_name , { } ) <EOL> for flavor_name , flavor in adapter_flavors . items ( ) : <EOL> flavor_metadata = flavors_metadata . setdefault ( <EOL> adapter_name , { } <EOL> ) . setdefault ( flavor_name , { } ) <EOL> util . merge_dict ( flavor_metadata , package_metadata , override = False ) <EOL> return flavors_metadata <EOL> def _filter_metadata ( metadata , ** kwargs ) : <EOL> if not isinstance ( metadata , dict ) : <EOL> return metadata <EOL> filtered_metadata = { } <EOL> for key , value in metadata . items ( ) : <EOL> if key == '<STR_LIT>' : <EOL> default_value = value . get ( '<STR_LIT>' , None ) <EOL> if default_value is None : <EOL> default_callback_params = value . get ( <EOL> '<STR_LIT>' , { } <EOL> ) <EOL> callback_params = dict ( kwargs ) <EOL> if default_callback_params : <EOL> callback_params . update ( default_callback_params ) <EOL> default_callback = value . get ( '<STR_LIT>' , None ) <EOL> if default_callback : <EOL> default_value = default_callback ( key , ** callback_params ) <EOL> options = value . get ( '<STR_LIT>' , None ) <EOL> if options is None : <EOL> options_callback_params = value . get ( <EOL> '<STR_LIT>' , { } <EOL> ) <EOL> callback_params = dict ( kwargs ) <EOL> if options_callback_params : <EOL> callback_params . update ( options_callback_params ) <EOL> options_callback = value . get ( '<STR_LIT>' , None ) <EOL> if options_callback : <EOL> options = options_callback ( key , ** callback_params ) <EOL> filtered_metadata [ key ] = value <EOL> if default_value is not None : <EOL> filtered_metadata [ key ] [ '<STR_LIT>' ] = default_value <EOL> if options is not None : <EOL> filtered_metadata [ key ] [ '<STR_LIT>' ] = options <EOL> else : <EOL> filtered_metadata [ key ] = _filter_metadata ( value , ** kwargs ) <EOL> return filtered_metadata <EOL> def _load_metadata ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> adapter_api . load_adapters_internal ( force_reload = force_reload ) <EOL> global OS_FIELDS <EOL> if force_reload or OS_FIELDS is None : <EOL> OS_FIELDS = _get_os_fields_from_configuration ( ) <EOL> global PACKAGE_FIELDS <EOL> if force_reload or PACKAGE_FIELDS is None : <EOL> PACKAGE_FIELDS = _get_package_fields_from_configuration ( ) <EOL> global FLAVOR_FIELDS <EOL> if force_reload or FLAVOR_FIELDS is None : <EOL> FLAVOR_FIELDS = _get_flavor_fields_from_configuration ( ) <EOL> global OSES_METADATA <EOL> if force_reload or OSES_METADATA is None : <EOL> OSES_METADATA = _get_oses_metadata_from_configuration ( ) <EOL> global PACKAGES_METADATA <EOL> if force_reload or PACKAGES_METADATA is None : <EOL> PACKAGES_METADATA = _get_packages_metadata_from_configuration ( ) <EOL> global FLAVORS_METADATA <EOL> if force_reload or FLAVORS_METADATA is None : <EOL> FLAVORS_METADATA = _get_flavors_metadata_from_configuration ( ) <EOL> global OSES_METADATA_UI_CONVERTERS <EOL> if force_reload or OSES_METADATA_UI_CONVERTERS is None : <EOL> OSES_METADATA_UI_CONVERTERS = ( <EOL> _get_oses_metadata_ui_converters_from_configuration ( ) <EOL> ) <EOL> global FLAVORS_METADATA_UI_CONVERTERS <EOL> if force_reload or FLAVORS_METADATA_UI_CONVERTERS is None : <EOL> FLAVORS_METADATA_UI_CONVERTERS = ( <EOL> _get_flavors_metadata_ui_converters_from_configuration ( ) <EOL> ) <EOL> def _get_oses_metadata_ui_converters_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> oses_metadata_ui_converters = { } <EOL> configs = util . load_configs ( setting . OS_MAPPING_DIR ) <EOL> for config in configs : <EOL> os_name = config [ '<STR_LIT>' ] <EOL> oses_metadata_ui_converters [ os_name ] = config . get ( '<STR_LIT>' , { } ) <EOL> oses = adapter_api . OSES <EOL> parents = { } <EOL> for os_name , os in oses . items ( ) : <EOL> parent = os . get ( '<STR_LIT>' , None ) <EOL> parents [ os_name ] = parent <EOL> for os_name , os in oses . items ( ) : <EOL> oses_metadata_ui_converters [ os_name ] = util . recursive_merge_dict ( <EOL> os_name , oses_metadata_ui_converters , parents <EOL> ) <EOL> return oses_metadata_ui_converters <EOL> def _get_flavors_metadata_ui_converters_from_configuration ( ) : <EOL> """<STR_LIT>""" <EOL> flavors_metadata_ui_converters = { } <EOL> configs = util . load_configs ( setting . FLAVOR_MAPPING_DIR ) <EOL> for config in configs : <EOL> adapter_name = config [ '<STR_LIT>' ] <EOL> flavor_name = config [ '<STR_LIT>' ] <EOL> flavors_metadata_ui_converters . setdefault ( <EOL> adapter_name , { } <EOL> ) [ flavor_name ] = config . get ( '<STR_LIT>' , { } ) <EOL> adapters = adapter_api . ADAPTERS <EOL> parents = { } <EOL> for adapter_name , adapter in adapters . items ( ) : <EOL> parent = adapter . get ( '<STR_LIT>' , None ) <EOL> parents [ adapter_name ] = parent <EOL> for adapter_name , adapter in adapters . items ( ) : <EOL> flavors_metadata_ui_converters [ adapter_name ] = ( <EOL> util . recursive_merge_dict ( <EOL> adapter_name , flavors_metadata_ui_converters , parents <EOL> ) <EOL> ) <EOL> return flavors_metadata_ui_converters <EOL> def get_packages_metadata_internal ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> _load_metadata ( force_reload = force_reload ) <EOL> metadata_mapping = { } <EOL> adapters = adapter_api . ADAPTERS <EOL> for adapter_name , adapter in adapters . items ( ) : <EOL> if adapter . get ( '<STR_LIT>' ) : <EOL> metadata_mapping [ adapter_name ] = _filter_metadata ( <EOL> PACKAGES_METADATA . get ( adapter_name , { } ) <EOL> ) <EOL> else : <EOL> logging . info ( <EOL> '<STR_LIT>' , <EOL> adapter_name <EOL> ) <EOL> return metadata_mapping <EOL> def get_flavors_metadata_internal ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> _load_metadata ( force_reload = force_reload ) <EOL> metadata_mapping = { } <EOL> adapters_flavors = adapter_api . ADAPTERS_FLAVORS <EOL> for adapter_name , adapter_flavors in adapters_flavors . items ( ) : <EOL> adapter = adapter_api . ADAPTERS [ adapter_name ] <EOL> if not adapter . get ( '<STR_LIT>' ) : <EOL> logging . info ( <EOL> '<STR_LIT>' , <EOL> adapter_name <EOL> ) <EOL> continue <EOL> for flavor_name , flavor in adapter_flavors . items ( ) : <EOL> flavor_metadata = FLAVORS_METADATA . get ( <EOL> adapter_name , { } <EOL> ) . get ( flavor_name , { } ) <EOL> metadata = _filter_metadata ( flavor_metadata ) <EOL> metadata_mapping . setdefault ( <EOL> adapter_name , { } <EOL> ) [ flavor_name ] = metadata <EOL> return metadata_mapping <EOL> def get_flavors_metadata_ui_converters_internal ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> _load_metadata ( force_reload = force_reload ) <EOL> return FLAVORS_METADATA_UI_CONVERTERS <EOL> def get_oses_metadata_internal ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> _load_metadata ( force_reload = force_reload ) <EOL> metadata_mapping = { } <EOL> oses = adapter_api . OSES <EOL> for os_name , os in oses . items ( ) : <EOL> if os . get ( '<STR_LIT>' ) : <EOL> metadata_mapping [ os_name ] = _filter_metadata ( <EOL> OSES_METADATA . get ( os_name , { } ) <EOL> ) <EOL> else : <EOL> logging . info ( <EOL> '<STR_LIT>' , <EOL> os_name <EOL> ) <EOL> return metadata_mapping <EOL> def get_oses_metadata_ui_converters_internal ( force_reload = False ) : <EOL> """<STR_LIT>""" <EOL> _load_metadata ( force_reload = force_reload ) <EOL> return OSES_METADATA_UI_CONVERTERS </s>
