Skip to content

Commit bb31225

Browse files
committed
chore(actions): Make Build Run.
Fix ruff and mypy errors. Updated `env` to ensure existence of certain directories on startup. Added missing deps.
1 parent 1c400d1 commit bb31225

7 files changed

Lines changed: 201 additions & 47 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ blog/dev/_live_quarto_table_files/
4545
blog/projects/blog/typedoc
4646
blog/projects/blog/quartodoc
4747
blog/dsa/leetcode/calendar_2/_oops_files/
48+
.env.actions

acederbergio/env.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import logging.config
99
import logging.handlers
10+
import os
1011
import pathlib
1112
import secrets
1213
from os import environ
@@ -82,7 +83,11 @@ def require(varname: str, default: str | None = None) -> str:
8283

8384

8485
def require_path(
85-
varname: str, default: pathlib.Path | None = None, *, strict: bool = True
86+
varname: str,
87+
default: pathlib.Path | None = None,
88+
*,
89+
strict: bool = True,
90+
ensure_dir: bool = False,
8691
) -> pathlib.Path:
8792
"""Require a setting from environment that is an existing path.
8893
@@ -96,12 +101,16 @@ def require_path(
96101

97102
var = get(varname)
98103
if var is not None:
99-
return pathlib.Path(var).resolve(strict=strict)
100-
101-
if default is None:
104+
output = pathlib.Path(var)
105+
elif default is None:
102106
raise ValueError(f"Value `{var}` for `{varname}` is falsy.")
107+
else:
108+
output = default
103109

104-
return default.resolve(strict=strict)
110+
if ensure_dir and not os.path.exists(output := output.resolve()):
111+
os.mkdir(output)
112+
113+
return output.resolve(strict=strict)
105114

106115

107116
def create_validator(varname: str, default: str | None = None):
@@ -160,24 +169,19 @@ def validator(v: Any) -> Any:
160169
WORKDIR = ROOT
161170
PYPROJECT_TOML = ROOT / "pyproject.toml"
162171
CONFIGS = require_path("config_dir", ROOT / "config", strict=False)
163-
164-
BUILD = BLOG / "build"
165-
ICONS = BLOG / "icons"
166-
167172
else:
168173
# NOTE:
169174
WORKDIR = require_path("workdir")
170175
BLOG = require_path("blog", WORKDIR / "blog")
171176
PYPROJECT_TOML = require_path("pyproject_toml", WORKDIR / "pyproject.toml")
172177
CONFIGS = require_path("config_dir", pathlib.Path.home() / "config", strict=False)
173178

174-
BUILD = require_path("build", BLOG / "build")
175-
ICONS = require_path("icons", BLOG / "icons")
176-
179+
BUILD = require_path("build", BLOG / "build", ensure_dir=True)
180+
ICONS = require_path("icons", BLOG / "icons", ensure_dir=True)
177181
VERBOSE = require("verbose", "0") != "0"
178182
TEMPLATES = require_path("templates", SCRIPTS / "templates")
179-
ICONS_SETS = require_path("icon_sets", ICONS / "sets")
180-
BUILD_JSON = require_path("build_json", BLOG / "build.json")
183+
ICONS_SETS = require_path("icon_sets", ICONS / "sets", ensure_dir=True)
184+
BUILD_JSON = require_path("build_json", BLOG / "build.json", strict=False)
181185

182186
LEVEL = require("log_level", "INFO").upper()
183187
LEVEL_FILTERS = require("log_level_filters", LEVEL).upper()
@@ -239,13 +243,16 @@ def create_logging_config() -> dict[str, Any]:
239243
config_pandoc_filters = {"level": "INFO", "handlers": ["_socket"]}
240244
else:
241245
# NOTE: Add build logs to build in ci mode.
246+
log_file = BUILD / "build.jsonl"
247+
log_file.touch()
248+
242249
handlers.update(
243250
{
244251
"_file": {
245252
"class": "logging.FileHandler",
246253
"level": LEVEL_FILTERS,
247254
"formatter": "json",
248-
"filename": str(BUILD / "build.jsonl"),
255+
"filename": str(log_file),
249256
}
250257
}
251258
)

acederbergio/pdf.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
import yaml
2-
3-
yaml.safe_load(
4-
"""
5-
some: yaml
6-
it: should
7-
be: highlighted
8-
"""
9-
)
10-
11-
# | Yes! Thank god.
12-
# |
13-
# | some: yaml
14-
# | it: should
15-
# | be: highlighted
16-
171
import asyncio
182
import enum
3+
import hashlib
194
import math
205
import pathlib
216
from typing import Annotated, Any, ClassVar, Generator, Optional, Protocol, Self
@@ -28,7 +13,6 @@
2813
import pypdf as pdf
2914
import rake_nltk as rake
3015
import typer
31-
from gitdb.util import hashlib
3216

3317
from acederbergio import db, env, util
3418

@@ -172,7 +156,7 @@ def createDF(cls, text: str, **rake_kwargs) -> pd.DataFrame:
172156

173157
df = df.transpose() # .rename(columns=[metric.name for metric in rake.])
174158
df.reset_index(inplace=True)
175-
df.columns = MetricsColumns
159+
df.columns = MetricsColumns # type: ignore[assignment]
176160

177161
return df
178162

@@ -183,7 +167,7 @@ def create(
183167
"""Transform a compatible dataframe into this object."""
184168

185169
metrics = {row[0]: MetricsRow.fromDFRow(row) for _, row in df.iterrows()}
186-
return cls(metrics=metrics, text=text, metadata=metadata) # type: ignore[missing-args]
170+
return cls(metrics=metrics, text=text, metadata=metadata) # type: ignore[call-arg,arg-type]
187171

188172
@classmethod
189173
def match_text(cls, text: str) -> dict:
@@ -253,7 +237,7 @@ def to_df(self) -> pd.DataFrame:
253237

254238
def trie(self) -> dict[str, Any]:
255239

256-
root = {}
240+
root: dict[str, Any] = {}
257241

258242
for phrase, metrics in self.metrics.items():
259243
node = root
@@ -274,7 +258,7 @@ def chunks(self) -> Generator[dict[str, Any], None, None]:
274258
tokenized = nltk.tokenize.word_tokenize(self.text)
275259
k, n = 0, len(tokenized)
276260

277-
phrase = []
261+
phrase: list[str] = []
278262
while k < n:
279263
word = tokenized[k]
280264
word_lower = word.lower()
@@ -585,7 +569,9 @@ def cli_highlight(
585569
util.CONSOLE.print(res.highlight_html())
586570
return
587571

588-
def highlighter(phrase: str, match: str, *, metrics: Metrics | None = None) -> str:
572+
def highlighter(
573+
phrase: str, match: str, *, metrics: MetricsRow | None = None
574+
) -> str:
589575
print("metrics", metrics)
590576
# if match is None:
591577
# return phrase

acederbergio/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging.handlers
44
import queue
55
from datetime import datetime
6-
from typing import Annotated, Any, Mapping, overload
6+
from typing import Annotated, Any, Mapping
77

88
import pandas
99
import pydantic
@@ -99,8 +99,7 @@ def print_yaml(
9999
code = MagicEncoder(kwargs_model_dump, indent=2).encode(data)
100100

101101
if not pretty:
102-
print(code)
103-
return
102+
return None
104103

105104
s = rich.syntax.Syntax(
106105
code,

0 commit comments

Comments
 (0)