-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiObjectWithInheritingDataStructure.py
86 lines (79 loc) · 3.66 KB
/
iObjectWithInheritingDataStructure.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
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
import json;
try:
from mNotProvided import fAssertTypes as f0AssertTypes;
except ModuleNotFoundError as oException:
if oException.args[0] != "No module named 'mNotProvided'":
raise;
f0AssertTypes = None;
class iObjectWithInheritingDataStructure(object):
# The following MUST be defined by a subclass:
oDataStructure = None; # Latest data structure
dxInheritingValues = None;
toCompatibleDataStructures = tuple(); # All historical supported data structures, ordered from newest to oldest.
@classmethod
def foConstructFromJSONString(cClass, sbJSON, sDataNameInError, s0BasePath = None, dxInheritingValues = {}):
# dxInheritingValues needs to be updated with cClass.dxInheritingValues but this
# will be done in a call to cClass.foConstructFromJSONData later
if f0AssertTypes: f0AssertTypes({
"sbJSON": (sbJSON, bytes),
"sDataNameInError": (sDataNameInError, str),
"s0BasePath": (s0BasePath, str, None),
"dxInheritingValues": (dxInheritingValues, dict),
});
try:
xJSONData = json.loads(sbJSON);
except:
raise cJSONDataSyntaxException("%s does not contain valid JSON" % sDataNameInError);
return cClass.foConstructFromJSONData(xJSONData, sDataNameInError, s0BasePath, dxInheritingValues);
@classmethod
def foConstructFromJSONData(cClass, xJSONData, sDataNameInError, s0BasePath = None, dxInheritingValues = {}):
dxInheritingValues = dxInheritingValues.copy();
for sValueName in cClass.dxInheritingValues:
if sValueName not in dxInheritingValues:
dxInheritingValues[sValueName] = cClass.dxInheritingValues[sValueName];
if f0AssertTypes: f0AssertTypes({
"sDataNameInError": (sDataNameInError, str),
"s0BasePath": (s0BasePath, str, None),
"dxInheritingValues": (dxInheritingValues, dict),
});
return fxConvertFromJSONData(
xStructureDetails = (
cClass.oDataStructure,
cClass.toCompatibleDataStructures,
),
xJSONData = xJSONData,
sDataNameInError = sDataNameInError,
s0BasePath = s0BasePath,
dxInheritingValues = dxInheritingValues,
);
def fsbConvertToJSONString(oSelf, sDataNameInError, s0BasePath = None, dxInheritingValues = {}):
# dxInheritingValues needs to be updated with cClass.dxInheritingValues but this
# will be done in a call to oSelf.fxConvertToJSONData later
if f0AssertTypes: f0AssertTypes({
"sDataNameInError": (sDataNameInError, str),
"s0BasePath": (s0BasePath, str, None),
"dxInheritingValues": (dxInheritingValues, dict),
});
xJSONData = oSelf.fxConvertToJSONData(sDataNameInError, s0BasePath, dxInheritingValues);
sJSON = json.dumps(xJSONData, sort_keys = True, indent = 2).replace("\n", "\r\n"); # We're on Windows
return bytes(sJSON, "ascii", "strict");
def fxConvertToJSONData(oSelf, sDataNameInError, s0BasePath = None, dxInheritingValues = {}):
dxInheritingValues = dxInheritingValues.copy();
for sValueName in oSelf.__class__.dxInheritingValues:
if sValueName not in dxInheritingValues:
dxInheritingValues[sValueName] = oSelf.__class__.dxInheritingValues[sValueName];
if f0AssertTypes: f0AssertTypes({
"sDataNameInError": (sDataNameInError, str),
"s0BasePath": (s0BasePath, str, None),
"dxInheritingValues": (dxInheritingValues, dict),
});
return fxConvertToJSONData(
xStructureDetails = oSelf.__class__.oDataStructure,
xData = oSelf,
sDataNameInError = sDataNameInError,
s0BasePath = s0BasePath,
dxInheritingValues = dxInheritingValues,
);
from .fxConvertFromJSONData import fxConvertFromJSONData;
from .fxConvertToJSONData import fxConvertToJSONData;
from .mExceptions import *;