1- from dataclasses import dataclass , field
21from functools import partial
32from typing import Any , Callable , Dict , List , Optional , Union
43
54from libcst import CSTNode
5+ from printo import repred
66
77from cstvis .dto import Context
88from cstvis .wrapper import CallableWrapper
99
1010
11- @dataclass
11+ @repred ( getters = { 'meta' : lambda x : x . _meta }, filters = { 'meta' : lambda x : x })
1212class Collector :
13- _filters : List [CallableWrapper [bool ]] = field (default_factory = list )
14- _converters : List [CallableWrapper [CSTNode ]] = field (default_factory = list )
13+ def __init__ (self , meta : Optional [Dict [str , Any ]] = None ) -> None :
14+ self ._meta : Dict [str , Any ] = meta if meta else {}
15+ self ._filters : List [CallableWrapper [bool ]] = []
16+ self ._converters : List [CallableWrapper [CSTNode ]] = []
1517
1618 def __add__ (self , other : 'Collector' ) -> 'Collector' :
1719 if not isinstance (other , type (self )):
@@ -30,12 +32,21 @@ def filter(self, function: Optional[Union[Callable[[CSTNode], bool], Callable[[C
3032 if function is None :
3133 return partial (self .filter , meta = meta ) # type: ignore[return-value]
3234
33- self ._filters . append ( CallableWrapper ( function , meta = meta ) )
35+ self ._add_to_collection ( function , meta , self . _filters )
3436 return function
3537
3638 def converter (self , function : Optional [Union [Callable [[CSTNode ], CSTNode ], Callable [[CSTNode , Context ], CSTNode ]]] = None , meta : Optional [Dict [str , Any ]] = None ) -> Union [Union [Callable [[CSTNode ], CSTNode ], Callable [[CSTNode , Context ], CSTNode ]], Callable [[Union [Callable [[CSTNode ], CSTNode ], Callable [[CSTNode , Context ], CSTNode ]]], Union [Callable [[CSTNode ], CSTNode ], Callable [[CSTNode , Context ], CSTNode ]]]]:
3739 if function is None :
3840 return partial (self .converter , meta = meta ) # type: ignore[return-value]
3941
40- self ._converters . append ( CallableWrapper ( function , meta = meta ) )
42+ self ._add_to_collection ( function , meta , self . _converters )
4143 return function
44+
45+ def _add_to_collection (self , function , meta , collection ) -> None :
46+ if meta is not None :
47+ meta = meta .copy ()
48+ meta .update (self ._meta )
49+ elif self ._meta :
50+ meta = self ._meta .copy ()
51+
52+ collection .append (CallableWrapper (function , meta = meta ))
0 commit comments