Coverage for /opt/hostedtoolcache/Python/3.12.10/x64/lib/python3.12/site-packages/netflix_open_content_helper/parser.py: 100%
30 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 17:05 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 17:05 +0000
1import csv
2import pathlib
4import yaml
5from pydantic import TypeAdapter
7from netflix_open_content_helper.models import Shot
10def parse_shotfile(shotfile: str) -> list[Shot]:
11 shots = []
12 if shotfile.endswith(".yaml"):
13 shots = parse_shotfile_yaml(shotfile)
14 elif shotfile.endswith(".json"):
15 shots = parse_shotfile_json(shotfile)
16 elif shotfile.endswith(".csv"):
17 shots = parse_shotfile_csv(shotfile)
18 else:
19 raise ValueError(f"Unknown file type: {shotfile}")
20 return shots
23def parse_shotfile_csv(shotfile: str) -> list[Shot]:
24 with open(shotfile) as f:
25 reader = csv.DictReader(f)
26 shots = [Shot.model_validate(row) for row in reader]
27 return shots
30def parse_shotfile_yaml(shotfile: str) -> list[Shot]:
31 with open(shotfile) as stream:
32 yaml_contents = yaml.safe_load(stream)
33 shots = [Shot.model_validate(shot) for shot in yaml_contents]
34 return shots
37def parse_shotfile_json(shotfile: str) -> list[Shot]:
38 shot_list_adapter = TypeAdapter(list[Shot])
39 json_string = pathlib.Path(shotfile).read_text()
40 shots = shot_list_adapter.validate_json(json_string)
41 return shots