Skip to content

Commit 31a8b99

Browse files
committed
fixup! chore: enable more linting rules
1 parent e6dcc34 commit 31a8b99

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ lines-after-imports = 2
130130
convention = "google"
131131

132132
[tool.ruff.lint.per-file-ignores]
133+
"__main__.py" = ["T20"]
133134
"tests/**" = ["ANN", "D", "SLF"]

src/git_draft/__main__.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .common import PROGRAM, Config, UnreachableError, ensure_state_home
1414
from .drafter import Drafter
1515
from .editor import open_editor
16-
from .prompt import Template, TemplatedPrompt, templates_table
16+
from .prompt import Template, TemplatedPrompt, find_template, templates_table
1717
from .store import Store
1818
from .toolbox import ToolVisitor
1919

@@ -47,6 +47,7 @@ def callback(
4747
_value: object,
4848
parser: optparse.OptionParser,
4949
) -> None:
50+
assert parser.values
5051
parser.values.command = name
5152

5253
parser.add_option(
@@ -133,20 +134,20 @@ class ToolPrinter(ToolVisitor):
133134
def on_list_files(
134135
self, _paths: Sequence[PurePosixPath], _reason: str | None
135136
) -> None:
136-
pass
137+
print("Listing available files...")
137138

138139
def on_read_file(
139140
self, path: PurePosixPath, _contents: str | None, _reason: str | None
140141
) -> None:
141-
pass
142+
print(f"Reading {path!r}...")
142143

143144
def on_write_file(
144145
self, path: PurePosixPath, _contents: str, _reason: str | None
145146
) -> None:
146-
pass
147+
print(f"Updated {path!r}.")
147148

148149
def on_delete_file(self, path: PurePosixPath, _reason: str | None) -> None:
149-
pass
150+
print(f"Deleted {path!r}.")
150151

151152

152153
def edit(*, path: Path | None = None, text: str | None = None) -> str:
@@ -158,23 +159,26 @@ def edit(*, path: Path | None = None, text: str | None = None) -> str:
158159
# https://unix.stackexchange.com/q/604260
159160
elif path is None:
160161
assert text, "Empty path and text"
162+
print(text)
161163
sys.exit(198)
162164
else:
163165
if text is not None:
164166
with open(path, "w") as f:
165167
f.write(text)
168+
print(path)
166169
sys.exit(199)
167170

168171

169172
_PROMPT_PLACEHOLDER = "Enter your prompt here..."
170173

171174

172-
def main() -> None: # noqa: PLR0912
175+
def main() -> None: # noqa: PLR0912 PLR0915
173176
config = Config.load()
174177
(opts, args) = new_parser().parse_args()
175178

176179
log_path = ensure_state_home() / "log"
177180
if opts.log:
181+
print(log_path)
178182
return
179183
logging.basicConfig(level=config.log_level, filename=str(log_path))
180184

@@ -212,36 +216,43 @@ def main() -> None: # noqa: PLR0912
212216
reset=config.auto_reset if opts.reset is None else opts.reset,
213217
sync=opts.sync,
214218
)
219+
print(f"Refined {name}.")
215220
elif command == "finalize":
216221
name = drafter.exit_draft(
217222
revert=opts.revert, clean=opts.clean, delete=opts.delete
218223
)
224+
verb = "Reverted" if opts.revert else "Finalized"
225+
print(f"{verb} {name}.")
219226
elif command == "show-drafts":
220227
table = drafter.history_table(args[0] if args else None)
221228
if table:
222-
pass
229+
print(table.to_json() if opts.json else table)
223230
elif command == "show-prompts":
224231
raise NotImplementedError() # TODO: Implement
225232
elif command == "show-templates":
226233
if args:
227234
name = args[0]
228-
tpl = Template.find(name)
235+
tpl = find_template(name)
229236
if opts.edit:
230237
if tpl:
231238
edit(path=tpl.local_path(), text=tpl.source)
232239
else:
233240
edit(path=Template.local_path_for(name))
234-
elif not tpl:
235-
raise ValueError(f"No template named {name!r}")
241+
else:
242+
if not tpl:
243+
raise ValueError(f"No template named {name!r}")
244+
print(tpl.source)
236245
else:
237246
table = templates_table()
247+
print(table.to_json() if opts.json else table)
238248
else:
239249
raise UnreachableError()
240250

241251

242252
if __name__ == "__main__":
243253
try:
244254
main()
245-
except Exception:
255+
except Exception as err:
246256
_logger.exception("Program failed.")
257+
print(f"Error: {err}", file=sys.stderr)
247258
sys.exit(1)

src/git_draft/prompt.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ def _load_template(rel_path: str, env: jinja2.Environment) -> Template:
104104
return Template(Path(rel_path), Path(abs_path), source, preamble)
105105

106106

107+
def find_template(name: str) -> Template | None:
108+
env = _jinja_environment()
109+
try:
110+
return _load_template(f"{name}.{_extension}", env)
111+
except jinja2.TemplateNotFound:
112+
return None
113+
114+
107115
@dataclasses.dataclass(frozen=True)
108116
class Template:
109117
"""An available template"""
@@ -135,14 +143,6 @@ def extract_variables(self, env: jinja2.Environment) -> frozenset[str]:
135143
ast = env.parse(self.source)
136144
return frozenset(jinja2.meta.find_undeclared_variables(ast))
137145

138-
@classmethod
139-
def find(cls, name: str) -> Self | None:
140-
env = _jinja_environment()
141-
try:
142-
return cls._load(f"{name}.{_extension}", env)
143-
except jinja2.TemplateNotFound:
144-
return None
145-
146146
@staticmethod
147147
def local_path_for(name: str) -> Path:
148148
return _PromptFolder.LOCAL.path / Path(f"{name}.{_extension}")

tests/__init__.py

Whitespace-only changes.

tests/git_draft/prompt_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ def test_extract_variables(self):
4747
assert "repo" not in variables
4848

4949
def test_find_ok(self) -> None:
50-
tpl = sut.Template.find("add-test")
50+
tpl = sut.find_template("add-test")
5151
assert tpl
5252
assert "symbol" in tpl.source
5353

5454
def test_find_missing(self) -> None:
55-
assert sut.Template.find("foo") is None
55+
assert sut.find_template("foo") is None
5656

5757

5858
def test_templates_table() -> None:

0 commit comments

Comments
 (0)