spcd/spcd/util.py

61 lines
1.5 KiB
Python
Raw Normal View History

2024-08-19 15:20:41 +00:00
"""Basic utilities."""
2024-07-12 23:46:18 +00:00
import os
from pathlib import Path
2024-07-14 18:04:59 +00:00
from urllib.parse import urlparse, urlunparse
2024-07-12 23:46:18 +00:00
import env
from rwx import fs
from rwx.log import stream as log
2024-07-14 18:00:30 +00:00
def add_url_path(url: str, extra_path: str) -> str:
2024-08-19 15:18:41 +00:00
"""Append an extra segment to an existing URL."""
2024-07-14 18:00:30 +00:00
parts = urlparse(url)
2024-07-14 20:08:51 +00:00
parts = parts._replace(path=str(Path(parts.path) / extra_path))
2024-07-14 18:00:30 +00:00
return urlunparse(parts)
2024-07-12 23:46:18 +00:00
def browse(root: str) -> None:
2024-08-19 15:16:34 +00:00
"""Frame the browsing of a root directory in the log output."""
2024-07-12 23:46:18 +00:00
paths = []
for directory, _, files in os.walk(root):
for file in files:
absolute_path = Path(directory) / file
relative_path = os.path.relpath(absolute_path, start=root)
paths.append(relative_path)
frame(root)
for path in sorted(paths):
log.info(path)
shut(root)
def cat(file: str) -> None:
2024-08-19 15:16:34 +00:00
"""Frame the content of a file in the log output."""
2024-07-12 23:46:18 +00:00
frame(file)
log.info(fs.read_file_text(file).rstrip())
shut(file)
def frame(text: str) -> None:
2024-08-19 15:13:04 +00:00
"""Open a new frame in the log output."""
2024-07-12 23:46:18 +00:00
log.info(f"{env.SPCD_OPEN}{text}")
def shut(text: str) -> None:
2024-08-19 15:13:04 +00:00
"""Close current frame in the log output."""
2024-07-12 23:46:18 +00:00
log.info(f"{env.SPCD_SHUT}{text}")
def split() -> None:
2024-08-19 14:34:47 +00:00
"""Separate previous log outputs from the ones following."""
2024-07-12 23:46:18 +00:00
log.info(env.SPCD_SPLT)
def step(text: str) -> None:
2024-08-19 14:33:07 +00:00
"""Increment the step number of the current build process."""
2024-07-12 23:46:18 +00:00
env.SPCD_STEP += 1
log.info(env.SPCD_DOWN)
log.info(f"{env.SPCD_VERT} {env.SPCD_STEP} {text}")
log.info(env.SPCD___UP)