modelica-builder


Namemodelica-builder JSON
Version 0.5.1 PyPI version JSON
download
home_pagehttps://github.com/urbanopt/modelica-builder/releases
SummaryModelica builder enables programmatic parsing and modification of Modelica files.
upload_time2024-01-05 21:43:37
maintainer
docs_urlNone
authorNicholas Long
requires_python>=3.9,<3.13
licenseBSD-3-Clause
keywords modelica physics-based modeling abstract syntax tree ast
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ================
Modelica Builder
================

The Modelica Builder project aims to make in-place modifications to Modelica language files easier.
The principal use case is to load, modify using higher level abstracted methods, and then save the
resulting file. The user has access to the entire Abstract Syntax Tree of the entire Modelica grammar.

The Modelica Builder project does not:

* Compile nor check for syntax validity

Install
-------

:code:`pip install modelica-builder`


Usage
-----

ModBuild provides specific methods for reading and modifying files.

.. code-block:: python

    from modelica_builder.model import Model

    # parse the model file
    source_file = 'DCMotor.mo'
    model = Model(source_file)

    # do read and modify the model
    # refer to modelica_builder.model.Model class methods to see what's available
    name = model.get_name()
    model.set_name('New' + name)
    model.add_connect('some.component.port_a', 'another.component.port_b')
    model.insert_component('MyComponentType', 'myInst',
                            modifications={'arg1': '1234'}, string_comment='my comment',
                            annotations=['my annotation'], insert_index=0)

    # save the result
    model.save_as('NewDCMotor.mo')

You can also define your own classes for editing the file

.. code-block:: python

    from modelica_builder.edit import Edit
    from modelica_builder.selector import Selector
    from modelica_builder.transformation import SimpleTransformation

    class MySelector(Selector):
        # implement class for selecting AST nodes

    # define the edit to make to the node's text and combine it with the selector
    edit = Edit.make_replace('FOOBAR')
    selector = MySelector()
    transformation = SimpleTransformation(selector, edit)

    model = Model('my_modelica_file.mo')
    # add your custom transformation
    model.add(transformation)
    model.save_as('result.mo')

Transformations specify what nodes to change and how to change them. This is done by combining
Selectors and Edits. Selectors specify how to select nodes in the AST, and edits are modifications
(insert, replace, delete) to the text of selected nodes.

A Transformer is a collection of Transformations, which can then be applied to a file.

See the tests for more examples and information.


Development
-----------

For developers, dependency management is through `Poetry`_. Installation is accomplished by running :code:`pip install poetry`.

.. code-block:: bash

    pip install poetry

    # install after cloning repo
    poetry install

If you change the source grammar file you need to regenerate the parser and lexer.

With docker installed, run these commands from this the repo's root directory

.. code-block:: bash

    # build Antlr container
    docker build -t antlr4:latest -f antlr/Dockerfile .

    # run parser generator for python
    docker run -v "$(pwd)/modelica_builder/modelica_parser":/var/antlrResult \
        antlr4:latest \
        -Dlanguage=Python3 /var/antlrResult/modelica.g4

    # commit results along with grammar file


If not using Docker, Install antlr4 following `these instructions <https://github.com/antlr/antlr4/blob/master/doc/getting-started.md#installation>`_

.. code-block:: bash

    # in modelica_building/modelica_parser
    antlr4 -Dlanguage=Python3 modelica.g4

    # commit results along with grammar file

Managed Tasks
-------------

Updating Licenses
*****************

To apply the copyright/license to all the files, run the following managed task. To update, then update the
script, and then rerun the managed task.

.. code-block:: bash

    ./setup.py update_licenses


Testing
*******

To run the tests, simply run the following:

.. code-block:: bash

    poetry run pytest

Known Issues
------------

* The transformations occur on strings which are immutable. Need to investigate using byte arrays. This does not cause errors, but can be slow when parsing really large modelica files.

Release Instructions
--------------------

* Bump version to <NEW_VERSION> in setup.cfg (use semantic versioning as much as possible).
* Run `poetry run pre-commit --all-files`
* In a prep-release branch, push the changes to GitHub and draft a release against the latest branch.
    * Run 'auto-generate changelog' and copy the contents to the CHANGELOG.rst. Cull any items that are repeated.
    * Discard the draft release (you will create an official one off of the main branch)
