def assert_parser_after_write(self, parser_type, in_file, out_file_path, use_template=False): parser = parser_type(in_file, out_file_path) complex_defs = get_complex_definitions() # Update each value and read the file in again for prop in get_supported_props(): if prop in (ATTRIBUTES, CONTACTS, DIGITAL_FORMS, PROCESS_STEPS): value = [{}.fromkeys(complex_defs[prop], "test"), {}.fromkeys(complex_defs[prop], prop)] elif prop in (BOUNDING_BOX, LARGER_WORKS): value = {}.fromkeys(complex_defs[prop], "test " + prop) elif prop == DATES: value = {DATE_TYPE: DATE_TYPE_RANGE, DATE_VALUES: ["test", prop]} elif prop in (KEYWORDS_PLACE, KEYWORDS_THEME): value = ["test", prop] else: value = "test " + prop if prop in get_complex_definitions(): value = get_default_for_complex(prop, value) setattr(parser, prop, value) parser.write(use_template=use_template) with open(out_file_path) as out_file: self.assert_parsers_are_equal(parser, parser_type(out_file))
def assert_reparsed_complex_for(self, parser, prop, value, target): setattr(parser, prop, value) parser_type = type(parser) parser_name = parser_type.__name__ reparsed = getattr(parser_type(parser.serialize()), prop) if prop in get_complex_definitions(): target = get_default_for_complex(prop, target) if isinstance(reparsed, dict): # Reparsed is a dict: compare each value with corresponding in target for key, val in iteritems(reparsed): self.assert_equal_for(parser_name, "{0}.{1}".format(prop, key), val, target.get(key, u"")) elif len(reparsed) <= 1: # Reparsed is empty or a single-item list: do a single value comparison self.assert_equal_for(parser_name, prop, reparsed, target) else: # Reparsed is a multiple-item list: compare each value with corresponding in target for idx, value in enumerate(reparsed): if not isinstance(value, dict): self.assert_equal_for(parser_name, "{0}[{1}]".format(prop, idx), value, target[idx]) else: for key, val in iteritems(value): self.assert_equal_for(parser_name, "{0}.{1}".format(prop, key), val, target[idx].get(key, u""))
def _parse_digital_forms(self, prop=DIGITAL_FORMS): """ Concatenates a list of Digital Form data structures parsed from the metadata """ xpath_map = self._data_structures[prop] # Parse base digital form fields: 'name', 'content', 'decompression', 'version', 'specification' xpath_root = self._data_map['_digital_forms_root'] digital_forms = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Parse digital form transfer option fields: 'access_desc', 'access_instrs', 'network_resource' xpath_root = self._data_map['_transfer_options_root'] transfer_opts = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Combine digital forms and transfer options into a single complex struct df_len = len(digital_forms) to_len = len(transfer_opts) parsed_forms = [] for idx in xrange(0, max(df_len, to_len)): digital_form = {}.fromkeys(_agis_definitions[prop], u'') if idx < df_len: digital_form.update(i for i in digital_forms[idx].items() if i[1]) if idx < to_len: digital_form.update(i for i in transfer_opts[idx].items() if i[1]) if any(digital_form.values()): parsed_forms.append(digital_form) return get_default_for_complex(prop, parsed_forms)
def _parse_digital_forms(self, prop=DIGITAL_FORMS): """ Concatenates a list of Digital Form data structures parsed from the metadata """ xpath_map = self._data_structures[prop] # Parse base digital form fields: 'name', 'content', 'decompression', 'version', 'specification' xpath_root = self._data_map['_digital_forms_root'] digital_forms = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Parse digital form transfer option fields: 'access_desc', 'access_instrs', 'network_resource' xpath_root = self._data_map['_transfer_options_root'] transfer_opts = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Combine digital forms and transfer options into a single complex struct df_len = len(digital_forms) to_len = len(transfer_opts) parsed_forms = [] for idx in xrange(0, max(df_len, to_len)): digital_form = {}.fromkeys(_agis_definitions[prop], u'') if idx < df_len: digital_form.update(i for i in digital_forms[idx].items() if i[1]) if idx < to_len: digital_form.update(i for i in transfer_opts[idx].items() if i[1]) if any(digital_form.values()): parsed_forms.append(digital_form) return get_default_for_complex(prop, parsed_forms)
def _parse_digital_forms(self, prop=DIGITAL_FORMS): """ Concatenates a list of Digital Form data structures parsed from the metadata """ xpath_map = self._data_structures[prop] # Parse base digital form fields: 'name', 'content', 'decompression', 'version', 'specification' xpath_root = self._data_map['_digital_forms_root'] digital_forms = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Parse digital form transfer option fields: 'access_desc', 'access_instrs', 'network_resource' xpath_root = self._data_map['_transfer_options_root'] transfer_opts = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Split out digital form content that has been appended to specifications content_delim = _DIGITAL_FORMS_CONTENT_DELIM for digital_form in digital_forms: specs = reduce_value(digital_form['specification']) specs = specs.splitlines() if isinstance(specs, string_types) else specs specifications = wrap_value(s.strip() for s in specs) digital_form['content'] = [] digital_form['specification'] = [] has_content = False # For each specification, insert delim before appending content for spec in specifications: has_content = has_content or spec == content_delim if not has_content: digital_form['specification'].append(spec) elif spec != content_delim: digital_form['content'].append(spec) # Reduce spec and content to single string values if possible for form_prop in ('content', 'specification'): digital_form[form_prop] = reduce_value(filter_empty(digital_form[form_prop], u'')) # Combine digital forms and transfer options into a single complex struct df_len = len(digital_forms) to_len = len(transfer_opts) parsed_forms = [] for idx in xrange(0, max(df_len, to_len)): digital_form = {}.fromkeys(_iso_definitions[prop], u'') if idx < df_len: digital_form.update(i for i in digital_forms[idx].items() if i[1]) if idx < to_len: digital_form.update(i for i in transfer_opts[idx].items() if i[1]) if any(digital_form.values()): parsed_forms.append(digital_form) return get_default_for_complex(prop, parsed_forms)
def _parse_digital_forms(self, prop=DIGITAL_FORMS): """ Concatenates a list of Digital Form data structures parsed from the metadata """ xpath_map = self._data_structures[prop] # Parse base digital form fields: 'name', 'content', 'decompression', 'version', 'specification' xpath_root = self._data_map['_digital_forms_root'] digital_forms = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Parse digital form transfer option fields: 'access_desc', 'access_instrs', 'network_resource' xpath_root = self._data_map['_transfer_options_root'] transfer_opts = parse_complex_list(self._xml_tree, xpath_root, xpath_map, prop) # Split out digital form content that has been appended to specifications content_delim = _DIGITAL_FORMS_CONTENT_DELIM for digital_form in digital_forms: specs = reduce_value(digital_form['specification']) specs = specs.splitlines() if isinstance(specs, string_types) else specs specifications = wrap_value(s.strip() for s in specs) digital_form['content'] = [] digital_form['specification'] = [] has_content = False # For each specification, insert delim before appending content for spec in specifications: has_content = has_content or spec == content_delim if not has_content: digital_form['specification'].append(spec) elif spec != content_delim: digital_form['content'].append(spec) # Reduce spec and content to single string values if possible for form_prop in ('content', 'specification'): digital_form[form_prop] = reduce_value(filter_empty(digital_form[form_prop], u'')) # Combine digital forms and transfer options into a single complex struct df_len = len(digital_forms) to_len = len(transfer_opts) parsed_forms = [] for idx in xrange(0, max(df_len, to_len)): digital_form = {}.fromkeys(_iso_definitions[prop], u'') if idx < df_len: digital_form.update(i for i in digital_forms[idx].items() if i[1]) if idx < to_len: digital_form.update(i for i in transfer_opts[idx].items() if i[1]) if any(digital_form.values()): parsed_forms.append(digital_form) return get_default_for_complex(prop, parsed_forms)
def _parse_attribute_details(self, prop=ATTRIBUTES): """ Concatenates a list of Attribute Details data structures parsed from a remote file """ parsed_attributes = self._parse_attribute_details_file(prop) if parsed_attributes is None: # If not in the (official) remote location, try the tree itself parsed_attributes = self._parse_complex_list(prop) for attribute in (a for a in parsed_attributes if not a['aliases']): # Aliases are not in ISO standard: default to label attribute['aliases'] = attribute['label'] return get_default_for_complex(prop, parsed_attributes)
def _parse_attribute_details(self, prop=ATTRIBUTES): """ Concatenates a list of Attribute Details data structures parsed from a remote file """ parsed_attributes = self._parse_attribute_details_file(prop) if parsed_attributes is None: # If not in the (official) remote location, try the tree itself parsed_attributes = self._parse_complex_list(prop) for attribute in (a for a in parsed_attributes if not a['aliases']): # Aliases are not in ISO standard: default to label attribute['aliases'] = attribute['label'] return get_default_for_complex(prop, parsed_attributes)