Skip to content

Commit 170b63c

Browse files
committed
add more checks
1 parent 692ca4b commit 170b63c

13 files changed

Lines changed: 793 additions & 25 deletions

File tree

.github/workflows/publish-to-test-pypi.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ jobs:
3232
.
3333
- name: Publish distribution 📦 to Test PyPI
3434
if: endsWith(github.ref, 'master')
35-
uses: pypa/gh-action-pypi-publish@master
35+
uses: pypa/gh-action-pypi-publish@release/v1
3636
with:
3737
password: ${{ secrets.test_pypi_password }}
3838
repository_url: https://test.pypi.org/legacy/
3939
- name: Publish distribution 📦 to PyPI
4040
if: startsWith(github.ref, 'refs/tags')
41-
uses: pypa/gh-action-pypi-publish@master
41+
uses: pypa/gh-action-pypi-publish@release/v1
4242
with:
4343
password: ${{ secrets.pypi_password }}
4444
#verbose: true

devpack/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919

2020
from typing import Any
2121

22+
__all__ = [
23+
"PROJECT_APP_PATH",
24+
"PROJECT_NAME",
25+
"PROJECT_VERSION",
26+
"get_version",
27+
"PROJECT_ORGANISATION",
28+
"PROJECT_AUTHOR",
29+
"PROJECT_YEAR",
30+
# "INCLUDE_PROJECT_READMES",
31+
# "PACKAGE_DATA_PATH"
32+
]
33+
2234

2335
def dist_is_editable(dist: Any) -> bool:
2436
"""
@@ -38,6 +50,7 @@ def dist_is_editable(dist: Any) -> bool:
3850
PROJECT_YEAR = 2018
3951
PROJECT_AUTHOR = __author__.lower().strip().replace(" ", "_")
4052
PROJECT_APP_PATH = AppPath(app_name=PROJECT_NAME, app_author=PROJECT_AUTHOR)
53+
PROJECT_ORGANISATION = "pything"
4154

4255
distributions = {v.key: v for v in pkg_resources.working_set}
4356
if PROJECT_NAME in distributions:

devpack/batch_tools/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@
88
"""
99

1010
__all__ = []
11+
12+
# from .alls import *
13+
# from .inits import *
14+
# from .aliases import *
15+
# from .readmes import *
16+
# from .privates import *
17+
# from .docs import *
18+
# from .shebangs import *
19+
# from .entry_points import *
20+
# from .authors import *

devpack/batch_tools/aliases.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "heider"
5+
__doc__ = r"""
6+
7+
TODO: IMPLEMENT
8+
9+
Created on 9/2/22
10+
"""
11+
12+
__all__ = ["recursive_detect_import_aliasing"]
13+
14+
from pathlib import Path
15+
from typing import Iterable, Callable, Optional, Sequence, Mapping, List
16+
17+
from warg.os_utilities.filtering import negate, is_python_module, is_python_package
18+
19+
20+
def has_import_aliases(path: Path, *, verbose: bool = False) -> bool:
21+
"""
22+
23+
:param verbose:
24+
:type verbose:
25+
:param path:
26+
:type path:
27+
:return:
28+
:rtype:
29+
"""
30+
if path.is_file():
31+
with open(path) as f:
32+
for ln, l in enumerate(f.readlines()):
33+
if "import" in l and "as" in l:
34+
if verbose:
35+
print(f"Found alias at line {ln}: {l}")
36+
return True
37+
return False
38+
39+
40+
def recursive_detect_import_aliasing(
41+
path: Path,
42+
file_extensions: Sequence[str] = (".py", ".txt", ".yaml"),
43+
*,
44+
exclusion_filter: Optional[Iterable[Callable]] = (negate(is_python_package),),
45+
verbose: bool = True,
46+
) -> None:
47+
"""we do not like them!"""
48+
path = Path(path)
49+
file_extensions = [f".{f.lstrip('.')}" for f in file_extensions]
50+
51+
for child in path.iterdir():
52+
if child.is_dir():
53+
if exclusion_filter is None or not any(
54+
flt(child) for flt in exclusion_filter
55+
):
56+
recursive_detect_import_aliasing(
57+
child,
58+
file_extensions=file_extensions,
59+
exclusion_filter=exclusion_filter,
60+
verbose=verbose,
61+
)
62+
else:
63+
if verbose:
64+
print(
65+
f"{child} was excluded, filters:\n{[(flt.__name__, flt(child)) for flt in exclusion_filter]}"
66+
)
67+
elif child.is_file():
68+
if child.suffix in file_extensions:
69+
if has_import_aliases(child, verbose=verbose):
70+
print(child)
71+
72+
73+
COMMON_ALIAS_REPLACEMENTS = {
74+
"plt.": "pyplot.",
75+
"import matplotlib.pyplot as plt": "from matplotlib import pyplot",
76+
"np.": "numpy.",
77+
"import numpy as np": "import numpy",
78+
}
79+
80+
81+
def auto_replace_aliases(
82+
path: Path,
83+
replacement_mapping: Mapping = COMMON_ALIAS_REPLACEMENTS,
84+
*,
85+
verbose: bool = False,
86+
) -> List[str]:
87+
"""
88+
replaces plt. with pyplot. and changes import to from matplotlib import pyplot. for numpy numpy. becomes numpy. and so on
89+
"""
90+
path = Path(path)
91+
92+
working_copy = []
93+
94+
if path.is_file():
95+
with open(path) as f:
96+
for ln, line in enumerate(f.readlines()):
97+
line_copy = line
98+
for k, v in COMMON_ALIAS_REPLACEMENTS.items():
99+
if k in line:
100+
if verbose:
101+
print(f"Found {k} at line {ln}: {line}")
102+
line_copy = line_copy.replace(k, v)
103+
if verbose:
104+
print(f"{line} -> {line_copy}")
105+
working_copy.append(line_copy)
106+
return working_copy
107+
108+
109+
if __name__ == "__main__":
110+
recursive_detect_import_aliasing(Path(__file__).parent.parent / "exclude")
111+
112+
import numpy as np # for demonstration
113+
import matplotlib.pyplot as plt
114+
115+
print(plt.__doc__[0])
116+
print(np.__version__)
117+
118+
print(auto_replace_aliases(Path(__file__)))

0 commit comments

Comments
 (0)