* Merge the prep-release branch into develop.
* Create a PR against develop into main.
* Run `git tag <NEW_VERSION>`. (Note that `python setup.py --version` pulls from the latest tag`.)
* After main branch passes, then merge and checkout the main branch. Build the distribution using the following code:

.. code-block:: bash

    # Remove old dist packages
    rm -rf dist/*

    poetry publish --build

* Push the tag to GitHub after everything is published to PyPi, then go to GitHub and add in the CHANGELOG.rst notes into the tagged release and officially release.

.. code-block:: bash

    git push origin <NEW_VERSION>


.. _Poetry: https://python-poetry.org/docs/


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/urbanopt/modelica-builder/releases",
    "name": "modelica-builder",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<3.13",
    "maintainer_email": "",
    "keywords": "Modelica,Physics-based Modeling,Abstract Syntax Tree,AST",
    "author": "Nicholas Long",
    "author_email": "nicholas.long@nrel.gov",
    "download_url": "https://files.pythonhosted.org/packages/bc/28/d733b68d88e33a1d4c50f859d5b7a158bb0fcbc90748a7babf10dfc7b62b/modelica_builder-0.5.1.tar.gz",
    "platform": null,
    "description": "================\nModelica Builder\n================\n\nThe Modelica Builder project aims to make in-place modifications to Modelica language files easier.\nThe principal use case is to load, modify using higher level abstracted methods, and then save the\nresulting file. The user has access to the entire Abstract Syntax Tree of the entire Modelica grammar.\n\nThe Modelica Builder project does not:\n\n* Compile nor check for syntax validity\n\nInstall\n-------\n\n:code:`pip install modelica-builder`\n\n\nUsage\n-----\n\nModBuild provides specific methods for reading and modifying files.\n\n.. code-block:: python\n\n    from modelica_builder.model import Model\n\n    # parse the model file\n    source_file = 'DCMotor.mo'\n    model = Model(source_file)\n\n    # do read and modify the model\n    # refer to modelica_builder.model.Model class methods to see what's available\n    name = model.get_name()\n    model.set_name('New' + name)\n    model.add_connect('some.component.port_a', 'another.component.port_b')\n    model.insert_component('MyComponentType', 'myInst',\n                            modifications={'arg1': '1234'}, string_comment='my comment',\n                            annotations=['my annotation'], insert_index=0)\n\n    # save the result\n    model.save_as('NewDCMotor.mo')\n\nYou can also define your own classes for editing the file\n\n.. code-block:: python\n\n    from modelica_builder.edit import Edit\n    from modelica_builder.selector import Selector\n    from modelica_builder.transformation import SimpleTransformation\n\n    class MySelector(Selector):\n        # implement class for selecting AST nodes\n\n    # define the edit to make to the node's text and combine it with the selector\n    edit = Edit.make_replace('FOOBAR')\n    selector = MySelector()\n    transformation = SimpleTransformation(selector, edit)\n\n    model = Model('my_modelica_file.mo')\n    # add your custom transformation\n    model.add(transformation)\n    model.save_as('result.mo')\n\nTransformations specify what nodes to change and how to change them. This is done by combining\nSelectors and Edits. Selectors specify how to select nodes in the AST, and edits are modifications\n(insert, replace, delete) to the text of selected nodes.\n\nA Transformer is a collection of Transformations, which can then be applied to a file.\n\nSee the tests for more examples and information.\n\n\nDevelopment\n-----------\n\nFor developers, dependency management is through `Poetry`_. Installation is accomplished by running :code:`pip install poetry`.\n\n.. code-block:: bash\n\n    pip install poetry\n\n    # install after cloning repo\n    poetry install\n\nIf you change the source grammar file you need to regenerate the parser and lexer.\n\nWith docker installed, run these commands from this the repo's root directory\n\n.. code-block:: bash\n\n    # build Antlr container\n    docker build -t antlr4:latest -f antlr/Dockerfile .\n\n    # run parser generator for python\n    docker run -v \"$(pwd)/modelica_builder/modelica_parser\":/var/antlrResult \\\n        antlr4:latest \\\n        -Dlanguage=Python3 /var/antlrResult/modelica.g4\n\n    # commit results along with grammar file\n\n\nIf not using Docker, Install antlr4 following `these instructions <https://github.com/antlr/antlr4/blob/master/doc/getting-started.md#installation>`_\n\n.. code-block:: bash\n\n    # in modelica_building/modelica_parser\n    antlr4 -Dlanguage=Python3 modelica.g4\n\n    # commit results along with grammar file\n\nManaged Tasks\n-------------\n\nUpdating Licenses\n*****************\n\nTo apply the copyright/license to all the files, run the following managed task. To update, then update the\nscript, and then rerun the managed task.\n\n.. code-block:: bash\n\n    ./setup.py update_licenses\n\n\nTesting\n*******\n\nTo run the tests, simply run the following:\n\n.. code-block:: bash\n\n    poetry run pytest\n\nKnown Issues\n------------\n\n* The transformations occur on strings which are immutable. Need to investigate using byte arrays. This does not cause errors, but can be slow when parsing really large modelica files.\n\nRelease Instructions\n--------------------\n\n* Bump version to <NEW_VERSION> in setup.cfg (use semantic versioning as much as possible).\n* Run `poetry run pre-commit --all-files`\n* In a prep-release branch, push the changes to GitHub and draft a release against the latest branch.\n    * Run 'auto-generate changelog' and copy the contents to the CHANGELOG.rst. Cull any items that are repeated.\n    * Discard the draft release (you will create an official one off of the main branch)\n* Merge the prep-release branch into develop.\n* Create a PR against develop into main.\n* Run `git tag <NEW_VERSION>`. (Note that `python setup.py --version` pulls from the latest tag`.)\n* After main branch passes, then merge and checkout the main branch. Build the distribution using the following code:\n\n.. code-block:: bash\n\n    # Remove old dist packages\n    rm -rf dist/*\n\n    poetry publish --build\n\n* Push the tag to GitHub after everything is published to PyPi, then go to GitHub and add in the CHANGELOG.rst notes into the tagged release and officially release.\n\n.. code-block:: bash\n\n    git push origin <NEW_VERSION>\n\n\n.. _Poetry: https://python-poetry.org/docs/\n\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Modelica builder enables programmatic parsing and modification of Modelica files.",
    "version": "0.5.1",
    "project_urls": {
        "Documentation": "https://github.com/urbanopt/modelica-builder",
        "Homepage": "https://github.com/urbanopt/modelica-builder/releases",
        "Repository": "https://github.com/urbanopt/modelica-builder"
    },
    "split_keywords": [
        "modelica",
        "physics-based modeling",
        "abstract syntax tree",
        "ast"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb40eb72f8bba6ee0d0e99a8e52658eb89cc96beb5665f8b4c901cb2ae93e227",
                "md5": "7daa6517a4bcff39a2f9cb120af5cb40",
                "sha256": "4187d09bb4e1d5233554d8ede11d956a20fd4eb6be667c32039ecbbcd681ede1"
            },
            "downloads": -1,
            "filename": "modelica_builder-0.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7daa6517a4bcff39a2f9cb120af5cb40",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<3.13",
            "size": 87828,
            "upload_time": "2024-01-05T21:43:36",
            "upload_time_iso_8601": "2024-01-05T21:43:36.200532Z",
            "url": "https://files.pythonhosted.org/packages/eb/40/eb72f8bba6ee0d0e99a8e52658eb89cc96beb5665f8b4c901cb2ae93e227/modelica_builder-0.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc28d733b68d88e33a1d4c50f859d5b7a158bb0fcbc90748a7babf10dfc7b62b",
                "md5": "89d22d4a7ec31f56b127c6d454a4019a",
                "sha256": "69c3a618d9528d38ae9449f67ceb5a2321db4c09430ba20050f373238bb4e569"
            },
            "downloads": -1,
            "filename": "modelica_builder-0.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "89d22d4a7ec31f56b127c6d454a4019a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<3.13",
            "size": 81484,
            "upload_time": "2024-01-05T21:43:37",
            "upload_time_iso_8601": "2024-01-05T21:43:37.380339Z",
            "url": "https://files.pythonhosted.org/packages/bc/28/d733b68d88e33a1d4c50f859d5b7a158bb0fcbc90748a7babf10dfc7b62b/modelica_builder-0.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 21:43:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "urbanopt",
    "github_project": "modelica-builder",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "modelica-builder"
}
        
Elapsed time: 0.16384s