domain_contexts_diagrams.py¶
Last source¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | #!/usr/bin/env python
"""Make the diagrams of entities for each isshub domain contexts."""
import importlib
import inspect
import os.path
import pkgutil
import re
import sys
from enum import Enum
from types import ModuleType
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Type,
Union,
get_type_hints,
)
import attr
from isshub.domain import contexts
from isshub.domain.utils.entity import BaseEntity
from isshub.domain.utils.repository import AbstractRepository
if TYPE_CHECKING:
from attr import _Fields # pylint: disable=no-name-in-module
try:
from typing import get_args, get_origin # type: ignore
except ImportError:
# pylint: disable=C,W
# this happen in my python 3.8 virtualenv: it shouldn't but can't figure out the problem
def get_args(tp: Any) -> Any: # noqa
return getattr(tp, "__args__", ())
def get_origin(tp: Any) -> Any: # noqa
return getattr(tp, "__origin__", None)
else:
from typing import get_args, get_origin
def import_submodules(
package: Union[str, ModuleType], skip_names: Optional[List[str]] = None
) -> Dict[str, ModuleType]:
"""Import all submodules of a module, recursively, including subpackages.
Parameters
----------
package : Union[str, ModuleType]
The package to import recursively
skip_names : Optional[List[str]]
A list of names of packages to ignore. For example ``['tests']`` to ignore packages
named "tests" (and subpackages)
Returns
-------
Dict[str, ModuleType]
Dict containing all imported packages
"""
if skip_names is None:
skip_names = []
if isinstance(package, str):
package = importlib.import_module(package)
results = {}
for __, name, is_package in pkgutil.walk_packages(
path=package.__path__, prefix=package.__name__ + "." # type: ignore
):
if any(
name.endswith(f".{skip_name}") or f".{skip_name}." in name
for skip_name in skip_names
):
continue
results[name] = importlib.import_module(name)
if is_package:
results.update(import_submodules(name, skip_names=skip_names))
return results
def get_python_path(klass: Type) -> str:
"""Get the full python path of a class.
Parameters
----------
klass: type
The class for which we want the python path
Returns
-------
str
The python path of the class, like "path.to.module.class"
"""
return f"{klass.__module__}.{klass.__name__}"
def get_dot_identifier(name: str) -> str:
"""Convert a string to be ready to be used as an identifier in a .dot file.
It actually only handles "python paths", ie the only thing to be replaced is the dot character.
We replace theses dots by three underscores.
Parameters
----------
name : str
The string to convert
Returns
-------
str
The converted string ready to be used as a dot identifier
"""
return name.replace(".", "___")
def get_final_subclasses(klass: Type) -> Dict[str, Type]:
"""Get all the subclasses of `klass` that don't have subclasses.
Parameters
----------
klass : Type
The subclasses to analyze
Returns
-------
Dict[str, Type]
A dict with one entry for each "final subclass", with the python paths as keys and the class
themselves as values.
"""
if not klass.__subclasses__():
return {get_python_path(klass): klass}
result = {}
for subclass in klass.__subclasses__():
result.update(get_final_subclasses(subclass))
return result
NoneType = type(None)
AlignLeft = chr(92) + "l" # "\l"
def filter_classes_from_module(
classes: Dict[str, Type], module_name: str
) -> Dict[str, Type]:
"""Restrict the given classes to the one found in the given module.
Parameters
----------
classes : Dict[str, Type]
A dict of classes from which to extract the ones to return. Full python path as keys, and
the classes as values.
module_name : str
The python path of the module for which we want the classes
Returns
-------
Dict[str, Type]
The filtered `classes` (same format as the given `classes` argument)
"""
prefix = f"{module_name}."
return {
class_name: klass
for class_name, klass in classes.items()
if class_name.startswith(prefix)
}
def render_dot_file(output_path: str, name: str, content: str) -> None:
"""Save `content` of a dot file.
Parameters
----------
output_path : str
The directory where to store the dot file
name : str
The base name (without extension) of the final file
content : str
The content to save in the dot file
"""
dot_path = os.path.join(output_path, f"{name}.dot")
print(f"Writing diagram {dot_path}")
with open(dot_path, "w") as file_d:
file_d.write(content)
def render_dot_record(identifier: str, title: str, lines: Iterable[str]) -> str:
"""Render a record in a dot file.
Parameters
----------
identifier : str
The identifier of the record in the dot file
title : str
The title of the record. Will be centered.
lines : Iterable[str]
The lines of the record. Will be left aligned.
Returns
-------
str
The line representing the record for the dot file.
"""
lines_parts = "|".join(f"{line} {AlignLeft}" for line in lines)
return f'{identifier} [label="{title}|{lines_parts}"]'
def render_dot_link(source: str, dest: str, label: Optional[str]) -> str:
"""Render a link between a `source` and a `dest` in a dot file.
Parameters
----------
source : str
The source of the link in the dot file
dest : str
The destination of the link in the dot file
label : Optional[str]
If set, will be the label of the link.
Returns
-------
str
The line representing the link for the dot file.
"""
result = f"{source} -> {dest}"
if label:
result += f' [label="{label}"]'
return result
def render_enum(enum: Type[Enum]) -> Tuple[str, str]:
"""Render the given `enum` to be incorporated in a dot file.
Parameters
----------
enum : Type[Enum]
The enum to render
Returns
-------
str
The name of the enum as a dot identifier
str
The definition of the enum to represent it in the diagram
"""
dot_name = get_dot_identifier(get_python_path(enum))
return dot_name, render_dot_record(
dot_name, f"<__class__> Enum: {enum.__name__}", (value.value for value in enum)
)
def get_optional_type(type_: Any) -> Union[None, Any]:
"""Get the optional type defined in the given `type_`.
Only works for one of these syntax:
- ``Optional[TheType]``
- ``Union[TheType, None'``
Parameters
----------
type_ : Any
The type (from from a call to ``get_type_hints``) to analyse
Returns
-------
Union[None, Any]
Will be ``None`` if the `type_`
"""
if get_origin(type_) is not Union:
return None
args = get_args(type_)
if len(args) != 2:
return None
if NoneType not in args:
return None
return [arg for arg in args if arg is not NoneType][0]
def validate_entity(
name: str,
entity: Type[BaseEntity],
context: str,
linkable_entities: Dict[str, Type[BaseEntity]],
) -> Dict[str, Tuple[Any, bool]]:
"""Validate that we can handle the given entity and its fields.
We only handle fields defined with a "type hint", restricted to:
- the ones with a direct type
- the ones defined as ``Optional`` (which is, in fact, a ``Union`` with the type and
``NoneType``)
The direct type, if in the ``isshub`` namespace, must be in the given `context` (in the given
`linkable_entities`.
Parameters
----------
name : str
The name of the `entity`
entity : Type[BaseEntity]
The entity to validate
context : str
The name of the context, ie the name of the module containing the `entity` and the
`linkable_entities`
linkable_entities : Dict[str, Type[BaseEntity]]
A dict containing all the entities the `entity` to validate can link to, with their full python
path as keys, and the entities themselves as values
Returns
-------
Dict[str, Tuple[Any, bool]]
A dict with an entry for each field. Each field has its name as key, and, as value, a tuple
with the final type and if the field is required or not.
Raises
------
NotImplementedError
If the type is a ``Union`` of more than two types or with one not being ``NoneType``
TypeError
If the type is an object in the ``isshub`` namespace that is not in the given
`linkable_entities` (except for enums, actually)
"""
types = get_type_hints(entity)
fields = {}
for field_name, field_type in types.items():
required = True
if get_origin(field_type) is Union:
args = get_args(field_type)
if len(args) != 2:
raise NotImplementedError(
f"{name}.{field_name} : {field_type}"
" - Union type with more that two choices is not implemented"
)
if NoneType not in args:
raise NotImplementedError(
f"{name}.{field_name} : {field_type}"
" - Union type without None is not implemented"
)
required = False
field_type = [arg for arg in args if arg is not NoneType][0]
if field_type.__module__.startswith("isshub") and not issubclass(
field_type, Enum
):
if get_python_path(field_type) not in linkable_entities:
raise TypeError(
f"{name}.{field_name} : {field_type}"
f" - It's not a valid entity in context {context}"
)
fields[field_name] = (field_type, required)
return fields
def render_entity_link(
source_name: str,
field_name: str,
dest_name: str,
required: bool,
attr_fields: "_Fields",
) -> str:
"""Render a link between the field of an entity to another class.
Parameters
----------
source_name : str
The dot identifier of the source class. The source class is expected to be an entity class
field_name : str
The field in the source class that is linked to the dest class
dest_name : str
The dot identifier of the dest class.
required : bool
If the link is mandatory or optional
attr_fields : NamedTuple
A named tuple containing all fields as viewed by the ``attr`` module, to access the metadata
of such fields, to get the ``relation_verbose_name`` metadata. Without such a medata, the
link will be simply labelled "0..1" or "1" (depending on the `required` attribute), else
this verbose name will be used.
Returns
-------
str
The string to be used in the dot file to represent the link.
"""
try:
link_label = (
f'{getattr(attr_fields, field_name).metadata["relation_verbose_name"]}'
)
except Exception: # pylint: disable=broad-except
link_label = "(" + ("1" if required else "0..1") + ")"
return render_dot_link(
f"{source_name}:{field_name}", f"{dest_name}:__class__", link_label
)
def render_entity(
name: str,
entity: Type[BaseEntity],
context: str,
linkable_entities: Dict[str, Type[BaseEntity]],
) -> Tuple[Dict[str, str], Set[str]]:
"""Render the given `entity` to be incorporated in a dot file, with links.
Parameters
----------
name : str
The name of the `entity`
entity : Type[BaseEntity]
The entity to render
context : str
The name of the context, ie the name of the module containing the `entity` and the
`linkable_entities`
linkable_entities : Dict[str, Type[BaseEntity]]
A dict containing all the entities the `entity` to validate can link to, with their full python
path as keys, and the entities themselves as values
Returns
-------
Dict[str, str]
Lines representing the entities (or enums) to render in the graph.
The keys are the dot identifier of the entity (or enum), and the values are the line to put
in the dot file to render them.
There is at least one entry, the rendered `entity`, but there can be more entries, if the
`entity` is linked to some enums (we use a dict to let the caller to deduplicate enums with
the same identifiers if called from many entities)
Set[str]
Lines representing the links between the `entity` and other entities or enums.
"""
lines = {}
links = set()
dot_name = get_dot_identifier(name)
attr_fields = attr.fields(entity)
fields = {}
for field_name, (field_type, required) in validate_entity(
name, entity, context, linkable_entities
).items():
link_to = None
if issubclass(field_type, Enum):
link_to, enum_line = render_enum(field_type)
lines[link_to] = enum_line
elif field_type.__module__.startswith("isshub"):
link_to = get_dot_identifier(get_python_path(field_type))
if link_to:
links.add(
render_entity_link(dot_name, field_name, link_to, required, attr_fields)
)
fields[field_name] = field_type.__name__
if not required:
fields[field_name] = f"{fields[field_name]} (optional)"
lines[dot_name] = render_dot_record(
dot_name,
f"<__class__> Entity: {entity.__name__}",
(f"<{f_name}> {f_name} : {f_type}" for f_name, f_type in fields.items()),
)
return lines, links
def make_domain_context_entities_diagram(
context_name: str, subclasses: Dict[str, Type[BaseEntity]], output_path: str
) -> None:
"""Make the graph of entities in the given contexts.
Parameters
----------
context_name : str
The name of the context, represented by the python path of its module
subclasses : Dict[str, Type[BaseEntity]]
All the subclasses of ``BaseEntity`` from which to extract the ones to render.
Only subclasses present in the given context will be rendered.
output_path : str
The path where to save the generated graph
"""
# restrict the subclasses of ``BaseEntity`` to the ones in the given module name
context_subclasses = filter_classes_from_module(subclasses, context_name)
# render entities and all links between them
entity_lines, links = {}, set()
for subclass_name, subclass in context_subclasses.items():
subclass_lines, subclass_links = render_entity(
subclass_name,
subclass,
context_name,
context_subclasses,
)
entity_lines.update(subclass_lines)
links.update(subclass_links)
# compose the content of the dot file
dot_file_content = (
"""\
digraph domain_context_entities {
label = "Domain context entities [%s]"
#labelloc = "t"
rankdir=LR
node[shape=record]
"""
% context_name
)
for line in tuple(entity_lines.values()) + tuple(links):
dot_file_content += f" {line}\n"
dot_file_content += "}"
render_dot_file(output_path, f"{context_name}-entities", dot_file_content)
re_optional = re.compile(r"(?:typing\.)?Union\[(.*), NoneType]")
re_literal = re.compile(r"(?:typing\.)?Literal\[(.*?)]")
def render_repository( # pylint: disable=too-many-locals
name: str, repository: Type[AbstractRepository], context: str
) -> str:
"""Render the content of the dot file for the given `repository`.
Parameters
----------
name : str
The name of the `repository`
repository : Type[AbstractRepository]
The repository to render
context : str
The name of the context containing the `repository`
Returns
-------
str
The content of the dot file for the diagram of the given `repository`
"""
members = {
name: value
for name, value in inspect.getmembers(repository)
if not name.startswith("_")
}
methods = {
name: value for name, value in members.items() if inspect.isfunction(value)
}
entity_class = members["entity_class"]
re_context = re.compile(context + r".(?:\w+\.)*(\w+)")
def optimize_annotation(type_: Any) -> str: # pylint: disable=W
if isinstance(type_, type):
return type_.__name__
result = str(type_)
for regexp, replacement in (
(re_context, r"\1"),
(re_literal, r"\1"),
(re_optional, r"Optional[\1]"),
):
result = regexp.sub(replacement, result)
return result.replace("~Entity", entity_class.__name__).replace("typing.", "")
methods_lines = []
for method_name, method in methods.items():
signature = inspect.signature(method)
params = []
for param_name, param in signature.parameters.items():
if param_name == "self":
continue
params.append(
"".join(
(
param_name,
""
if not param.annotation or param.annotation is param.empty
else ": %s" % optimize_annotation(param.annotation),
"" if param.default is param.empty else " = %s" % param.default,
)
)
)
methods_lines.append(
f"{method_name}(%s)%s"
% (
", ".join(params),
""
if not signature.return_annotation
or signature.return_annotation is signature.empty
else " → %s" % optimize_annotation(signature.return_annotation),
)
)
return render_dot_record(
get_dot_identifier(get_python_path(repository)),
f"{repository.__name__} (for {entity_class.__name__} entity)",
methods_lines,
)
def make_domain_context_repositories_diagram(
context_name: str, subclasses: Dict[str, Type[AbstractRepository]], output_path: str
) -> None:
"""Make the graph of entities in the given contexts.
Parameters
----------
context_name : str
The name of the context, represented by the python path of its module
subclasses : Dict[str, Type[AbstractRepository]]
All the subclasses of ``AbstractRepository`` from which to extract the ones to render.
Only subclasses present in the given context will be rendered.
output_path : str
The path where to save the generated graph
"""
# restrict the subclasses of ``AbstractRepository`` to the ones in the given module name
context_subclasses = filter_classes_from_module(subclasses, context_name)
rendered_repositories = [
render_repository(subclass_name, subclass, context_name)
for subclass_name, subclass in context_subclasses.items()
]
# compose the content of the dot file
dot_file_content = (
"""\
digraph domain_context_repositories {
label = "Domain context repositories [%s]"
#labelloc = "t"
rankdir=LR
node[shape=record]
"""
% context_name
)
for line in rendered_repositories:
dot_file_content += f" {line}\n"
dot_file_content += "}"
render_dot_file(output_path, f"{context_name}-repositories", dot_file_content)
def make_domain_contexts_diagrams(output_path: str) -> None:
"""Make the diagrams of entities for each domain contexts.
Parameters
----------
output_path : str
The path where to save the generated diagrams
"""
# we need to import all python files (except tests) to be sure we have access to all python code
import_submodules(contexts, skip_names=["tests"])
entities = get_final_subclasses(BaseEntity)
repositories = get_final_subclasses(AbstractRepository)
# we render each context independently, assuming that each one is directly at the root of
# the ``contexts`` package
for module in pkgutil.iter_modules(
path=contexts.__path__, prefix=contexts.__name__ + "." # type: ignore
):
make_domain_context_entities_diagram(module.name, entities, output_path)
make_domain_context_repositories_diagram(module.name, repositories, output_path)
if __name__ == "__main__":
assert len(sys.argv) > 1 and sys.argv[1], "Missing output directory"
make_domain_contexts_diagrams(sys.argv[1])
|
Changes¶
docs(domain): Add diagrams for repositories¶
- Commit
- Hash
- Date
2020-10-07 12:10:48 +0200
- Type
Modified
- Stats
+296 -35
@@ -3,8 +3,10 @@
"""Make the diagrams of entities for each isshub domain contexts."""
import importlib
+import inspect
import os.path
import pkgutil
+import re
import sys
from enum import Enum
from types import ModuleType
@@ -12,6 +14,7 @@ from typing import (
TYPE_CHECKING,
Any,
Dict,
+ Iterable,
List,
Optional,
Set,
@@ -23,12 +26,28 @@ from typing import (
import attr
+from isshub.domain import contexts
+from isshub.domain.utils.entity import BaseEntity
+from isshub.domain.utils.repository import AbstractRepository
+
if TYPE_CHECKING:
from attr import _Fields # pylint: disable=no-name-in-module
-from isshub.domain import contexts
-from isshub.domain.utils.entity import BaseEntity
+ try:
+ from typing import get_args, get_origin # type: ignore
+ except ImportError:
+ # pylint: disable=C,W
+ # this happen in my python 3.8 virtualenv: it shouldn't but can't figure out the problem
+ def get_args(tp: Any) -> Any: # noqa
+ return getattr(tp, "__args__", ())
+
+ def get_origin(tp: Any) -> Any: # noqa
+ return getattr(tp, "__origin__", None)
+
+
+else:
+ from typing import get_args, get_origin
def import_submodules(
@@ -133,6 +152,97 @@ NoneType = type(None)
AlignLeft = chr(92) + "l" # "\l"
+def filter_classes_from_module(
+ classes: Dict[str, Type], module_name: str
+) -> Dict[str, Type]:
+ """Restrict the given classes to the one found in the given module.
+
+ Parameters
+ ----------
+ classes : Dict[str, Type]
+ A dict of classes from which to extract the ones to return. Full python path as keys, and
+ the classes as values.
+ module_name : str
+ The python path of the module for which we want the classes
+
+ Returns
+ -------
+ Dict[str, Type]
+ The filtered `classes` (same format as the given `classes` argument)
+
+ """
+ prefix = f"{module_name}."
+ return {
+ class_name: klass
+ for class_name, klass in classes.items()
+ if class_name.startswith(prefix)
+ }
+
+
+def render_dot_file(output_path: str, name: str, content: str) -> None:
+ """Save `content` of a dot file.
+
+ Parameters
+ ----------
+ output_path : str
+ The directory where to store the dot file
+ name : str
+ The base name (without extension) of the final file
+ content : str
+ The content to save in the dot file
+ """
+ dot_path = os.path.join(output_path, f"{name}.dot")
+ print(f"Writing diagram {dot_path}")
+ with open(dot_path, "w") as file_d:
+ file_d.write(content)
+
+
+def render_dot_record(identifier: str, title: str, lines: Iterable[str]) -> str:
+ """Render a record in a dot file.
+
+ Parameters
+ ----------
+ identifier : str
+ The identifier of the record in the dot file
+ title : str
+ The title of the record. Will be centered.
+ lines : Iterable[str]
+ The lines of the record. Will be left aligned.
+
+ Returns
+ -------
+ str
+ The line representing the record for the dot file.
+
+ """
+ lines_parts = "|".join(f"{line} {AlignLeft}" for line in lines)
+ return f'{identifier} [label="{title}|{lines_parts}"]'
+
+
+def render_dot_link(source: str, dest: str, label: Optional[str]) -> str:
+ """Render a link between a `source` and a `dest` in a dot file.
+
+ Parameters
+ ----------
+ source : str
+ The source of the link in the dot file
+ dest : str
+ The destination of the link in the dot file
+ label : Optional[str]
+ If set, will be the label of the link.
+
+ Returns
+ -------
+ str
+ The line representing the link for the dot file.
+
+ """
+ result = f"{source} -> {dest}"
+ if label:
+ result += f' [label="{label}"]'
+ return result
+
+
def render_enum(enum: Type[Enum]) -> Tuple[str, str]:
"""Render the given `enum` to be incorporated in a dot file.
@@ -146,17 +256,44 @@ def render_enum(enum: Type[Enum]) -> Tuple[str, str]:
str
The name of the enum as a dot identifier
str
- The definition of the enum to represent it in the graph
+ The definition of the enum to represent it in the diagram
"""
dot_name = get_dot_identifier(get_python_path(enum))
- enum_parts = "|".join(f"{value.value} {AlignLeft}" for value in enum)
- return (
- dot_name,
- f'{dot_name} [label="<__class__> Enum: {enum.__name__}|{enum_parts}"]',
+ return dot_name, render_dot_record(
+ dot_name, f"<__class__> Enum: {enum.__name__}", (value.value for value in enum)
)
+def get_optional_type(type_: Any) -> Union[None, Any]:
+ """Get the optional type defined in the given `type_`.
+
+ Only works for one of these syntax:
+
+ - ``Optional[TheType]``
+ - ``Union[TheType, None'``
+
+ Parameters
+ ----------
+ type_ : Any
+ The type (from from a call to ``get_type_hints``) to analyse
+
+ Returns
+ -------
+ Union[None, Any]
+ Will be ``None`` if the `type_`
+
+ """
+ if get_origin(type_) is not Union:
+ return None
+ args = get_args(type_)
+ if len(args) != 2:
+ return None
+ if NoneType not in args:
+ return None
+ return [arg for arg in args if arg is not NoneType][0]
+
+
def validate_entity(
name: str,
entity: Type[BaseEntity],
@@ -206,19 +343,20 @@ def validate_entity(
for field_name, field_type in types.items():
required = True
- if getattr(field_type, "__origin__", None) is Union:
- if len(field_type.__args__) != 2:
+ if get_origin(field_type) is Union:
+ args = get_args(field_type)
+ if len(args) != 2:
raise NotImplementedError(
f"{name}.{field_name} : {field_type}"
" - Union type with more that two choices is not implemented"
)
- if NoneType not in field_type.__args__:
+ if NoneType not in args:
raise NotImplementedError(
f"{name}.{field_name} : {field_type}"
" - Union type without None is not implemented"
)
required = False
- field_type = [arg for arg in field_type.__args__ if arg is not NoneType][0]
+ field_type = [arg for arg in args if arg is not NoneType][0]
if field_type.__module__.startswith("isshub") and not issubclass(
field_type, Enum
@@ -234,7 +372,7 @@ def validate_entity(
return fields
-def render_link(
+def render_entity_link(
source_name: str,
field_name: str,
dest_name: str,
@@ -272,7 +410,9 @@ def render_link(
except Exception: # pylint: disable=broad-except
link_label = "(" + ("1" if required else "0..1") + ")"
- return f'{source_name}:{field_name} -> {dest_name}:__class__ [label="{link_label}"]'
+ return render_dot_link(
+ f"{source_name}:{field_name}", f"{dest_name}:__class__", link_label
+ )
def render_entity(
@@ -329,24 +469,24 @@ def render_entity(
link_to = get_dot_identifier(get_python_path(field_type))
if link_to:
- links.add(render_link(dot_name, field_name, link_to, required, attr_fields))
+ links.add(
+ render_entity_link(dot_name, field_name, link_to, required, attr_fields)
+ )
fields[field_name] = field_type.__name__
if not required:
fields[field_name] = f"{fields[field_name]} (optional)"
- fields_parts = "|".join(
- f"<{f_name}> {f_name} : {f_type} {AlignLeft}"
- for f_name, f_type in fields.items()
+ lines[dot_name] = render_dot_record(
+ dot_name,
+ f"<__class__> Entity: {entity.__name__}",
+ (f"<{f_name}> {f_name} : {f_type}" for f_name, f_type in fields.items()),
)
- lines[
- dot_name
- ] = f'{dot_name} [label="<__class__> Entity: {entity.__name__}|{fields_parts}"]'
return lines, links
-def make_domain_context_graph(
+def make_domain_context_entities_diagram(
context_name: str, subclasses: Dict[str, Type[BaseEntity]], output_path: str
) -> None:
"""Make the graph of entities in the given contexts.
@@ -356,18 +496,14 @@ def make_domain_context_graph(
context_name : str
The name of the context, represented by the python path of its module
subclasses : Dict[str, Type[BaseEntity]]
- All the subclasses of ``BaseEntity`` from which to extract the modules to render.
+ All the subclasses of ``BaseEntity`` from which to extract the ones to render.
Only subclasses present in the given context will be rendered.
output_path : str
The path where to save the generated graph
"""
# restrict the subclasses of ``BaseEntity`` to the ones in the given module name
- context_subclasses = {
- subclass_name: subclass
- for subclass_name, subclass in subclasses.items()
- if subclass_name.startswith(context_name + ".")
- }
+ context_subclasses = filter_classes_from_module(subclasses, context_name)
# render entities and all links between them
entity_lines, links = {}, set()
@@ -385,7 +521,7 @@ def make_domain_context_graph(
dot_file_content = (
"""\
digraph domain_context_entities {
- label = "Domain context [%s]"
+ label = "Domain context entities [%s]"
#labelloc = "t"
rankdir=LR
node[shape=record]
@@ -396,10 +532,132 @@ digraph domain_context_entities {
dot_file_content += f" {line}\n"
dot_file_content += "}"
- dot_path = os.path.join(output_path, f"{context_name}-entities.dot")
- print(f"Writing graph for domain context {context_name} in {dot_path}")
- with open(dot_path, "w") as file_d:
- file_d.write(dot_file_content)
+ render_dot_file(output_path, f"{context_name}-entities", dot_file_content)
+
+
+re_optional = re.compile(r"(?:typing\.)?Union\[(.*), NoneType]")
+re_literal = re.compile(r"(?:typing\.)?Literal\[(.*?)]")
+
+
+def render_repository( # pylint: disable=too-many-locals
+ name: str, repository: Type[AbstractRepository], context: str
+) -> str:
+ """Render the content of the dot file for the given `repository`.
+
+ Parameters
+ ----------
+ name : str
+ The name of the `repository`
+ repository : Type[AbstractRepository]
+ The repository to render
+ context : str
+ The name of the context containing the `repository`
+
+ Returns
+ -------
+ str
+ The content of the dot file for the diagram of the given `repository`
+
+ """
+ members = {
+ name: value
+ for name, value in inspect.getmembers(repository)
+ if not name.startswith("_")
+ }
+ methods = {
+ name: value for name, value in members.items() if inspect.isfunction(value)
+ }
+ entity_class = members["entity_class"]
+
+ re_context = re.compile(context + r".(?:\w+\.)*(\w+)")
+
+ def optimize_annotation(type_: Any) -> str: # pylint: disable=W
+ if isinstance(type_, type):
+ return type_.__name__
+ result = str(type_)
+ for regexp, replacement in (
+ (re_context, r"\1"),
+ (re_literal, r"\1"),
+ (re_optional, r"Optional[\1]"),
+ ):
+ result = regexp.sub(replacement, result)
+ return result.replace("~Entity", entity_class.__name__).replace("typing.", "")
+
+ methods_lines = []
+ for method_name, method in methods.items():
+ signature = inspect.signature(method)
+ params = []
+ for param_name, param in signature.parameters.items():
+ if param_name == "self":
+ continue
+ params.append(
+ "".join(
+ (
+ param_name,
+ ""
+ if not param.annotation or param.annotation is param.empty
+ else ": %s" % optimize_annotation(param.annotation),
+ "" if param.default is param.empty else " = %s" % param.default,
+ )
+ )
+ )
+ methods_lines.append(
+ f"{method_name}(%s)%s"
+ % (
+ ", ".join(params),
+ ""
+ if not signature.return_annotation
+ or signature.return_annotation is signature.empty
+ else " → %s" % optimize_annotation(signature.return_annotation),
+ )
+ )
+
+ return render_dot_record(
+ get_dot_identifier(get_python_path(repository)),
+ f"{repository.__name__} (for {entity_class.__name__} entity)",
+ methods_lines,
+ )
+
+
+def make_domain_context_repositories_diagram(
+ context_name: str, subclasses: Dict[str, Type[AbstractRepository]], output_path: str
+) -> None:
+ """Make the graph of entities in the given contexts.
+
+ Parameters
+ ----------
+ context_name : str
+ The name of the context, represented by the python path of its module
+ subclasses : Dict[str, Type[AbstractRepository]]
+ All the subclasses of ``AbstractRepository`` from which to extract the ones to render.
+ Only subclasses present in the given context will be rendered.
+ output_path : str
+ The path where to save the generated graph
+
+ """
+ # restrict the subclasses of ``AbstractRepository`` to the ones in the given module name
+ context_subclasses = filter_classes_from_module(subclasses, context_name)
+ rendered_repositories = [
+ render_repository(subclass_name, subclass, context_name)
+ for subclass_name, subclass in context_subclasses.items()
+ ]
+
+ # compose the content of the dot file
+ dot_file_content = (
+ """\
+digraph domain_context_repositories {
+ label = "Domain context repositories [%s]"
+ #labelloc = "t"
+ rankdir=LR
+ node[shape=record]
+"""
+ % context_name
+ )
+ for line in rendered_repositories:
+ dot_file_content += f" {line}\n"
+ dot_file_content += "}"
+
+ render_dot_file(output_path, f"{context_name}-repositories", dot_file_content)
def make_domain_contexts_diagrams(output_path: str) -> None:
@@ -411,16 +669,19 @@ def make_domain_contexts_diagrams(output_path: str) -> None:
The path where to save the generated diagrams
"""
- # we need to import all python files (except tests) to find all subclasses of ``BaseEntity``
+ # we need to import all python files (except tests) to be sure we have access to all python code
import_submodules(contexts, skip_names=["tests"])
- subclasses = get_final_subclasses(BaseEntity)
+
+ entities = get_final_subclasses(BaseEntity)
+ repositories = get_final_subclasses(AbstractRepository)
# we render each context independently, assuming that each one is directly at the root of
# the ``contexts`` package
for module in pkgutil.iter_modules(
path=contexts.__path__, prefix=contexts.__name__ + "." # type: ignore
):
- make_domain_context_graph(module.name, subclasses, output_path)
+ make_domain_context_entities_diagram(module.name, entities, output_path)
+ make_domain_context_repositories_diagram(module.name, repositories, output_path)
if __name__ == "__main__":
style(entity): Change the world “model” by “entity”¶
- Commit
- Hash
- Date
2020-10-04 21:07:00 +0200
- Type
Modified
- Stats
+57 -57
@@ -1,6 +1,6 @@
#!/usr/bin/env python
-"""Make the diagrams of models for each isshub domain contexts."""
+"""Make the diagrams of entities for each isshub domain contexts."""
import importlib
import os.path
@@ -28,7 +28,7 @@ if TYPE_CHECKING:
from attr import _Fields # pylint: disable=no-name-in-module
from isshub.domain import contexts
-from isshub.domain.utils.entity import BaseModel
+from isshub.domain.utils.entity import BaseEntity
def import_submodules(
@@ -157,13 +157,13 @@ def render_enum(enum: Type[Enum]) -> Tuple[str, str]:
)
-def validate_model(
+def validate_entity(
name: str,
- model: Type[BaseModel],
+ entity: Type[BaseEntity],
context: str,
- linkable_models: Dict[str, Type[BaseModel]],
+ linkable_entities: Dict[str, Type[BaseEntity]],
) -> Dict[str, Tuple[Any, bool]]:
- """Validate that we can handle the given model and its fields.
+ """Validate that we can handle the given entity and its fields.
We only handle fields defined with a "type hint", restricted to:
- the ones with a direct type
@@ -171,20 +171,20 @@ def validate_model(
``NoneType``)
The direct type, if in the ``isshub`` namespace, must be in the given `context` (in the given
- `linkable_models`.
+ `linkable_entities`.
Parameters
----------
name : str
- The name of the `model`
- model : Type[BaseModel]
- The model to validate
+ The name of the `entity`
+ entity : Type[BaseEntity]
+ The entity to validate
context : str
- The name of the context, ie the name of the module containing the `model` and the
- `linkable_models`
- linkable_models : Dict[str, Type[BaseModel]]
- A dict containing all the models the `model` to validate can link to, with their full python
- path as keys, and the models themselves as values
+ The name of the context, ie the name of the module containing the `entity` and the
+ `linkable_entities`
+ linkable_entities : Dict[str, Type[BaseEntity]]
+ A dict containing all the entities the `entity` to validate can link to, with their full python
+ path as keys, and the entities themselves as values
Returns
-------
@@ -198,10 +198,10 @@ def validate_model(
If the type is a ``Union`` of more than two types or with one not being ``NoneType``
TypeError
If the type is an object in the ``isshub`` namespace that is not in the given
- `linkable_models` (except for enums, actually)
+ `linkable_entities` (except for enums, actually)
"""
- types = get_type_hints(model)
+ types = get_type_hints(entity)
fields = {}
for field_name, field_type in types.items():
required = True
@@ -223,10 +223,10 @@ def validate_model(
if field_type.__module__.startswith("isshub") and not issubclass(
field_type, Enum
):
- if get_python_path(field_type) not in linkable_models:
+ if get_python_path(field_type) not in linkable_entities:
raise TypeError(
f"{name}.{field_name} : {field_type}"
- f" - It's not a valid model in context {context}"
+ f" - It's not a valid entity in context {context}"
)
fields[field_name] = (field_type, required)
@@ -241,12 +241,12 @@ def render_link(
required: bool,
attr_fields: "_Fields",
) -> str:
- """Render a link between the field of a model to another class.
+ """Render a link between the field of an entity to another class.
Parameters
----------
source_name : str
- The dot identifier of the source class. The source class is expected to be a entity model
+ The dot identifier of the source class. The source class is expected to be an entity class
field_name : str
The field in the source class that is linked to the dest class
dest_name : str
@@ -275,49 +275,49 @@ def render_link(
return f'{source_name}:{field_name} -> {dest_name}:__class__ [label="{link_label}"]'
-def render_model(
+def render_entity(
name: str,
- model: Type[BaseModel],
+ entity: Type[BaseEntity],
context: str,
- linkable_models: Dict[str, Type[BaseModel]],
+ linkable_entities: Dict[str, Type[BaseEntity]],
) -> Tuple[Dict[str, str], Set[str]]:
- """Render the given `model` to be incorporated in a dot file, with links.
+ """Render the given `entity` to be incorporated in a dot file, with links.
Parameters
----------
name : str
- The name of the `model`
- model : Type[BaseModel]
- The model to render
+ The name of the `entity`
+ entity : Type[BaseEntity]
+ The entity to render
context : str
- The name of the context, ie the name of the module containing the `model` and the
- `linkable_models`
- linkable_models : Dict[str, Type[BaseModel]]
- A dict containing all the models the `model` to validate can link to, with their full python
- path as keys, and the models themselves as values
+ The name of the context, ie the name of the module containing the `entity` and the
+ `linkable_entities`
+ linkable_entities : Dict[str, Type[BaseEntity]]
+ A dict containing all the entities the `entity` to validate can link to, with their full python
+ path as keys, and the entities themselves as values
Returns
-------
Dict[str, str]
- Lines representing the models (or enums) to render in the graph.
- The keys are the dot identifier of the model (or enum), and the values are the line to put
+ Lines representing the entities (or enums) to render in the graph.
+ The keys are the dot identifier of the entity (or enum), and the values are the line to put
in the dot file to render them.
- There is at least one entry, the rendered `model`, but there can be more entries, if the
- `model` is linked to some enums (we use a dict to let the caller to deduplicate enums with
- the same identifiers if called from many models)
+ There is at least one entry, the rendered `entity`, but there can be more entries, if the
+ `entity` is linked to some enums (we use a dict to let the caller to deduplicate enums with
+ the same identifiers if called from many entities)
Set[str]
- Lines representing the links between the `model` and other models or enums.
+ Lines representing the links between the `entity` and other entities or enums.
"""
lines = {}
links = set()
dot_name = get_dot_identifier(name)
- attr_fields = attr.fields(model)
+ attr_fields = attr.fields(entity)
fields = {}
- for field_name, (field_type, required) in validate_model(
- name, model, context, linkable_models
+ for field_name, (field_type, required) in validate_entity(
+ name, entity, context, linkable_entities
).items():
link_to = None
@@ -341,50 +341,50 @@ def render_model(
)
lines[
dot_name
- ] = f'{dot_name} [label="<__class__> Model: {model.__name__}|{fields_parts}"]'
+ ] = f'{dot_name} [label="<__class__> Entity: {entity.__name__}|{fields_parts}"]'
return lines, links
def make_domain_context_graph(
- context_name: str, subclasses: Dict[str, Type[BaseModel]], output_path: str
+ context_name: str, subclasses: Dict[str, Type[BaseEntity]], output_path: str
) -> None:
- """Make the graph of models in the given contexts.
+ """Make the graph of entities in the given contexts.
Parameters
----------
context_name : str
The name of the context, represented by the python path of its module
- subclasses : Dict[str, Type[BaseModel]]
- All the subclasses of ``BaseModel`` from which to extract the modules to render.
+ subclasses : Dict[str, Type[BaseEntity]]
+ All the subclasses of ``BaseEntity`` from which to extract the modules to render.
Only subclasses present in the given context will be rendered.
output_path : str
The path where to save the generated graph
"""
- # restrict the subclasses of ``BaseModel`` to the ones in the given module name
+ # restrict the subclasses of ``BaseEntity`` to the ones in the given module name
context_subclasses = {
subclass_name: subclass
for subclass_name, subclass in subclasses.items()
if subclass_name.startswith(context_name + ".")
}
- # render models and all links between them
- model_lines, links = {}, set()
+ # render entities and all links between them
+ entity_lines, links = {}, set()
for subclass_name, subclass in context_subclasses.items():
- subclass_lines, subclass_links = render_model(
+ subclass_lines, subclass_links = render_entity(
subclass_name,
subclass,
context_name,
context_subclasses,
)
- model_lines.update(subclass_lines)
+ entity_lines.update(subclass_lines)
links.update(subclass_links)
# compose the content of the dot file
dot_file_content = (
"""\
-digraph domain_context_models {
+digraph domain_context_entities {
label = "Domain context [%s]"
#labelloc = "t"
rankdir=LR
@@ -392,7 +392,7 @@ digraph domain_context_models {
"""
% context_name
)
- for line in tuple(model_lines.values()) + tuple(links):
+ for line in tuple(entity_lines.values()) + tuple(links):
dot_file_content += f" {line}\n"
dot_file_content += "}"
@@ -403,7 +403,7 @@ digraph domain_context_models {
def make_domain_contexts_diagrams(output_path: str) -> None:
- """Make the diagrams of models for each domain contexts.
+ """Make the diagrams of entities for each domain contexts.
Parameters
----------
@@ -411,9 +411,9 @@ def make_domain_contexts_diagrams(output_path: str) -> None:
The path where to save the generated diagrams
"""
- # we need to import all python files (except tests) to find all submodels of ``BaseModel``
+ # we need to import all python files (except tests) to find all subclasses of ``BaseEntity``
import_submodules(contexts, skip_names=["tests"])
- subclasses = get_final_subclasses(BaseModel)
+ subclasses = get_final_subclasses(BaseEntity)
# we render each context independently, assuming that each one is directly at the root of
# the ``contexts`` package
docs(domain): Add diagram of entities for each domain context¶
- Commit
- Hash
- Date
2020-10-04 11:50:37 +0200
- Type
Added
- Stats
+428 -0
@@ -0,0 +1,428 @@
+#!/usr/bin/env python
+
+"""Make the diagrams of models for each isshub domain contexts."""
+
+import importlib
+import os.path
+import pkgutil
+import sys
+from enum import Enum
+from types import ModuleType
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ Dict,
+ List,
+ Optional,
+ Set,
+ Tuple,
+ Type,
+ Union,
+ get_type_hints,
+)
+
+import attr
+
+
+if TYPE_CHECKING:
+ from attr import _Fields # pylint: disable=no-name-in-module
+
+from isshub.domain import contexts
+from isshub.domain.utils.entity import BaseModel
+
+
+def import_submodules(
+ package: Union[str, ModuleType], skip_names: Optional[List[str]] = None
+) -> Dict[str, ModuleType]:
+ """Import all submodules of a module, recursively, including subpackages.
+
+ Parameters
+ ----------
+ package : Union[str, ModuleType]
+ The package to import recursively
+ skip_names : Optional[List[str]]
+ A list of names of packages to ignore. For example ``['tests']`` to ignore packages
+ named "tests" (and subpackages)
+
+ Returns
+ -------
+ Dict[str, ModuleType]
+ Dict containing all imported packages
+ """
+ if skip_names is None:
+ skip_names = []
+ if isinstance(package, str):
+ package = importlib.import_module(package)
+ results = {}
+ for __, name, is_package in pkgutil.walk_packages(
+ path=package.__path__, prefix=package.__name__ + "." # type: ignore
+ ):
+ if any(
+ name.endswith(f".{skip_name}") or f".{skip_name}." in name
+ for skip_name in skip_names
+ ):
+ continue
+ results[name] = importlib.import_module(name)
+ if is_package:
+ results.update(import_submodules(name, skip_names=skip_names))
+ return results
+
+
+def get_python_path(klass: Type) -> str:
+ """Get the full python path of a class.
+
+ Parameters
+ ----------
+ klass: type
+ The class for which we want the python path
+
+ Returns
+ -------
+ str
+ The python path of the class, like "path.to.module.class"
+
+ """
+ return f"{klass.__module__}.{klass.__name__}"
+
+
+def get_dot_identifier(name: str) -> str:
+ """Convert a string to be ready to be used as an identifier in a .dot file.
+
+ It actually only handles "python paths", ie the only thing to be replaced is the dot character.
+
+ We replace theses dots by three underscores.
+
+ Parameters
+ ----------
+ name : str
+ The string to convert
+
+ Returns
+ -------
+ str
+ The converted string ready to be used as a dot identifier
+
+ """
+ return name.replace(".", "___")
+
+
+def get_final_subclasses(klass: Type) -> Dict[str, Type]:
+ """Get all the subclasses of `klass` that don't have subclasses.
+
+ Parameters
+ ----------
+ klass : Type
+ The subclasses to analyze
+
+ Returns
+ -------
+ Dict[str, Type]
+ A dict with one entry for each "final subclass", with the python paths as keys and the class
+ themselves as values.
+
+ """
+ if not klass.__subclasses__():
+ return {get_python_path(klass): klass}
+ result = {}
+ for subclass in klass.__subclasses__():
+ result.update(get_final_subclasses(subclass))
+ return result
+
+
+NoneType = type(None)
+AlignLeft = chr(92) + "l" # "\l"
+
+
+def render_enum(enum: Type[Enum]) -> Tuple[str, str]:
+ """Render the given `enum` to be incorporated in a dot file.
+
+ Parameters
+ ----------
+ enum : Type[Enum]
+ The enum to render
+
+ Returns
+ -------
+ str
+ The name of the enum as a dot identifier
+ str
+ The definition of the enum to represent it in the graph
+
+ """
+ dot_name = get_dot_identifier(get_python_path(enum))
+ enum_parts = "|".join(f"{value.value} {AlignLeft}" for value in enum)
+ return (
+ dot_name,
+ f'{dot_name} [label="<__class__> Enum: {enum.__name__}|{enum_parts}"]',
+ )
+
+
+def validate_model(
+ name: str,
+ model: Type[BaseModel],
+ context: str,
+ linkable_models: Dict[str, Type[BaseModel]],
+) -> Dict[str, Tuple[Any, bool]]:
+ """Validate that we can handle the given model and its fields.
+
+ We only handle fields defined with a "type hint", restricted to:
+ - the ones with a direct type
+ - the ones defined as ``Optional`` (which is, in fact, a ``Union`` with the type and
+ ``NoneType``)
+
+ The direct type, if in the ``isshub`` namespace, must be in the given `context` (in the given
+ `linkable_models`.
+
+ Parameters
+ ----------
+ name : str
+ The name of the `model`
+ model : Type[BaseModel]
+ The model to validate
+ context : str
+ The name of the context, ie the name of the module containing the `model` and the
+ `linkable_models`
+ linkable_models : Dict[str, Type[BaseModel]]
+ A dict containing all the models the `model` to validate can link to, with their full python
+ path as keys, and the models themselves as values
+
+ Returns
+ -------
+ Dict[str, Tuple[Any, bool]]
+ A dict with an entry for each field. Each field has its name as key, and, as value, a tuple
+ with the final type and if the field is required or not.
+
+ Raises
+ ------
+ NotImplementedError
+ If the type is a ``Union`` of more than two types or with one not being ``NoneType``
+ TypeError
+ If the type is an object in the ``isshub`` namespace that is not in the given
+ `linkable_models` (except for enums, actually)
+
+ """
+ types = get_type_hints(model)
+ fields = {}
+ for field_name, field_type in types.items():
+ required = True
+
+ if getattr(field_type, "__origin__", None) is Union:
+ if len(field_type.__args__) != 2:
+ raise NotImplementedError(
+ f"{name}.{field_name} : {field_type}"
+ " - Union type with more that two choices is not implemented"
+ )
+ if NoneType not in field_type.__args__:
+ raise NotImplementedError(
+ f"{name}.{field_name} : {field_type}"
+ " - Union type without None is not implemented"
+ )
+ required = False
+ field_type = [arg for arg in field_type.__args__ if arg is not NoneType][0]
+
+ if field_type.__module__.startswith("isshub") and not issubclass(
+ field_type, Enum
+ ):
+ if get_python_path(field_type) not in linkable_models:
+ raise TypeError(
+ f"{name}.{field_name} : {field_type}"
+ f" - It's not a valid model in context {context}"
+ )
+
+ fields[field_name] = (field_type, required)
+
+ return fields
+
+
+def render_link(
+ source_name: str,
+ field_name: str,
+ dest_name: str,
+ required: bool,
+ attr_fields: "_Fields",
+) -> str:
+ """Render a link between the field of a model to another class.
+
+ Parameters
+ ----------
+ source_name : str
+ The dot identifier of the source class. The source class is expected to be a entity model
+ field_name : str
+ The field in the source class that is linked to the dest class
+ dest_name : str
+ The dot identifier of the dest class.
+ required : bool
+ If the link is mandatory or optional
+ attr_fields : NamedTuple
+ A named tuple containing all fields as viewed by the ``attr`` module, to access the metadata
+ of such fields, to get the ``relation_verbose_name`` metadata. Without such a medata, the
+ link will be simply labelled "0..1" or "1" (depending on the `required` attribute), else
+ this verbose name will be used.
+
+ Returns
+ -------
+ str
+ The string to be used in the dot file to represent the link.
+
+ """
+ try:
+ link_label = (
+ f'{getattr(attr_fields, field_name).metadata["relation_verbose_name"]}'
+ )
+ except Exception: # pylint: disable=broad-except
+ link_label = "(" + ("1" if required else "0..1") + ")"
+
+ return f'{source_name}:{field_name} -> {dest_name}:__class__ [label="{link_label}"]'
+
+
+def render_model(
+ name: str,
+ model: Type[BaseModel],
+ context: str,
+ linkable_models: Dict[str, Type[BaseModel]],
+) -> Tuple[Dict[str, str], Set[str]]:
+ """Render the given `model` to be incorporated in a dot file, with links.
+
+ Parameters
+ ----------
+ name : str
+ The name of the `model`
+ model : Type[BaseModel]
+ The model to render
+ context : str
+ The name of the context, ie the name of the module containing the `model` and the
+ `linkable_models`
+ linkable_models : Dict[str, Type[BaseModel]]
+ A dict containing all the models the `model` to validate can link to, with their full python
+ path as keys, and the models themselves as values
+
+ Returns
+ -------
+ Dict[str, str]
+ Lines representing the models (or enums) to render in the graph.
+ The keys are the dot identifier of the model (or enum), and the values are the line to put
+ in the dot file to render them.
+ There is at least one entry, the rendered `model`, but there can be more entries, if the
+ `model` is linked to some enums (we use a dict to let the caller to deduplicate enums with
+ the same identifiers if called from many models)
+ Set[str]
+ Lines representing the links between the `model` and other models or enums.
+
+ """
+ lines = {}
+ links = set()
+
+ dot_name = get_dot_identifier(name)
+ attr_fields = attr.fields(model)
+ fields = {}
+
+ for field_name, (field_type, required) in validate_model(
+ name, model, context, linkable_models
+ ).items():
+
+ link_to = None
+
+ if issubclass(field_type, Enum):
+ link_to, enum_line = render_enum(field_type)
+ lines[link_to] = enum_line
+ elif field_type.__module__.startswith("isshub"):
+ link_to = get_dot_identifier(get_python_path(field_type))
+
+ if link_to:
+ links.add(render_link(dot_name, field_name, link_to, required, attr_fields))
+
+ fields[field_name] = field_type.__name__
+ if not required:
+ fields[field_name] = f"{fields[field_name]} (optional)"
+
+ fields_parts = "|".join(
+ f"<{f_name}> {f_name} : {f_type} {AlignLeft}"
+ for f_name, f_type in fields.items()
+ )
+ lines[
+ dot_name
+ ] = f'{dot_name} [label="<__class__> Model: {model.__name__}|{fields_parts}"]'
+
+ return lines, links
+
+
+def make_domain_context_graph(
+ context_name: str, subclasses: Dict[str, Type[BaseModel]], output_path: str
+) -> None:
+ """Make the graph of models in the given contexts.
+
+ Parameters
+ ----------
+ context_name : str
+ The name of the context, represented by the python path of its module
+ subclasses : Dict[str, Type[BaseModel]]
+ All the subclasses of ``BaseModel`` from which to extract the modules to render.
+ Only subclasses present in the given context will be rendered.
+ output_path : str
+ The path where to save the generated graph
+
+ """
+ # restrict the subclasses of ``BaseModel`` to the ones in the given module name
+ context_subclasses = {
+ subclass_name: subclass
+ for subclass_name, subclass in subclasses.items()
+ if subclass_name.startswith(context_name + ".")
+ }
+
+ # render models and all links between them
+ model_lines, links = {}, set()
+ for subclass_name, subclass in context_subclasses.items():
+ subclass_lines, subclass_links = render_model(
+ subclass_name,
+ subclass,
+ context_name,
+ context_subclasses,
+ )
+ model_lines.update(subclass_lines)
+ links.update(subclass_links)
+
+ # compose the content of the dot file
+ dot_file_content = (
+ """\
+digraph domain_context_models {
+ label = "Domain context [%s]"
+ #labelloc = "t"
+ rankdir=LR
+ node[shape=record]
+"""
+ % context_name
+ )
+ for line in tuple(model_lines.values()) + tuple(links):
+ dot_file_content += f" {line}\n"
+ dot_file_content += "}"
+
+ dot_path = os.path.join(output_path, f"{context_name}-entities.dot")
+ print(f"Writing graph for domain context {context_name} in {dot_path}")
+ with open(dot_path, "w") as file_d:
+ file_d.write(dot_file_content)
+
+
+def make_domain_contexts_diagrams(output_path: str) -> None:
+ """Make the diagrams of models for each domain contexts.
+
+ Parameters
+ ----------
+ output_path : str
+ The path where to save the generated diagrams
+
+ """
+ # we need to import all python files (except tests) to find all submodels of ``BaseModel``
+ import_submodules(contexts, skip_names=["tests"])
+ subclasses = get_final_subclasses(BaseModel)
+
+ # we render each context independently, assuming that each one is directly at the root of
+ # the ``contexts`` package
+ for module in pkgutil.iter_modules(
+ path=contexts.__path__, prefix=contexts.__name__ + "." # type: ignore
+ ):
+ make_domain_context_graph(module.name, subclasses, output_path)
+
+
+if __name__ == "__main__":
+ assert len(sys.argv) > 1 and sys.argv[1], "Missing output directory"
+ make_domain_contexts_diagrams(sys.argv[1])