Skip to content

Commit 56565ea

Browse files
committed
Add ability to load from remote URL
1 parent 065dcdb commit 56565ea

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

poetry.lock

+160-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typing-extensions = "^4.11.0"
2020
rich = "^13.7.1"
2121
typer = "^0.12.3"
2222
lxml = "^5.3.0"
23+
requests = "^2.32.3"
2324

2425
[tool.poetry.group.dev.dependencies]
2526
pytest = "^7.1.2"

src/mysoc_validator/__main__.py

+18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ def validate_popolo(file: Path):
2727
rich.print("[green]Valid Popolo file[/green]")
2828

2929

30+
@app.command()
31+
def validate_popolo_url(url: str):
32+
"""
33+
Validate a mysoc style Popolo file.
34+
Returns Exit 1 if a validation error.
35+
"""
36+
try:
37+
people = Popolo.from_url(url)
38+
except Exception as e:
39+
typer.echo(f"Error: {e}")
40+
rich.print("[red]Invalid Popolo file[/red]")
41+
raise typer.Exit(code=1)
42+
print(
43+
f"Loaded {len(people.organizations)} organizations, {len(people.posts)} posts, {len(people.persons)} people, and {len(people.memberships)} memberships."
44+
)
45+
rich.print("[green]Valid Popolo file[/green]")
46+
47+
3048
@app.command()
3149
def validate_transcript(file: Path):
3250
"""

src/mysoc_validator/models/popolo.py

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
get_origin,
2222
)
2323

24+
import requests
2425
from mysoc_validator.models.dates import ApproxDate, FixedDate
2526
from pydantic import (
2627
AliasChoices,
@@ -1138,6 +1139,10 @@ def model_post_init(self, __context: Any) -> None:
11381139
def from_path(cls, json_path: Path) -> Popolo:
11391140
return cls.model_validate_json(json_path.read_text())
11401141

1142+
@classmethod
1143+
def from_url(cls, url: str) -> Popolo:
1144+
return cls.model_validate_json(requests.get(url).text)
1145+
11411146
def to_path(self, json_path: Path) -> None:
11421147
data = self.model_dump_json(
11431148
indent=2, exclude_unset=True, exclude_defaults=True, exclude_none=True

0 commit comments

Comments
 (0)