added support for json schemas
This commit is contained in:
20
schema.py
20
schema.py
@@ -1,18 +1,36 @@
|
||||
import json
|
||||
import os
|
||||
import yaml
|
||||
|
||||
from config import Config
|
||||
from renderer import Renderer
|
||||
from structure import Structure
|
||||
|
||||
class UnsupportedFormatException(Exception):
|
||||
...
|
||||
|
||||
class InstructionSetSchema:
|
||||
VALID_EXTENSIONS = ("yaml", "json")
|
||||
|
||||
def __init__(self, path: str, configPath: str = "config.json") -> None:
|
||||
self.config = Config(configPath)
|
||||
self.path = path
|
||||
self.load()
|
||||
|
||||
def load(self) -> None:
|
||||
_, ext = os.path.splitext(self.path)
|
||||
ext = ext[1:].lower()
|
||||
|
||||
if ext not in InstructionSetSchema.VALID_EXTENSIONS:
|
||||
fmts = tuple(map(lambda fmt: f".{fmt}", InstructionSetSchema.VALID_EXTENSIONS))
|
||||
raise UnsupportedFormatException(f"'.{ext}' files are not supported. Valid formats: {fmts}")
|
||||
|
||||
with open(self.path, "r") as f:
|
||||
schema = yaml.safe_load(f)
|
||||
if ext == "yaml":
|
||||
schema = yaml.safe_load(f)
|
||||
|
||||
elif ext == "json":
|
||||
schema = json.load(f)
|
||||
|
||||
self.structures = {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user