@lumeo_hq/openbooks 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +16 -3
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +3 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/install-engine.d.ts +23 -0
- package/dist/install-engine.js +78 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +145 -0
- package/package.json +28 -3
- package/scripts/postinstall.mjs +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# openbooks
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
npm install -g @lumeo_hq/openbooks # also installs the Python engine (pip install openbooks)
|
|
5
|
+
openbooks statement.csv --currency INR --opening 1000.00 # same as: openbooks run statement.csv ...
|
|
6
|
+
openbooks gateways stripe.csv # only gateway layouts; anything else is refused
|
|
7
|
+
openbooks doctor
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Commands: `run`, `statements run`, `gateways run`, `tax run`, `doctor`, `install-engine`, `engine-info`. Flags: `--currency --opening --account
|
|
11
|
+
--allow-derived-opening --date-order dmy|mdy --no-ocr --model --out <dir> --json --python <path> --timeout <ms>`.
|
|
12
|
+
|
|
13
|
+
Exit codes: `0` everything released, `1` something was refused, `2` usage error, `3` the engine could not run.
|
|
14
|
+
Needs the Python engine; this package does not parse documents itself. Installing this package fetches it with `pip install openbooks`
|
|
15
|
+
(Python 3.11+). Skip that with `--ignore-scripts` or `OPENBOOKS_SKIP_PYTHON=1`, and run `openbooks install-engine` (`--scan` for OCR) later.
|
|
16
|
+
If your system Python refuses global installs, create a `.venv` in your project and run `.venv/bin/pip install openbooks`; it is picked up automatically.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface InstallOptions {
|
|
2
|
+
/** Also install OCR and the typed heads (EasyOCR, PyTorch, Pillow). Large. */
|
|
3
|
+
scan?: boolean;
|
|
4
|
+
/** Install into this interpreter instead of searching for one. */
|
|
5
|
+
python?: string;
|
|
6
|
+
/** Reinstall even when a working engine is already present. */
|
|
7
|
+
force?: boolean;
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
log?: (line: string) => void;
|
|
10
|
+
}
|
|
11
|
+
export type InstallResult = {
|
|
12
|
+
status: "present";
|
|
13
|
+
detail: string;
|
|
14
|
+
} | {
|
|
15
|
+
status: "installed";
|
|
16
|
+
detail: string;
|
|
17
|
+
} | {
|
|
18
|
+
status: "failed";
|
|
19
|
+
detail: string;
|
|
20
|
+
hint?: string;
|
|
21
|
+
};
|
|
22
|
+
/** Install the Python engine (the `openbooks` PyPI package: core, statements with PDF, gateways, tax, doctor) so the JS packages can run. Never throws. */
|
|
23
|
+
export declare function installEngine(opts?: InstallOptions): Promise<InstallResult>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { clearEngineCache, EngineError, findEngine, runProcess } from "@lumeo_hq/openbooks-core";
|
|
3
|
+
/** npm and PyPI share a major.minor line (the contract version); patch releases are independent. So 0.1.x of this CLI takes any openbooks 0.1.x. */
|
|
4
|
+
function engineRange() {
|
|
5
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
6
|
+
const [major, minor] = pkg.version.split(".");
|
|
7
|
+
return `~=${major}.${minor}.0`;
|
|
8
|
+
}
|
|
9
|
+
function pythonCandidates(explicit) {
|
|
10
|
+
const given = explicit ?? process.env.OPENBOOKS_PYTHON;
|
|
11
|
+
if (given)
|
|
12
|
+
return [[given]];
|
|
13
|
+
return process.platform === "win32" ? [["py", "-3"], ["python"]] : [["python3"], ["python"]];
|
|
14
|
+
}
|
|
15
|
+
/** The first interpreter that is Python 3.11 or newer, or why none was. */
|
|
16
|
+
async function pickPython(explicit) {
|
|
17
|
+
let sawOld = false;
|
|
18
|
+
for (const python of pythonCandidates(explicit)) {
|
|
19
|
+
try {
|
|
20
|
+
const r = await runProcess([...python, "-c", "import sys; print(int(sys.version_info >= (3, 11)))"], { timeoutMs: 15_000 });
|
|
21
|
+
if (r.code !== 0)
|
|
22
|
+
continue;
|
|
23
|
+
if (r.stdout.trim() === "1")
|
|
24
|
+
return { python };
|
|
25
|
+
sawOld = true;
|
|
26
|
+
}
|
|
27
|
+
catch { /* not found or not runnable: try the next one */ }
|
|
28
|
+
}
|
|
29
|
+
return { problem: sawOld ? "Python was found but it is older than 3.11" : "no Python interpreter was found" };
|
|
30
|
+
}
|
|
31
|
+
/** Install the Python engine (the `openbooks` PyPI package: core, statements with PDF, gateways, tax, doctor) so the JS packages can run. Never throws. */
|
|
32
|
+
export async function installEngine(opts = {}) {
|
|
33
|
+
const log = opts.log ?? (() => { });
|
|
34
|
+
const range = engineRange();
|
|
35
|
+
if (!opts.force && !opts.scan) { // --scan adds an extra to an existing engine, so it always runs pip (which is a no-op when nothing is missing)
|
|
36
|
+
try {
|
|
37
|
+
const e = await findEngine(opts.python ? { python: opts.python } : {});
|
|
38
|
+
return { status: "present", detail: `the Open Books engine ${e.info.version} is already installed` };
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
// A version mismatch is fixed by installing the matching release; anything else means "not there yet".
|
|
42
|
+
if (!(e instanceof EngineError))
|
|
43
|
+
return { status: "failed", detail: e.message };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const picked = await pickPython(opts.python);
|
|
47
|
+
if ("problem" in picked) {
|
|
48
|
+
return { status: "failed", detail: picked.problem, hint: "install Python 3.11 or newer from https://www.python.org/downloads/, then run: openbooks install-engine" };
|
|
49
|
+
}
|
|
50
|
+
const spec = `openbooks${opts.scan ? "[scan]" : ""}${range}`;
|
|
51
|
+
log(`installing the Python engine: pip install ${spec}`);
|
|
52
|
+
let r;
|
|
53
|
+
try {
|
|
54
|
+
r = await runProcess([...picked.python, "-m", "pip", "install", "--disable-pip-version-check", spec], { timeoutMs: opts.timeoutMs ?? 15 * 60_000 });
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return { status: "failed", detail: `pip did not finish: ${e.message}`, hint: `run it yourself: pip install ${spec}` };
|
|
58
|
+
}
|
|
59
|
+
if (r.code !== 0) {
|
|
60
|
+
const tail = r.stderr.trim().split(/\r?\n/).slice(-6).join("\n");
|
|
61
|
+
if (/externally-managed-environment/.test(r.stderr)) {
|
|
62
|
+
return {
|
|
63
|
+
status: "failed",
|
|
64
|
+
detail: "this Python is managed by your operating system and refuses global installs",
|
|
65
|
+
hint: `create a virtual environment and point at it: python3 -m venv .venv && .venv/bin/pip install ${spec} (Open Books uses ./.venv automatically), or set OPENBOOKS_PYTHON`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return { status: "failed", detail: `pip exited with code ${r.code}\n${tail}`, hint: `run it yourself: pip install ${spec}` };
|
|
69
|
+
}
|
|
70
|
+
clearEngineCache();
|
|
71
|
+
try {
|
|
72
|
+
const e = await findEngine({ python: picked.python });
|
|
73
|
+
return { status: "installed", detail: `installed the Open Books engine ${e.info.version}` };
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
return { status: "failed", detail: `pip succeeded but the engine does not load: ${e.message}`, hint: "run: openbooks doctor" };
|
|
77
|
+
}
|
|
78
|
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Exit codes match the Python CLI: 0 all released, 1 something was refused, 2 usage, 3 the engine could not run. */
|
|
2
|
+
export declare const EXIT: {
|
|
3
|
+
readonly ok: 0;
|
|
4
|
+
readonly refused: 1;
|
|
5
|
+
readonly usage: 2;
|
|
6
|
+
readonly engine: 3;
|
|
7
|
+
};
|
|
8
|
+
export interface IO {
|
|
9
|
+
out: (s: string) => void;
|
|
10
|
+
err: (s: string) => void;
|
|
11
|
+
}
|
|
12
|
+
export declare function main(argv: string[], io?: IO): Promise<number>;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { Command, CommanderError } from "commander";
|
|
5
|
+
import { EngineError, findEngine, runFiles } from "@lumeo_hq/openbooks-core";
|
|
6
|
+
import * as gateways from "@lumeo_hq/openbooks-gateways";
|
|
7
|
+
import * as statements from "@lumeo_hq/openbooks-statements";
|
|
8
|
+
import * as tax from "@lumeo_hq/openbooks-tax";
|
|
9
|
+
import { runDoctor } from "@lumeo_hq/openbooks-doctor";
|
|
10
|
+
import { installEngine } from "./install-engine.js";
|
|
11
|
+
/** Exit codes match the Python CLI: 0 all released, 1 something was refused, 2 usage, 3 the engine could not run. */
|
|
12
|
+
export const EXIT = { ok: 0, refused: 1, usage: 2, engine: 3 };
|
|
13
|
+
function addRunOptions(c) {
|
|
14
|
+
return c
|
|
15
|
+
.option("--currency <code>", "currency, if the file does not state one")
|
|
16
|
+
.option("--opening <amount>", "opening balance as a plain decimal such as 1000.00, if the file does not state one")
|
|
17
|
+
.option("--account <number>")
|
|
18
|
+
.option("--allow-derived-opening", "derive the opening balance from the first row (not independent evidence)")
|
|
19
|
+
.option("--date-order <order>", "day/month order of numeric dates: dmy or mdy")
|
|
20
|
+
.option("--no-ocr", "refuse scans instead of running OCR")
|
|
21
|
+
.option("--model", "cross-check PDFs and scans with the typed model heads")
|
|
22
|
+
.option("--out <dir>", "also write each document, named <sha256>.json, into this directory")
|
|
23
|
+
.option("--json", "print the results as JSON instead of one line per file")
|
|
24
|
+
.option("--python <path>", "the Python interpreter that has Open Books installed (default: OPENBOOKS_PYTHON, ./.venv, then the system Python)")
|
|
25
|
+
.option("--timeout <ms>", "stop the engine after this many milliseconds (default 120000)");
|
|
26
|
+
}
|
|
27
|
+
function toOptions(f) {
|
|
28
|
+
if (f.dateOrder && f.dateOrder !== "dmy" && f.dateOrder !== "mdy")
|
|
29
|
+
throw new CommanderError(2, "openbooks.badOption", "--date-order must be dmy or mdy");
|
|
30
|
+
const o = {};
|
|
31
|
+
if (f.currency)
|
|
32
|
+
o.currency = f.currency;
|
|
33
|
+
if (f.opening !== undefined)
|
|
34
|
+
o.opening = f.opening;
|
|
35
|
+
if (f.account)
|
|
36
|
+
o.account = f.account;
|
|
37
|
+
if (f.allowDerivedOpening)
|
|
38
|
+
o.allowDerivedOpening = true;
|
|
39
|
+
if (f.dateOrder)
|
|
40
|
+
o.dateOrder = f.dateOrder;
|
|
41
|
+
if (f.ocr === false)
|
|
42
|
+
o.ocr = false;
|
|
43
|
+
if (f.model)
|
|
44
|
+
o.model = true;
|
|
45
|
+
if (f.python)
|
|
46
|
+
o.python = f.python;
|
|
47
|
+
if (f.timeout !== undefined) {
|
|
48
|
+
const n = Number(f.timeout);
|
|
49
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
50
|
+
throw new CommanderError(2, "openbooks.badOption", "--timeout must be a positive whole number of milliseconds");
|
|
51
|
+
o.runTimeoutMs = n;
|
|
52
|
+
}
|
|
53
|
+
return o;
|
|
54
|
+
}
|
|
55
|
+
async function writeAtomic(path, text) {
|
|
56
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
57
|
+
await writeFile(tmp, text, "utf8");
|
|
58
|
+
await rename(tmp, path);
|
|
59
|
+
}
|
|
60
|
+
async function runAndReport(files, flags, runner, io) {
|
|
61
|
+
const results = await runner(files, toOptions(flags));
|
|
62
|
+
if (flags.out) {
|
|
63
|
+
await mkdir(flags.out, { recursive: true });
|
|
64
|
+
for (const r of results) {
|
|
65
|
+
const sha = r.kind === "released" ? r.document.file.sha256 : r.document.error.file_sha256;
|
|
66
|
+
await writeAtomic(join(flags.out, `${sha}.json`), JSON.stringify(r.document, null, 2) + "\n");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (flags.json) {
|
|
70
|
+
io.out(JSON.stringify(results.map((r) => ({ file: r.file, status: r.kind, ...(r.kind === "refused" ? { code: r.code, message: r.message } : {}), document: r.document })), null, 2) + "\n");
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
for (const r of results) {
|
|
74
|
+
io.out(r.kind === "released" ? `${r.file}: released ${r.document.file.layout_version}\n` : `${r.file}: refused ${r.code}: ${r.message}\n`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return results.every((r) => r.kind === "released") ? EXIT.ok : EXIT.refused;
|
|
78
|
+
}
|
|
79
|
+
export async function main(argv, io = { out: (s) => process.stdout.write(s), err: (s) => process.stderr.write(s) }) {
|
|
80
|
+
let code = EXIT.ok;
|
|
81
|
+
const program = new Command("openbooks")
|
|
82
|
+
.description("Financial documents in, one proven JSON per file out (or a typed refusal). Usage: openbooks <files...> [options]")
|
|
83
|
+
.version(JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version, "-V, --version", "print the version")
|
|
84
|
+
.exitOverride()
|
|
85
|
+
.configureOutput({ writeOut: io.out, writeErr: io.err });
|
|
86
|
+
const set = (n) => { code = n; };
|
|
87
|
+
addRunOptions(program.command("run", { isDefault: true }).description("read any supported file").argument("<files...>"))
|
|
88
|
+
.action(async (files, f) => set(await runAndReport(files, f, runFiles, io)));
|
|
89
|
+
const family = (name, desc, run) => {
|
|
90
|
+
const g = program.command(name).description(desc);
|
|
91
|
+
addRunOptions(g.command("run", { isDefault: true }).description(`read ${name} files only; anything else is refused`).argument("<files...>"))
|
|
92
|
+
.action(async (files, f) => set(await runAndReport(files, f, run, io)));
|
|
93
|
+
};
|
|
94
|
+
family("statements", "bank statements: CSV, OFX, MT940, PDF and scans", statements.runFiles);
|
|
95
|
+
family("gateways", "payment gateways: Stripe, Razorpay, PayPal, Wise", gateways.runFiles);
|
|
96
|
+
family("tax", "tax and ledger documents: 26AS, AIS, FIRA, 1099-B, trial balance", tax.runFiles);
|
|
97
|
+
program.command("doctor").description("check this machine: Node, Python, the engine, providers and model integrity")
|
|
98
|
+
.option("--json", "machine-readable output").option("--strict", "treat warnings as failures").option("--deep", "also import PyTorch and load the model")
|
|
99
|
+
.option("--python <path>", "the Python interpreter to check")
|
|
100
|
+
.action(async (f) => {
|
|
101
|
+
const rep = await runDoctor({ deep: f.deep, strict: f.strict, ...(f.python ? { python: f.python } : {}) });
|
|
102
|
+
if (f.json)
|
|
103
|
+
io.out(JSON.stringify(rep, null, 2) + "\n");
|
|
104
|
+
else {
|
|
105
|
+
for (const c of rep.checks)
|
|
106
|
+
io.out(`[${c.status.padEnd(4)}] ${c.id}: ${c.detail}\n${c.fix && (c.status === "fail" || c.status === "warn") ? ` fix: ${c.fix}\n` : ""}`);
|
|
107
|
+
io.out(`${rep.summary.pass} passed, ${rep.summary.warn} warnings, ${rep.summary.fail} failed, ${rep.summary.skip} skipped -> ${rep.ok ? "OK" : "NOT OK"}\n`);
|
|
108
|
+
}
|
|
109
|
+
set(rep.ok ? EXIT.ok : EXIT.refused);
|
|
110
|
+
});
|
|
111
|
+
program.command("install-engine").description("install the Python engine (pip install openbooks) that these packages run on")
|
|
112
|
+
.option("--scan", "also install OCR and the typed heads (EasyOCR, PyTorch: a large download)")
|
|
113
|
+
.option("--force", "reinstall even if a working engine is already present")
|
|
114
|
+
.option("--python <path>", "install into this Python interpreter")
|
|
115
|
+
.action(async (f) => {
|
|
116
|
+
const r = await installEngine({ ...(f.scan ? { scan: true } : {}), ...(f.force ? { force: true } : {}), ...(f.python ? { python: f.python } : {}), log: (l) => io.out(`${l}\n`) });
|
|
117
|
+
if (r.status === "failed") {
|
|
118
|
+
io.err(`openbooks: ${r.detail}\n${r.hint ? ` ${r.hint}\n` : ""}`);
|
|
119
|
+
set(EXIT.engine);
|
|
120
|
+
}
|
|
121
|
+
else
|
|
122
|
+
io.out(`${r.detail}\n`);
|
|
123
|
+
});
|
|
124
|
+
program.command("engine-info").description("print the engine version, contract version and installed layouts as JSON")
|
|
125
|
+
.option("--python <path>", "the Python interpreter to ask")
|
|
126
|
+
.action(async (f) => {
|
|
127
|
+
io.out(JSON.stringify((await findEngine(f.python ? { python: f.python } : {})).info, null, 2) + "\n");
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
await program.parseAsync(argv, { from: "user" });
|
|
131
|
+
return code;
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
if (e instanceof CommanderError)
|
|
135
|
+
return e.exitCode === 0 ? EXIT.ok : EXIT.usage;
|
|
136
|
+
if (e instanceof EngineError) {
|
|
137
|
+
io.err(`openbooks: ${e.message}\n${e.hint ? ` ${e.hint}\n` : ""}`);
|
|
138
|
+
if (/\[scan\]/.test(e.message))
|
|
139
|
+
io.err(" or, with the npm package: openbooks install-engine --scan\n");
|
|
140
|
+
return EXIT.engine;
|
|
141
|
+
}
|
|
142
|
+
io.err(`openbooks: unexpected error: ${e.message}\n`);
|
|
143
|
+
return EXIT.engine;
|
|
144
|
+
}
|
|
145
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumeo_hq/openbooks",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Open Books: financial documents in, one proven JSON per file out (or a typed refusal). `npm i @lumeo_hq/openbooks` installs every JS package and the Python engine.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"openbooks": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"scripts",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"commander": "^14.0.0",
|
|
22
|
+
"@lumeo_hq/openbooks-core": "0.1.0",
|
|
23
|
+
"@lumeo_hq/openbooks-statements": "0.1.0",
|
|
24
|
+
"@lumeo_hq/openbooks-gateways": "0.1.0",
|
|
25
|
+
"@lumeo_hq/openbooks-tax": "0.1.0",
|
|
26
|
+
"@lumeo_hq/openbooks-doctor": "0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"postinstall": "node scripts/postinstall.mjs"
|
|
30
|
+
}
|
|
6
31
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Runs after `npm i @lumeo_hq/openbooks`: installs the Python engine so one npm command gives a working setup.
|
|
2
|
+
// Best effort and always exits 0: a failure here must never break `npm install`; `openbooks install-engine` retries it.
|
|
3
|
+
// OPENBOOKS_SKIP_PYTHON=1 skip this step npm i --ignore-scripts skips it too
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const here = fileURLToPath(import.meta.url);
|
|
8
|
+
// Only when installed from the registry. In a source checkout the engine comes from `pip install -e` (scripts/setup.py).
|
|
9
|
+
if (!here.split(/[\\/]/).includes("node_modules")) process.exit(0);
|
|
10
|
+
if (process.env.OPENBOOKS_SKIP_PYTHON) process.exit(0);
|
|
11
|
+
|
|
12
|
+
const { installEngine } = await import("../dist/install-engine.js");
|
|
13
|
+
const r = await installEngine({ log: (l) => console.log(`openbooks: ${l}`) });
|
|
14
|
+
console.log(`openbooks: ${r.detail}`);
|
|
15
|
+
if (r.status === "failed") {
|
|
16
|
+
console.log(`openbooks: the JavaScript packages are installed, but the Python engine is not.`);
|
|
17
|
+
if (r.hint) console.log(`openbooks: ${r.hint}`);
|
|
18
|
+
console.log(`openbooks: you can retry any time with: npx openbooks install-engine`);
|
|
19
|
+
}
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.log(`openbooks: skipped the Python engine step (${e instanceof Error ? e.message : e}). Run: npx openbooks install-engine`);
|
|
22
|
+
}
|
|
23
|
+
process.exit(0);
|