コード例 #1
0
def add_file_to_filesec(workspace, path, filegrp):
    """Add file element to fileGrp element given as parameter.

    :param workspace: Workspace directorye from which administrative MD
                      files and amd reference files searched.
    :param path: url encoded path of the file
    :param lxml.etree.Element filegrp: fileGrp element
    :param str returns: id of file added to fileGrp
    :returns: unique identifier of file element
    """
    fileid = '_{}'.format(uuid4())

    # Create list of IDs of amdID elements
    amdids = get_md_references(workspace, path=path)

    # Create XML element and add it to fileGrp
    file_el = mets.file_elem(fileid,
                             admid_elements=set(amdids),
                             loctype='URL',
                             xlink_href='file://%s' %
                             encode_path(path, safe='/'),
                             xlink_type='simple',
                             groupid=None)

    streams = get_objectlist(workspace, path)
    if streams:
        for stream in streams:
            stream_ids = get_md_references(workspace, path=path, stream=stream)
            stream_el = mets.stream(admid_elements=stream_ids)
            file_el.append(stream_el)

    filegrp.append(file_el)

    return fileid
コード例 #2
0
def get_reference_lists(**attributes):
    """
    Fill the attributes with various lists.

    :attributes: The following keys:
                 workspace: Workspace path
                 object_refs: XML tree of digital objects.
                              Will be created if missing.
                 filelist: ID list of objects. Will be created if missing.
                 all_amd_refs: All administrative metadata references.
                               Will be created if missing.
                 all_dmd_refs: All descriptive metadata references.
                               Will be created if missing.
    :returns: Attributes filled with the lists listed above.
    """
    attributes["object_refs"] = attributes.get(
        "object_refs",
        read_md_references(attributes["workspace"],
                           "import-object-md-references.jsonl"))
    attributes["filelist"] = attributes.get(
        "filelist", get_objectlist(attributes["object_refs"]))
    attributes["all_amd_refs"] = attributes.get(
        "all_amd_refs", read_all_amd_references(attributes["workspace"]))
    attributes["all_dmd_refs"] = attributes.get(
        "all_dmd_refs",
        read_md_references(attributes["workspace"],
                           "import-description-md-references.jsonl"))

    return attributes
コード例 #3
0
def add_file_to_filesec(all_amd_refs, object_refs, path, filegrp):
    """Add file element to fileGrp element given as parameter.

    :all_amd_refs: XML element tree of administrative metadata references
    :object_refs: XML tree of object references
    :path: url encoded path of the file
    :filegrp: fileGrp element
    :returns: unique identifier of file element
    """
    fileid = '_{}'.format(uuid4())

    # Create list of IDs of amdID elements
    amdids = get_md_references(all_amd_refs, path=path)

    # Create XML element and add it to fileGrp
    file_el = mets.file_elem(fileid,
                             admid_elements=set(amdids),
                             loctype='URL',
                             xlink_href='file://%s' %
                             encode_path(path, safe='/'),
                             xlink_type='simple',
                             groupid=None)

    streams = get_objectlist(object_refs, path)
    if streams:
        for stream in streams:
            stream_ids = get_md_references(all_amd_refs,
                                           path=path,
                                           stream=stream)
            stream_el = mets.stream(admid_elements=stream_ids)
            file_el.append(stream_el)

    filegrp.append(file_el)

    return fileid
コード例 #4
0
def copy_objects(workspace, data_dir):
    """Copy digital objects to workspace
    """
    files = get_objectlist(workspace)
    for source in files:
        target = os.path.join(workspace, source)
        if not os.path.exists(os.path.dirname(target)):
            os.makedirs(os.path.dirname(target))
        copyfile(os.path.join(data_dir, source), target)
コード例 #5
0
def copy_objects(workspace, data_dir):
    """
    Copy digital objects to workspace.

    :workspace: Workspace path
    :data_dir: Path to digital objects
    """
    files = get_objectlist(read_md_references(
        workspace, "import-object-md-references.jsonl"
    ))
    for source in files:
        target = os.path.join(workspace, source)
        if not os.path.exists(os.path.dirname(target)):
            os.makedirs(os.path.dirname(target))
        copyfile(os.path.join(data_dir, source), target)
コード例 #6
0
def compile_structmap(workspace="./workspace/",
                      structmap_type=None,
                      root_type=None,
                      dmdsec_loc=None,
                      stdout=False):
    """Generate METS file section and structural map based on
    created/imported administrative metada and descriptive metadata.
    """
    filelist = get_objectlist(workspace)

    if structmap_type == 'EAD3-logical':
        # If structured descriptive metadata for structMap divs is used, also
        # the fileSec element (apparently?) is different. The
        # create_ead3_structmap function populates the fileGrp element.
        filegrp = mets.filegrp()
        filesec_element = mets.filesec(child_elements=[filegrp])
        filesec = mets.mets(child_elements=[filesec_element])

        structmap = create_ead3_structmap(dmdsec_loc, workspace, filegrp,
                                          filelist, structmap_type)
    else:
        filesec = create_filesec(workspace, filelist)
        structmap = create_structmap(workspace, filesec.getroot(), filelist,
                                     structmap_type, root_type)

    if stdout:
        print(xml_utils.serialize(filesec).decode("utf-8"))
        print(xml_utils.serialize(structmap).decode("utf-8"))

    output_sm_file = os.path.join(workspace, 'structmap.xml')
    output_fs_file = os.path.join(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, 'wb+') as outfile:
        outfile.write(xml_utils.serialize(structmap))

    with open(output_fs_file, 'wb+') as outfile:
        outfile.write(xml_utils.serialize(filesec))

    print("compile_structmap created files: %s %s" %
          (output_sm_file, output_fs_file))