1414from typing import Callable , Iterable , Optional
1515
1616from sorcery import assigned_names
17+ from warg .os .filtering import negate , is_python_package
1718
1819
1920class TouchModeEnum (Enum ):
@@ -32,10 +33,13 @@ def auto_add_readme(
3233 readme_name : str = "README.md" ,
3334 root_path : Optional [Path ] = Path .cwd (),
3435 prefix = "# " ,
36+ verbose : bool = True ,
3537) -> None :
3638 """
3739 Add a readme to the root_path if it does not exist
3840
41+ :param verbose:
42+ :type verbose:
3943 :param path:
4044 :param touch_mode:
4145 :param readme_name:
@@ -48,6 +52,8 @@ def auto_add_readme(
4852 readme_file = path / readme_name
4953 if not readme_file .exists ():
5054 readme_file .touch ()
55+ if verbose :
56+ print (f"Created { readme_file } " )
5157 if touch_mode == TouchModeEnum .breadcrumb :
5258 assert root_path is not None
5359 readme_file .write_text (prefix + str (path .relative_to (root_path )))
@@ -57,27 +63,9 @@ def auto_add_readme(
5763 readme_file .write_text (prefix + str (readme_file .parent .name ))
5864 else :
5965 raise ValueError (f"Unknown touch mode { touch_mode } " )
60-
61-
62- def is_python_module (path : Path ) -> bool :
63- """
64- Check if path is a python module
65- """
66- return path .is_file () and path .suffix == ".py"
67-
68-
69- def is_python_package (path : Path ) -> bool :
70- """
71- Check if path is a python package
72- """
73- return path .is_dir () and (path / "__init__.py" ).exists ()
74-
75-
76- def negate (f : Callable ) -> Callable :
77- """
78- Negate a function
79- """
80- return lambda * args , ** kwargs : not f (* args , ** kwargs )
66+ else :
67+ if verbose :
68+ print (f"{ readme_name } already exists at { path } " )
8169
8270
8371def recursive_add_readmes (
@@ -87,6 +75,7 @@ def recursive_add_readmes(
8775 touch_mode : TouchModeEnum = TouchModeEnum .breadcrumb ,
8876 readme_name : str = "README.md" ,
8977 root_path : Optional [Path ] = None ,
78+ verbose : bool = True ,
9079) -> None :
9180 """
9281 recursively add readmes to all children spanning from root_path
@@ -97,22 +86,34 @@ def recursive_add_readmes(
9786 root_path = path .parent
9887
9988 auto_add_readme (
100- path , touch_mode = touch_mode , readme_name = readme_name , root_path = root_path
89+ path ,
90+ touch_mode = touch_mode ,
91+ readme_name = readme_name ,
92+ root_path = root_path ,
93+ verbose = verbose ,
10194 )
10295
10396 for child in path .iterdir ():
104- if exclusion_filter is None or not any (flt (child ) for flt in exclusion_filter ):
105- if child .is_dir ():
97+ if child .is_dir ():
98+ if exclusion_filter is None or not any (
99+ flt (child ) for flt in exclusion_filter
100+ ):
106101 recursive_add_readmes (
107102 child ,
108103 exclusion_filter ,
109104 touch_mode = touch_mode ,
110105 readme_name = readme_name ,
111106 root_path = root_path ,
107+ verbose = verbose ,
112108 )
113- elif child .is_file ():
114- pass
109+ else :
110+ if verbose :
111+ print (
112+ f"{ child } was excluded, filters:\n { [(flt .__name__ , flt (child )) for flt in exclusion_filter ]} "
113+ )
114+ elif child .is_file ():
115+ pass
115116
116117
117118if __name__ == "__main__" :
118- recursive_add_readmes ("exclude" )
119+ recursive_add_readmes ("../ exclude" )
0 commit comments