def create_structmap(workspace, filesec, filelist, type_attr=None, root_type=None): """Creates METS document element tree that contains structural map. :param workspace: directory from which some files are searhed :param filesec: fileSec element :param filelist: Sorted list of digital objects (file paths) :param type_attr: TYPE attribute of structMap element :param root_type: TYPE attribute of root div element :returns: structural map element """ amdids = get_md_references(workspace, directory='.') dmdids = get_md_references(workspace, directory='.', ref_type='dmd') if type_attr == 'Directory-physical': container_div = mets.div(type_attr='directory', label='.', dmdid=dmdids, admid=amdids) else: root_type = root_type if root_type else 'directory' container_div = mets.div(type_attr=root_type, dmdid=dmdids, admid=amdids) structmap = mets.structmap(type_attr=type_attr) structmap.append(container_div) divs = div_structure(filelist) create_div(workspace, divs, container_div, filesec, filelist, type_attr=type_attr) mets_element = mets.mets(child_elements=[structmap]) ET.cleanup_namespaces(mets_element) return ET.ElementTree(mets_element)
def main(arguments=None): """The main method for compile_sturctmap""" args = parse_arguments(arguments) structmap = mets.structmap(type_attr=args.type_attr) mets_structmap = mets.mets(child_elements=[structmap]) filegrp = mets.filegrp() filesec = mets.filesec(child_elements=[filegrp]) mets_filesec = mets.mets(child_elements=[filesec]) _, dmdsec_id = ids_for_files(args.workspace, None, 'dmdsec.xml', dash_count=0) if args.dmdsec_struct == 'ead3': container_div = mets.div(type_attr='logical') structmap.append(container_div) create_ead3_structmap(args.dmdsec_loc, args.workspace, container_div, filegrp, dmdsec_id) else: amdids = get_links_event_agent(args.workspace, None) container_div = mets.div(type_attr='directory', dmdid=dmdsec_id, admid=amdids) structmap.append(container_div) divs = div_structure(args.workspace) create_structmap(args.workspace, divs, container_div, filegrp) if args.stdout: print h.serialize(mets_filesec) print h.serialize(mets_structmap) output_sm_file = os.path.join(args.workspace, 'structmap.xml') output_fs_file = os.path.join(args.workspace, 'filesec.xml') if not os.path.exists(os.path.dirname(output_sm_file)): os.makedirs(os.path.dirname(output_sm_file)) if not os.path.exists(os.path.dirname(output_fs_file)): os.makedirs(os.path.dirname(output_fs_file)) with open(output_sm_file, 'w+') as outfile: outfile.write(h.serialize(mets_structmap)) with open(output_fs_file, 'w+') as outfile: outfile.write(h.serialize(mets_filesec)) print "compile_structmap created files: %s %s" % (output_sm_file, output_fs_file) return 0
def create_ead3_structmap(filegrp, attributes): """Create structmap based on ead3 descriptive metadata structure. :filegrp: fileGrp element :attributes: The following keys: all_amd_refs: XML element tree of administrative metadata references all_dmd_refs: XML element tree of descriptive metadata references filelist: Sorted list of digital objects (file paths) dmdsec_loc: EAD3 descriptive metadata file structmap_type: TYPE attribute of structMap element workspace: Workspace path """ structmap = mets.structmap(type_attr=attributes["structmap_type"]) container_div = mets.div(type_attr='logical') root = ET.parse(attributes["dmdsec_loc"]).getroot() try: label = root.xpath(("//ead3:archdesc/@otherlevel | " "//ead3:archdesc/@level"), namespaces=NAMESPACES)[0] except IndexError: label = 'archdesc' amdids = get_md_references(attributes["all_amd_refs"], directory='.') dmdids = get_md_references(attributes["all_dmd_refs"], directory='.') div_ead = mets.div(type_attr='archdesc', label=label, dmdid=dmdids, admid=amdids) if root.xpath("//ead3:archdesc/ead3:dsc", namespaces=NAMESPACES): for elem in root.xpath("//ead3:dsc/*", namespaces=NAMESPACES): if ET.QName(elem.tag).localname in ALLOWED_C_SUBS: ead3_c_div(elem, div_ead, filegrp, attributes) container_div.append(div_ead) structmap.append(container_div) mets_element = mets.mets(child_elements=[structmap]) ET.cleanup_namespaces(mets_element) return ET.ElementTree(mets_element)
def create_ead3_structmap(descfile, workspace, filegrp, filelist, type_attr): """Create structmap based on ead3 descriptive metadata structure. :desc_file: EAD3 descriptive metadata file :workspace: Workspace path :structmap: Structmap element :filegrp: fileGrp element :filelist: Sorted list of digital objects (file paths) :type_attr: TYPE attribute of structMap element """ structmap = mets.structmap(type_attr=type_attr) container_div = mets.div(type_attr='logical') root = ET.parse(descfile).getroot() try: label = root.xpath(("//ead3:archdesc/@otherlevel | " "//ead3:archdesc/@level"), namespaces=NAMESPACES)[0] except IndexError: label = 'archdesc' amdids = get_md_references(workspace, directory='.') dmdids = get_md_references(workspace, directory='.', ref_type='dmd') div_ead = mets.div(type_attr='archdesc', label=label, dmdid=dmdids, admid=amdids) if len(root.xpath("//ead3:archdesc/ead3:dsc", namespaces=NAMESPACES)) > 0: for elem in root.xpath("//ead3:dsc/*", namespaces=NAMESPACES): if ET.QName(elem.tag).localname in ALLOWED_C_SUBS: ead3_c_div(elem, div_ead, filegrp, workspace, filelist) container_div.append(div_ead) structmap.append(container_div) mets_element = mets.mets(child_elements=[structmap]) ET.cleanup_namespaces(mets_element) return ET.ElementTree(mets_element)
def create_structmap(filesec, **attributes): """ Creates METS document element tree that contains structural map. :filesec: fileSec element :attributes: The following keys: all_amd_refs: XML element tree of administrative metadata references all_dmd_refs: XML element tree of descriptive metadata references filelist: Sorted list of digital objects (file paths) structmap_type: TYPE attribute of structMap element root_type: TYPE attribute of root div element file_ids: Dict with file paths and identifiers workspace: Workspace path :returns: structural map element """ attributes = get_reference_lists(**_attribute_values(attributes)) amdids = get_md_references(attributes["all_amd_refs"], directory='.') dmdids = get_md_references(attributes["all_dmd_refs"], directory='.') if attributes["structmap_type"] == 'Directory-physical': container_div = mets.div(type_attr='directory', label='.', dmdid=dmdids, admid=amdids) else: container_div = mets.div(type_attr=attributes["root_type"], dmdid=dmdids, admid=amdids) structmap = mets.structmap(type_attr=attributes["structmap_type"]) structmap.append(container_div) divs = div_structure(attributes["filelist"]) create_div(divs, container_div, filesec, attributes) mets_element = mets.mets(child_elements=[structmap]) ET.cleanup_namespaces(mets_element) return ET.ElementTree(mets_element)
def run(self): """Create a METS document that contains logical structural map. Logical structural map is based on dataset metadata retrieved from Metax. :returns: ``None`` """ # Read the generated physical structmap from file physical_structmap = ET.parse( os.path.join(self.sip_creation_path, 'structmap.xml')) # Get dmdsec id from physical_structmap dmdsec_id = physical_structmap.getroot()[0][0].attrib['DMDID'] # Get provenance id's provenance_ids = self.get_provenance_ids() # Init logical structmap logical_structmap = mets.structmap(type_attr='Fairdata-logical') mets_structmap = mets.mets(child_elements=[logical_structmap]) # Create logical structmap categories = self.find_file_categories() wrapper_div = mets.div(type_attr='logical', dmdid=[dmdsec_id], admid=provenance_ids) for category in categories: div = mets.div(type_attr=category) for filename in categories.get(category): fileid = self.get_fileid(encode_path(filename, safe='/')) div.append(mets.fptr(fileid)) wrapper_div.append(div) logical_structmap.append(wrapper_div) with self.output().open('wb') as output: output.write(h.serialize(mets_structmap))