Files
hermes-agent/utils/__init__.py
alt-glitch a1bbd329ea refactor: split utils.py into the utils package
utils.py held three subjects in one 871-line file. It is now a package with
one module per subject: utils/files.py (atomic writes, temp files, file mode
and owner, signatures and digests, JSON and YAML file helpers), utils/urls.py
(the base_url helpers) and utils/env.py (truthy values, env numbers, proxy
env helpers). utils/__init__.py re-exports every public name, so no importer
changes. Function bodies are unchanged, no function-level import moved, and
the logger keeps the name "utils".

Tests patch a name in the module that now defines it. Patches stay on the
package where production reads the name through a function-level
"from utils import". The tests of utils itself move to tests/utils/.

A root module that becomes a folder drops out of anything that copies root
*.py files plus a fixed list of packages, so the fixtures that build a partial
source tree now list utils, as do pyproject's packages.find, three check
scripts and the CI change classifier.
2026-11-16 17:16:20 +05:30

102 lines
2.6 KiB
Python

"""Small helpers that several top-level packages share and that know nothing about Hermes as a product.
- ``utils.files``: files on disk: atomic writes and replaces, temp files, directory sync,
cleanup, file mode and owner, file signatures for caches, JSON and YAML file helpers.
- ``utils.urls``: base URL hostname, path and origin, and domain matching.
- ``utils.env``: truthy strings, numeric env vars, and proxy env var normalization.
Callers import from ``utils``; tests patch a name in the module that defines it.
"""
from utils.env import (
env_float,
env_int,
env_var_enabled,
is_truthy_value,
normalize_proxy_env_vars,
normalize_proxy_url,
sanitize_no_proxy_entries,
TRUTHY_STRINGS,
)
from utils.files import (
atomic_json_write,
atomic_replace,
atomic_roundtrip_yaml_save,
atomic_roundtrip_yaml_update,
atomic_write_bytes,
atomic_write_text,
atomic_yaml_write,
content_digest,
default_new_file_mode,
fast_safe_load,
file_cache_version,
file_signature,
fsync_directory,
load_yaml_file_readonly,
logger,
mkstemp_beside,
mtime_is_racy,
preserve_file_mode,
preserve_file_owner,
racy_content_digest,
RACY_MTIME_WINDOW_NS,
read_json_or_empty,
restore_file_mode,
restore_file_owner,
rmtree_readonly,
safe_json_loads,
unlink_files_older_than,
warn_if_credential_file_broadly_readable,
)
from utils.urls import (
base_url_host_matches,
base_url_hostname,
base_url_origin,
base_url_path,
same_base_url_origin,
)
__all__ = [
"RACY_MTIME_WINDOW_NS",
"TRUTHY_STRINGS",
"atomic_json_write",
"atomic_replace",
"atomic_roundtrip_yaml_save",
"atomic_roundtrip_yaml_update",
"atomic_write_bytes",
"atomic_write_text",
"atomic_yaml_write",
"base_url_host_matches",
"base_url_hostname",
"base_url_origin",
"base_url_path",
"content_digest",
"default_new_file_mode",
"env_float",
"env_int",
"env_var_enabled",
"fast_safe_load",
"file_cache_version",
"file_signature",
"fsync_directory",
"is_truthy_value",
"load_yaml_file_readonly",
"logger",
"mkstemp_beside",
"mtime_is_racy",
"normalize_proxy_env_vars",
"normalize_proxy_url",
"preserve_file_mode",
"preserve_file_owner",
"racy_content_digest",
"read_json_or_empty",
"restore_file_mode",
"restore_file_owner",
"rmtree_readonly",
"safe_json_loads",
"same_base_url_origin",
"sanitize_no_proxy_entries",
"unlink_files_older_than",
"warn_if_credential_file_broadly_readable",
]