Skip to content

create archive function #34

@andreaa93

Description

@andreaa93

I prepared the create_archive function. Let's discuss this after the IKZ workshop @hampusnasstrom:

def get_reference(upload_id, entry_id):
    return f'../uploads/{upload_id}/archive/{entry_id}#data'


def get_entry_id_from_file_name(filename, upload_id):
    from nomad.utils import hash
    return hash(upload_id, filename)


def create_archive(
    entry_dict, context, filename, file_type, logger, *, bypass_check: bool = False
):
    import yaml
    import json
    from nomad.datamodel.context import ClientContext
    if isinstance(context, ClientContext):
        return None
    if context.raw_path_exists(filename):
        with context.raw_file(filename, "r") as file:
            existing_dict = yaml.safe_load(file)
    if context.raw_path_exists(filename) and existing_dict != entry_dict:
        logger.error(
            f"{filename} archive file already exists. "
            f"You are trying to overwrite it with a different content. "
            f"To do so, remove the existing archive and click reprocess again."
        )
    if not context.raw_path_exists(filename) or existing_dict == entry_dict or bypass_check:
        with context.raw_file(filename, "w") as newfile:
            if file_type == "json":
                json.dump(entry_dict, newfile)
            elif file_type == "yaml":
                yaml.dump(entry_dict, newfile)
        context.upload.process_updated_raw_file(filename, allow_modify=True)

    return get_reference(
        context.upload_id,
        get_entry_id_from_file_name(filename, context.upload_id)
    )

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions