All Projects → lucsorel → py2puml

lucsorel / py2puml

Licence: MIT License
Generate PlantUML class diagrams to document your Python application.

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to py2puml

com2ann
Tool for translation type comments to type annotations in Python
Stars: ✭ 115 (+125.49%)
Mutual labels:  annotations, type-hints
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (-19.61%)
Mutual labels:  annotations
CrowdTruth-core
CrowdTruth framework for crowdsourcing ground truth for training & evaluation of AI systems
Stars: ✭ 45 (-11.76%)
Mutual labels:  annotations
super-csv-annotation
'Super CSV' extention library for annotation
Stars: ✭ 29 (-43.14%)
Mutual labels:  annotations
dcdg.dart
Dart Class Diagram Generator
Stars: ✭ 98 (+92.16%)
Mutual labels:  plantuml
IntDefs
Android constants wrapped in @IntDef annotations
Stars: ✭ 17 (-66.67%)
Mutual labels:  annotations
better-serializer
General serializer for PHP. An alternative to JmsSerializer.
Stars: ✭ 27 (-47.06%)
Mutual labels:  annotations
spring4-hibernate5-example
Spring 4 and Hibernate 5 integration example using annotations.
Stars: ✭ 16 (-68.63%)
Mutual labels:  annotations
SonataAnnotationBundle
Annotations for Sonata Admin
Stars: ✭ 23 (-54.9%)
Mutual labels:  annotations
salesforce-plantuml
Salesforce app to generate UML class & ER-diagrams from your org data. Leverages the PlantUML library.
Stars: ✭ 89 (+74.51%)
Mutual labels:  plantuml
mkdocs build plantuml
MkDocs plugin to help generate your plantuml images locally or remotely as files (NOT inline)
Stars: ✭ 31 (-39.22%)
Mutual labels:  plantuml
python-autoclass
A python 3 library providing functions and decorators to automatically generate class code, such as constructor body or properties getters/setters along with optional support of validation contracts on the generated setters. Its objective is to reduce the amount of copy/paste code in your classes - and hence to help reducing human mistakes :).
Stars: ✭ 30 (-41.18%)
Mutual labels:  type-hints
plantuml-libs
A set of PlantUML libraries and a NPM cli tool to design diagrams which focus on several technologies/approaches: Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), C4 Model or even EventStorming and more.
Stars: ✭ 75 (+47.06%)
Mutual labels:  plantuml
hexo-filter-plantuml
Using PlantUML to generate UML Diagram for hexo
Stars: ✭ 23 (-54.9%)
Mutual labels:  plantuml
idle
parse source code(objective-c, java) generate uml(class diagram)
Stars: ✭ 44 (-13.73%)
Mutual labels:  plantuml
tfvars-annotations
[not-WIP] Update values in terraform.tfvars using annotations
Stars: ✭ 20 (-60.78%)
Mutual labels:  annotations
dnaPipeTE
dnaPipeTE (for de-novo assembly & annotation Pipeline for Transposable Elements), is a pipeline designed to find, annotate and quantify Transposable Elements in small samples of NGS datasets. It is very useful to quantify the proportion of TEs in newly sequenced genomes since it does not require genome assembly and works on small datasets (< 1X).
Stars: ✭ 28 (-45.1%)
Mutual labels:  annotations
AutoBindings
Set of annotations that aims to make your Android development experience easier along with lint checks.
Stars: ✭ 15 (-70.59%)
Mutual labels:  annotations
SingleFile-Lite
Feel the power of the Manifest V3. The future, right now!
Stars: ✭ 55 (+7.84%)
Mutual labels:  annotations
videojs-annotation-comments
A plugin for video.js to add support for timeline moment/range comments and annotations
Stars: ✭ 129 (+152.94%)
Mutual labels:  annotations
Python logo PlantUML logo

Python to PlantUML

Generate PlantUML class diagrams to document your Python application.

How it works

py2puml produces a class diagram PlantUML script representing classes properties (static and instance attributes) and their relations (composition and inheritance relationships).

py2puml internally uses code inspection (also called reflexion in other programming languages) and abstract tree parsing to retrieve relevant information. Some parsing features are available only since Python 3.8 (like ast.get_source_segment).

Features

From a given path corresponding to a folder containing Python code, py2puml processes each file as a module and generates a PlantUML script of its classe-like definitions using:

  • inspection and type annotations to detect:

    • static class attributes and dataclass fields
    • fields of namedtuples
    • members of enumerations
    • composition and inheritance relationships (between your domain classes only, for documentation sake). The detection of composition relationships relies on type annotations only, assigned values or expressions are never evaluated to prevent unwanted side-effects
  • parsing abstract syntax trees to detect the instance attributes defined in __init__ constructors

py2puml outputs diagrams in PlantUML syntax, which can be:

  • versioned along your code with a unit-test ensuring its consistency (see the test_py2puml.py's test_py2puml_model_on_py2uml_domain example)
  • generated and hosted along other code documentation (better option: generated documentation should not be versioned with the codebase)

To generate image files, use the PlantUML runtime, a docker image of the runtime (see think/plantuml) or of a server (see the CLI documentation below)

If you like tools related with PlantUML, you may also be interested in this lucsorel/plantuml-file-loader project: a webpack loader which converts PlantUML files into images during the webpack processing (useful to include PlantUML diagrams in your slides with RevealJS or RemarkJS).

Install

Install from PyPI:

  • with pip:
pip install py2puml
poetry add py2puml
pipenv install py2puml

Usage

CLI

Once py2puml is installed at the system level, an eponymous command is available in your environment shell.

For example, to create the diagram of the classes used by py2puml, run:

py2puml py2puml/domain py2puml.domain

This outputs the following PlantUML script:

@startuml
class py2puml.domain.umlclass.UmlAttribute {
  name: str
  type: str
  static: bool
}
class py2puml.domain.umlclass.UmlClass {
  attributes: List[UmlAttribute]
}
class py2puml.domain.umlitem.UmlItem {
  name: str
  fqn: str
}
class py2puml.domain.umlenum.Member {
  name: str
  value: str
}
class py2puml.domain.umlenum.UmlEnum {
  members: List[Member]
}
enum py2puml.domain.umlrelation.RelType {
  COMPOSITION: * {static}
  INHERITANCE: <| {static}
}
class py2puml.domain.umlrelation.UmlRelation {
  source_fqn: str
  target_fqn: str
  type: RelType
}
py2puml.domain.umlclass.UmlClass *-- py2puml.domain.umlclass.UmlAttribute
py2puml.domain.umlitem.UmlItem <|-- py2puml.domain.umlclass.UmlClass
py2puml.domain.umlenum.UmlEnum *-- py2puml.domain.umlenum.Member
py2puml.domain.umlitem.UmlItem <|-- py2puml.domain.umlenum.UmlEnum
py2puml.domain.umlrelation.UmlRelation *-- py2puml.domain.umlrelation.RelType
@enduml

Using PlantUML, this script renders this diagram:

py2puml UML Diagram

For a full overview of the CLI, run:

py2puml --help

The CLI can also be launched as a python module:

python -m py2puml py2puml/domain py2puml.domain

Pipe the result of the CLI with a PlantUML server for instantaneous documentation (rendered by ImageMagick):

# runs a local PlantUML server from a docker container:
docker run -d --rm -p 1234:8080 --name plantumlserver plantuml/plantuml-server:jetty 

py2puml py2puml/domain py2puml.domain | curl -X POST --data-binary @- http://localhost:1234/svg/ --output - | display

# stops the container when you don't need it anymore, restarts it later
docker stop plantumlserver
docker start plantumlserver

Python API

For example, to create the diagram of the classes used by py2puml:

from py2puml.py2puml import py2puml

# outputs the PlantUML content in the terminal
print(''.join(py2puml('py2puml/domain', 'py2puml.domain')))

# writes the PlantUML content in a file
with open('py2puml/domain.puml', 'w') as puml_file:
    puml_file.writelines(py2puml('py2puml/domain', 'py2puml.domain'))
  • running it (python3 -m py2puml.example) will output the previous PlantUML diagram in the terminal and write it in a file.

Tests

# directly with poetry
poetry run python -m pytest -v

# in a virtual environment
python3 -m pytest -v

Code coverage (with missed branch statements):

poetry run python -m pytest -v --cov=py2puml --cov-branch --cov-report term-missing --cov-fail-under 90

Changelog

  • 0.5.4: fixed the packaging so that the contribution guide is included in the published package
  • 0.5.3: handle constructors decorated by wrapping decorators (decorators making uses of functools.wrap)
  • 0.5.2: specify in pyproject.toml that py2puml requires python 3.8+ (ast.get_source_segment was introduced in 3.8)
  • 0.5.1: prevent from parsing inherited constructors
  • 0.5.0: handle instance attributes in class constructors, add code coverage of unit tests
  • 0.4.0: add a simple CLI
  • 0.3.1: inspect sub-folders recursively
  • 0.3.0: handle classes derived from namedtuples (attribute types are Any)
  • 0.2.0: handle inheritance relationships and enums
  • 0.1.3: first release, handle all modules of a folder and compositions of domain classes

Licence

Unless stated otherwise all works are licensed under the MIT license, a copy of which is included here.

Contributions

Pull-requests are welcome and will be processed on a best-effort basis. Follow the contributing guide.

Current limitations

  • regarding inspection

    • type hinting is optional when writing Python code and discarded when it is executed, as mentionned in the typing official documentation. The quality of the diagram output by py2puml depends on the reliability with which the type annotations were written

    The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

    • complex type hints with more than one level of genericity are not properly handled for the moment: List[MyClass] or Dict[str, MyClass] are handled properly, Dict[str, List[MyClass]] is not. If your domain classes (also called business objects or DTOs) have attributes with complex type hints, it may be a code smell indicating that you should write a class which would better represent the business logic. But I may improve this part of the library as well 😀
  • regarding the detection of instance attributes with AST parsing:

    • only constructors are visited, attributes assigned in other functions won't be documented
    • attribute types are inferred from type annotations:
      • of the attribute itself
      • of the variable assigned to the attribute: a signature parameter or a locale variable
      • to avoid side-effects, no code is executed nor interpreted

Alternatives

If py2puml does not meet your needs (suggestions and pull-requests are welcome), you can have a look at these projects which follow other approaches (AST, linting, modeling):

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].