Files
tfm_ainventory/venv/lib/python3.12/site-packages/pluggy/_tracing.py
Daniel Bedeleanu ea49cd6e4a feat(phase1): add image storage utilities
- Create backend/services/image_storage.py with 4 core functions:
  - sanitize_filename(): remove unsafe chars, limit to 255 chars, convert to lowercase
  - get_unique_filename(): handle collisions with UUID suffix (format: {name}_{uuid8}_{variant}.jpg)
  - ensure_image_directories(): create /images/ root and category subdirs on startup
  - save_image(): save bytes to /images/{category}/{filename}, returns relative path
- Create comprehensive test suite (22 tests) covering all functionality
- Integrate ensure_image_directories() into FastAPI startup event
- Directory structure: /images/{category}/{filename}
- Collision handling: auto-suffix with UUID if filename exists
- All tests passing, pathlib.Path for safe operations
2026-04-20 21:57:26 +03:00

73 lines
2.0 KiB
Python

"""
Tracing utils
"""
from __future__ import annotations
from collections.abc import Sequence
from typing import Any
from typing import Callable
_Writer = Callable[[str], object]
_Processor = Callable[[tuple[str, ...], tuple[Any, ...]], object]
class TagTracer:
def __init__(self) -> None:
self._tags2proc: dict[tuple[str, ...], _Processor] = {}
self._writer: _Writer | None = None
self.indent = 0
def get(self, name: str) -> TagTracerSub:
return TagTracerSub(self, (name,))
def _format_message(self, tags: Sequence[str], args: Sequence[object]) -> str:
if isinstance(args[-1], dict):
extra = args[-1]
args = args[:-1]
else:
extra = {}
content = " ".join(map(str, args))
indent = " " * self.indent
lines = ["{}{} [{}]\n".format(indent, content, ":".join(tags))]
for name, value in extra.items():
lines.append(f"{indent} {name}: {value}\n")
return "".join(lines)
def _processmessage(self, tags: tuple[str, ...], args: tuple[object, ...]) -> None:
if self._writer is not None and args:
self._writer(self._format_message(tags, args))
try:
processor = self._tags2proc[tags]
except KeyError:
pass
else:
processor(tags, args)
def setwriter(self, writer: _Writer | None) -> None:
self._writer = writer
def setprocessor(self, tags: str | tuple[str, ...], processor: _Processor) -> None:
if isinstance(tags, str):
tags = tuple(tags.split(":"))
else:
assert isinstance(tags, tuple)
self._tags2proc[tags] = processor
class TagTracerSub:
def __init__(self, root: TagTracer, tags: tuple[str, ...]) -> None:
self.root = root
self.tags = tags
def __call__(self, *args: object) -> None:
self.root._processmessage(self.tags, args)
def get(self, name: str) -> TagTracerSub:
return self.__class__(self.root, self.tags + (name,))