fwforge 0.1.0__py3-none-any.whl
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.
- fwforge/__init__.py +1 -0
- fwforge/__main__.py +4 -0
- fwforge/cli.py +103 -0
- fwforge/fw_parser.py +47 -0
- fwforge/writer.py +11 -0
- fwforge-0.1.0.dist-info/METADATA +108 -0
- fwforge-0.1.0.dist-info/RECORD +11 -0
- fwforge-0.1.0.dist-info/WHEEL +5 -0
- fwforge-0.1.0.dist-info/entry_points.txt +2 -0
- fwforge-0.1.0.dist-info/licenses/LICENSE +21 -0
- fwforge-0.1.0.dist-info/top_level.txt +1 -0
fwforge/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .cli import main
|
fwforge/__main__.py
ADDED
fwforge/cli.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
import yaml
|
|
4
|
+
import os
|
|
5
|
+
from .fw_parser import FixedWidthParser
|
|
6
|
+
from .writer import write_csv, write_json
|
|
7
|
+
|
|
8
|
+
def infer_layout(filepath):
|
|
9
|
+
try:
|
|
10
|
+
with open(filepath, 'r', encoding='utf-8', errors='replace') as f:
|
|
11
|
+
line = f.readline().rstrip('\n\r')
|
|
12
|
+
|
|
13
|
+
columns = []
|
|
14
|
+
import re
|
|
15
|
+
for match in re.finditer(r'\S+', line):
|
|
16
|
+
columns.append({
|
|
17
|
+
"name": f"col_{match.start()}",
|
|
18
|
+
"start": match.start(),
|
|
19
|
+
"length": match.end() - match.start(),
|
|
20
|
+
"trim": True,
|
|
21
|
+
"type": "string"
|
|
22
|
+
})
|
|
23
|
+
return {"name": "InferredLayout", "columns": columns}
|
|
24
|
+
except FileNotFoundError:
|
|
25
|
+
print(f"Error: File '{filepath}' not found.", file=sys.stderr)
|
|
26
|
+
sys.exit(1)
|
|
27
|
+
|
|
28
|
+
def process_file(input_path, output, layout, format_type):
|
|
29
|
+
try:
|
|
30
|
+
column_names = [col['name'] for col in layout['columns']]
|
|
31
|
+
file_parser = FixedWidthParser(layout)
|
|
32
|
+
|
|
33
|
+
records = []
|
|
34
|
+
with open(input_path, 'r', encoding='utf-8', errors='replace') as f:
|
|
35
|
+
for line in f:
|
|
36
|
+
line = line.rstrip('\n\r')
|
|
37
|
+
if line.strip():
|
|
38
|
+
records.append(file_parser.parse_line(line))
|
|
39
|
+
|
|
40
|
+
if format_type == "csv":
|
|
41
|
+
write_csv(records, column_names, output)
|
|
42
|
+
else:
|
|
43
|
+
write_json(records, output)
|
|
44
|
+
|
|
45
|
+
except Exception as e:
|
|
46
|
+
print(f"Error processing {input_path}: {e}", file=sys.stderr)
|
|
47
|
+
finally:
|
|
48
|
+
if output is not sys.stdout:
|
|
49
|
+
output.close()
|
|
50
|
+
|
|
51
|
+
def main():
|
|
52
|
+
parser = argparse.ArgumentParser(description="FixedWidth Forge: Legacy data parser")
|
|
53
|
+
parser.add_argument("-i", "--input", required=True, help="Input file or directory")
|
|
54
|
+
parser.add_argument("-s", "--schema", help="YAML schema file")
|
|
55
|
+
parser.add_argument("-f", "--format", choices=["csv", "json"], default="csv")
|
|
56
|
+
parser.add_argument("-o", "--output", help="Output file or directory")
|
|
57
|
+
parser.add_argument("--infer", action="store_true", help="Infer schema from input file")
|
|
58
|
+
args = parser.parse_args()
|
|
59
|
+
|
|
60
|
+
if args.infer:
|
|
61
|
+
layout = infer_layout(args.input)
|
|
62
|
+
print(yaml.dump(layout))
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
if not args.schema:
|
|
66
|
+
parser.error("the following arguments are required: -s/--schema")
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
with open(args.schema, 'r') as f:
|
|
70
|
+
layout = yaml.safe_load(f)
|
|
71
|
+
except FileNotFoundError:
|
|
72
|
+
print(f"Error: Schema file '{args.schema}' not found.", file=sys.stderr)
|
|
73
|
+
sys.exit(1)
|
|
74
|
+
|
|
75
|
+
if os.path.isdir(args.input):
|
|
76
|
+
output_dir = args.output or args.input
|
|
77
|
+
if not os.path.exists(output_dir):
|
|
78
|
+
os.makedirs(output_dir)
|
|
79
|
+
|
|
80
|
+
for filename in os.listdir(args.input):
|
|
81
|
+
if filename.endswith((".txt", ".fwf")):
|
|
82
|
+
input_path = os.path.join(args.input, filename)
|
|
83
|
+
output_filename = os.path.splitext(filename)[0] + "." + args.format
|
|
84
|
+
output_path = os.path.join(output_dir, output_filename)
|
|
85
|
+
print(f"Processing {input_path} -> {output_path}")
|
|
86
|
+
try:
|
|
87
|
+
with open(output_path, 'w', encoding='utf-8', newline='') as out_file:
|
|
88
|
+
process_file(input_path, out_file, layout, args.format)
|
|
89
|
+
except Exception as e:
|
|
90
|
+
print(f"Error writing to {output_path}: {e}", file=sys.stderr)
|
|
91
|
+
else:
|
|
92
|
+
try:
|
|
93
|
+
output = open(args.output, 'w', encoding='utf-8', newline='') if args.output else sys.stdout
|
|
94
|
+
process_file(args.input, output, layout, args.format)
|
|
95
|
+
except FileNotFoundError:
|
|
96
|
+
print(f"Error: Input file '{args.input}' not found.", file=sys.stderr)
|
|
97
|
+
sys.exit(1)
|
|
98
|
+
except Exception as e:
|
|
99
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
100
|
+
sys.exit(1)
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
main()
|
fwforge/fw_parser.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from typing import Any, Dict, List
|
|
2
|
+
|
|
3
|
+
class FixedWidthParser:
|
|
4
|
+
def __init__(self, layout: Dict):
|
|
5
|
+
self.layout = layout
|
|
6
|
+
self.columns = layout.get('columns', [])
|
|
7
|
+
|
|
8
|
+
def parse_line(self, line: str) -> Dict[str, Any]:
|
|
9
|
+
record = {}
|
|
10
|
+
for col in self.columns:
|
|
11
|
+
start = col.get('start', 0)
|
|
12
|
+
# Support both length and end
|
|
13
|
+
if 'length' in col:
|
|
14
|
+
end = start + col['length']
|
|
15
|
+
elif 'end' in col:
|
|
16
|
+
end = col['end']
|
|
17
|
+
else:
|
|
18
|
+
end = start # Default to zero-length if neither provided
|
|
19
|
+
|
|
20
|
+
trim = col.get('trim', True)
|
|
21
|
+
|
|
22
|
+
if end > len(line):
|
|
23
|
+
val = line[start:].strip() if trim else line[start:]
|
|
24
|
+
else:
|
|
25
|
+
val = line[start:end]
|
|
26
|
+
if trim:
|
|
27
|
+
val = val.strip()
|
|
28
|
+
|
|
29
|
+
# Basic type conversion
|
|
30
|
+
record[col['name']] = self._cast_value(val, col.get('type', 'string'))
|
|
31
|
+
|
|
32
|
+
return record
|
|
33
|
+
|
|
34
|
+
def _cast_value(self, value: str, col_type: str) -> Any:
|
|
35
|
+
if not value:
|
|
36
|
+
return None if col_type in ('int', 'float') else ""
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
if col_type == 'int':
|
|
40
|
+
return int(value)
|
|
41
|
+
elif col_type == 'float':
|
|
42
|
+
return float(value)
|
|
43
|
+
elif col_type == 'string':
|
|
44
|
+
return value
|
|
45
|
+
except ValueError:
|
|
46
|
+
pass # fallback to string
|
|
47
|
+
return value
|
fwforge/writer.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
def write_csv(records, columns, output=sys.stdout):
|
|
6
|
+
writer = csv.DictWriter(output, fieldnames=columns)
|
|
7
|
+
writer.writeheader()
|
|
8
|
+
writer.writerows(records)
|
|
9
|
+
|
|
10
|
+
def write_json(records, output=sys.stdout):
|
|
11
|
+
json.dump(records, output, indent=2)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fwforge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fixed-width file parser for legacy logistics & supply chain data
|
|
5
|
+
Author-email: TallowX92 <tallow072@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/TallowX92/fwforge
|
|
8
|
+
Project-URL: Repository, https://github.com/TallowX92/fwforge
|
|
9
|
+
Project-URL: Issues, https://github.com/TallowX92/fwforge/issues
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: pyyaml>=6.0
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# FixedWidth Forge
|
|
17
|
+
|
|
18
|
+
[](https://pypi.org/project/fwforge/)
|
|
19
|
+
[](https://pypi.org/project/fwforge/)
|
|
20
|
+
[](https://github.com/TallowX92/fwforge)
|
|
21
|
+
|
|
22
|
+
**Parse legacy fixed-width files from logistics, warehouses, ERP systems, and mainframes — instantly.**
|
|
23
|
+
|
|
24
|
+
Turn messy carrier reports, shipment manifests, and COBOL exports into clean CSV or JSON with a simple YAML schema.
|
|
25
|
+
|
|
26
|
+
## Why Logistics & Supply Chain?
|
|
27
|
+
Legacy formats like **fixed-width flat files**, **EDI**, and proprietary mainframes are the glue of the supply chain. `fwforge` provides the bridge to modern data pipelines without the enterprise bloat.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
- **Schema-Driven**: Define column layouts in YAML. Supports `start+length` or `start+end` positions.
|
|
31
|
+
- **Ultra-Fast**: Built on Python principles for rapid parsing of multi-gigabyte flat files.
|
|
32
|
+
- **Batch Processing**: Process entire directories of manifest exports with one command.
|
|
33
|
+
- **Inference Engine**: Use `--infer` to automatically generate a baseline schema from a sample data file.
|
|
34
|
+
- **Clean Output**: Transform legacy data to clean CSV or JSON with built-in type casting.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install fwforge
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Install from source (latest dev):
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install git+https://github.com/TallowX92/fwforge.git
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
### 1. Infer a schema
|
|
51
|
+
Start from scratch with a sample file:
|
|
52
|
+
```bash
|
|
53
|
+
fwforge --infer -i data.txt > my-layout.yaml
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 2. Parse data
|
|
57
|
+
Convert legacy data to CSV (using the included sample):
|
|
58
|
+
```bash
|
|
59
|
+
fwforge -i data.txt -s layout.yaml -f csv -o output.csv
|
|
60
|
+
cat output.csv
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Batch process a folder
|
|
64
|
+
```bash
|
|
65
|
+
fwforge -i ./daily_manifests/ -s manifest.yaml -f json
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Example Schema (`layout.yaml`)
|
|
69
|
+
```yaml
|
|
70
|
+
name: "Freight-Manifest-v1"
|
|
71
|
+
columns:
|
|
72
|
+
- name: "carrier_code"
|
|
73
|
+
start: 0
|
|
74
|
+
length: 5
|
|
75
|
+
trim: true
|
|
76
|
+
type: "string"
|
|
77
|
+
- name: "weight"
|
|
78
|
+
start: 20
|
|
79
|
+
length: 10
|
|
80
|
+
trim: true
|
|
81
|
+
type: "float"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
# Clone and setup
|
|
88
|
+
git clone https://github.com/TallowX92/fwforge.git
|
|
89
|
+
cd fwforge
|
|
90
|
+
uv sync
|
|
91
|
+
uv run pytest -v
|
|
92
|
+
|
|
93
|
+
# Run CLI
|
|
94
|
+
uv run fwforge --help
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Roadmap
|
|
98
|
+
|
|
99
|
+
- More robust type casting (dates, currency, custom)
|
|
100
|
+
- Schema validation + strict mode
|
|
101
|
+
- Better inference (header detection, multi-line records)
|
|
102
|
+
- Performance / memory improvements for GB+ files
|
|
103
|
+
- Standalone binary releases
|
|
104
|
+
- Expanded output formats (parquet, etc.)
|
|
105
|
+
|
|
106
|
+
## Changelog
|
|
107
|
+
|
|
108
|
+
See [CHANGELOG.md](CHANGELOG.md) for release notes.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
fwforge/__init__.py,sha256=ucQESw8DmMUlgbIvhowpcUSqVmdq9NvqLm_U5RE1nuA,22
|
|
2
|
+
fwforge/__main__.py,sha256=MSmt_5Xg84uHqzTN38JwgseJK8rsJn_11A8WD99VtEo,61
|
|
3
|
+
fwforge/cli.py,sha256=z4e8uaj4yRPGKR0jhsBa1ullMyiMZWWpRd_g0yayfEg,3934
|
|
4
|
+
fwforge/fw_parser.py,sha256=xVM38s-MmVHM2q1sRxEp7-0XMKwUgUq-Rplm-zzJWNs,1518
|
|
5
|
+
fwforge/writer.py,sha256=FVbY8LP6JmqUPjM2pMwJE_v3cU0afRzaQc5iQWZRFac,283
|
|
6
|
+
fwforge-0.1.0.dist-info/licenses/LICENSE,sha256=dN27ms-wIIG1x5nW55cTtKDhCwoFiM7DyFLO46e2gh4,1066
|
|
7
|
+
fwforge-0.1.0.dist-info/METADATA,sha256=kmCPueCDVyZ_kU_LugzzMQexYnytJ_TxmO6d_HtQnfU,3116
|
|
8
|
+
fwforge-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
9
|
+
fwforge-0.1.0.dist-info/entry_points.txt,sha256=0n-ltx9L2ozaYuBwX2EUuLdaKGQzzrn46fAa83ek_fQ,45
|
|
10
|
+
fwforge-0.1.0.dist-info/top_level.txt,sha256=INT0qWw-6pwgoLl24bpM-wZyUDfyqRBZf48rHzWeMPs,8
|
|
11
|
+
fwforge-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TallowX92
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fwforge
|