Skip to content
Snippets Groups Projects
Commit e9d48875 authored by Yan Sushchynski (EPAM)'s avatar Yan Sushchynski (EPAM)
Browse files

GONRG-1183: Add script to replace invalid values in schemas

parent f2a67962
No related branches found
No related tags found
1 merge request!6R3 Data Ingestion
import glob
import json
import os
import re
from collections import UserString
TENANT = "opendes"
AUTHORITY = "osdu"
SCHEMAS_DIR = os.environ["SCHEMAS_DIR"]
class JsonString(UserString):
REF_REGEXP = r"(?P<abstract_perfix>\.\.\/abstract/)(?P<kind_name>\w+)\.(?P<version>\d+\.\d+\.\d+)\.json"
NAMESPACE_REGEXP = r"\<namespace\>"
def repl_closure(self, match: re.match):
if not match.groups:
print(self.data)
raise Exception
kind_name = match.group('kind_name')
version = match.group('version')
repl = f"{TENANT}:{AUTHORITY}:{kind_name}:{version}"
return repl
def replace_refs(self):
self.data = re.sub(self.REF_REGEXP, self.repl_closure, self.data)
return self
def replace_namespaces(self):
self.data = re.sub(self.NAMESPACE_REGEXP, TENANT, self.data)
return self
@staticmethod
def lower_first_letter(val: str):
if val[0].islower():
pass
elif val in (
"ACL",
"Legals",
"ID"
):
val = val.lower()
else:
val = val.replace(val[0], val[0].lower(), 1)
return val
def to_pascal_case(self):
tmp_properties = {}
tmp_required = []
json_file_dict = json.loads(self.data)
try:
if "schemaInfo" in json_file_dict: # if schema has additional fields to be recorded
content = json_file_dict["schema"]
else:
content = json_file_dict
if "properties" in content:
for key, value in content["properties"].items():
tmp_properties[self.lower_first_letter(key)] = value
content["properties"] = tmp_properties
if "required" in content:
for i in content["required"]:
tmp_required.append(self.lower_first_letter(i))
content["required"] = tmp_required
self.data = json.dumps(json_file_dict, indent=4)
return self
except Exception as e:
print(self.data)
raise e
for file_path in glob.glob(SCHEMAS_DIR + "/*.json"):
try:
with open(file_path, "r") as file:
content = file.read()
content = JsonString(content).replace_refs().replace_namespaces().to_pascal_case().data
with open(file_path, "w") as file:
file.write(content)
except Exception as e:
print(f"Error on file {file_path}")
raise e
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment