Skip to content

Commit 70da309

Browse files
committed
chore: cleanup generated files to be readable
Break this out to use full import paths (eliminating the possibility of collision of names- checks.attrs and reporters.attrs for example). Additionally write this in a way a human can read it, rather than single giant lines. For example, it now writes this: ``` REPORTERS = ( ('CsvReporter', pkgcheck.reporters.CsvReporter), ('FancyReporter', pkgcheck.reporters.FancyReporter), ('FlycheckReporter', pkgcheck.reporters.FlycheckReporter), ('FormatReporter', pkgcheck.reporters.FormatReporter), ('JsonReporter', pkgcheck.reporters.JsonReporter), ('JsonStream', pkgcheck.reporters.JsonStream), ('StrReporter', pkgcheck.reporters.StrReporter), ('XmlReporter', pkgcheck.reporters.XmlReporter), ) ``` rather than this: ``` REPORTERS = (('CsvReporter', reporters.CsvReporter), ('FancyReporter', reporters.FancyReporter), ('FlycheckReporter', reporters.FlycheckReporter), ('FormatReporter', reporters.FormatReporter), ('JsonReporter', reporters.JsonReporter), ('JsonStream', reporters.JsonStream), ('StrReporter', reporters.StrReporter), ('XmlReporter', reporters.XmlReporter)) ``` Finally, use fchmod since it's the correct 'secure' approach, and since it's my muscle memory now. Signed-off-by: Brian Harring <ferringb@gmail.com>
1 parent 1ea8d01 commit 70da309

2 files changed

Lines changed: 22 additions & 36 deletions

File tree

py_build.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import os
12
import sys
2-
from collections import defaultdict
33
from contextlib import contextmanager
44
from pathlib import Path
55
from textwrap import dedent
@@ -30,7 +30,7 @@ def write_const(cleanup_files):
3030
cleanup_files.append(path := Path.cwd() / "src/pkgcheck/_const.py")
3131
print(f"writing path constants to {path}")
3232
with path.open("w") as f:
33-
path.chmod(0o644)
33+
os.fchmod(f.fileno(), 0o644)
3434
f.write(
3535
dedent(
3636
"""\
@@ -49,41 +49,27 @@ def write_objects(cleanup_files):
4949
cleanup_files.append(path := Path.cwd() / "src/pkgcheck/_objects.py")
5050
print(f"writing objects to {path}")
5151

52-
class _kls:
53-
def __init__(self, module):
54-
self.module = module
55-
56-
def __repr__(self):
57-
return self.module
58-
5952
with sys_path():
6053
from pkgcheck import objects
6154

62-
modules = defaultdict(set)
63-
objs = defaultdict(list)
64-
for obj in ("KEYWORDS", "CHECKS", "REPORTERS"):
65-
for name, cls in getattr(objects, obj).items():
66-
parent, module = cls.__module__.rsplit(".", 1)
67-
modules[parent].add(module)
68-
objs[obj].append((name, _kls(f"{module}.{name}")))
55+
targets = ["CHECKS", "KEYWORDS", "REPORTERS"]
56+
with path.open("w") as f:
57+
os.fchmod(f.fileno(), 0o644)
58+
modules = set()
59+
for cls_type in targets:
60+
modules.update(cls.__module__ for cls in getattr(objects, cls_type).values())
61+
for module in sorted(modules):
62+
f.write(f"import {module}\n")
6963

70-
keywords = tuple(objs["KEYWORDS"])
71-
checks = tuple(objs["CHECKS"])
72-
reporters = tuple(objs["REPORTERS"])
64+
for cls_type in targets:
65+
f.write("\n")
7366

74-
with path.open("w") as f:
75-
path.chmod(0o644)
76-
for k, v in sorted(modules.items()):
77-
f.write(f"from {k} import {', '.join(sorted(v))}\n")
78-
f.write(
79-
dedent(
80-
f"""\
81-
KEYWORDS = {keywords}
82-
CHECKS = {checks}
83-
REPORTERS = {reporters}
84-
"""
85-
)
86-
)
67+
registry = getattr(objects, cls_type)
68+
69+
f.write(f"{cls_type} = (\n")
70+
for name, cls in sorted(registry.items(), key=lambda x: x[0]):
71+
f.write(f" ({name!r}, {cls.__module__}.{cls.__name__}),\n")
72+
f.write(")\n")
8773

8874

8975
def write_files(cleanup_files):
@@ -105,7 +91,7 @@ def write_files(cleanup_files):
10591
for obj in ("KEYWORDS", "CHECKS", "REPORTERS"):
10692
print(f"Generating {obj.lower()} list")
10793
cleanup_files.append(path := dst / obj.lower())
108-
path.write_text("\n".join(getattr(objects, obj)) + "\n")
94+
path.write_text("\n".join(sorted(getattr(objects, obj))) + "\n")
10995

11096

11197
@contextmanager

src/pkgcheck/objects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ def __getitem__(self, key):
9999
return self._dict[key]
100100

101101
def keys(self):
102-
return iter(self._dict.keys())
102+
return self._dict.keys()
103103

104104
def values(self):
105-
return iter(self._dict.values())
105+
return self._dict.values()
106106

107107
def items(self):
108-
return iter(self._dict.items())
108+
return self._dict.items()
109109

110110
def select(self, cls):
111111
"""Return mapping of object classes inheriting a given class."""

0 commit comments

Comments
 (0)