statementkit 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- {statementkit-0.1.0 → statementkit-0.2.0}/Cargo.lock +2 -2
- {statementkit-0.1.0 → statementkit-0.2.0}/Cargo.toml +1 -1
- {statementkit-0.1.0 → statementkit-0.2.0}/PKG-INFO +3 -2
- {statementkit-0.1.0 → statementkit-0.2.0}/README.md +2 -1
- {statementkit-0.1.0 → statementkit-0.2.0}/bindings/python/README.md +13 -1
- statementkit-0.2.0/bindings/python/statementkit/__init__.py +171 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/bindings/python/statementkit.egg-info/PKG-INFO +3 -2
- statementkit-0.2.0/bindings/python/statementkit.egg-info/SOURCES.txt +133 -0
- statementkit-0.2.0/bindings/python/test_stream.py +44 -0
- statementkit-0.2.0/crates/statementkit/examples/stream.rs +181 -0
- statementkit-0.2.0/crates/statementkit/src/csv.rs +155 -0
- statementkit-0.2.0/crates/statementkit/src/dates.rs +189 -0
- statementkit-0.2.0/crates/statementkit/src/dec.rs +193 -0
- statementkit-0.2.0/crates/statementkit/src/json.rs +90 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/crates/statementkit/src/lib.rs +17 -1
- statementkit-0.2.0/crates/statementkit/src/session.rs +493 -0
- statementkit-0.2.0/crates/statementkit/src/target.rs +132 -0
- statementkit-0.2.0/crates/statementkit/src/targets/alloc.rs +306 -0
- statementkit-0.2.0/crates/statementkit/src/targets/attribution.rs +227 -0
- statementkit-0.2.0/crates/statementkit/src/targets/basis_recon.rs +201 -0
- statementkit-0.2.0/crates/statementkit/src/targets/borrow_fees.rs +324 -0
- statementkit-0.2.0/crates/statementkit/src/targets/capital_base.rs +337 -0
- statementkit-0.2.0/crates/statementkit/src/targets/cash_coverage.rs +166 -0
- statementkit-0.2.0/crates/statementkit/src/targets/dividends.rs +232 -0
- statementkit-0.2.0/crates/statementkit/src/targets/family_detected.rs +260 -0
- statementkit-0.2.0/crates/statementkit/src/targets/family_stated.rs +337 -0
- statementkit-0.2.0/crates/statementkit/src/targets/financing_legs.rs +205 -0
- statementkit-0.2.0/crates/statementkit/src/targets/flow_timing.rs +378 -0
- statementkit-0.2.0/crates/statementkit/src/targets/fx_exposure.rs +251 -0
- statementkit-0.2.0/crates/statementkit/src/targets/idle_cash.rs +298 -0
- statementkit-0.2.0/crates/statementkit/src/targets/income.rs +228 -0
- statementkit-0.2.0/crates/statementkit/src/targets/interest.rs +314 -0
- statementkit-0.2.0/crates/statementkit/src/targets/leverage.rs +221 -0
- statementkit-0.2.0/crates/statementkit/src/targets/mod.rs +34 -0
- statementkit-0.2.0/crates/statementkit/src/targets/nav_series.rs +248 -0
- statementkit-0.2.0/crates/statementkit/src/targets/quality.rs +273 -0
- statementkit-0.2.0/crates/statementkit/src/targets/realized_stlt.rs +158 -0
- statementkit-0.2.0/crates/statementkit/src/targets/risk_series.rs +309 -0
- statementkit-0.2.0/crates/statementkit/src/targets/roll_yield.rs +338 -0
- statementkit-0.2.0/crates/statementkit/src/targets/subaccount_ledger.rs +288 -0
- statementkit-0.2.0/crates/statementkit/src/targets/subset_calendar.rs +342 -0
- statementkit-0.2.0/crates/statementkit/src/targets/subset_ledger.rs +439 -0
- statementkit-0.2.0/crates/statementkit/src/targets/syep.rs +245 -0
- statementkit-0.2.0/crates/statementkit/src/targets/symbol_pnl.rs +194 -0
- statementkit-0.2.0/crates/statementkit/src/targets/ticker_map.rs +163 -0
- statementkit-0.2.0/crates/statementkit/src/targets/trades.rs +380 -0
- statementkit-0.2.0/crates/statementkit/src/targets/twr.rs +269 -0
- statementkit-0.2.0/crates/statementkit/src/targets/wash_sales.rs +214 -0
- statementkit-0.2.0/crates/statementkit/src/targets/weights.rs +301 -0
- statementkit-0.2.0/crates/statementkit/src/targets/xirr.rs +341 -0
- statementkit-0.2.0/crates/statementkit/src/tickers.rs +109 -0
- statementkit-0.2.0/crates/statementkit/tests/dec.rs +46 -0
- statementkit-0.2.0/crates/statementkit/tests/session_selection.rs +118 -0
- statementkit-0.2.0/crates/statementkit/tests/stream.rs +184 -0
- statementkit-0.2.0/crates/statementkit/tests/target_allocation.rs +136 -0
- statementkit-0.2.0/crates/statementkit/tests/target_attribution.rs +107 -0
- statementkit-0.2.0/crates/statementkit/tests/target_basis_recon.rs +105 -0
- statementkit-0.2.0/crates/statementkit/tests/target_borrow_fees.rs +130 -0
- statementkit-0.2.0/crates/statementkit/tests/target_capital_base.rs +93 -0
- statementkit-0.2.0/crates/statementkit/tests/target_cash_coverage.rs +80 -0
- statementkit-0.2.0/crates/statementkit/tests/target_dividends.rs +121 -0
- statementkit-0.2.0/crates/statementkit/tests/target_family_detected.rs +86 -0
- statementkit-0.2.0/crates/statementkit/tests/target_family_stated.rs +101 -0
- statementkit-0.2.0/crates/statementkit/tests/target_financing_legs.rs +80 -0
- statementkit-0.2.0/crates/statementkit/tests/target_flow_timing.rs +81 -0
- statementkit-0.2.0/crates/statementkit/tests/target_fx_exposure.rs +86 -0
- statementkit-0.2.0/crates/statementkit/tests/target_idle_cash.rs +127 -0
- statementkit-0.2.0/crates/statementkit/tests/target_income.rs +96 -0
- statementkit-0.2.0/crates/statementkit/tests/target_interest.rs +147 -0
- statementkit-0.2.0/crates/statementkit/tests/target_leverage.rs +80 -0
- statementkit-0.2.0/crates/statementkit/tests/target_nav_series.rs +132 -0
- statementkit-0.2.0/crates/statementkit/tests/target_quality.rs +101 -0
- statementkit-0.2.0/crates/statementkit/tests/target_realized_stlt.rs +110 -0
- statementkit-0.2.0/crates/statementkit/tests/target_risk_series.rs +127 -0
- statementkit-0.2.0/crates/statementkit/tests/target_roll_yield.rs +88 -0
- statementkit-0.2.0/crates/statementkit/tests/target_subaccount_ledger.rs +87 -0
- statementkit-0.2.0/crates/statementkit/tests/target_subset_calendar.rs +82 -0
- statementkit-0.2.0/crates/statementkit/tests/target_subset_ledger.rs +107 -0
- statementkit-0.2.0/crates/statementkit/tests/target_syep.rs +102 -0
- statementkit-0.2.0/crates/statementkit/tests/target_symbol_pnl.rs +153 -0
- statementkit-0.2.0/crates/statementkit/tests/target_ticker_map.rs +78 -0
- statementkit-0.2.0/crates/statementkit/tests/target_trades.rs +133 -0
- statementkit-0.2.0/crates/statementkit/tests/target_twr.rs +96 -0
- statementkit-0.2.0/crates/statementkit/tests/target_wash_sales.rs +94 -0
- statementkit-0.2.0/crates/statementkit/tests/target_weights.rs +123 -0
- statementkit-0.2.0/crates/statementkit/tests/target_xirr.rs +145 -0
- statementkit-0.2.0/crates/statementkit-c/include/statementkit.h +64 -0
- statementkit-0.2.0/crates/statementkit-c/src/lib.rs +473 -0
- statementkit-0.2.0/crates/statementkit-c/tests/smoke.c +141 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/pyproject.toml +1 -1
- statementkit-0.2.0/tests/vectors/allocation.json +1 -0
- statementkit-0.2.0/tests/vectors/attribution.json +1 -0
- statementkit-0.2.0/tests/vectors/basis_recon.json +1 -0
- statementkit-0.2.0/tests/vectors/borrow_fees.json +1 -0
- statementkit-0.2.0/tests/vectors/capital_base.json +1 -0
- statementkit-0.2.0/tests/vectors/cash_coverage.json +1 -0
- statementkit-0.2.0/tests/vectors/dec.tsv +28 -0
- statementkit-0.2.0/tests/vectors/dividends.json +1 -0
- statementkit-0.2.0/tests/vectors/family_detected.json +1 -0
- statementkit-0.2.0/tests/vectors/family_stated.json +1 -0
- statementkit-0.2.0/tests/vectors/financing_legs.json +1 -0
- statementkit-0.2.0/tests/vectors/flow_timing.json +1 -0
- statementkit-0.2.0/tests/vectors/fx_exposure.json +1 -0
- statementkit-0.2.0/tests/vectors/idle_cash.json +1 -0
- statementkit-0.2.0/tests/vectors/income.json +1 -0
- statementkit-0.2.0/tests/vectors/interest.json +1 -0
- statementkit-0.2.0/tests/vectors/leverage.json +1 -0
- statementkit-0.2.0/tests/vectors/nav_series.json +1 -0
- statementkit-0.2.0/tests/vectors/quality.json +1 -0
- statementkit-0.2.0/tests/vectors/realized_stlt.json +1 -0
- statementkit-0.2.0/tests/vectors/risk_series.json +2 -0
- statementkit-0.2.0/tests/vectors/roll_yield.json +1 -0
- statementkit-0.2.0/tests/vectors/stream.json +1 -0
- statementkit-0.2.0/tests/vectors/stream_filter.json +1 -0
- statementkit-0.2.0/tests/vectors/subaccount_ledger.json +1 -0
- statementkit-0.2.0/tests/vectors/subset_calendar.json +1 -0
- statementkit-0.2.0/tests/vectors/subset_ledger.json +1 -0
- statementkit-0.2.0/tests/vectors/syep.json +1 -0
- statementkit-0.2.0/tests/vectors/symbol_pnl.json +1 -0
- statementkit-0.2.0/tests/vectors/ticker_map.json +1 -0
- statementkit-0.2.0/tests/vectors/trades.json +1 -0
- statementkit-0.2.0/tests/vectors/twr.json +1 -0
- statementkit-0.2.0/tests/vectors/wash_sales.json +1 -0
- statementkit-0.2.0/tests/vectors/weights.json +1 -0
- statementkit-0.2.0/tests/vectors/xirr.json +1 -0
- statementkit-0.1.0/bindings/python/statementkit/__init__.py +0 -97
- statementkit-0.1.0/bindings/python/statementkit.egg-info/SOURCES.txt +0 -21
- statementkit-0.1.0/crates/statementkit-c/include/statementkit.h +0 -30
- statementkit-0.1.0/crates/statementkit-c/src/lib.rs +0 -129
- statementkit-0.1.0/crates/statementkit-c/tests/smoke.c +0 -50
- {statementkit-0.1.0 → statementkit-0.2.0}/LICENSE +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/MANIFEST.in +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/bindings/python/statementkit.egg-info/dependency_links.txt +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/bindings/python/statementkit.egg-info/top_level.txt +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/bindings/python/test_parse_float.py +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/crates/statementkit/Cargo.toml +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/crates/statementkit/tests/parse_float.rs +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/crates/statementkit-c/Cargo.toml +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/setup.cfg +0 -0
- {statementkit-0.1.0 → statementkit-0.2.0}/tests/vectors/parse_float.tsv +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: statementkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Parse brokerage statement content into actionable numbers. Independent tool. Not affiliated with or endorsed by Interactive Brokers LLC.
|
|
5
5
|
Author: heshiming
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -41,7 +41,8 @@ crates/statementkit-c C ABI: status codes, out-params, panic-proofed
|
|
|
41
41
|
include/statementkit.h hand-kept header; discriminants contractual
|
|
42
42
|
bindings/python ctypes shim over the C ABI (pyo3 wheels later)
|
|
43
43
|
bindings/node raw-WASM npm package, TypeScript types included
|
|
44
|
-
bindings/java Panama FFM
|
|
44
|
+
bindings/java Panama FFM, JDK 22+, no JNI; Maven
|
|
45
|
+
io.github.heshiming:statementkit (natives embedded)
|
|
45
46
|
bindings/go cgo over the static lib, plain `go test ./...`
|
|
46
47
|
tests/vectors/ shared ground-truth vectors every binding runs
|
|
47
48
|
scripts/c-smoke.sh links tests/smoke.c against the static lib, runs it
|
|
@@ -22,7 +22,8 @@ crates/statementkit-c C ABI: status codes, out-params, panic-proofed
|
|
|
22
22
|
include/statementkit.h hand-kept header; discriminants contractual
|
|
23
23
|
bindings/python ctypes shim over the C ABI (pyo3 wheels later)
|
|
24
24
|
bindings/node raw-WASM npm package, TypeScript types included
|
|
25
|
-
bindings/java Panama FFM
|
|
25
|
+
bindings/java Panama FFM, JDK 22+, no JNI; Maven
|
|
26
|
+
io.github.heshiming:statementkit (natives embedded)
|
|
26
27
|
bindings/go cgo over the static lib, plain `go test ./...`
|
|
27
28
|
tests/vectors/ shared ground-truth vectors every binding runs
|
|
28
29
|
scripts/c-smoke.sh links tests/smoke.c against the static lib, runs it
|
|
@@ -15,7 +15,19 @@ LLC.
|
|
|
15
15
|
import statementkit
|
|
16
16
|
statementkit.parse_float("12.25") # 12.25
|
|
17
17
|
statementkit.parse_float("1,2") # raises InvalidInputError
|
|
18
|
-
statementkit.version() # "0.
|
|
18
|
+
statementkit.version() # "0.2.0"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Streaming session over named targets (ticker selection optional; the
|
|
22
|
+
report then carries a `filter` fragment):
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from statementkit import Session
|
|
26
|
+
with Session(["income", "trades"], tickers=["ABC"]) as s:
|
|
27
|
+
s.feed(open("statement.csv", "rb").read())
|
|
28
|
+
s.end_file()
|
|
29
|
+
text = s.report() # compact JSON, numeric literals unquoted
|
|
30
|
+
data = s.report_json() # floats as Decimal; ints stay int
|
|
19
31
|
```
|
|
20
32
|
|
|
21
33
|
Errors mirror the ABI status codes: `NullInputError`, `EmptyInputError`,
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""statementkit — ctypes shim over the C ABI.
|
|
2
|
+
|
|
3
|
+
Interim binding, zero dependencies: dlopens the C ABI shared library.
|
|
4
|
+
pip wheels bundle the library next to this file; in-repo builds find it in
|
|
5
|
+
the workspace target/release dir. The pyo3 wheel crate supersedes this
|
|
6
|
+
when the API is real. Library discovery: $STATEMENTKIT_LIB, else the
|
|
7
|
+
package dir, else workspace target/release, else the loader path.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import ctypes
|
|
13
|
+
import ctypes.util
|
|
14
|
+
import glob
|
|
15
|
+
import os
|
|
16
|
+
import pathlib
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class StatementKitError(Exception):
|
|
20
|
+
"""Base for all statementkit failures, carrying the ABI status code."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, code: int, message: str):
|
|
23
|
+
super().__init__(message)
|
|
24
|
+
self.code = code
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class NullInputError(StatementKitError):
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class EmptyInputError(StatementKitError):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class InvalidInputError(StatementKitError):
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class PanicError(StatementKitError):
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_ERRORS = {
|
|
44
|
+
1: NullInputError,
|
|
45
|
+
2: EmptyInputError,
|
|
46
|
+
3: InvalidInputError,
|
|
47
|
+
4: PanicError,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _find_library() -> str:
|
|
52
|
+
env = os.environ.get("STATEMENTKIT_LIB")
|
|
53
|
+
if env:
|
|
54
|
+
return env
|
|
55
|
+
here = pathlib.Path(__file__).resolve().parent
|
|
56
|
+
for hit in glob.glob(str(here / "*statementkit_c*.so")) + \
|
|
57
|
+
glob.glob(str(here / "*statementkit_c*.dylib")) + \
|
|
58
|
+
glob.glob(str(here / "*statementkit_c*.dll")) + \
|
|
59
|
+
glob.glob(str(here / "*statementkit_c*.pyd")):
|
|
60
|
+
return hit
|
|
61
|
+
root = here.parents[2]
|
|
62
|
+
names = ("libstatementkit_c.so", "libstatementkit_c.dylib", "statementkit_c.dll")
|
|
63
|
+
for name in names:
|
|
64
|
+
candidate = root / "target" / "release" / name
|
|
65
|
+
if candidate.exists():
|
|
66
|
+
return str(candidate)
|
|
67
|
+
for name in names:
|
|
68
|
+
found = ctypes.util.find_library(name)
|
|
69
|
+
if found:
|
|
70
|
+
return found
|
|
71
|
+
raise ImportError(
|
|
72
|
+
"statementkit native library not found; install the statementkit "
|
|
73
|
+
"wheel, run `cargo build --release -p statementkit-c` in the "
|
|
74
|
+
"workspace, or set $STATEMENTKIT_LIB"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
_lib = ctypes.CDLL(_find_library())
|
|
79
|
+
_lib.sk_parse_float.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_double)]
|
|
80
|
+
_lib.sk_parse_float.restype = ctypes.c_int
|
|
81
|
+
_lib.sk_version.restype = ctypes.c_char_p
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def parse_float(text: str | None) -> float:
|
|
85
|
+
"""Parse a bare float literal; raises a StatementKitError subclass."""
|
|
86
|
+
if text is None:
|
|
87
|
+
raise NullInputError(1, "input is None")
|
|
88
|
+
out = ctypes.c_double()
|
|
89
|
+
status = _lib.sk_parse_float(text.encode("utf-8"), ctypes.byref(out))
|
|
90
|
+
if status != 0:
|
|
91
|
+
error = _ERRORS.get(status, PanicError)
|
|
92
|
+
raise error(status, f"sk_parse_float failed with status {status}")
|
|
93
|
+
return out.value
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def version() -> str:
|
|
97
|
+
return _lib.sk_version().decode()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
_lib.sk_session_open.argtypes = [ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_char_p),
|
|
101
|
+
ctypes.c_size_t, ctypes.POINTER(ctypes.c_char_p),
|
|
102
|
+
ctypes.c_size_t, ctypes.c_char_p, ctypes.c_size_t]
|
|
103
|
+
_lib.sk_session_feed.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t]
|
|
104
|
+
_lib.sk_session_end_file.argtypes = [ctypes.c_void_p]
|
|
105
|
+
_lib.sk_session_report_len.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t)]
|
|
106
|
+
_lib.sk_session_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
|
|
107
|
+
_lib.sk_session_close.argtypes = [ctypes.c_void_p]
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Session:
|
|
111
|
+
"""Push streaming session: feed bytes per file, end_file between files.
|
|
112
|
+
|
|
113
|
+
report() returns the compact JSON text; report_json() parses it with
|
|
114
|
+
decimal.Decimal so numeric literals keep full precision."""
|
|
115
|
+
|
|
116
|
+
def __init__(self, targets, tickers=()):
|
|
117
|
+
names = [t.encode("utf-8") for t in targets]
|
|
118
|
+
tick = [t.encode("utf-8") for t in tickers]
|
|
119
|
+
arr_n = (ctypes.c_char_p * len(names))(*names) if names else None
|
|
120
|
+
arr_t = (ctypes.c_char_p * len(tick))(*tick) if tick else None
|
|
121
|
+
err = ctypes.create_string_buffer(512)
|
|
122
|
+
handle = ctypes.c_void_p()
|
|
123
|
+
status = _lib.sk_session_open(ctypes.byref(handle), arr_n, len(names),
|
|
124
|
+
arr_t, len(tick), err, 512)
|
|
125
|
+
if status != 0:
|
|
126
|
+
error = _ERRORS.get(status, PanicError)
|
|
127
|
+
raise error(status, err.value.decode("utf-8", "replace") or
|
|
128
|
+
f"session open failed with status {status}")
|
|
129
|
+
self._handle = handle
|
|
130
|
+
|
|
131
|
+
def _check(self, status):
|
|
132
|
+
if status != 0:
|
|
133
|
+
error = _ERRORS.get(status, PanicError)
|
|
134
|
+
raise error(status, f"session call failed with status {status}")
|
|
135
|
+
|
|
136
|
+
def feed(self, data: bytes):
|
|
137
|
+
if data:
|
|
138
|
+
self._check(_lib.sk_session_feed(self._handle, data, len(data)))
|
|
139
|
+
|
|
140
|
+
def end_file(self):
|
|
141
|
+
self._check(_lib.sk_session_end_file(self._handle))
|
|
142
|
+
|
|
143
|
+
def report(self) -> str:
|
|
144
|
+
length = ctypes.c_size_t()
|
|
145
|
+
self._check(_lib.sk_session_report_len(self._handle, ctypes.byref(length)))
|
|
146
|
+
buf = ctypes.create_string_buffer(length.value + 1)
|
|
147
|
+
self._check(_lib.sk_session_report(self._handle, buf, length.value + 1))
|
|
148
|
+
return buf.value.decode("utf-8")
|
|
149
|
+
|
|
150
|
+
def report_json(self, **loads_kwargs):
|
|
151
|
+
import json
|
|
152
|
+
from decimal import Decimal
|
|
153
|
+
loads_kwargs.setdefault("parse_float", Decimal)
|
|
154
|
+
return json.loads(self.report(), **loads_kwargs)
|
|
155
|
+
|
|
156
|
+
def close(self):
|
|
157
|
+
if self._handle is not None:
|
|
158
|
+
_lib.sk_session_close(self._handle)
|
|
159
|
+
self._handle = None
|
|
160
|
+
|
|
161
|
+
def __enter__(self):
|
|
162
|
+
return self
|
|
163
|
+
|
|
164
|
+
def __exit__(self, *_exc):
|
|
165
|
+
self.close()
|
|
166
|
+
|
|
167
|
+
def __del__(self):
|
|
168
|
+
try:
|
|
169
|
+
self.close()
|
|
170
|
+
except Exception:
|
|
171
|
+
pass
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: statementkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Parse brokerage statement content into actionable numbers. Independent tool. Not affiliated with or endorsed by Interactive Brokers LLC.
|
|
5
5
|
Author: heshiming
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -41,7 +41,8 @@ crates/statementkit-c C ABI: status codes, out-params, panic-proofed
|
|
|
41
41
|
include/statementkit.h hand-kept header; discriminants contractual
|
|
42
42
|
bindings/python ctypes shim over the C ABI (pyo3 wheels later)
|
|
43
43
|
bindings/node raw-WASM npm package, TypeScript types included
|
|
44
|
-
bindings/java Panama FFM
|
|
44
|
+
bindings/java Panama FFM, JDK 22+, no JNI; Maven
|
|
45
|
+
io.github.heshiming:statementkit (natives embedded)
|
|
45
46
|
bindings/go cgo over the static lib, plain `go test ./...`
|
|
46
47
|
tests/vectors/ shared ground-truth vectors every binding runs
|
|
47
48
|
scripts/c-smoke.sh links tests/smoke.c against the static lib, runs it
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Cargo.lock
|
|
2
|
+
Cargo.toml
|
|
3
|
+
LICENSE
|
|
4
|
+
MANIFEST.in
|
|
5
|
+
README.md
|
|
6
|
+
pyproject.toml
|
|
7
|
+
bindings/python/README.md
|
|
8
|
+
bindings/python/test_parse_float.py
|
|
9
|
+
bindings/python/test_stream.py
|
|
10
|
+
bindings/python/statementkit/__init__.py
|
|
11
|
+
bindings/python/statementkit.egg-info/PKG-INFO
|
|
12
|
+
bindings/python/statementkit.egg-info/SOURCES.txt
|
|
13
|
+
bindings/python/statementkit.egg-info/dependency_links.txt
|
|
14
|
+
bindings/python/statementkit.egg-info/top_level.txt
|
|
15
|
+
crates/statementkit/Cargo.toml
|
|
16
|
+
crates/statementkit-c/Cargo.toml
|
|
17
|
+
crates/statementkit-c/include/statementkit.h
|
|
18
|
+
crates/statementkit-c/src/lib.rs
|
|
19
|
+
crates/statementkit-c/tests/smoke.c
|
|
20
|
+
crates/statementkit/examples/stream.rs
|
|
21
|
+
crates/statementkit/src/csv.rs
|
|
22
|
+
crates/statementkit/src/dates.rs
|
|
23
|
+
crates/statementkit/src/dec.rs
|
|
24
|
+
crates/statementkit/src/json.rs
|
|
25
|
+
crates/statementkit/src/lib.rs
|
|
26
|
+
crates/statementkit/src/session.rs
|
|
27
|
+
crates/statementkit/src/target.rs
|
|
28
|
+
crates/statementkit/src/tickers.rs
|
|
29
|
+
crates/statementkit/src/targets/alloc.rs
|
|
30
|
+
crates/statementkit/src/targets/attribution.rs
|
|
31
|
+
crates/statementkit/src/targets/basis_recon.rs
|
|
32
|
+
crates/statementkit/src/targets/borrow_fees.rs
|
|
33
|
+
crates/statementkit/src/targets/capital_base.rs
|
|
34
|
+
crates/statementkit/src/targets/cash_coverage.rs
|
|
35
|
+
crates/statementkit/src/targets/dividends.rs
|
|
36
|
+
crates/statementkit/src/targets/family_detected.rs
|
|
37
|
+
crates/statementkit/src/targets/family_stated.rs
|
|
38
|
+
crates/statementkit/src/targets/financing_legs.rs
|
|
39
|
+
crates/statementkit/src/targets/flow_timing.rs
|
|
40
|
+
crates/statementkit/src/targets/fx_exposure.rs
|
|
41
|
+
crates/statementkit/src/targets/idle_cash.rs
|
|
42
|
+
crates/statementkit/src/targets/income.rs
|
|
43
|
+
crates/statementkit/src/targets/interest.rs
|
|
44
|
+
crates/statementkit/src/targets/leverage.rs
|
|
45
|
+
crates/statementkit/src/targets/mod.rs
|
|
46
|
+
crates/statementkit/src/targets/nav_series.rs
|
|
47
|
+
crates/statementkit/src/targets/quality.rs
|
|
48
|
+
crates/statementkit/src/targets/realized_stlt.rs
|
|
49
|
+
crates/statementkit/src/targets/risk_series.rs
|
|
50
|
+
crates/statementkit/src/targets/roll_yield.rs
|
|
51
|
+
crates/statementkit/src/targets/subaccount_ledger.rs
|
|
52
|
+
crates/statementkit/src/targets/subset_calendar.rs
|
|
53
|
+
crates/statementkit/src/targets/subset_ledger.rs
|
|
54
|
+
crates/statementkit/src/targets/syep.rs
|
|
55
|
+
crates/statementkit/src/targets/symbol_pnl.rs
|
|
56
|
+
crates/statementkit/src/targets/ticker_map.rs
|
|
57
|
+
crates/statementkit/src/targets/trades.rs
|
|
58
|
+
crates/statementkit/src/targets/twr.rs
|
|
59
|
+
crates/statementkit/src/targets/wash_sales.rs
|
|
60
|
+
crates/statementkit/src/targets/weights.rs
|
|
61
|
+
crates/statementkit/src/targets/xirr.rs
|
|
62
|
+
crates/statementkit/tests/dec.rs
|
|
63
|
+
crates/statementkit/tests/parse_float.rs
|
|
64
|
+
crates/statementkit/tests/session_selection.rs
|
|
65
|
+
crates/statementkit/tests/stream.rs
|
|
66
|
+
crates/statementkit/tests/target_allocation.rs
|
|
67
|
+
crates/statementkit/tests/target_attribution.rs
|
|
68
|
+
crates/statementkit/tests/target_basis_recon.rs
|
|
69
|
+
crates/statementkit/tests/target_borrow_fees.rs
|
|
70
|
+
crates/statementkit/tests/target_capital_base.rs
|
|
71
|
+
crates/statementkit/tests/target_cash_coverage.rs
|
|
72
|
+
crates/statementkit/tests/target_dividends.rs
|
|
73
|
+
crates/statementkit/tests/target_family_detected.rs
|
|
74
|
+
crates/statementkit/tests/target_family_stated.rs
|
|
75
|
+
crates/statementkit/tests/target_financing_legs.rs
|
|
76
|
+
crates/statementkit/tests/target_flow_timing.rs
|
|
77
|
+
crates/statementkit/tests/target_fx_exposure.rs
|
|
78
|
+
crates/statementkit/tests/target_idle_cash.rs
|
|
79
|
+
crates/statementkit/tests/target_income.rs
|
|
80
|
+
crates/statementkit/tests/target_interest.rs
|
|
81
|
+
crates/statementkit/tests/target_leverage.rs
|
|
82
|
+
crates/statementkit/tests/target_nav_series.rs
|
|
83
|
+
crates/statementkit/tests/target_quality.rs
|
|
84
|
+
crates/statementkit/tests/target_realized_stlt.rs
|
|
85
|
+
crates/statementkit/tests/target_risk_series.rs
|
|
86
|
+
crates/statementkit/tests/target_roll_yield.rs
|
|
87
|
+
crates/statementkit/tests/target_subaccount_ledger.rs
|
|
88
|
+
crates/statementkit/tests/target_subset_calendar.rs
|
|
89
|
+
crates/statementkit/tests/target_subset_ledger.rs
|
|
90
|
+
crates/statementkit/tests/target_syep.rs
|
|
91
|
+
crates/statementkit/tests/target_symbol_pnl.rs
|
|
92
|
+
crates/statementkit/tests/target_ticker_map.rs
|
|
93
|
+
crates/statementkit/tests/target_trades.rs
|
|
94
|
+
crates/statementkit/tests/target_twr.rs
|
|
95
|
+
crates/statementkit/tests/target_wash_sales.rs
|
|
96
|
+
crates/statementkit/tests/target_weights.rs
|
|
97
|
+
crates/statementkit/tests/target_xirr.rs
|
|
98
|
+
tests/vectors/allocation.json
|
|
99
|
+
tests/vectors/attribution.json
|
|
100
|
+
tests/vectors/basis_recon.json
|
|
101
|
+
tests/vectors/borrow_fees.json
|
|
102
|
+
tests/vectors/capital_base.json
|
|
103
|
+
tests/vectors/cash_coverage.json
|
|
104
|
+
tests/vectors/dec.tsv
|
|
105
|
+
tests/vectors/dividends.json
|
|
106
|
+
tests/vectors/family_detected.json
|
|
107
|
+
tests/vectors/family_stated.json
|
|
108
|
+
tests/vectors/financing_legs.json
|
|
109
|
+
tests/vectors/flow_timing.json
|
|
110
|
+
tests/vectors/fx_exposure.json
|
|
111
|
+
tests/vectors/idle_cash.json
|
|
112
|
+
tests/vectors/income.json
|
|
113
|
+
tests/vectors/interest.json
|
|
114
|
+
tests/vectors/leverage.json
|
|
115
|
+
tests/vectors/nav_series.json
|
|
116
|
+
tests/vectors/parse_float.tsv
|
|
117
|
+
tests/vectors/quality.json
|
|
118
|
+
tests/vectors/realized_stlt.json
|
|
119
|
+
tests/vectors/risk_series.json
|
|
120
|
+
tests/vectors/roll_yield.json
|
|
121
|
+
tests/vectors/stream.json
|
|
122
|
+
tests/vectors/stream_filter.json
|
|
123
|
+
tests/vectors/subaccount_ledger.json
|
|
124
|
+
tests/vectors/subset_calendar.json
|
|
125
|
+
tests/vectors/subset_ledger.json
|
|
126
|
+
tests/vectors/syep.json
|
|
127
|
+
tests/vectors/symbol_pnl.json
|
|
128
|
+
tests/vectors/ticker_map.json
|
|
129
|
+
tests/vectors/trades.json
|
|
130
|
+
tests/vectors/twr.json
|
|
131
|
+
tests/vectors/wash_sales.json
|
|
132
|
+
tests/vectors/weights.json
|
|
133
|
+
tests/vectors/xirr.json
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Session vector test: shared vectors from tests/vectors/."""
|
|
2
|
+
import json
|
|
3
|
+
import pathlib
|
|
4
|
+
import sys
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
|
|
7
|
+
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
8
|
+
from statementkit import Session # noqa: E402
|
|
9
|
+
|
|
10
|
+
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
|
11
|
+
VECTORS = ROOT / "tests" / "vectors"
|
|
12
|
+
|
|
13
|
+
for name in ("stream.json", "stream_filter.json"):
|
|
14
|
+
vec = json.loads((VECTORS / name).read_text())
|
|
15
|
+
files = [(VECTORS / f).resolve() for f in vec["files"]]
|
|
16
|
+
with Session(vec["targets"], vec.get("tickers") or []) as s:
|
|
17
|
+
for f in files:
|
|
18
|
+
s.feed(f.read_bytes())
|
|
19
|
+
s.end_file()
|
|
20
|
+
text = s.report()
|
|
21
|
+
for frag in vec["expect"]:
|
|
22
|
+
assert frag in text, f"{name}: missing fragment {frag[:60]!r}"
|
|
23
|
+
parsed = s.report_json()
|
|
24
|
+
assert parsed["files"] == len(files)
|
|
25
|
+
|
|
26
|
+
# numeric literals must survive as exact decimals
|
|
27
|
+
vec = json.loads((VECTORS / "stream_filter.json").read_text())
|
|
28
|
+
with Session(vec["targets"], vec["tickers"]) as s:
|
|
29
|
+
for f in vec["files"]:
|
|
30
|
+
s.feed((VECTORS / f).resolve().read_bytes())
|
|
31
|
+
s.end_file()
|
|
32
|
+
p = s.report_json()["filter"]["partition"]
|
|
33
|
+
assert p["grand_total"] == 9918, p
|
|
34
|
+
assert isinstance(p["grand_total"], (int, Decimal))
|
|
35
|
+
assert p["selected_roots"] == -96 and p["every_other_root"] == -1
|
|
36
|
+
|
|
37
|
+
# unknown target carries the ABI message
|
|
38
|
+
try:
|
|
39
|
+
Session(["nope"])
|
|
40
|
+
raise AssertionError("unknown target accepted")
|
|
41
|
+
except Exception as e:
|
|
42
|
+
assert "unknown targets: nope" in str(e), e
|
|
43
|
+
|
|
44
|
+
print("python session vectors ok")
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
//! Streaming usage demo: feed statement CSVs into selected targets and
|
|
2
|
+
//! print the report.
|
|
3
|
+
//!
|
|
4
|
+
//! Usage: stream [--targets a,b|all] [--tickers A,B] [--json] <file-or-dir>...
|
|
5
|
+
|
|
6
|
+
use statementkit::Session;
|
|
7
|
+
use statementkit::Value;
|
|
8
|
+
use statementkit::target_names;
|
|
9
|
+
use std::io::Write;
|
|
10
|
+
use std::path::{Path, PathBuf};
|
|
11
|
+
|
|
12
|
+
fn main() {
|
|
13
|
+
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
14
|
+
let mut names: Option<Vec<String>> = None;
|
|
15
|
+
let mut tickers: Vec<String> = Vec::new();
|
|
16
|
+
let mut json = false;
|
|
17
|
+
let mut paths: Vec<PathBuf> = Vec::new();
|
|
18
|
+
let mut iter = args.into_iter();
|
|
19
|
+
while let Some(a) = iter.next() {
|
|
20
|
+
match a.as_str() {
|
|
21
|
+
"--targets" => {
|
|
22
|
+
let list = iter.next().unwrap_or_default();
|
|
23
|
+
if list == "all" {
|
|
24
|
+
names = Some(target_names().iter().map(|s| s.to_string()).collect());
|
|
25
|
+
} else {
|
|
26
|
+
names = Some(list.split(',').map(|s| s.trim().to_string()).collect());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
"--tickers" => {
|
|
30
|
+
let list = iter.next().unwrap_or_default();
|
|
31
|
+
tickers = list
|
|
32
|
+
.split([',', ' '])
|
|
33
|
+
.map(|s| s.trim().to_string())
|
|
34
|
+
.filter(|s| !s.is_empty())
|
|
35
|
+
.collect();
|
|
36
|
+
}
|
|
37
|
+
"--json" => json = true,
|
|
38
|
+
"-h" | "--help" => {
|
|
39
|
+
usage();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
other => paths.push(PathBuf::from(other)),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if paths.is_empty() {
|
|
46
|
+
usage();
|
|
47
|
+
std::process::exit(2);
|
|
48
|
+
}
|
|
49
|
+
let owned: Vec<String> = match names {
|
|
50
|
+
Some(v) => v,
|
|
51
|
+
None => target_names().iter().map(|s| s.to_string()).collect(),
|
|
52
|
+
};
|
|
53
|
+
let names: Vec<&str> = owned.iter().map(String::as_str).collect();
|
|
54
|
+
let opened = if tickers.is_empty() {
|
|
55
|
+
Session::open(&names)
|
|
56
|
+
} else {
|
|
57
|
+
let t: Vec<&str> = tickers.iter().map(String::as_str).collect();
|
|
58
|
+
Session::open_selected(&names, &t)
|
|
59
|
+
};
|
|
60
|
+
let mut session = match opened {
|
|
61
|
+
Ok(s) => s,
|
|
62
|
+
Err(e) => {
|
|
63
|
+
eprintln!("error: {e}");
|
|
64
|
+
std::process::exit(1);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
let mut files = Vec::new();
|
|
68
|
+
for p in &paths {
|
|
69
|
+
if let Err(e) = collect_files(p, &mut files) {
|
|
70
|
+
eprintln!("error: {e}");
|
|
71
|
+
std::process::exit(1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
files.sort();
|
|
75
|
+
for path in &files {
|
|
76
|
+
let data = match std::fs::read(path) {
|
|
77
|
+
Ok(d) => d,
|
|
78
|
+
Err(e) => {
|
|
79
|
+
eprintln!("error: {}: {e}", path.display());
|
|
80
|
+
std::process::exit(1);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
for chunk in data.chunks(64 * 1024) {
|
|
84
|
+
session.feed(chunk);
|
|
85
|
+
}
|
|
86
|
+
session.end_file();
|
|
87
|
+
}
|
|
88
|
+
let stdout = std::io::stdout();
|
|
89
|
+
let mut out = stdout.lock();
|
|
90
|
+
let emitted = if json {
|
|
91
|
+
writeln!(out, "{}", session.report_text())
|
|
92
|
+
} else {
|
|
93
|
+
writeln!(out, "files: {}", files.len()).and_then(|_| {
|
|
94
|
+
for (i, f) in files.iter().enumerate() {
|
|
95
|
+
writeln!(out, " [{}] {}", i + 1, f.display())?;
|
|
96
|
+
}
|
|
97
|
+
pretty(&session.report(), 0, &mut out)?;
|
|
98
|
+
writeln!(out)
|
|
99
|
+
})
|
|
100
|
+
};
|
|
101
|
+
if let Err(e) = emitted {
|
|
102
|
+
if e.kind() != std::io::ErrorKind::BrokenPipe {
|
|
103
|
+
eprintln!("error: {e}");
|
|
104
|
+
std::process::exit(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fn usage() {
|
|
110
|
+
println!("usage: stream [--targets a,b|all] [--tickers A,B] [--json] <file-or-dir>...");
|
|
111
|
+
println!("targets: {}", target_names().join(", "));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn collect_files(path: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> {
|
|
115
|
+
if path.is_dir() {
|
|
116
|
+
for entry in std::fs::read_dir(path).map_err(|e| format!("{}: {e}", path.display()))? {
|
|
117
|
+
let entry = entry.map_err(|e| format!("{}: {e}", path.display()))?;
|
|
118
|
+
collect_files(&entry.path(), out)?;
|
|
119
|
+
}
|
|
120
|
+
} else if path.extension().is_some_and(|e| e == "csv") {
|
|
121
|
+
out.push(path.to_path_buf());
|
|
122
|
+
} else if !path.exists() {
|
|
123
|
+
return Err(format!("{}: not found", path.display()));
|
|
124
|
+
} else {
|
|
125
|
+
out.push(path.to_path_buf());
|
|
126
|
+
}
|
|
127
|
+
Ok(())
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Human rendering: JSON structure with thousands separators on
|
|
131
|
+
/// numbers. Formatting lives here, never in the library report.
|
|
132
|
+
fn pretty(v: &Value, indent: usize, out: &mut impl Write) -> std::io::Result<()> {
|
|
133
|
+
match v {
|
|
134
|
+
Value::Obj(members) if !members.is_empty() => {
|
|
135
|
+
writeln!(out, "{{")?;
|
|
136
|
+
for (k, val) in members {
|
|
137
|
+
write!(out, "{:width$}\"{k}\": ", "", width = indent * 2 + 2)?;
|
|
138
|
+
pretty(val, indent + 1, out)?;
|
|
139
|
+
writeln!(out, ",")?;
|
|
140
|
+
}
|
|
141
|
+
write!(out, "{:width$}}}", "", width = indent * 2)
|
|
142
|
+
}
|
|
143
|
+
Value::Arr(items) if !items.is_empty() => {
|
|
144
|
+
writeln!(out, "[")?;
|
|
145
|
+
for val in items {
|
|
146
|
+
write!(out, "{:width$}", "", width = indent * 2 + 2)?;
|
|
147
|
+
pretty(val, indent + 1, out)?;
|
|
148
|
+
writeln!(out, ",")?;
|
|
149
|
+
}
|
|
150
|
+
write!(out, "{:width$}]", "", width = indent * 2)
|
|
151
|
+
}
|
|
152
|
+
Value::Obj(_) => write!(out, "{{}}"),
|
|
153
|
+
Value::Arr(_) => write!(out, "[]"),
|
|
154
|
+
Value::Num(n) => write!(out, "{}", group_thousands(n)),
|
|
155
|
+
Value::Str(s) => write!(out, "\"{s}\""),
|
|
156
|
+
Value::Bool(b) => write!(out, "{}", if *b { "true" } else { "false" }),
|
|
157
|
+
Value::Null => write!(out, "null"),
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn group_thousands(num: &str) -> String {
|
|
162
|
+
let (int, frac) = match num.split_once('.') {
|
|
163
|
+
Some((a, b)) => (a, Some(b)),
|
|
164
|
+
None => (num, None),
|
|
165
|
+
};
|
|
166
|
+
let (sign, digits) = match int.strip_prefix('-') {
|
|
167
|
+
Some(d) => ("-", d),
|
|
168
|
+
None => ("", int),
|
|
169
|
+
};
|
|
170
|
+
let mut grouped = String::new();
|
|
171
|
+
for (i, c) in digits.chars().enumerate() {
|
|
172
|
+
if i > 0 && (digits.len() - i) % 3 == 0 {
|
|
173
|
+
grouped.push(',');
|
|
174
|
+
}
|
|
175
|
+
grouped.push(c);
|
|
176
|
+
}
|
|
177
|
+
match frac {
|
|
178
|
+
Some(f) => format!("{sign}{grouped}.{f}"),
|
|
179
|
+
None => format!("{sign}{grouped}"),
|
|
180
|
+
}
|
|
181
|
+
}
|