2023-10-21 17:51:38 +00:00
|
|
|
from jsonschema import validate
|
|
|
|
from json import dumps, loads
|
|
|
|
|
|
|
|
class PalleteSchema :
|
|
|
|
colors = None
|
|
|
|
schema = {
|
|
|
|
"type" : "array",
|
|
|
|
"items" : {
|
|
|
|
"type" : "object",
|
|
|
|
"properties" : {
|
|
|
|
"name" : { "type" : "string" },
|
|
|
|
"color" : {
|
|
|
|
"type" : "array",
|
|
|
|
"items" : { "type" : "number" }
|
2023-10-24 02:05:58 +00:00
|
|
|
},
|
|
|
|
"space" : { "type" : "string" }
|
2023-10-21 17:51:38 +00:00
|
|
|
},
|
2023-10-24 02:05:58 +00:00
|
|
|
"required" : [ "name", "color", "space" ]
|
2023-10-21 17:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-24 02:05:58 +00:00
|
|
|
def __init__ (self, file = None, obj = None):
|
|
|
|
if file is not None and obj is None:
|
2023-10-21 17:51:38 +00:00
|
|
|
self.parse_file(file)
|
2023-10-24 02:05:58 +00:00
|
|
|
else :
|
|
|
|
self.write(file, obj)
|
2023-10-21 17:51:38 +00:00
|
|
|
|
|
|
|
def parse_file (self, file) :
|
|
|
|
with open(file) as f :
|
|
|
|
self.parse(f.read())
|
|
|
|
print(f'Parsed pallete file {file}')
|
|
|
|
|
|
|
|
def parse (self, jsonstr) :
|
|
|
|
obj = loads(jsonstr)
|
|
|
|
validate( instance = obj, schema = self.schema)
|
2023-10-24 02:05:58 +00:00
|
|
|
self.colors = obj
|
|
|
|
|
|
|
|
def write(self, filepath, obj) :
|
|
|
|
validate( instance = obj, schema = self.schema)
|
|
|
|
jsonstr = dumps(obj, indent = 4)
|
|
|
|
with open(filepath, 'w') as outfile :
|
|
|
|
outfile.write(jsonstr)
|