Repository navigation
fix: harden error handling, argument validation and setup file handling - #37
Merged
Merged
Conversation
Fixes a set of robustness bugs found while auditing the repo. OCR4Linux.py: - Wrap the Tesseract language query so a missing or misconfigured tesseract produces a clear error instead of an unhandled traceback thrown before main()'s try block is reached. - Reject malformed trailing arguments. Previously any 4- or 5-argument invocation that was not exactly "--langs value" or "--langs=value" fell through silently as "no languages specified", so a typo changed behaviour without warning. - Validate requested languages against the installed ones and fail with an actionable message instead of letting pytesseract raise downstream. - Drop the stale header/docstring references to a preprocess_image() method and to OpenCV/numpy, none of which exist in the file. OCR4Linux.sh: - Enable "set -euo pipefail". - Detect screenshot capture failure, including a cancelled selection that exits 0 without producing a file. Previously the script carried on and pushed a stale or nonexistent output file to the clipboard. - Detect OCR failure, remove any stale output file before extraction, verify the output exists before copying, and guard the deletions with "rm -f". - Fail fast on languages that are not installed instead of only warning. - Reject "-d" and "--lang" without a value, and an empty "--lang" value that previously produced an empty language list. - Buffer the newest cliphist entry instead of piping into head, which exits non-zero under pipefail. - Handle an unset XDG_SESSION_TYPE under "set -u", and never let logging abort the run. setup.sh: - Enable "set -euo pipefail". - Build yay in a temporary directory instead of cloning into the caller's working directory, and clean it up afterwards. - Install only OCR4Linux.sh and OCR4Linux.py into the config directory, resolved from the script's own location, rather than copying the entire working directory. README.md: - Drop the image preprocessing claims to match what the code does. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 14 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
The added error handling pushed Program.main to 7 return statements, one over pylint's limit of 6, failing the Python Lint workflow. - Collapse the two identical except clauses into a single "except (TesseractUnavailableError, ValueError)" since both are handled the same way. - Return check_arguments()'s code directly instead of branching on it, as anything other than 2 is already the final exit code. Behaviour is unchanged; pylint now scores 10.00/10. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the robustness issues found while auditing the repo. No feature changes — every fix turns a silent-wrong-behaviour path into either correct behaviour or a clear error.
What was broken
OCR4Linux.pypytesseract.get_languages()ran outside anytry/exceptinTesseractConfig.__init__, so a missing or misconfigured tesseract raised beforemain()'stryblock was reached — raw traceback, no handling.--langs value/--langs=value. A typo like--langz=engwas silently reinterpreted as "no languages specified" rather than erroring.preprocess_image()method and OpenCV/numpy dependencies that do not exist anywhere in the file.OCR4Linux.shset -euo pipefailand no exit-code checks. If the screenshot was cancelled or OCR failed, the script still ran the clipboard copy against a stale or nonexistent file and then unconditionallyrm'd it.--lang ""setLANG_SPECIFIED=truewith an empty value, yielding an empty language array instead of falling back or erroring.-dand--langwith no value at all ranshift 2off the end of the argument list.setup.shyayinto whatever directory the script was run from, with no collision check and no cleanup.cp -r ./* ~/.config/OCR4Linuxcopied the whole working directory —PKGBUILD,setup.sh, the clonedyay/— into the config directory, and silently did the wrong thing when run from outside the repo root.Validation
shellcheckclean on both scripts,bash -nandpython3 -m py_compilepass.Ran 25 end-to-end scenarios against a real tesseract with stubbed desktop tools (
scrot,rofi,xclip) — all pass:--langs eng,--langs=eng+ara,--lang all, interactive rofi selection,-h,-v,-l,-r--langs, empty--langsvalue, uninstalled language, missing image, too many args, tesseract not on PATH, screenshot tool exiting non-zero, screenshot cancelled while exiting 0,-d/--langwithout a valueConfirmed against
mainthat the three headline bugs were real: the typo'd flag was silently ignored,eng+klingonwas accepted, and a cancelled screenshot exited 0 and still invoked xclip.Also verified
setup.shinstalls only the two runtime files, works when run from an unrelated working directory, leaves no junk behind, and that the installed copy runs standalone from the config directory.Note, not addressed here
PKGBUILDandsetup.shstill listpython-numpyandpython-opencvas dependencies, but the Python script imports onlyPILandpytesseract. Dropping them looks safe but changes a published AUR package's dependency set, so I left that call to you.