-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring
cript.API._is_node_schema_valid
taking the getting of node type from JSON and putting it into its own function that can be easily called and make the code cleaner
- Loading branch information
Showing
3 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# trunk-ignore-all(ruff/F401) | ||
|
||
from .get_host_token import resolve_host_and_token | ||
from .helper_functions import _get_node_type_from_json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
from typing import Dict, List, Union | ||
|
||
from cript.nodes.exceptions import CRIPTJsonNodeError | ||
from cript.nodes.util import _is_node_field_valid | ||
|
||
|
||
def _get_node_type_from_json(node_json: Union[Dict, str]) -> str: | ||
""" | ||
takes a node JSON and output the node_type `Project`, `Material`, etc. | ||
1. convert node JSON dict or str to dict | ||
1. do check the node list to be sure it only has a single type in it | ||
1. get the node type and return it | ||
Parameters | ||
---------- | ||
node_json: [Dict, str] | ||
Notes | ||
----- | ||
Takes a str or dict to be more versatile | ||
Returns | ||
------- | ||
str: | ||
node type | ||
""" | ||
# convert all JSON node strings to dict for easier handling | ||
if isinstance(node_json, str): | ||
node_json: Dict = json.loads(node_json) | ||
|
||
try: | ||
node_type_list: List[str] = node_json["node"] | ||
except KeyError: | ||
raise CRIPTJsonNodeError(node_list=node_json["node"], json_str=json.dumps(node_json)) | ||
|
||
# check to be sure the node list has a single type "node": ["Material"] | ||
if _is_node_field_valid(node_type_list=node_type_list): | ||
return node_type_list[0] | ||
|
||
# if invalid then raise error | ||
else: | ||
raise CRIPTJsonNodeError(node_list=node_type_list, json_str=str(node_json)) |