Skip to main content

A program for converting NOMAD metainfo YAML schemas into Python class definitions

Project description

metainfo-yaml2py

A program for converting NOMAD metainfo YAML schemas into Python class definitions.

Installation

metainfo-yaml2py can be installed from PyPI using pip:

pip install metainfoyaml2py

You can then run the program with:

metainfo-yaml2py <target file>

For development

metainfo-yaml2py is currently under development and for installing and contributing you should clone the repository and install the package in editable mode (-e) using:

pip install -e .

Example

Running metainfo-yaml2py on the following YAML file (with the -n flag):

definitions:
  name: Example Schema
  sections:
    Activity:
      description: |
        A base class for any activity in relation to an entity.
        This docstring can span multiple lines.
      quantities:
        start_time:
          description: |
            The starting date and time of the activity.
          type: Datetime
          m_annotations:
            eln:
              component: DateTimeEditQuantity
        end_time:
          description: |
            The ending date and time of the activity.
          type: Datetime
          m_annotations:
            eln:
              component: DateTimeEditQuantity
    Entity:
      description: |
        A base class for any entity which can be related to an activity.

Will create the following python file:

#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from nomad.datamodel.data import ArchiveSection
from typing import (
    TYPE_CHECKING,
)
from nomad.metainfo import (
    Package,
    Quantity,
    Datetime,
    Section,
)
from nomad.datamodel.data import (
    ArchiveSection,
)
if TYPE_CHECKING:
    from nomad.datamodel.datamodel import (
        EntryArchive,
    )
    from structlog.stdlib import (
        BoundLogger,
    )

m_package = Package(name='Example Schema')


class Activity(ArchiveSection):
    '''
    A base class for any activity in relation to an entity.
    This docstring can span multiple lines.
    '''
    m_def = Section()
    start_time = Quantity(
        type=Datetime,
        description='''
        The starting date and time of the activity.
        ''',
        a_eln={
            "component": "DateTimeEditQuantity"
        },
    )
    end_time = Quantity(
        type=Datetime,
        description='''
        The ending date and time of the activity.
        ''',
        a_eln={
            "component": "DateTimeEditQuantity"
        },
    )

    def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
        '''
        The normalizer for the `Activity` class.

        Args:
            archive (EntryArchive): The archive containing the section that is being
            normalized.
            logger (BoundLogger): A structlog logger.
        '''
        super().normalize(archive, logger)


class Entity(ArchiveSection):
    '''
    A base class for any entity which can be related to an activity.
    '''
    m_def = Section()

    def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
        '''
        The normalizer for the `Entity` class.

        Args:
            archive (EntryArchive): The archive containing the section that is being
            normalized.
            logger (BoundLogger): A structlog logger.
        '''
        super().normalize(archive, logger)


m_package.__init_metainfo__()

Command Line Interface

metainfo-yaml2py --help
usage: metainfo-yaml2py [-h] [-o OUTPUT_DIR] [-n] [-p] yaml_path

positional arguments:
  yaml_path             The path to the YAML schema that should be converted to Python
                        classes.

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT_DIR, --output_dir OUTPUT_DIR
                        The path to the output directory of the conversion. Defaults to
                        the current directory.
  -n, --normalizers     Add empty normalizers to all class definitions.
  -p, --plugin          Create all the necessary files for a nomad plugin.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metainfoyaml2py-0.0.13.tar.gz (32.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metainfoyaml2py-0.0.13-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file metainfoyaml2py-0.0.13.tar.gz.

File metadata

  • Download URL: metainfoyaml2py-0.0.13.tar.gz
  • Upload date:
  • Size: 32.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for metainfoyaml2py-0.0.13.tar.gz
Algorithm Hash digest
SHA256 99c739ed1f3a83b78b3bd34e2ad99c8bce135b41544256a7b9516cbc568d9451
MD5 b2e5f3a0549819dd47b483049137581d
BLAKE2b-256 df13efa84b57ed323378bcb5705fa380f78363d2320115a32039a49c6cdde5e3

See more details on using hashes here.

File details

Details for the file metainfoyaml2py-0.0.13-py3-none-any.whl.

File metadata

File hashes

Hashes for metainfoyaml2py-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 5a97f20c12631bf9a7a08d9786f50a7df46acd6d9fe99f87b329a184cd39afec
MD5 bb8718e2b9bb1d1bc1d908c7e05a9535
BLAKE2b-256 e9dd1ae9ca1c0666b0970fe519df7bf63bb5ff3fd294155efc0d6deefcc554e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page