Skip to content

Commit

Permalink
Merge pull request #5 from thunderbird/features/4-yaml-to-json-pipeline
Browse files Browse the repository at this point in the history
Features/4 yaml to json pipeline
  • Loading branch information
radishmouse authored Jun 7, 2024
2 parents 56c444f + 29d0531 commit e2a2c72
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
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
16 changes: 16 additions & 0 deletions .yamllint
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
91 changes: 91 additions & 0 deletions NotificationSchema.py
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))

68 changes: 68 additions & 0 deletions convert-yaml.py
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()

17 changes: 17 additions & 0 deletions generate-json-schema.py
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()

5 changes: 5 additions & 0 deletions requirements.txt
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

0 comments on commit e2a2c72

Please sign in to comment.