Semantic parser/writer for Skatteverket SRU files (BLANKETTER.SRU, INFO.SRU)
Project description
pysru-mab
A small Python library and CLI for working with SRU files — the file format used by the Swedish Tax Agency (Skatteverket) for filing corporate income tax declarations (INK2 and its appendices), as defined in the SKV 269 technical specification.
pysru-mab cn:
- Parse
BLANKETTER.SRUandINFO.SRUinto typed Python objects. - Write them back out, with byte-identical round-trip for unmodified data.
- Look up what a numeric field code (
7011,7104, ...) actually means, via a small built-in code table for INK2/INK2R/INK2S. - A
pysru-mabCLI for converting SRU files to/from JSON.
Install
pip install pysru-mab
Quick start
from pysru_mab import parse_file, describe
sru_file = parse_file("BLANKETTER.SRU") # defaults to cp850 encoding
for blankett in sru_file.blanketter:
print(blankett.form_id, blankett.namn)
for uppgift in blankett.uppgifter:
info = describe(blankett.form_name, uppgift.code)
label = info["label"] if info else "?"
print(f" {uppgift.code} ({label}): {uppgift.value}")
Access individual fields:
ink2 = sru_file.find("INK2")[0]
ink2.get("7104") # -> "-67222" (or None if absent)
ink2.get_all("7104") # -> all values for that code, in order
ink2["7104"] # -> "-67222" (raises KeyError if absent)
Writing / round-trip
from pysru_mab import parse_file, write_file
sru_file = parse_file("BLANKETTER.SRU")
# ... modify sru_file.blanketter[...] ...
write_file(sru_file, "BLANKETTER_OUT.SRU")
write_file always emits CRLF line endings (as required by the SRU
format), regardless of the input. Unmodified files round-trip
byte-for-byte.
INFO.SRU
INFO.SRU uses a different, nested-section format and has its own
parser/writer:
from pysru_mab import parse_info_file, write_info_file
info = parse_info_file("INFO.SRU")
print(info.orgnr, info.namn)
write_info_file(info, "INFO_OUT.SRU")
CLI usage
# Convert to JSON (optionally annotate fields with human-readable labels)
pysru-mab to-json BLANKETTER.SRU --describe -o blanketter.json
# Convert back to SRU
pysru-mab from-json blanketter.json -o BLANKETTER.SRU
# Look up what a field code means
pysru-mab describe INK2 7011
pysru-mab describe INK2 # list all known codes for a form
# INFO.SRU <-> JSON
pysru-mab info-to-json INFO.SRU -o info.json
pysru-mab info-from-json info.json -o INFO.SRU
If -o/--output is omitted, output goes to stdout.
Code tables
describe(form_name, code) looks up a field code against the bundled
tables in pysru_mab/data/ (ink2.json, ink2r.json, ink2s.json).
To add support for another form, drop in a new data/<form>.json file
with the schema:
{
"form": "INK2",
"source": "SKV 269 (INK2, period 2025P4)",
"codes": {
"7011": {"label": "...", "description": "...", "field_type": "date"}
}
}
field_type is one of "amount", "date", "checkbox", "text"
(defaults to "amount"). You can also load a table from your own JSON
file at runtime with load_custom_table(path).
Status: code tables are currently shipped for INK2, INK2R and INK2S
covering the fields most commonly used. Labels are best-effort based on
common SKV269/INK2 terminology — contributions and corrections against the
official SKV269 appendix are welcome. Other forms parse and write
correctly, but describe() returns None for them until a table is added.
Format notes
- Encoding: SKV269 specifies CP850/IBM850.
cp850is the default for all read/write functions, but it's configurable via theencodingkeyword argument. - Line endings: always CRLF (
\r\n) on output. - Form IDs:
#BLANKETTlines use IDs likeINK2-2025P4, parsed intoform_name="INK2",year=2025,period="P4". - Values:
#UPPGIFTvalues are kept as raw strings to preserve sign, leading zeros, and the"X"checkbox marker; useUppgift.as_int()/Uppgift.is_checkboxfor typed access.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysru_mab-0.4.0-py3-none-any.whl.
File metadata
- Download URL: pysru_mab-0.4.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72319a732fa2bfcb51726b62b08aeb15c84da6b4b73e9f95a3e39be4db41fb4c
|
|
| MD5 |
0089032371456da7f32c72bd6bc2e6da
|
|
| BLAKE2b-256 |
8a40f67bc703ab66acd7426de9942749c1613044b2905c7e8b53c905d25ecb9f
|
Provenance
The following attestation bundles were made for pysru_mab-0.4.0-py3-none-any.whl:
Publisher:
pypi-publish.yml on monperrus/pysru-mab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysru_mab-0.4.0-py3-none-any.whl -
Subject digest:
72319a732fa2bfcb51726b62b08aeb15c84da6b4b73e9f95a3e39be4db41fb4c - Sigstore transparency entry: 1807878391
- Sigstore integration time:
-
Permalink:
monperrus/pysru-mab@8dce51e573681b70cde718b636a12e3c8e546cf3 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/monperrus
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@8dce51e573681b70cde718b636a12e3c8e546cf3 -
Trigger Event:
push
-
Statement type: