Skip to content

Commit 9386324

Browse files
use Filters instead of kwargs
1 parent 739740b commit 9386324

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

py2puml/export/__init__.py

Whitespace-only changes.

py2puml/export/puml.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Iterable, List
1+
from dataclasses import dataclass
2+
from typing import Callable, Iterable, List, Optional
23

34
from py2puml.domain.umlclass import UmlClass
45
from py2puml.domain.umlenum import UmlEnum
@@ -26,14 +27,39 @@
2627
FEATURE_INSTANCE = ''
2728

2829

30+
@dataclass
31+
class Filters:
32+
skip_block: Optional[Callable[[UmlItem], bool]] = None
33+
skip_relation: Optional[Callable[[UmlRelation], bool]] = None
34+
35+
36+
def should_skip(filter: Callable | None, item: UmlItem | UmlRelation) -> bool:
37+
if filter is None:
38+
return False
39+
40+
if not callable(filter):
41+
raise ValueError('Filter must be a callable')
42+
43+
try:
44+
_should_skip = filter(item)
45+
if not isinstance(_should_skip, bool):
46+
raise ValueError('Filter must return a boolean value')
47+
return _should_skip
48+
except Exception as e:
49+
raise ValueError('Error while applying filter') from e
50+
51+
2952
def to_puml_content(
30-
diagram_name: str, uml_items: List[UmlItem], uml_relations: List[UmlRelation], **kwargs
53+
diagram_name: str, uml_items: List[UmlItem], uml_relations: List[UmlRelation], filters: Optional[Filters] = None
3154
) -> Iterable[str]:
55+
if filters is None:
56+
filters = Filters()
57+
3258
yield PUML_FILE_START.format(diagram_name=diagram_name)
3359

3460
# exports the domain classes and enums
3561
for uml_item in uml_items:
36-
if 'is_block_valid' in kwargs and callable(kwargs['is_block_valid']) and not kwargs['is_block_valid'](uml_item):
62+
if should_skip(filters.skip_block, uml_item):
3763
continue
3864
if isinstance(uml_item, UmlEnum):
3965
uml_enum: UmlEnum = uml_item
@@ -52,26 +78,14 @@ def to_puml_content(
5278
attr_type=uml_attr.type,
5379
staticity=FEATURE_STATIC if uml_attr.static else FEATURE_INSTANCE,
5480
)
55-
for uml_method in uml_class.methods:
56-
if (
57-
'is_method_valid' in kwargs
58-
and callable(kwargs['is_method_valid'])
59-
and not kwargs['is_method_valid'](uml_method)
60-
):
61-
continue
62-
63-
yield f' {uml_method.represent_as_puml()}\n'
81+
# TODO: Add skip_method filter here once PR #43 is merged
6482
yield PUML_ITEM_END
6583
else:
6684
raise TypeError(f'cannot process uml_item of type {uml_item.__class__}')
6785

6886
# exports the domain relationships between classes and enums
6987
for uml_relation in uml_relations:
70-
if (
71-
'is_relation_valid' in kwargs
72-
and callable(kwargs['is_relation_valid'])
73-
and not kwargs['is_relation_valid'](uml_relation)
74-
):
88+
if should_skip(filters.skip_relation, uml_relation):
7589
continue
7690
yield PUML_RELATION_TPL.format(
7791
source_fqn=uml_relation.source_fqn, rel_type=uml_relation.type.value, target_fqn=uml_relation.target_fqn

py2puml/py2puml.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from typing import Dict, Iterable, List
1+
from typing import Dict, Iterable, List, Optional
22

33
from py2puml.domain.umlitem import UmlItem
44
from py2puml.domain.umlrelation import UmlRelation
5-
from py2puml.export.puml import to_puml_content
5+
from py2puml.export.puml import Filters, to_puml_content
66
from py2puml.inspection.inspectpackage import inspect_package
77

88

9-
def py2puml(domain_path: str, domain_module: str, **kwargs) -> Iterable[str]:
9+
def py2puml(domain_path: str, domain_module: str, filters: Optional[Filters] = None) -> Iterable[str]:
1010
domain_items_by_fqn: Dict[str, UmlItem] = {}
1111
domain_relations: List[UmlRelation] = []
1212
inspect_package(domain_path, domain_module, domain_items_by_fqn, domain_relations)
1313

14-
return to_puml_content(domain_module, domain_items_by_fqn.values(), domain_relations, **kwargs)
14+
return to_puml_content(domain_module, domain_items_by_fqn.values(), domain_relations, filters)

tests/modules/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)