Skip to content

Commit 30c2aca

Browse files
committed
Tidied up validate command
1 parent f342c72 commit 30c2aca

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ transcript = Transcript.from_path(<path to xml file>)
1717
```
1818

1919
```bash
20-
python -m validate_transcript <path-to-transcript.xml>
20+
python -m mysoc_validator validate --path <path-to-transcript.xml> --type transcript
2121
```
2222

2323
## Popolo
@@ -39,7 +39,7 @@ popolo = Popolo.from_path(<path to people.json>)
3939
```
4040

4141
```bash
42-
python -m validate_popolo <path-to-people.json>
42+
python -m mysoc_validator validate --path <path-to-people.json> --type popolo
4343
```
4444

4545
### People.json

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ include = [
1313

1414
[tool.poetry_bumpversion.file."src/mysoc_popolo/__init__.py"]
1515

16+
[tool.poetry.scripts]
17+
mysoc-validator = "mysoc_validator.__main__:app"
18+
1619
[tool.poetry.dependencies]
1720
python = "^3.9"
1821
pydantic = "^2.7.1"

src/mysoc_validator/__main__.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from enum import Enum
12
from pathlib import Path
3+
from typing import Optional
24

35
import rich
46
import typer
@@ -8,9 +10,40 @@
810

911
app = typer.Typer()
1012

13+
class ValidateOptions(str, Enum):
14+
POPOLO = "popolo"
15+
TRANSCRIPT = "transcript"
1116

1217
@app.command()
13-
def validate_popolo(file: Path):
18+
def blank():
19+
"""
20+
Holding command to make 'validate' not the default.
21+
"""
22+
pass
23+
24+
25+
@app.command()
26+
def validate(file: Optional[Path] = None, url: Optional[str] = None, type: ValidateOptions=ValidateOptions.POPOLO):
27+
# must be at least one of file or url, but not both
28+
if not file and not url:
29+
typer.echo("Must provide either a file or a URL.")
30+
raise typer.Exit(code=1)
31+
if file and url:
32+
typer.echo("Must provide either a file or a URL, not both.")
33+
raise typer.Exit(code=1)
34+
if type == ValidateOptions.POPOLO:
35+
if file:
36+
validate_popolo_file(file)
37+
if url:
38+
validate_popolo_url_file(url)
39+
elif type == ValidateOptions.TRANSCRIPT:
40+
if not file:
41+
typer.echo("Must provide a file for a transcript.")
42+
raise typer.Exit(code=1)
43+
validate_transcript(file)
44+
45+
46+
def validate_popolo_file(file: Path):
1447
"""
1548
Validate a mysoc style Popolo file.
1649
Returns Exit 1 if a validation error.
@@ -26,9 +59,7 @@ def validate_popolo(file: Path):
2659
)
2760
rich.print("[green]Valid Popolo file[/green]")
2861

29-
30-
@app.command()
31-
def validate_popolo_url(url: str):
62+
def validate_popolo_url_file(url: str):
3263
"""
3364
Validate a mysoc style Popolo file.
3465
Returns Exit 1 if a validation error.
@@ -45,7 +76,6 @@ def validate_popolo_url(url: str):
4576
rich.print("[green]Valid Popolo file[/green]")
4677

4778

48-
@app.command()
4979
def validate_transcript(file: Path):
5080
"""
5181
Validate a mysoc style Popolo file.

0 commit comments

Comments
 (0)