def Main(schema, output): """Generates markdown documentation based on the JSON schema. Args: schema: Schema file. output: Output file. """ schema_yaml = yaml.load(libcros_schema.ApplyImports(schema), Loader=yaml.SafeLoader) ref_types = {} for type_def in schema_yaml.get('typeDefs', []): ref_types['#/typeDefs/%s' % type_def] = schema_yaml['typeDefs'][type_def] type_def_outputs = [] type_def_outputs.append('[](begin_definitions)') type_def_outputs.append('') PopulateTypeDef( 'model', schema_yaml['properties']['chromeos']['properties']['configs'] ['items'], ref_types, type_def_outputs) type_def_outputs.append('') type_def_outputs.append('[](end_definitions)') type_def_outputs.append('') if output: pre_lines = [] post_lines = [] if os.path.isfile(output): with open(output) as output_stream: output_lines = output_stream.readlines() pre_section = True post_section = False for line in output_lines: if 'begin_definitions' in line: pre_section = False if pre_section: pre_lines.append(line) if post_section: post_lines.append(line) if 'end_definitions' in line: post_section = True with open(output, 'w') as output_stream: if pre_lines: output_stream.writelines(pre_lines) output_stream.write('\n'.join(type_def_outputs)) if post_lines: output_stream.writelines(post_lines) else: print('\n'.join(type_def_outputs))
def ReadSchema(schema=None): """Reads the schema file and evaluates all import statements. Args: schema: Schema file used to verify the config. Returns: Schema contents with imports evaluated. """ if not schema: schema = os.path.join(this_dir, 'cros_config_schema.yaml') return libcros_schema.ApplyImports(schema)
def MergeConfig(yaml_file, filter_name): """Evaluates and merges all config files into a single configuration. Args: yaml_file: List of source config files that will be transformed/merged. filter_name: Name of device to filter on. Returns: Final merged JSON result. """ yaml_with_imports = libcros_schema.ApplyImports(yaml_file) json_transformed_file = TransformConfig(yaml_with_imports, filter_name) return json_transformed_file
def MergeConfigs(configs): """Evaluates and merges all config files into a single configuration. Args: configs: List of source config files that will be transformed/merged. Returns: Final merged JSON result. """ json_files = [] for yaml_file in configs: yaml_with_imports = libcros_schema.ApplyImports(yaml_file) json_transformed_file = TransformConfig(yaml_with_imports) json_files.append(json.loads(json_transformed_file)) result_json = json_files[0] for overlay_json in json_files[1:]: for to_merge_config in overlay_json['chromeos']['configs']: to_merge_identity = to_merge_config.get('identity', {}) to_merge_name = to_merge_config.get('name', '') matched = False # Find all existing configs where there is a full/partial identity # match or name match and merge that config into the source. # If there are no matches, then append the config. for source_config in result_json['chromeos']['configs']: identity_match = False if to_merge_identity: source_identity = source_config['identity'] # If we are missing anything from the source identity, copy # it into to_merge_identity before doing the comparison, as # missing attributes in the to_merge_identity should be # treated as matched. to_merge_identity_extended = to_merge_identity.copy() for key, value in source_identity.items(): if key not in to_merge_identity_extended: to_merge_identity_extended[key] = value identity_match = _IdentityEq(source_identity, to_merge_identity_extended) elif to_merge_name: identity_match = to_merge_name == source_config.get('name', '') if identity_match: MergeDictionaries(source_config, to_merge_config) matched = True if not matched: result_json['chromeos']['configs'].append(to_merge_config) return libcros_schema.FormatJson(result_json)
def MergeConfigs(configs): """Evaluates and merges all config files into a single configuration. Args: configs: List of source config files that will be transformed/merged. Returns: Final merged JSON result. """ json_files = [] for yaml_file in configs: yaml_with_imports = libcros_schema.ApplyImports(yaml_file) json_transformed_file = TransformConfig(yaml_with_imports) json_files.append(json.loads(json_transformed_file)) result_json = json_files[0] for overlay_json in json_files[1:]: for to_merge_config in overlay_json['chromeos']['configs']: to_merge_identity = to_merge_config.get('identity', {}) to_merge_name = to_merge_config.get('name', '') matched = False # Find all existing configs where there is a full/partial identity # match or name match and merge that config into the source. # If there are no matches, then append the config. for source_config in result_json['chromeos']['configs']: identity_match = False if to_merge_identity: source_identity = source_config['identity'] identity_match = True for identity_key, identity_value in to_merge_identity.iteritems( ): if (identity_key not in source_identity or source_identity[identity_key] != identity_value): identity_match = False break elif to_merge_name: identity_match = to_merge_name == source_config.get( 'name', '') if identity_match: MergeDictionaries(source_config, to_merge_config) matched = True if not matched: result_json['chromeos']['configs'].append(to_merge_config) return libcros_schema.FormatJson(result_json)