Stop an empty env var from shadowing .env, and refuse a total OCR blackout
Two faults found by running the pipeline inside the Docker image for the first time, both of which let a 25-page run finish "successfully" having stored nothing. The image declares ENV DASHSCOPE_API_KEY= so the variable is visible to docker inspect and overridable at docker run. That leaves it set-but-empty in every container, and load_dotenv used os.environ.setdefault, so the empty string won and the mounted .env was ignored. An empty value now counts as unset; a real value still wins, because overriding on the command line is the point. The consequence was worse than a missing key. Every page failed, the per-page fallback kept the font-mangled embedded text -- exactly the text OCR is there to replace -- and the run went on to spend a full detection pass on it. Tolerating one unreadable page is right; tolerating every page is a configuration error wearing a success message, so Document now raises OcrUnavailable when OCR was attempted and not one page came back, quoting the first real failure. A page range that needed no OCR at all is still not an error. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ci5T3gWDhhmqDec3e1XtYZ
This commit is contained in:
+13
-2
@@ -17,7 +17,15 @@ DEFAULT_API_KEY_ENV = "DASHSCOPE_API_KEY"
|
||||
|
||||
|
||||
def load_dotenv(path: Path | str = ".env") -> None:
|
||||
"""Load ``KEY=VALUE`` lines into os.environ without overwriting what is set."""
|
||||
"""Load ``KEY=VALUE`` lines into os.environ without overwriting a real value.
|
||||
|
||||
A variable that is set but *empty* counts as unset. This is not pedantry: the
|
||||
Docker image declares ``ENV DASHSCOPE_API_KEY=`` so the variable is documented
|
||||
and overridable at ``docker run``, which left it present-and-empty in every
|
||||
container. With plain ``setdefault`` that empty string shadowed the .env file,
|
||||
and the only symptom was every OCR page failing with "No API key" -- a whole
|
||||
25-page run spent on text the pipeline had already judged unreadable.
|
||||
"""
|
||||
path = Path(path)
|
||||
if not path.is_file():
|
||||
return
|
||||
@@ -26,7 +34,10 @@ def load_dotenv(path: Path | str = ".env") -> None:
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, _, value = line.partition("=")
|
||||
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
|
||||
key = key.strip()
|
||||
if os.environ.get(key): # a non-empty value wins; empty or absent does not
|
||||
continue
|
||||
os.environ[key] = value.strip().strip('"').strip("'")
|
||||
|
||||
|
||||
def _env(name: str, default: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user