Skip to content

Commit

Permalink
Make json file path a parser parameter; fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
radishmouse committed Oct 2, 2024
1 parent a69a5a9 commit 46effb0
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions scripts/convert_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,63 @@

from notifications.lib.notification_model import NotificationModel

JSON_FILE_NAME = "notifications.json"
DEFAULT_JSON_FILE_PATH = 'json/notifications.json'

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 = 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(
"--overwrite", help="Overwrite existing JSON files", action="store_true"
'json_file_path',
type=str,
nargs='?',
default=DEFAULT_JSON_FILE_PATH,
help=f'File path of the JSON file to write (default: {DEFAULT_JSON_FILE_PATH})',
)
args = parser.parse_args()


class YAMLtoJSONConverter:
def __init__(self, yaml_dir, json_dir):
def __init__(self, yaml_dir, json_file_path):
self.yaml_dir = yaml_dir
self.json_dir = json_dir
self.json_file_path = json_file_path

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)
raise ValueError(f"Argument for yaml_dir '{self.yaml_dir}' is not a directory")
json_dir = os.path.dirname(self.json_file_path)
if not os.path.exists(json_dir):
os.makedirs(json_dir, exist_ok=True)

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":
if ext.lower() != '.yaml':
return None
return f"{os.path.basename(base_name)}.json"
return f'{os.path.basename(base_name)}.json'

def write_schema_as_json(self, schema, json_file):
try:
dump = schema.model_dump_json(indent=2)
json_file.write(dump)
except Exception as e:
print(f"Error writing JSON file: {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."""

schema = NotificationModel.from_yaml_dir(self.yaml_dir)
json_file_path = os.path.join(self.json_dir, JSON_FILE_NAME)
with open(json_file_path, "w") as f:
print(f"Writing JSON to: {json_file_path}")

with open(self.json_file_path, 'w') as f:
print(f'Writing JSON to: {self.json_file_path}')
pprint(schema)
self.write_schema_as_json(schema, f)


def main():
if len(sys.argv) < 3 or len(sys.argv) > 4:
if len(sys.argv) < 2 or len(sys.argv) > 4:
parser.print_help()
sys.exit(1)

converter = YAMLtoJSONConverter(args.yaml_dir, args.json_dir)
args = parser.parse_args()
converter = YAMLtoJSONConverter(args.yaml_dir, args.json_file_path)
converter.convert()


if __name__ == "__main__":
if __name__ == '__main__':
main()

0 comments on commit 46effb0

Please sign in to comment.