Don't save `id` in json files

Added helper method `write_json_file`
This commit is contained in:
mike cullerton 2022-11-18 14:19:29 -05:00
parent 48986e3392
commit c54aeace2d
1 changed files with 21 additions and 17 deletions

View File

@ -48,6 +48,11 @@ class ProcessModelService(FileSystemService):
return True return True
return False return False
@staticmethod
def write_json_file(file_path, json_data, indent=4, sort_keys=True):
with open(file_path, "w") as h_open:
json.dump(json_data, h_open, indent=indent, sort_keys=sort_keys)
@staticmethod @staticmethod
def get_batch( def get_batch(
items: list[T], items: list[T],
@ -84,10 +89,8 @@ class ProcessModelService(FileSystemService):
# this allows us to move models around on the filesystem # this allows us to move models around on the filesystem
# the id is determined by its location on the filesystem # the id is determined by its location on the filesystem
delattr(process_model, 'id') delattr(process_model, 'id')
with open(json_path, "w") as wf_json: json_data = self.PROCESS_MODEL_SCHEMA.dump(process_model)
json.dump( self.write_json_file(json_path, json_data)
self.PROCESS_MODEL_SCHEMA.dump(process_model), wf_json, indent=4, sort_keys=True
)
process_model.id = process_model_id process_model.id = process_model_id
def process_model_delete(self, process_model_id: str) -> None: def process_model_delete(self, process_model_id: str) -> None:
@ -216,13 +219,11 @@ class ProcessModelService(FileSystemService):
cat_path = self.process_group_path(process_group.id) cat_path = self.process_group_path(process_group.id)
os.makedirs(cat_path, exist_ok=True) os.makedirs(cat_path, exist_ok=True)
json_path = os.path.join(cat_path, self.PROCESS_GROUP_JSON_FILE) json_path = os.path.join(cat_path, self.PROCESS_GROUP_JSON_FILE)
with open(json_path, "w") as cat_json: serialized_process_group = process_group.serialized
json.dump( # we don't store `id` in the json files
process_group.serialized, # this allows us to move groups around on the filesystem
cat_json, del serialized_process_group['id']
indent=4, self.write_json_file(json_path, serialized_process_group)
sort_keys=True,
)
return process_group return process_group
def __get_all_nested_models(self, group_path: str) -> list: def __get_all_nested_models(self, group_path: str) -> list:
@ -293,6 +294,9 @@ class ProcessModelService(FileSystemService):
if os.path.exists(cat_path): if os.path.exists(cat_path):
with open(cat_path) as cat_json: with open(cat_path) as cat_json:
data = json.load(cat_json) data = json.load(cat_json)
# we don't store `id` in the json files, so we add it back in here
relative_path = os.path.relpath(dir_path, FileSystemService.root_path())
data['id'] = relative_path
process_group = ProcessGroup(**data) process_group = ProcessGroup(**data)
if process_group is None: if process_group is None:
raise ApiError( raise ApiError(
@ -302,13 +306,13 @@ class ProcessModelService(FileSystemService):
else: else:
process_group_id = dir_path.replace(FileSystemService.root_path(), "") process_group_id = dir_path.replace(FileSystemService.root_path(), "")
process_group = ProcessGroup( process_group = ProcessGroup(
id=process_group_id,
display_name=process_group_id, display_name=process_group_id,
display_order=10000, display_order=10000,
admin=False, admin=False,
) )
with open(cat_path, "w") as wf_json: self.write_json_file(cat_path, self.GROUP_SCHEMA.dump(process_group))
json.dump(self.GROUP_SCHEMA.dump(process_group), wf_json, indent=4) # we don't store `id` in the json files, so we add it in here
process_group.id = process_group_id
with os.scandir(dir_path) as nested_items: with os.scandir(dir_path) as nested_items:
process_group.process_models = [] process_group.process_models = []
process_group.process_groups = [] process_group.process_groups = []
@ -363,14 +367,14 @@ class ProcessModelService(FileSystemService):
) )
process_model_info = ProcessModelInfo( process_model_info = ProcessModelInfo(
id=name,
display_name=name, display_name=name,
description="", description="",
display_order=0, display_order=0,
is_review=False, is_review=False,
) )
with open(json_file_path, "w") as wf_json: self.write_json_file(json_file_path, self.PROCESS_MODEL_SCHEMA.dump(process_model_info))
json.dump(self.PROCESS_MODEL_SCHEMA.dump(process_model_info), wf_json, indent=4) # we don't store `id` in the json files, so we add it in here
process_model_info.id = name
if process_group: if process_group:
process_model_info.process_group = process_group.id process_model_info.process_group = process_group.id
return process_model_info return process_model_info