-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
216 lines (170 loc) · 6.83 KB
/
Copy pathmain.py
File metadata and controls
216 lines (170 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
"""
Brazilian Law Parser CLI
Parses consolidated Brazilian laws from planalto.gov.br into structured JSON
with full legislative history (revocations, modifications, additions).
"""
import json
import re
import sys
from datetime import datetime
from pathlib import Path
import click
from models.law import LawMeta, ParsedLaw, TestedLaw, TestedLawsRegistry
from parser.html_parser import parse_html, extract_meta_from_soup
from parser.classifier import classify_provisions
def parse_law_identifier(title: str) -> dict:
"""Parse law type, number, and year from title."""
result = {
"law_type": "lei",
"number": "",
"year": 0,
}
# Normalize whitespace (newlines break regex)
title_clean = re.sub(r"\s+", " ", title)
title_upper = title_clean.upper()
# Special case: Constitution
if "CONSTITUI" in title_upper and "EMENDA" not in title_upper:
year_match = re.search(r"(\d{4})", title)
result["law_type"] = "constituicao"
result["number"] = "1"
result["year"] = int(year_match.group(1)) if year_match else 1988
return result
# Match patterns like "LEI Nº 8.213, DE 24 DE JULHO DE 1991"
patterns = [
(r"LEI\s+COMPLEMENTAR\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "lei-complementar"),
(r"LEI\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "lei"),
(r"DECRETO-LEI\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "decreto-lei"),
(r"DECRETO\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "decreto"),
(r"MEDIDA\s+PROVIS[ÓO]RIA\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "medida-provisoria"),
(r"EMENDA\s+CONSTITUCIONAL\s+N[º°o]?\s*([\d\.]+).*?(\d{4})", "emenda-constitucional"),
]
for pattern, law_type in patterns:
match = re.search(pattern, title_upper)
if match:
result["law_type"] = law_type
result["number"] = match.group(1)
result["year"] = int(match.group(2))
break
return result
def generate_law_id(law_type: str, number: str, year: int) -> str:
"""Generate normalized law ID."""
# Normalize number (remove dots)
num_clean = number.replace(".", "")
return f"{law_type}-{num_clean}-{year}"
@click.group()
def cli():
"""Brazilian Law Parser - Parse consolidated laws from planalto.gov.br"""
pass
def _minimal_provision(p) -> dict:
"""Convert provision to minimal dict, excluding null/empty fields."""
d = {"id": p.id, "t": p.text}
if p.hierarchy:
d["h"] = p.hierarchy
if p.modified_by:
d["mod"] = p.modified_by.law
if p.added_by:
d["add"] = p.added_by.law
return d
@cli.command()
@click.argument("file_path", type=click.Path(exists=True))
@click.option("--output", "-o", type=click.Path(), default="output",
help="Output directory for JSON files")
@click.option("--encoding", "-e", default="windows-1252",
help="Source file encoding")
def parse(file_path: str, output: str, encoding: str):
"""Parse a Brazilian law HTML file into structured JSON."""
file_path = Path(file_path)
output_dir = Path(output)
output_dir.mkdir(parents=True, exist_ok=True)
click.echo(f"Parsing {file_path}...")
# Parse HTML
soup, raw_provisions = parse_html(file_path, encoding)
click.echo(f" Found {len(raw_provisions)} raw provisions")
# Extract metadata
meta_raw = extract_meta_from_soup(soup)
law_info = parse_law_identifier(meta_raw.get("title", "") or file_path.stem)
# Build law ID
law_id = generate_law_id(law_info["law_type"], law_info["number"], law_info["year"])
# Classify provisions
provisions = classify_provisions(raw_provisions)
click.echo(f" Classified {len(provisions)} provisions")
# Count statuses
in_force = [p for p in provisions if p.status.value == "in_force"]
revoked = [p for p in provisions if p.status.value == "revoked"]
vetoed = [p for p in provisions if p.status.value == "vetoed"]
click.echo(f" - in_force: {len(in_force)}")
if revoked:
click.echo(f" - revoked: {len(revoked)} (excluded)")
if vetoed:
click.echo(f" - vetoed: {len(vetoed)} (excluded)")
# Build minimal output (only in_force)
output_data = {
"id": law_id,
"title": re.sub(r"\s+", " ", meta_raw.get("title") or file_path.stem).strip(),
"provisions": [_minimal_provision(p) for p in in_force],
}
# Write compact JSON
output_file = output_dir / f"{law_id}.json"
with open(output_file, "w", encoding="utf-8") as f:
json.dump(output_data, f, ensure_ascii=False, separators=(",", ":"))
size_kb = output_file.stat().st_size / 1024
click.echo(f" Output: {output_file} ({size_kb:.1f} KB)")
# Update tested laws registry
_update_tested_laws(law_id, meta_raw.get("title", law_id), meta_raw.get("source_url", ""))
click.echo("Done!")
def _update_tested_laws(law_id: str, name: str, url: str):
"""Update the tested_laws.json registry."""
registry_path = Path(__file__).parent / "tested_laws.json"
if registry_path.exists():
with open(registry_path, "r", encoding="utf-8") as f:
data = json.load(f)
registry = TestedLawsRegistry(**data)
else:
registry = TestedLawsRegistry()
# Check if already exists
existing = next((t for t in registry.tested if t.law_id == law_id), None)
if existing:
# Update existing entry
existing.tested_at = datetime.now().strftime("%Y-%m-%d")
existing.status = "working"
else:
# Add new entry
new_entry = TestedLaw(
law_id=law_id,
name=name,
url=url or "",
tested_at=datetime.now().strftime("%Y-%m-%d"),
tested_by="parser",
status="working",
notes="Auto-added on successful parse",
)
registry.tested.append(new_entry)
# Write back
with open(registry_path, "w", encoding="utf-8") as f:
json.dump(registry.model_dump(), f, ensure_ascii=False, indent=2)
@cli.command()
def list_tested():
"""List all tested laws."""
registry_path = Path(__file__).parent / "tested_laws.json"
if not registry_path.exists():
click.echo("No tested laws registry found.")
return
with open(registry_path, "r", encoding="utf-8") as f:
data = json.load(f)
registry = TestedLawsRegistry(**data)
if not registry.tested:
click.echo("No laws have been tested yet.")
return
click.echo("Tested Laws:")
click.echo("-" * 60)
for law in registry.tested:
status_icon = "✓" if law.status == "working" else "✗"
click.echo(f" {status_icon} {law.law_id}")
click.echo(f" Name: {law.name}")
click.echo(f" Tested: {law.tested_at}")
if law.notes:
click.echo(f" Notes: {law.notes}")
click.echo()
if __name__ == "__main__":
cli()