|
@@ -9,17 +9,23 @@
|
9
|
9
|
import json
|
10
|
10
|
import datetime
|
11
|
11
|
from Lodel.utils.mlstring import MlString
|
|
12
|
+import EditorialModel
|
12
|
13
|
|
13
|
14
|
|
14
|
15
|
def date_cast(date):
|
15
|
16
|
if len(date):
|
16
|
|
- return datetime.datetime(date)
|
|
17
|
+ return datetime.datetime.strptime(date, '%c')
|
17
|
18
|
else:
|
18
|
19
|
return None
|
19
|
20
|
|
20
|
21
|
|
|
22
|
+## @brief dirty obsolote cast function
|
21
|
23
|
def int_or_none(i):
|
22
|
|
- if len(i):
|
|
24
|
+ if isinstance(i, int):
|
|
25
|
+ return i
|
|
26
|
+ elif i is None:
|
|
27
|
+ return None
|
|
28
|
+ elif len(i):
|
23
|
29
|
return int(i)
|
24
|
30
|
else:
|
25
|
31
|
return None
|
|
@@ -47,27 +53,35 @@ class EmBackendJson(object):
|
47
|
53
|
#
|
48
|
54
|
# @param json_file str: path to the json_file used as data source
|
49
|
55
|
def __init__(self, json_file):
|
50
|
|
- with open(json_file) as json_data:
|
51
|
|
- self.data = json.loads(json_data.read())
|
|
56
|
+ self.json_file = json_file
|
|
57
|
+ pass
|
|
58
|
+
|
|
59
|
+ @staticmethod
|
|
60
|
+ ## @brief Used by json.dumps to serialize date and datetime
|
|
61
|
+ def date_handler(obj):
|
|
62
|
+ return obj.strftime('%c') if isinstance(obj, datetime.datetime) or isinstance(obj, datetime.date) else None
|
|
63
|
+
|
52
|
64
|
|
53
|
65
|
## Loads the data in the data source json file
|
54
|
66
|
#
|
55
|
67
|
# @return list
|
56
|
68
|
def load(self):
|
57
|
69
|
data = {}
|
58
|
|
- for index, component in self.data.items():
|
59
|
|
- attributes = {}
|
60
|
|
- for attr_name, attr_value in component.items():
|
61
|
|
- if attr_name in EmBackendJson.cast_methods:
|
62
|
|
- attributes[attr_name] = EmBackendJson.cast_methods[attr_name](attr_value)
|
63
|
|
- else:
|
64
|
|
- attributes[attr_name] = attr_value
|
65
|
|
- data[int(index)] = attributes
|
|
70
|
+ with open(self.json_file) as json_data:
|
|
71
|
+ rdata = json.loads(json_data.read())
|
|
72
|
+ for index, component in rdata.items():
|
|
73
|
+ attributes = {}
|
|
74
|
+ for attr_name, attr_value in component.items():
|
|
75
|
+ if attr_name in EmBackendJson.cast_methods:
|
|
76
|
+ attributes[attr_name] = EmBackendJson.cast_methods[attr_name](attr_value)
|
|
77
|
+ else:
|
|
78
|
+ attributes[attr_name] = attr_value
|
|
79
|
+ data[int(index)] = attributes
|
66
|
80
|
return data
|
67
|
81
|
|
68
|
82
|
## Saves the data in the data source json file
|
69
|
|
- #
|
70
|
|
- # @return bool
|
71
|
|
- # @todo à implémenter
|
72
|
|
- def save(self, em):
|
73
|
|
- return True
|
|
83
|
+ # @param filename str : The filename to save the EM in (if None use self.json_file provided at init )
|
|
84
|
+ def save(self, em, filename = None):
|
|
85
|
+ with open(self.json_file if filename is None else filename, 'w') as fp:
|
|
86
|
+ fp.write(json.dumps({ component.uid : component.dumps() for component in em.components() }, default=self.date_handler))
|
|
87
|
+
|