-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
215 lines (172 loc) · 6.37 KB
/
__init__.py
File metadata and controls
215 lines (172 loc) · 6.37 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import difflib
import json
import logging
import time
import uuid
from contextlib import contextmanager
from datetime import datetime, timezone
from typeid import TypeID
from jsondoc.models.block.base import CreatedBy
ARBITRARY_JSON_SCHEMA_OBJECT = {
"type": "object",
"properties": {},
"additionalProperties": True,
}
TYPEID_BLOCK_ID_PREFIX = "bk"
TYPEID_PAGE_ID_PREFIX = "pg"
def generate_block_id(typeid: bool = False) -> str:
if typeid:
return str(TypeID(prefix=TYPEID_BLOCK_ID_PREFIX))
return str(uuid.uuid4())
def generate_page_id(typeid: bool = False) -> str:
if typeid:
return str(TypeID(prefix=TYPEID_PAGE_ID_PREFIX))
return str(uuid.uuid4())
def get_current_time() -> datetime:
return datetime.now(tz=timezone.utc)
def replace_refs_with_arbitrary_object(data):
if isinstance(data, dict):
if "$ref" in data:
return ARBITRARY_JSON_SCHEMA_OBJECT
return {k: replace_refs_with_arbitrary_object(v) for k, v in data.items()}
elif isinstance(data, list):
return [replace_refs_with_arbitrary_object(item) for item in data]
else:
return data
def load_json_file(file_path, deserialize=True) -> dict | str:
with open(file_path) as schema_file:
content = schema_file.read()
# Remove the lines that begin with //. Account for spaces before the //
content = "\n".join(
[line for line in content.split("\n") if not line.lstrip().startswith("//")]
)
if deserialize:
return json.loads(content)
else:
return content
def get_nested_value(obj: dict | object, coordinates: str) -> any:
keys = coordinates.strip(".").split(".")
current = obj
for key in keys:
if isinstance(current, dict):
current = current.get(key)
elif hasattr(current, key):
current = getattr(current, key)
else:
raise ValueError(f"Cannot get '{key}' from object: {current}")
return current
def set_nested_value(obj: dict | object, coordinates: str, value: any) -> None:
keys = coordinates.strip(".").split(".")
current = obj
for key in keys[:-1]:
if isinstance(current, dict):
if key not in current:
current[key] = {}
current = current[key]
elif hasattr(current, key):
current = getattr(current, key)
else:
raise ValueError(f"Cannot set '{key}' on object: {current}")
if isinstance(current, dict):
current[keys[-1]] = value
else:
setattr(current, keys[-1], value)
@contextmanager
def timer(name, unit="s"):
start_time = time.time()
yield
end_time = time.time()
elapsed = end_time - start_time
if unit == "ms":
print(f"{name} took {elapsed * 1000:.4f} milliseconds")
else:
print(f"{name} took {end_time - start_time:.4f} seconds")
def diff_strings(string1, string2):
lines1 = string1.splitlines(keepends=True)
lines2 = string2.splitlines(keepends=True)
diff = difflib.unified_diff(lines1, lines2, lineterm="")
return "".join(diff)
def diff_jsonable_dict(d1: dict, d2: dict) -> str:
"""
Diffs two Python dictionaries that can be serialized to JSON.
"""
try:
d1_json = json.dumps(d1)
d2_json = json.dumps(d2)
except Exception as e:
raise ValueError(f"Failed to diff dictionaries: {e}")
return diff_json(d1_json, d2_json)
def diff_json(j1: str, j2: str) -> str:
"""
Diffs two given JSON strings.
TBD: Handle other inputs, like files, bytes, etc.
"""
j1_canonical = json.dumps(json.loads(j1), indent=2, sort_keys=True)
j2_canonical = json.dumps(json.loads(j2), indent=2, sort_keys=True)
return diff_strings(j1_canonical, j2_canonical)
def set_dict_recursive(d: dict | list, key: str, value: str):
"""
Set all values that match a key in a nested dictionary or list.
"""
if isinstance(d, dict):
for k, v in d.items():
if isinstance(v, dict):
set_dict_recursive(v, key, value)
elif isinstance(v, list):
for item in v:
if isinstance(item, dict):
set_dict_recursive(item, key, value)
elif k == key:
d[k] = value
elif isinstance(d, list):
for item in d:
if isinstance(item, dict):
set_dict_recursive(item, key, value)
def set_field_recursive(obj: any, field_name: str, value: any) -> None:
"""
Recursively sets all fields with name 'field_name' to 'value' in the given object.
Works with dictionaries, lists, Pydantic models, and other objects with attributes.
Args:
obj: The object to traverse (dict, list, Pydantic model, or other object)
field_name: The name of the field to set
value: The value to set the field to
"""
from pydantic import BaseModel
# Handle dictionary
if isinstance(obj, dict):
for k, v in list(obj.items()):
if k == field_name:
obj[k] = value
else:
set_field_recursive(v, field_name, value)
# Handle list
elif isinstance(obj, list):
for item in obj:
set_field_recursive(item, field_name, value)
# Handle Pydantic models
elif isinstance(obj, BaseModel):
# Get the model fields as a dict and process them
data = obj.model_dump()
for k, v in data.items():
if k == field_name:
setattr(obj, k, value)
else:
model_value = getattr(obj, k)
set_field_recursive(model_value, field_name, value)
# # Handle other objects with attributes (non-primitive types)
# elif hasattr(obj, "__dict__") and not isinstance(
# obj, (str, int, float, bool, type(None))
# ):
# for k, v in list(obj.__dict__.items()):
# if k == field_name:
# setattr(obj, k, value)
# else:
# set_field_recursive(v, field_name, value)
def set_created_by(obj: any, created_by: str | CreatedBy) -> None:
"""
Recursively sets the 'created_by' field to the given value in the given object.
"""
assert isinstance(created_by, (str, CreatedBy))
if isinstance(created_by, str):
created_by = CreatedBy(id=created_by, object="user")
set_field_recursive(obj, "created_by", created_by)