Skip to content

Commit 1c643db

Browse files
committed
Reshuffle DEVPACK code again
1 parent 63701c0 commit 1c643db

5 files changed

Lines changed: 93 additions & 39 deletions

File tree

devpack/batch_tools/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "heider"
5+
__doc__ = r"""
6+
7+
Created on 8/30/22
8+
"""
9+
10+
__all__ = []

devpack/batch_tools/inits.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "heider"
5+
__doc__ = r"""
6+
7+
Created on 8/30/22
8+
"""
9+
10+
__all__ = []
11+
12+
from pathlib import Path
13+
from typing import Callable, Iterable, Optional
14+
15+
from warg.os.filtering import negate, is_python_package
16+
17+
18+
def recursive_remove_inits(
19+
path: Path,
20+
exclusion_filter: Optional[Iterable[Callable]] = (negate(is_python_package),),
21+
*,
22+
init_name: str = "__init__.py",
23+
verbose: bool = True,
24+
):
25+
path = Path(path)
26+
27+
init_file = path / init_name
28+
if init_file.exists():
29+
init_file.unlink()
30+
31+
for child in path.iterdir():
32+
if child.is_dir():
33+
if exclusion_filter is None or not any(
34+
flt(child) for flt in exclusion_filter
35+
):
36+
recursive_remove_inits(
37+
child,
38+
exclusion_filter,
39+
init_name=init_name,
40+
verbose=verbose,
41+
)
42+
else:
43+
if verbose:
44+
print(
45+
f"{child} was excluded, filters:\n{[(flt.__name__, flt(child)) for flt in exclusion_filter]}"
46+
)
47+
elif child.is_file():
48+
pass
49+
50+
51+
if __name__ == "__main__":
52+
recursive_remove_inits("../exclude/samples")
Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
__all__ = ["auto_add_readme", "recursive_add_readmes", "TouchModeEnum"]
1111

1212
from enum import Enum
13-
from functools import wraps
1413
from pathlib import Path
15-
from typing import Callable, Iterable, Optional, MutableMapping, Sequence
14+
from typing import Callable, Iterable, Optional
1615

1716
from sorcery import assigned_names
17+
from warg.os.filtering import negate, is_python_package
1818

1919

2020
class TouchModeEnum(Enum):
@@ -68,41 +68,6 @@ def auto_add_readme(
6868
print(f"{readme_name} already exists at {path}")
6969

7070

71-
def is_python_module(path: Path) -> bool:
72-
"""
73-
Check if path is a python module
74-
"""
75-
return path.is_file() and path.suffix == ".py"
76-
77-
78-
def is_python_package(path: Path) -> bool:
79-
"""
80-
Check if path is a python package
81-
"""
82-
return path.is_dir() and (path / "__init__.py").exists()
83-
84-
85-
def negate(f: Callable) -> Callable:
86-
"""
87-
Negate a function return
88-
"""
89-
90-
@wraps(f)
91-
def wrapper(*args: Sequence, **kwargs: MutableMapping):
92-
"""
93-
94-
:param args:
95-
:type args:
96-
:param kwargs:
97-
:type kwargs:
98-
:return:
99-
:rtype:
100-
"""
101-
return not f(*args, **kwargs)
102-
103-
return wrapper
104-
105-
10671
def recursive_add_readmes(
10772
path: Path,
10873
exclusion_filter: Optional[Iterable[Callable]] = (negate(is_python_package),),
@@ -151,4 +116,4 @@ def recursive_add_readmes(
151116

152117

153118
if __name__ == "__main__":
154-
recursive_add_readmes("exclude")
119+
recursive_add_readmes("../exclude")

devpack/entry_points/batch.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import argparse
1313
from pathlib import Path
1414

15-
from devpack.batch_tools import TouchModeEnum, recursive_add_readmes
15+
from devpack.batch_tools.readmes import TouchModeEnum, recursive_add_readmes
16+
from devpack.batch_tools.inits import recursive_remove_inits
1617

1718

1819
def recursively_add_readmes_from_here():
@@ -46,5 +47,30 @@ def recursively_add_readmes_from_here():
4647
)
4748

4849

50+
def recursively_remove_inits_from_here():
51+
"""
52+
Add readmes to all python modules in the current directory
53+
"""
54+
parser = argparse.ArgumentParser(description="DevPack remove inits from here")
55+
parser.add_argument(
56+
"--path", "-p", type=Path, default=Path.cwd(), help="Path to remove inits from"
57+
)
58+
59+
parser.add_argument(
60+
"--init-name", "-r", type=str, default="__init__.py", help="init name"
61+
)
62+
parser.add_argument(
63+
"--verbose", action="store_true", help="Verbose output of removed files"
64+
)
65+
args = parser.parse_args()
66+
67+
recursive_remove_inits(
68+
args.path,
69+
init_name=args.init_name,
70+
verbose=args.verbose,
71+
)
72+
73+
4974
if __name__ == "__main__":
5075
recursively_add_readmes_from_here()
76+
recursively_remove_inits_from_here()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def entry_points(self) -> dict:
188188
"devpack_install = devpack.entry_points.cli:install_develop",
189189
"devpack_uninstall = devpack.entry_points.cli:uninstall",
190190
"devpack_add_readmes = devpack.entry_points.batch:recursively_add_readmes_from_here",
191+
"devpack_remove_inits = devpack.entry_points.batch:recursively_remove_inits_from_here",
191192
]
192193
}
193194

0 commit comments

Comments
 (0)