no longer using orjson
This commit is contained in:
1 parent
f041346ad3
commit
e68a357e94
10 files changed
+36
-121
No files matched your search
+2
-4
@@ -9,9 +9,6 @@ WORKDIR /data
|
||||
# <--- System Wide Dependencies -->
|
||||
ENV LANG C.UTF-8
|
||||
RUN apk add --no-cache \
|
||||
cargo \
|
||||
rust \
|
||||
gcompat \
|
||||
nodejs \
|
||||
npm \
|
||||
build-base \
|
||||
@@ -24,8 +21,9 @@ RUN pip install . /data
|
||||
|
||||
# <--- Webapp Build -->
|
||||
COPY webapp/ /data/web-builder
|
||||
RUN cd /data/web-builder && npm install
|
||||
RUN cd /data/web-builder && \
|
||||
npm i -g yarn \
|
||||
npm install && \
|
||||
yarn build && \
|
||||
cp -r /data/web-builder/build /data/web && \
|
||||
rm -rf /data/web-builder
|
||||
|
||||
@@ -9,7 +9,6 @@ setuptools.setup(
|
||||
"pydantic==1.8.2",
|
||||
"uvicorn[standard]==0.15.0",
|
||||
"python-decouple==3.5",
|
||||
"orjson==3.6.4",
|
||||
],
|
||||
extras_require={
|
||||
"dev": [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import Iterable, Union
|
||||
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG, json_dumps, normalize_obj
|
||||
from src.yaml_serializer.types import SecretValue
|
||||
from src.json_serializer import json_dumps, normalize_obj
|
||||
from src.yaml_serializer.types import SecretValue, NOT_IMPLEMENTED_SV_MSG
|
||||
from .types import AutomationActionNode, AutomationConditionNode
|
||||
|
||||
|
||||
|
||||
+20
-29
@@ -1,28 +1,5 @@
|
||||
import orjson
|
||||
from typing import Any, Union
|
||||
from ruamel.yaml.scalarstring import ScalarString
|
||||
from ruamel.yaml.scalarint import ScalarInt
|
||||
from ruamel.yaml.scalarbool import ScalarBoolean
|
||||
from ruamel.yaml.scalarfloat import ScalarFloat
|
||||
from src.yaml_serializer.types import SecretValue
|
||||
from src.yaml_serializer.types import SecretValue
|
||||
|
||||
# TODO: add support for these
|
||||
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
||||
|
||||
|
||||
def __default(obj: Any) -> str:
|
||||
if isinstance(obj, SecretValue):
|
||||
return NOT_IMPLEMENTED_SV_MSG
|
||||
if isinstance(obj, ScalarString):
|
||||
return str(obj)
|
||||
if isinstance(obj, ScalarBoolean):
|
||||
return bool(obj)
|
||||
if isinstance(obj, ScalarFloat):
|
||||
return float(obj)
|
||||
if isinstance(obj, ScalarInt):
|
||||
return int(obj)
|
||||
raise TypeError
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
|
||||
def json_dumps(obj: Any) -> str:
|
||||
@@ -34,16 +11,30 @@ def json_dumps(obj: Any) -> str:
|
||||
Returns:
|
||||
str
|
||||
"""
|
||||
return orjson.dumps(obj, default=__default).decode("utf-8")
|
||||
return json.dumps(normalize_obj(obj))
|
||||
|
||||
|
||||
def normalize_obj(obj: Any) -> Union[list, dict]:
|
||||
def normalize_obj(obj: Any) -> Any:
|
||||
"""Converts any python object to primitive python types
|
||||
|
||||
Args:
|
||||
obj (Any)
|
||||
|
||||
Returns:
|
||||
Union[list, dict]
|
||||
Any
|
||||
"""
|
||||
return orjson.loads(json_dumps(obj))
|
||||
if hasattr(obj, "to_normalized_json"):
|
||||
return obj.to_normalized_json()
|
||||
elif isinstance(obj, dict):
|
||||
return {normalize_obj(k): normalize_obj(v) for k, v in obj.items()}
|
||||
elif isinstance(obj, list) or isinstance(obj, tuple):
|
||||
return list(map(normalize_obj, obj))
|
||||
elif (
|
||||
isinstance(obj, str)
|
||||
or isinstance(obj, int)
|
||||
or isinstance(obj, float)
|
||||
or isinstance(obj, bool)
|
||||
):
|
||||
return obj
|
||||
else:
|
||||
raise TypeError(f"Cannot normalized obj {obj}")
|
||||
@@ -19,6 +19,10 @@ class IncludedYaml(NamedTuple):
|
||||
return cls(node.value)
|
||||
|
||||
|
||||
# TODO: add support for these
|
||||
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
||||
|
||||
|
||||
class SecretValue(NamedTuple):
|
||||
name: str
|
||||
value: str
|
||||
@@ -31,6 +35,9 @@ class SecretValue(NamedTuple):
|
||||
def from_yaml(cls, _constructor, node: Node):
|
||||
return cls(node.value, "n/a")
|
||||
|
||||
def to_normalized_json(self):
|
||||
return NOT_IMPLEMENTED_SV_MSG
|
||||
|
||||
|
||||
class IncludedYamlDir(NamedTuple):
|
||||
"""
|
||||
|
||||
@@ -6,9 +6,9 @@ from src.automations.types import (
|
||||
AutomationMetdata,
|
||||
)
|
||||
from src.automations.loader import load_automation
|
||||
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
|
||||
from src.yaml_serializer.types import (
|
||||
SecretValue,
|
||||
NOT_IMPLEMENTED_SV_MSG,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ from src.automations.parsers import (
|
||||
_parse_conditions,
|
||||
_parse_actions,
|
||||
)
|
||||
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
|
||||
from src.yaml_serializer.types import (
|
||||
SecretValue,
|
||||
NOT_IMPLEMENTED_SV_MSG,
|
||||
)
|
||||
|
||||
|
||||
@@ -220,80 +220,3 @@ class parser_tests(TestCase):
|
||||
|
||||
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
|
||||
self.assertEqual(parsed, expected)
|
||||
|
||||
# def test_overwriting_one_auto_in_separate_file(self):
|
||||
# # create dummy data
|
||||
# config_path = create_dummy_config_folder(
|
||||
# auto=[
|
||||
# AutomationData(
|
||||
# metadata=AutomationMetdata(
|
||||
# id="01",
|
||||
# )
|
||||
# ),
|
||||
# AutomationData(
|
||||
# metadata=AutomationMetdata(
|
||||
# id="02",
|
||||
# )
|
||||
# ),
|
||||
# AutomationData(
|
||||
# metadata=AutomationMetdata(
|
||||
# id="03",
|
||||
# )
|
||||
# ),
|
||||
# ]
|
||||
# )
|
||||
# # check the data is loaded well
|
||||
# automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
# self.assertEqual(automation_loader.get(0).metadata.id, "01")
|
||||
# self.assertEqual(automation_loader.get(1).metadata.id, "02")
|
||||
# self.assertEqual(automation_loader.get(2).metadata.id, "03")
|
||||
# self.assertIsNone(automation_loader.get(3))
|
||||
# self.assertEqual(len(automation_loader), 3)
|
||||
# # update auto #1
|
||||
# automation_loader.save(
|
||||
# 1,
|
||||
# AutomationData(
|
||||
# metadata=AutomationMetdata(id="02", alias="flush toilet!"),
|
||||
# trigger=[{"event": "toilet.flushed"}],
|
||||
# ),
|
||||
# )
|
||||
# # check that in-memory data is correctly updated
|
||||
# self.assertEqual(automation_loader.get(1).metadata.id, "02")
|
||||
# self.assertEqual(automation_loader.get(1).metadata.alias, "flush toilet!")
|
||||
# self.assertListEqual(automation_loader.get(1).trigger, [{"event": "toilet.flushed"}])
|
||||
|
||||
# # check that disk data is correctly updated
|
||||
# reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
# self.assertEqual(len(automation_loader), 3)
|
||||
# self.assertEqual(reloaded_automation_loader.get(1).metadata.id, "02")
|
||||
# self.assertEqual(reloaded_automation_loader.get(1).metadata.alias, "flush toilet!")
|
||||
# self.assertListEqual(
|
||||
# reloaded_automation_loader.get(1).trigger, [{"event": "toilet.flushed"}]
|
||||
# )
|
||||
# # add new auto
|
||||
# automation_loader.save(
|
||||
# 3,
|
||||
# AutomationData(
|
||||
# metadata=AutomationMetdata(id="newest", alias="testing autos"),
|
||||
# sequence=[
|
||||
# AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})
|
||||
# ],
|
||||
# ),
|
||||
# )
|
||||
# # check that in-memory data is correctly updated
|
||||
# self.assertEqual(len(automation_loader), 4)
|
||||
# self.assertEqual(automation_loader.get(3).metadata.id, "newest")
|
||||
# self.assertEqual(automation_loader.get(3).metadata.alias, "testing autos")
|
||||
# self.assertListEqual(
|
||||
# automation_loader.get(3).sequence,
|
||||
# [AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
|
||||
# )
|
||||
# # check that disk data is correctly updated
|
||||
# reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
# self.assertEqual(len(reloaded_automation_loader), 4)
|
||||
# self.assertEqual(reloaded_automation_loader.get(3).metadata.id, "newest")
|
||||
# self.assertEqual(reloaded_automation_loader.get(3).metadata.alias, "testing autos")
|
||||
# self.assertListEqual(
|
||||
# reloaded_automation_loader.get(3).sequence,
|
||||
# [AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
|
||||
# )
|
||||
@@ -4,8 +4,8 @@ from ruamel.yaml.scalarstring import ScalarString
|
||||
from ruamel.yaml.scalarint import ScalarInt
|
||||
from ruamel.yaml.scalarbool import ScalarBoolean
|
||||
from ruamel.yaml.scalarfloat import ScalarFloat
|
||||
from src.yaml_serializer.types import SecretValue
|
||||
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
|
||||
from src.yaml_serializer.types import SecretValue, NOT_IMPLEMENTED_SV_MSG
|
||||
from src.json_serializer import json_dumps
|
||||
|
||||
|
||||
class json_serializer_tests(TestCase):
|
||||
@@ -30,7 +30,6 @@ class json_serializer_tests(TestCase):
|
||||
"tup": (1, "w"),
|
||||
"some": {"deep": {"obj": NOT_IMPLEMENTED_SV_MSG}},
|
||||
},
|
||||
separators=(",", ":"),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -53,6 +52,5 @@ class json_serializer_tests(TestCase):
|
||||
"list": [1, "w"],
|
||||
"tup": (1, "w"),
|
||||
},
|
||||
separators=(",", ":"),
|
||||
),
|
||||
)
|
||||
@@ -1,5 +1,4 @@
|
||||
import io
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from src.yaml_serializer import (
|
||||
dump_yaml,
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
name: Shortumation
|
||||
version: 0.0.6c
|
||||
version: 0.0.6d
|
||||
slug: shortumation
|
||||
url: "https://github.com/asosnovsky/Shortumation"
|
||||
description: "A homeassitant addon which provides better UI for automations & scripts."
|
||||
|
||||
Reference in new issue
Block a user