def _manifest_dict(self): if self.attributes['manifest_processor'] == 'jinja2': content_processor = content_processor_jinja2 elif self.attributes['manifest_processor'] == 'mako': content_processor = content_processor_mako else: content_processor = lambda item: item._template_content.encode( 'utf-8') if self.attributes['manifest'] is not None or self.attributes[ 'manifest_file'] is None: user_manifest = self.attributes['manifest'] or {} elif (self.attributes['manifest_file'].endswith(".yaml") or self.attributes['manifest_file'].endswith(".yml")): user_manifest = yaml.load(content_processor(self), Loader=yaml.SafeLoader) elif self.attributes['manifest_file'].endswith(".json"): user_manifest = json.loads(content_processor(self)) merged_manifest = merge_dict( { 'kind': self.KIND, 'metadata': { 'name': self.name.split("/")[-1], }, }, user_manifest, ) if merged_manifest.get('apiVersion') is None: raise BundleError( _("{item} from bundle '{bundle}' needs an apiVersion in its manifest" ).format(item=self.id, bundle=self.bundle.name)) return merged_manifest
def manifest(self): if self.attributes['manifest_processor'] == 'jinja2': content_processor = content_processor_jinja2 elif self.attributes['manifest_processor'] == 'mako': content_processor = content_processor_mako else: content_processor = lambda item: item._template_content.encode('utf-8') if self.attributes['manifest'] is not None or self.attributes['manifest_file'] is None: user_manifest = self.attributes['manifest'] or {} elif ( self.attributes['manifest_file'].endswith(".yaml") or self.attributes['manifest_file'].endswith(".yml") ): user_manifest = yaml.load(content_processor(self)) elif self.attributes['manifest_file'].endswith(".json"): user_manifest = json.loads(content_processor(self)) return json.dumps(merge_dict( { 'apiVersion': self.KUBERNETES_APIVERSION, 'kind': self.KIND, 'metadata': { 'name': self.resource_name, }, }, user_manifest, ), indent=4, sort_keys=True)
def test_atomic_no_merge_update(): assert merge_dict( {1: [5]}, {1: atomic([6, 7])}, ) == { 1: [6, 7] }
def test_atomic_no_merge_base(): assert merge_dict( {1: atomic([5])}, {1: [6, 7]}, ) == { 1: [6, 7] }
def _manifest_dict(self): if self.attributes['manifest_processor'] == 'jinja2': content_processor = content_processor_jinja2 elif self.attributes['manifest_processor'] == 'mako': content_processor = content_processor_mako else: content_processor = lambda item: item._template_content.encode('utf-8') if self.attributes['manifest'] is not None or self.attributes['manifest_file'] is None: user_manifest = self.attributes['manifest'] or {} elif ( self.attributes['manifest_file'].endswith(".yaml") or self.attributes['manifest_file'].endswith(".yml") ): user_manifest = yaml.load(content_processor(self)) elif self.attributes['manifest_file'].endswith(".json"): user_manifest = json.loads(content_processor(self)) merged_manifest = merge_dict( { 'apiVersion': self.KUBERNETES_APIVERSION, 'kind': self.KIND, 'metadata': { 'name': self.resource_name, }, }, user_manifest, ) if merged_manifest['apiVersion'] is None: raise BundleError(_( "{item} from bundle '{bundle}' needs an apiVersion in its manifest" ).format(item=self.id, bundle=self.bundle.name)) return merged_manifest
def copy_global_packages_into_all_versions(metadata): versions = {} for php_version, php_config in metadata.get('php/versions').items(): modules = merge_dict( metadata.get('php/global_modules', {}), php_config.get('modules', {}), ) versions[php_version] = { 'modules': modules, } return { 'php': { 'versions': versions, } }
def test_blame_and_merge(): dict1 = { 'key1': 11, 'key2': { 'key21': 121, 'key22': 122, }, 'key3': { 'key31': { 'key311': [1311], }, }, } dict2 = { 'key2': { 'key21': 221, }, 'key3': { 'key31': { 'key311': [2311], 'key312': 2312, }, }, 'key4': 24, } from pprint import pprint blame = {} merged = merge_dict( {}, dict1, ) blame_changed_paths( {}, merged, blame, "dict1", ) pprint(blame) merged2 = merge_dict( merged, dict2, ) blame_changed_paths( merged, merged2, blame, "dict2", ) pprint(blame) should = { ('key1',): ("dict1",), ('key2',): ("dict1", "dict2"), ('key2', 'key21'): ("dict2",), ('key2', 'key22'): ("dict1",), ('key3',): ("dict1", "dict2"), ('key3', 'key31',): ("dict1", "dict2"), ('key3', 'key31', 'key311'): ("dict1", "dict2"), ('key3', 'key31', 'key312'): ("dict2",), ('key4',): ("dict2",), } pprint(should) assert blame == should assert merged2 == { 'key1': 11, 'key2': { 'key21': 221, 'key22': 122, }, 'key3': { 'key31': { 'key311': [1311, 2311], 'key312': 2312, }, }, 'key4': 24, }
def test_blame_and_merge(): dict1 = { 'key1': 11, 'key2': { 'key21': 121, 'key22': 122, }, 'key3': { 'key31': { 'key311': [1311], }, }, } dict2 = { 'key2': { 'key21': 221, }, 'key3': { 'key31': { 'key311': [2311], 'key312': 2312, }, }, 'key4': 24, } from pprint import pprint blame = {} merged = merge_dict( {}, dict1, ) blame_changed_paths( {}, merged, blame, "dict1", ) pprint(blame) merged2 = merge_dict( merged, dict2, ) blame_changed_paths( merged, merged2, blame, "dict2", ) pprint(blame) should = { ('key1',): ("dict1",), ('key2',): ("dict1", "dict2"), ('key2', 'key21'): ("dict2",), ('key2', 'key22'): ("dict1",), ('key3',): ("dict1", "dict2"), ('key3', 'key31',): ("dict1", "dict2"), ('key3', 'key31', 'key311'): ("dict1", "dict2"), ('key3', 'key31', 'key312'): ("dict2",), ('key4',): ("dict2",), } pprint(should) assert blame == should assert merged2 == { 'key1': 11, 'key2': { 'key21': 221, 'key22': 122, }, 'key3': { 'key31': { 'key311': [1311, 2311], 'key312': 2312, }, }, 'key4': 24, }
def test_atomic_no_merge_base(): assert merge_dict( {1: atomic([5])}, {1: [6, 7]}, ) == {1: [6, 7]}
def test_atomic_no_merge_update(): assert merge_dict( {1: [5]}, {1: atomic([6, 7])}, ) == {1: [6, 7]}