Skip to content

Commit

Permalink
chore(actions): Make Build Run.
Browse files Browse the repository at this point in the history
Fix ruff and mypy errors.
Updated `env` to ensure existence of certain directories on startup.
Added missing deps.
  • Loading branch information
acederberg committed Feb 6, 2025
1 parent 1c400d1 commit bb31225
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ blog/dev/_live_quarto_table_files/
blog/projects/blog/typedoc
blog/projects/blog/quartodoc
blog/dsa/leetcode/calendar_2/_oops_files/
.env.actions
37 changes: 22 additions & 15 deletions acederbergio/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import logging.config
import logging.handlers
import os
import pathlib
import secrets
from os import environ
Expand Down Expand Up @@ -82,7 +83,11 @@ def require(varname: str, default: str | None = None) -> str:


def require_path(
varname: str, default: pathlib.Path | None = None, *, strict: bool = True
varname: str,
default: pathlib.Path | None = None,
*,
strict: bool = True,
ensure_dir: bool = False,
) -> pathlib.Path:
"""Require a setting from environment that is an existing path.
Expand All @@ -96,12 +101,16 @@ def require_path(

var = get(varname)
if var is not None:
return pathlib.Path(var).resolve(strict=strict)

if default is None:
output = pathlib.Path(var)
elif default is None:
raise ValueError(f"Value `{var}` for `{varname}` is falsy.")
else:
output = default

return default.resolve(strict=strict)
if ensure_dir and not os.path.exists(output := output.resolve()):
os.mkdir(output)

return output.resolve(strict=strict)


def create_validator(varname: str, default: str | None = None):
Expand Down Expand Up @@ -160,24 +169,19 @@ def validator(v: Any) -> Any:
WORKDIR = ROOT
PYPROJECT_TOML = ROOT / "pyproject.toml"
CONFIGS = require_path("config_dir", ROOT / "config", strict=False)

BUILD = BLOG / "build"
ICONS = BLOG / "icons"

else:
# NOTE:
WORKDIR = require_path("workdir")
BLOG = require_path("blog", WORKDIR / "blog")
PYPROJECT_TOML = require_path("pyproject_toml", WORKDIR / "pyproject.toml")
CONFIGS = require_path("config_dir", pathlib.Path.home() / "config", strict=False)

BUILD = require_path("build", BLOG / "build")
ICONS = require_path("icons", BLOG / "icons")

BUILD = require_path("build", BLOG / "build", ensure_dir=True)
ICONS = require_path("icons", BLOG / "icons", ensure_dir=True)
VERBOSE = require("verbose", "0") != "0"
TEMPLATES = require_path("templates", SCRIPTS / "templates")
ICONS_SETS = require_path("icon_sets", ICONS / "sets")
BUILD_JSON = require_path("build_json", BLOG / "build.json")
ICONS_SETS = require_path("icon_sets", ICONS / "sets", ensure_dir=True)
BUILD_JSON = require_path("build_json", BLOG / "build.json", strict=False)

LEVEL = require("log_level", "INFO").upper()
LEVEL_FILTERS = require("log_level_filters", LEVEL).upper()
Expand Down Expand Up @@ -239,13 +243,16 @@ def create_logging_config() -> dict[str, Any]:
config_pandoc_filters = {"level": "INFO", "handlers": ["_socket"]}
else:
# NOTE: Add build logs to build in ci mode.
log_file = BUILD / "build.jsonl"
log_file.touch()

handlers.update(
{
"_file": {
"class": "logging.FileHandler",
"level": LEVEL_FILTERS,
"formatter": "json",
"filename": str(BUILD / "build.jsonl"),
"filename": str(log_file),
}
}
)
Expand Down
30 changes: 8 additions & 22 deletions acederbergio/pdf.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import yaml

yaml.safe_load(
"""
some: yaml
it: should
be: highlighted
"""
)

# | Yes! Thank god.
# |
# | some: yaml
# | it: should
# | be: highlighted

import asyncio
import enum
import hashlib
import math
import pathlib
from typing import Annotated, Any, ClassVar, Generator, Optional, Protocol, Self
Expand All @@ -28,7 +13,6 @@
import pypdf as pdf
import rake_nltk as rake
import typer
from gitdb.util import hashlib

from acederbergio import db, env, util

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

df = df.transpose() # .rename(columns=[metric.name for metric in rake.])
df.reset_index(inplace=True)
df.columns = MetricsColumns
df.columns = MetricsColumns # type: ignore[assignment]

return df

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

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

@classmethod
def match_text(cls, text: str) -> dict:
Expand Down Expand Up @@ -253,7 +237,7 @@ def to_df(self) -> pd.DataFrame:

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

root = {}
root: dict[str, Any] = {}

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

phrase = []
phrase: list[str] = []
while k < n:
word = tokenized[k]
word_lower = word.lower()
Expand Down Expand Up @@ -585,7 +569,9 @@ def cli_highlight(
util.CONSOLE.print(res.highlight_html())
return

def highlighter(phrase: str, match: str, *, metrics: Metrics | None = None) -> str:
def highlighter(
phrase: str, match: str, *, metrics: MetricsRow | None = None
) -> str:
print("metrics", metrics)
# if match is None:
# return phrase
Expand Down
5 changes: 2 additions & 3 deletions acederbergio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging.handlers
import queue
from datetime import datetime
from typing import Annotated, Any, Mapping, overload
from typing import Annotated, Any, Mapping

import pandas
import pydantic
Expand Down Expand Up @@ -99,8 +99,7 @@ def print_yaml(
code = MagicEncoder(kwargs_model_dump, indent=2).encode(data)

if not pretty:
print(code)
return
return None

s = rich.syntax.Syntax(
code,
Expand Down
Loading

0 comments on commit bb31225

Please sign in to comment.