-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
60 lines (52 loc) · 1.5 KB
/
common.py
File metadata and controls
60 lines (52 loc) · 1.5 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
import json
class ChangeLog:
"""
A keeper of changes to files
"""
@property
def changes(self):
"""
:return: the list of changes
"""
return self._changes
@property
def counter(self):
"""
:return: the total number of recorded changes
"""
return self._counter
def __init__(self):
self._counter = 0
self._changes = []
self._root = {
'changes': self._changes
}
def addChange(self, operation: str, executed: bool, **kwargs):
"""
Record a new operation
:param operation: Arbitrary name of the operation
:param executed: Weather the action was carried out
:param kwargs: Any related data that will be appended
"""
change = {
'id': self._counter,
'operation': operation,
'executed': executed
}
change.update(kwargs)
self._changes.append(change)
self._counter += 1
def addRootProperties(self, properties: dict):
"""
Add data to the root object
:param properties: a dictionary of additional data
"""
self._root.update(properties)
def save(self, path: str, **kwargs):
"""
Write the changes to a file in JSON
:param path: the path to write to
:param kwargs: parameters to pass to the JSON encoder
"""
with open(path, 'w') as sf:
json.dump(self._root, sf, **kwargs)