forked from BigDevil82/docx_tag_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
37 lines (27 loc) · 1.04 KB
/
data_manager.py
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
import json
import os
class DataContextManager:
def __init__(self, json_data) -> None:
if os.path.exists(json_data):
with open(json_data, "r", encoding="utf-8") as f:
json_str = f.read()
self.json = json.loads(json_str)
else:
self.json = json.loads(json_data)
self.data_context = self.json
def get_json_value(self, key, data_context=None):
"""
Retrieve corresponding value for the given key from the JSON data
Args:
data_context (dict, optional): The data context to search for the key.
Returns:
str: The corresponding value from the JSON data.
"""
if data_context is None:
data_context = self.data_context
if isinstance(data_context, list):
return ""
val = data_context.get(key, None)
# try to get from the "Sharing" part
val = val if val is not None else self.json["Sharing"].get(key, None)
return val if val is not None else ""