File size: 4,478 Bytes
82ea528 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import json
from ..core import CONFIG, any, JSON_WIDGET, CATEGORY, STRING, INT, FLOAT, BOOLEAN, logger, get_nested_value
# class CParameter:
# def __init__(self):
# pass
#
# @classmethod
# def INPUT_TYPES(cls):
# return {
# "required": {
# },
# "optional": {
# "path_to_json": STRING,
# "key": STRING,
# "default": STRING,
# },
# }
#
# CATEGORY = CATEGORY.MAIN.value + CATEGORY.UTILS.value
# INPUT_IS_LIST = False
#
# RETURN_TYPES = (any,)
# RETURN_NAMES = ("any",)
#
# FUNCTION = "execute"
#
# def execute(self, path_to_json=None, key=True, default=None):
# text = default
# value = text
#
# if path_to_json is not None and path_to_json != "":
# logger.debug(f"External parameter from: '{path_to_json}'")
# try:
# with open(path_to_json, 'r') as file:
# data = json.load(file)
# logger.debug(f"File found, data: '{data}'")
#
# result = get_value(data, key, default)
# text = result["text"]
# value = result["value"]
#
# except Exception as e:
# logger.error(e)
# text = f"Error reading file: {e}\nReturning default value: '{default}'"
# value = default
#
# return {"ui": {"text": [text]}, "result": [value]}
class CJsonFile:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
},
"optional": {
"path_to_json": STRING,
},
}
CATEGORY = CATEGORY.MAIN.value + CATEGORY.UTILS.value
INPUT_IS_LIST = False
RETURN_TYPES = ("JSON",)
RETURN_NAMES = ("json",)
FUNCTION = "execute"
def IS_CHANGED(path_to_json=None):
return True
def execute(self, path_to_json=None):
text = ""
data = {}
if path_to_json is not None and path_to_json != "":
logger.debug(f"Open json file: '{path_to_json}'")
try:
with open(path_to_json, 'r') as file:
data = json.load(file)
text = json.dumps(data, indent=CONFIG["indent"])
logger.debug(f"File found, data: '{str(data)}'")
except Exception as e:
logger.error(e)
text = f"Error reading file: {e}"
return {"ui": {"text": [text]}, "result": [data]}
class CJsonExtractor:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"json": JSON_WIDGET,
},
"optional": {
"key": STRING,
"default": STRING,
},
}
CATEGORY = CATEGORY.MAIN.value + CATEGORY.UTILS.value
INPUT_IS_LIST = False
RETURN_TYPES = (any, "STRING", "INT", "FLOAT", "BOOLEAN")
RETURN_NAMES = ("any", "string", "int", "float", "boolean")
# OUTPUT_IS_LIST = (False,)
FUNCTION = "execute"
def execute(cls, json=None, key=True, default=None):
result = get_value(json, key, default)
result["any"] = result["value"]
try:
result["string"] = str(result["value"])
except Exception as e:
result["string"] = result["value"]
try:
result["int"] = int(result["value"])
except Exception as e:
result["int"] = result["value"]
try:
result["float"] = float(result["value"])
except Exception as e:
result["float"] = result["value"]
try:
result["boolean"] = result["value"].lower() == "true"
except Exception as e:
result["boolean"] = result["value"]
return {
"ui": {"text": [result["text"]]},
"result": [
result["any"],
result["string"],
result["int"],
result["float"],
result["boolean"]
]
}
def get_value(data, key, default=None):
text = ""
val = ""
if key is not None and key != "":
val = get_nested_value(data, key, default)
if default != val:
text = f"Key found, return value: '{val}'"
else:
text = f"Key no found, return default value: '{val}'"
else:
text = f"Key is empty, return default value: '{val}'"
return {
"text": text,
"value": val
}
|