crackerjack 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of crackerjack might be problematic. Click here for more details.
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Les Leslie & Wedgwood Web Works
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: crackerjack
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Crackerjack code formatting style.
|
|
5
|
+
Keywords: black ruff mypy deptry refurb
|
|
6
|
+
Home-page: https://github.com/lesleslie/crackerjack
|
|
7
|
+
Author-Email: lesleslie <les@wedgwoodwebworks.com>
|
|
8
|
+
Maintainer-Email: lesleslie <les@wedgwoodwebworks.com>
|
|
9
|
+
License: BSD-3-Clause
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Project-URL: Homepage, https://github.com/lesleslie/crackerjack
|
|
15
|
+
Project-URL: Documentation, https://github.com/lesleslie/crackerjack
|
|
16
|
+
Project-URL: Repository, https://github.com/lesleslie/crackerjack
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Requires-Dist: black>=23.3.0
|
|
19
|
+
Requires-Dist: ruff>=0.0.261
|
|
20
|
+
Requires-Dist: mypy>=1.2.0
|
|
21
|
+
Requires-Dist: deptry>=0.8.0
|
|
22
|
+
Requires-Dist: refurb>=1.15.0
|
|
23
|
+
Requires-Dist: creosote>=2.6.0
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# Crackerjack Python
|
|
27
|
+
|
|
28
|
+
[](https://docs.python.org/3/)
|
|
29
|
+
[](https://github.com/ambv/black)
|
|
30
|
+
[](https://github.com/lesleslie/crackerjack)
|
|
31
|
+
|
|
32
|
+
Crackerjack is a python coding, formatting, and documentation style for statically
|
|
33
|
+
typed python >=3.10
|
|
34
|
+
|
|
35
|
+
### **Why Crackerjack?**
|
|
36
|
+
|
|
37
|
+
Crackerjack exists to make modern python code more elegant and readable.
|
|
38
|
+
Let's face it, the python documentation system has been a major contributor to the
|
|
39
|
+
languages popularity, but over time python code in packages and repositories has
|
|
40
|
+
become so cluttered up with docstrings, doctests, and comments that it actually
|
|
41
|
+
becomes really hard to find the actual code in it - nonetheless read through it.
|
|
42
|
+
Yes, modern IDE's offer up options to fold the docstrings, but this doesn't help
|
|
43
|
+
when looking through code on GitHub, or in an console based editor like vi and
|
|
44
|
+
doesn't account for either for all the different ways that developers comment
|
|
45
|
+
up their code. There must be some sanity!
|
|
46
|
+
|
|
47
|
+
Enter Crackerjack. Crackerjack works on the theory that with statically typed python
|
|
48
|
+
code and explicit class, function, variable, and other object names the code should be
|
|
49
|
+
straight forward to read and the documentation should pretty much be able to write
|
|
50
|
+
itself (can you say ai). Crackerjack also has coding style
|
|
51
|
+
guidelines that exist to keep the codebase clean, elegant, standardized, and
|
|
52
|
+
easily readable - give it to me straight basically.
|
|
53
|
+
|
|
54
|
+
### **What does this package do?**
|
|
55
|
+
|
|
56
|
+
Crackjack first cleans up the codebase by removing all docstrings and comments that
|
|
57
|
+
do not conform to Crackerjack standards (see below). The code is then reformatted to
|
|
58
|
+
the [Black](https://github.com/ambv/black) code style, unused imports are removed
|
|
59
|
+
with [autoflake](https://github.com/myint/autoflake), and remaining imports sorted
|
|
60
|
+
by [reorder_python_imports](https://github.com/asottile/reorder_python_imports).
|
|
61
|
+
After that
|
|
62
|
+
[MonkeyType](https://monkeytype.readthedocs.io/en/stable/) is run to add type
|
|
63
|
+
annotations to the code and optionally run through
|
|
64
|
+
[Mypy](https://mypy.readthedocs.io/en/latest/) to check for errors.
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
### **What are the rules?**
|
|
68
|
+
|
|
69
|
+
(...more what you'd call "guidelines" than actual rules. -Captain Barbossa )
|
|
70
|
+
|
|
71
|
+
- All docstrings, README's, and other documentation is to be done in Markdown (md)
|
|
72
|
+
|
|
73
|
+
- Code needs to be [black](https://github.com/ambv/black)'ed
|
|
74
|
+
|
|
75
|
+
- Imports are one single import per line
|
|
76
|
+
(see [reorder_python_imports](https://github.com/asottile/reorder_python_imports
|
|
77
|
+
)) - this not only helps aviod merge conflicts but makes it easier to manipulate if
|
|
78
|
+
using hot-keys
|
|
79
|
+
|
|
80
|
+
- Use pathlib.Path not os.path
|
|
81
|
+
|
|
82
|
+
- If a function or class is performing file operations it should be passed a Path
|
|
83
|
+
object - not a string
|
|
84
|
+
|
|
85
|
+
- Work in progress
|
|
86
|
+
|
|
87
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Crackerjack Python
|
|
2
|
+
|
|
3
|
+
[](https://docs.python.org/3/)
|
|
4
|
+
[](https://github.com/ambv/black)
|
|
5
|
+
[](https://github.com/lesleslie/crackerjack)
|
|
6
|
+
|
|
7
|
+
Crackerjack is a python coding, formatting, and documentation style for statically
|
|
8
|
+
typed python >=3.10
|
|
9
|
+
|
|
10
|
+
### **Why Crackerjack?**
|
|
11
|
+
|
|
12
|
+
Crackerjack exists to make modern python code more elegant and readable.
|
|
13
|
+
Let's face it, the python documentation system has been a major contributor to the
|
|
14
|
+
languages popularity, but over time python code in packages and repositories has
|
|
15
|
+
become so cluttered up with docstrings, doctests, and comments that it actually
|
|
16
|
+
becomes really hard to find the actual code in it - nonetheless read through it.
|
|
17
|
+
Yes, modern IDE's offer up options to fold the docstrings, but this doesn't help
|
|
18
|
+
when looking through code on GitHub, or in an console based editor like vi and
|
|
19
|
+
doesn't account for either for all the different ways that developers comment
|
|
20
|
+
up their code. There must be some sanity!
|
|
21
|
+
|
|
22
|
+
Enter Crackerjack. Crackerjack works on the theory that with statically typed python
|
|
23
|
+
code and explicit class, function, variable, and other object names the code should be
|
|
24
|
+
straight forward to read and the documentation should pretty much be able to write
|
|
25
|
+
itself (can you say ai). Crackerjack also has coding style
|
|
26
|
+
guidelines that exist to keep the codebase clean, elegant, standardized, and
|
|
27
|
+
easily readable - give it to me straight basically.
|
|
28
|
+
|
|
29
|
+
### **What does this package do?**
|
|
30
|
+
|
|
31
|
+
Crackjack first cleans up the codebase by removing all docstrings and comments that
|
|
32
|
+
do not conform to Crackerjack standards (see below). The code is then reformatted to
|
|
33
|
+
the [Black](https://github.com/ambv/black) code style, unused imports are removed
|
|
34
|
+
with [autoflake](https://github.com/myint/autoflake), and remaining imports sorted
|
|
35
|
+
by [reorder_python_imports](https://github.com/asottile/reorder_python_imports).
|
|
36
|
+
After that
|
|
37
|
+
[MonkeyType](https://monkeytype.readthedocs.io/en/stable/) is run to add type
|
|
38
|
+
annotations to the code and optionally run through
|
|
39
|
+
[Mypy](https://mypy.readthedocs.io/en/latest/) to check for errors.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### **What are the rules?**
|
|
43
|
+
|
|
44
|
+
(...more what you'd call "guidelines" than actual rules. -Captain Barbossa )
|
|
45
|
+
|
|
46
|
+
- All docstrings, README's, and other documentation is to be done in Markdown (md)
|
|
47
|
+
|
|
48
|
+
- Code needs to be [black](https://github.com/ambv/black)'ed
|
|
49
|
+
|
|
50
|
+
- Imports are one single import per line
|
|
51
|
+
(see [reorder_python_imports](https://github.com/asottile/reorder_python_imports
|
|
52
|
+
)) - this not only helps aviod merge conflicts but makes it easier to manipulate if
|
|
53
|
+
using hot-keys
|
|
54
|
+
|
|
55
|
+
- Use pathlib.Path not os.path
|
|
56
|
+
|
|
57
|
+
- If a function or class is performing file operations it should be passed a Path
|
|
58
|
+
object - not a string
|
|
59
|
+
|
|
60
|
+
- Work in progress
|
|
61
|
+
|
|
62
|
+
|
|
File without changes
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from importlib import import_module
|
|
3
|
+
from importlib.util import module_from_spec
|
|
4
|
+
from importlib.util import spec_from_file_location
|
|
5
|
+
from inspect import cleandoc
|
|
6
|
+
from inspect import currentframe
|
|
7
|
+
from inspect import getdoc
|
|
8
|
+
from inspect import getfile
|
|
9
|
+
from inspect import getmembers
|
|
10
|
+
from inspect import isclass
|
|
11
|
+
from inspect import isfunction
|
|
12
|
+
from os import getcwd
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from shutil import copy2
|
|
15
|
+
from subprocess import call
|
|
16
|
+
|
|
17
|
+
from black import InvalidInput
|
|
18
|
+
from black import format_str
|
|
19
|
+
from click import command
|
|
20
|
+
from click import help_option
|
|
21
|
+
from click import option
|
|
22
|
+
|
|
23
|
+
tab_indent = 2
|
|
24
|
+
line_spacing = 2
|
|
25
|
+
pager_delay = 0.042
|
|
26
|
+
|
|
27
|
+
pd = pager_delay
|
|
28
|
+
ti = "\t" * tab_indent
|
|
29
|
+
ls = "\n" * line_spacing
|
|
30
|
+
|
|
31
|
+
our_path = getfile(currentframe())
|
|
32
|
+
our_parent = Path(our_path).resolve().parent
|
|
33
|
+
our_imports = get_all_imports(our_parent)
|
|
34
|
+
|
|
35
|
+
docs = []
|
|
36
|
+
comments = []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def process_doc(doc):
|
|
40
|
+
for l in (l.strip() for l in doc.splitlines() if not l.strip() in docs):
|
|
41
|
+
docs.append(l)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def process_obj(obj):
|
|
45
|
+
doc = getdoc(obj[1])
|
|
46
|
+
if doc:
|
|
47
|
+
print(f"Removing doc string from: {obj[0]}")
|
|
48
|
+
process_doc(doc)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_doc(obj):
|
|
52
|
+
process_obj(obj)
|
|
53
|
+
functions = getmembers(obj[1], isfunction)
|
|
54
|
+
for obj2 in functions:
|
|
55
|
+
process_obj(obj2)
|
|
56
|
+
objs = getmembers(obj2[1], isfunction)
|
|
57
|
+
for obj3 in objs:
|
|
58
|
+
process_obj(obj3)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass
|
|
62
|
+
class FormatResponse:
|
|
63
|
+
input: str = None
|
|
64
|
+
ouput: str = None
|
|
65
|
+
success: bool = False
|
|
66
|
+
error: str = None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def process_text(text):
|
|
70
|
+
resp = FormatResponse()
|
|
71
|
+
try:
|
|
72
|
+
text = cleandoc(text)
|
|
73
|
+
resp.output = format_str(text, line_length=88)
|
|
74
|
+
resp.success = True
|
|
75
|
+
except InvalidInput as err:
|
|
76
|
+
resp.error = err
|
|
77
|
+
print(f"!!! InvalidInput - {err}")
|
|
78
|
+
# except TokenError as err:
|
|
79
|
+
# resp.error = err
|
|
80
|
+
# print(f"!!! TokenError - {err}")
|
|
81
|
+
finally:
|
|
82
|
+
resp.output = cleandoc(text)
|
|
83
|
+
return resp
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def install(package):
|
|
87
|
+
try:
|
|
88
|
+
import_module(package)
|
|
89
|
+
except ImportError:
|
|
90
|
+
print("Installing: ", package)
|
|
91
|
+
call(["pip", "install", package, "--disable-pip-version-check"])
|
|
92
|
+
finally:
|
|
93
|
+
globals()[package] = import_module(package)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def uninstall(package):
|
|
97
|
+
if (package == "pip") or (package in our_imports):
|
|
98
|
+
return False
|
|
99
|
+
print("Uninstalling: ", package)
|
|
100
|
+
call(["pip", "uninstall", package, "-y"])
|
|
101
|
+
return True
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def crackerjack_it(
|
|
105
|
+
fp: Path,
|
|
106
|
+
exclude: bool = False,
|
|
107
|
+
interactive: bool = False,
|
|
108
|
+
dry_run: bool = False,
|
|
109
|
+
verbose: bool = False,
|
|
110
|
+
):
|
|
111
|
+
print("\nCrackerJacking...\n")
|
|
112
|
+
module_imports = get_all_imports(getcwd())
|
|
113
|
+
for m in module_imports:
|
|
114
|
+
install(m)
|
|
115
|
+
text = fp.read_text()
|
|
116
|
+
print("\nPre-processing text.....\n\n")
|
|
117
|
+
text = process_text(text).ouput
|
|
118
|
+
spec = spec_from_file_location(str(fp.resolve()))
|
|
119
|
+
module = module_from_spec(spec)
|
|
120
|
+
spec.loader.exec_module(module)
|
|
121
|
+
functions = getmembers(module, isfunction)
|
|
122
|
+
classes = getmembers(module, isclass)
|
|
123
|
+
for obj in classes + functions:
|
|
124
|
+
get_doc(obj)
|
|
125
|
+
for line in docs:
|
|
126
|
+
text = text.replace(line, "")
|
|
127
|
+
if not exclude:
|
|
128
|
+
print("\nRemoving remaining docstrings, comments, and blank lines.....\n\n")
|
|
129
|
+
lines = list()
|
|
130
|
+
del_lines = False
|
|
131
|
+
for line in text.splitlines(True):
|
|
132
|
+
print(line)
|
|
133
|
+
if len(line) < 2:
|
|
134
|
+
print(1)
|
|
135
|
+
continue
|
|
136
|
+
elif '"""' in line:
|
|
137
|
+
print(2)
|
|
138
|
+
del_lines = not del_lines
|
|
139
|
+
continue
|
|
140
|
+
elif del_lines:
|
|
141
|
+
print(3)
|
|
142
|
+
continue
|
|
143
|
+
elif line.lstrip().startswith("#"):
|
|
144
|
+
print(4)
|
|
145
|
+
continue
|
|
146
|
+
lines.append(line)
|
|
147
|
+
text = "".join(lines)
|
|
148
|
+
print("\nPost-processing text.....\n\n")
|
|
149
|
+
resp = process_text(text)
|
|
150
|
+
if resp.success:
|
|
151
|
+
print(resp.output)
|
|
152
|
+
print("\n\n\t*** Success! ***\n\n")
|
|
153
|
+
if not dry_run:
|
|
154
|
+
copy2(fp.name, (fp / ".old").name)
|
|
155
|
+
fp.write_text(resp.output)
|
|
156
|
+
elif resp.error:
|
|
157
|
+
print(f"Error formatting: {resp.error}\n\n")
|
|
158
|
+
for m in module_imports:
|
|
159
|
+
uninstall(m)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@command()
|
|
163
|
+
@help_option("-h", is_flag=True, help="help")
|
|
164
|
+
@option("-x", is_flag=True, help="exclude comments")
|
|
165
|
+
@option("-i", is_flag=True, help="interactive")
|
|
166
|
+
@option("-d", is_flag=True, help="dry run")
|
|
167
|
+
@option("-v", is_flag=True, help="verbose")
|
|
168
|
+
@option("-f", help="crackerjack format: -f [module]")
|
|
169
|
+
def crackerjack(f, x, i, d, v):
|
|
170
|
+
options = dict()
|
|
171
|
+
if x:
|
|
172
|
+
options["exclude"] = x
|
|
173
|
+
if i:
|
|
174
|
+
print("-i not currently implemented.")
|
|
175
|
+
options["interactive"] = i
|
|
176
|
+
if d:
|
|
177
|
+
options["dry_run"] = d
|
|
178
|
+
if v:
|
|
179
|
+
print("-v not currently implemented.")
|
|
180
|
+
options["verbose"] = v
|
|
181
|
+
if f:
|
|
182
|
+
cwd = Path(Path.cwd())
|
|
183
|
+
crackerjack_it(cwd / f, **options)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
[tool.pdm.dev-dependencies]
|
|
2
|
+
dev = [
|
|
3
|
+
"pytest>=7.3.0",
|
|
4
|
+
]
|
|
5
|
+
|
|
6
|
+
[tool.ruff]
|
|
7
|
+
line-length = 88
|
|
8
|
+
|
|
9
|
+
[tool.ruff.isort]
|
|
10
|
+
profile = "black"
|
|
11
|
+
line-length = 88
|
|
12
|
+
include-trailing-comma = true
|
|
13
|
+
use-parentheses = true
|
|
14
|
+
multi-line-output = 3
|
|
15
|
+
|
|
16
|
+
[tool.black]
|
|
17
|
+
target-version = [
|
|
18
|
+
"py311",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[tool.mypy]
|
|
22
|
+
strict = true
|
|
23
|
+
|
|
24
|
+
[tool.deptry]
|
|
25
|
+
|
|
26
|
+
[tool.refurb]
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = [
|
|
30
|
+
"pdm-backend",
|
|
31
|
+
]
|
|
32
|
+
build-backend = "pdm.backend"
|
|
33
|
+
|
|
34
|
+
[project]
|
|
35
|
+
name = "Crackerjack"
|
|
36
|
+
version = "0.1.0"
|
|
37
|
+
description = "Crackerjack code formatting style."
|
|
38
|
+
authors = [
|
|
39
|
+
{ name = "lesleslie", email = "les@wedgwoodwebworks.com" },
|
|
40
|
+
]
|
|
41
|
+
maintainers = [
|
|
42
|
+
{ name = "lesleslie", email = "les@wedgwoodwebworks.com" },
|
|
43
|
+
]
|
|
44
|
+
requires-python = ">=3.11"
|
|
45
|
+
readme = "README.md"
|
|
46
|
+
keywords = [
|
|
47
|
+
"black",
|
|
48
|
+
"ruff",
|
|
49
|
+
"mypy",
|
|
50
|
+
"deptry",
|
|
51
|
+
"refurb",
|
|
52
|
+
]
|
|
53
|
+
classifiers = [
|
|
54
|
+
"Environment :: Console",
|
|
55
|
+
"Operating System :: OS Independent",
|
|
56
|
+
"Programming Language :: Python",
|
|
57
|
+
"Programming Language :: Python :: 3.11",
|
|
58
|
+
]
|
|
59
|
+
dependencies = [
|
|
60
|
+
"black>=23.3.0",
|
|
61
|
+
"ruff>=0.0.261",
|
|
62
|
+
"mypy>=1.2.0",
|
|
63
|
+
"deptry>=0.8.0",
|
|
64
|
+
"refurb>=1.15.0",
|
|
65
|
+
"creosote>=2.6.0",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[project.license]
|
|
69
|
+
text = "BSD-3-Clause"
|
|
70
|
+
|
|
71
|
+
[project.optional-dependencies]
|
|
72
|
+
|
|
73
|
+
[project.urls]
|
|
74
|
+
homepage = "https://github.com/lesleslie/crackerjack"
|
|
75
|
+
documentation = "https://github.com/lesleslie/crackerjack"
|
|
76
|
+
repository = "https://github.com/lesleslie/crackerjack"
|
|
77
|
+
|
|
78
|
+
[project.scripts]
|
|
79
|
+
|
|
80
|
+
[project.entry-points]
|