1- from typing import Iterable , List
1+ from dataclasses import dataclass
2+ from typing import Callable , Iterable , List , Optional
23
34from py2puml .domain .umlclass import UmlClass
45from py2puml .domain .umlenum import UmlEnum
2627FEATURE_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+
2952def 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
0 commit comments