-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from thunderbird/features/4-yaml-to-json-pipeline
Features/4 yaml to json pipeline
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.vscode | ||
__pycache__ | ||
.pytest_cache | ||
dist | ||
*.db | ||
*.ini | ||
*.log | ||
*credentials.json | ||
*.pickle | ||
.env | ||
venv | ||
.idea | ||
*.egg-info | ||
.coverage | ||
htmlcov | ||
caldav | ||
|
||
# Mac noise | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
extends: default | ||
|
||
rules: | ||
document-start: disable | ||
commas: | ||
level: warning | ||
empty-lines: disable | ||
indentation: | ||
level: warning | ||
check-multi-line-strings: false | ||
line-length: | ||
max: 95 | ||
level: warning | ||
trailing-spaces: | ||
level: warning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import yaml | ||
import json | ||
from enum import Enum | ||
from typing import Optional | ||
from uuid import UUID | ||
from datetime import datetime | ||
from pydantic import BaseModel, Field, HttpUrl, RootModel, model_validator | ||
|
||
class ChannelEnum(str, Enum): | ||
default = "default" | ||
esr = "esr" | ||
release = "release" | ||
beta = "beta" | ||
daily = "daily" | ||
|
||
class OperatingSystemEnum(str, Enum): | ||
win = "win" | ||
macosx = "macosx" | ||
linux = "linux" | ||
freebsd = "freebsd" | ||
openbsd = "openbsd" | ||
netbsd = "netbsd" | ||
solaris = "solaris" | ||
other = "other" | ||
|
||
class Profile(BaseModel): | ||
locales: list[str] | None = None | ||
versions: list[str] | None = None | ||
channels: list[ChannelEnum] | None = None | ||
operating_systems: list[OperatingSystemEnum] | None = None | ||
|
||
class SeverityEnum(int, Enum): | ||
one = 1 | ||
two = 2 | ||
three = 3 | ||
four = 4 | ||
five = 5 | ||
|
||
class TypeEnum(str, Enum): | ||
donation = "donation" | ||
message = "message" | ||
security = "security" | ||
blog = "blog" | ||
|
||
class Targeting(BaseModel): | ||
percent_chance: Optional[float] = Field(None, ge=0, le=100) | ||
exclude: list[Profile] | None = None | ||
include: list[Profile] | None = None | ||
|
||
class Notification(BaseModel): | ||
id: UUID | ||
start_at: datetime | ||
end_at: datetime | ||
title: str | ||
description: str | ||
URL: HttpUrl | None = None | ||
CTA: str | None = None | ||
severity: SeverityEnum | ||
type: TypeEnum | ||
targeting: Targeting | ||
|
||
@model_validator(mode="after") | ||
def cta_requires_url(self): | ||
if self.CTA and not self.URL: | ||
raise ValueError("if 'CTA' is present, 'URL' must be present too") | ||
return self | ||
|
||
class NotificationSchema(RootModel): | ||
root: list[Notification] | ||
|
||
def yaml_to_data(yaml_str): | ||
"""Static method to load YAML from a string and return the corresponding python object""" | ||
try: | ||
return yaml.safe_load(yaml_str) | ||
except yaml.YAMLError as e: | ||
print(f'Error parsing YAML file: {e}') | ||
return None | ||
|
||
def from_yaml(file_name): | ||
"""Static method to generate NotificationSchema from a yaml file""" | ||
with open(file_name, "r") as fh: | ||
contents = fh.read() | ||
data = NotificationSchema.yaml_to_data(contents) | ||
return NotificationSchema(data) | ||
|
||
def generate_json_schema(schema_file_name): | ||
"""Static method to write a JSON schema file based on pydantic model""" | ||
schema = NotificationSchema.model_json_schema() | ||
with open(schema_file_name, "w") as f: | ||
f.write(json.dumps(schema, indent=2)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import glob | ||
import os | ||
import sys | ||
import argparse | ||
|
||
from NotificationSchema import NotificationSchema | ||
|
||
parser = argparse.ArgumentParser(description='Converts Thunderbird notifications from YAML to JSON.') | ||
parser.add_argument('yaml_dir', type=str, help='Directory containing notifications as YAML files') | ||
parser.add_argument('json_dir', type=str, help='Directory to output JSON files') | ||
parser.add_argument('--overwrite', help='Overwrite existing JSON files', action='store_true') | ||
args = parser.parse_args() | ||
|
||
class YAMLtoJSONConverter: | ||
def __init__(self, yaml_dir, json_dir): | ||
self.yaml_dir = yaml_dir | ||
self.json_dir = json_dir | ||
|
||
if not os.path.isdir(self.yaml_dir): | ||
raise ValueError(f"Argument for yaml_dir '{self.yaml_dir}' is not a directory") | ||
if not os.path.isdir(self.json_dir): | ||
os.makedirs(self.json_dir) | ||
|
||
def get_yaml_file_paths(self): | ||
# Using glob's ** pattern by specifying `recursive=True` | ||
yaml_file_paths = glob.glob(f'./{self.yaml_dir}/**/*.yaml', recursive=True) | ||
return yaml_file_paths | ||
|
||
def generate_json_file_name(self, yaml_file_path): | ||
# Extract and verify the file extension | ||
base_name, ext = os.path.splitext(yaml_file_path) | ||
if ext.lower() != '.yaml': | ||
return None | ||
return f'{os.path.basename(base_name)}.json' | ||
|
||
def write_schema_as_json(self, schema, json_file): | ||
try: | ||
json_file.write(schema.model_dump_json(indent=2)) | ||
except Exception as e: | ||
print(f'Error writing JSON file: {e}') | ||
|
||
def convert(self): | ||
"""Reads, validates YAML files from a directory and writes JSON files to separate directory.""" | ||
for yaml_file_path in self.get_yaml_file_paths(): | ||
schema = NotificationSchema.from_yaml(yaml_file_path) | ||
|
||
json_file_name = self.generate_json_file_name(yaml_file_path) | ||
if not json_file_name: | ||
continue | ||
|
||
json_file_path = os.path.join(self.json_dir, json_file_name) | ||
does_exist = os.path.exists(json_file_path) | ||
should_write = args.overwrite or not does_exist | ||
if should_write: | ||
with open(json_file_path, "w") as f: | ||
self.write_schema_as_json(schema, f) | ||
|
||
def main(): | ||
if len(sys.argv) < 3 or len(sys.argv) > 4: | ||
parser.print_help() | ||
sys.exit(1) | ||
|
||
converter = YAMLtoJSONConverter(args.yaml_dir, args.json_dir) | ||
converter.convert() | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import argparse | ||
import sys | ||
from NotificationSchema import NotificationSchema | ||
|
||
parser = argparse.ArgumentParser(description='Generates JSON Schema file.') | ||
parser.add_argument('schema_file_name', type=str, help='Name of file to write') | ||
args = parser.parse_args() | ||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
parser.print_help() | ||
sys.exit(1) | ||
NotificationSchema.generate_json_schema(args.schema_file_name) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
annotated-types==0.7.0 | ||
pydantic==2.7.3 | ||
pydantic_core==2.18.4 | ||
PyYAML==6.0.1 | ||
typing_extensions==4.12.1 |