-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreparsed_json.py
More file actions
74 lines (62 loc) · 2.31 KB
/
preparsed_json.py
File metadata and controls
74 lines (62 loc) · 2.31 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
#
# Copyright (c) 2019 - present. Boling Consulting Solutions (bcsw.net)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
import json
from parser_lib.versions import VersionList, VersionHeading
from parser_lib.section import SectionList, SectionHeading
class PreParsedJson(object):
def __init__(self):
self._versions = VersionList()
self._sections = SectionList()
def add(self, item):
assert isinstance(item, (VersionHeading, SectionHeading)), 'Invalid type'
if isinstance(item, VersionHeading):
self._versions.add(item)
elif isinstance(item, SectionHeading):
self._sections.add(item)
return self
@property
def versions(self):
return self._versions.versions
@property
def section_list(self):
return self._sections
@property
def sections(self):
return self._sections.sections
def save(self, filepath):
data = {
'versions': self._versions.as_dict_list(),
'sections': self._sections.as_dict_list()
}
json_data = json.dumps(data, indent=2, separators=(',', ': '))
with open(filepath, 'w') as f:
f.write(json_data)
def load(self, filepath):
self._versions = VersionList()
self._sections = SectionList()
with open(filepath, 'r') as f:
data = json.load(f)
self._versions.load(data.get('versions', dict()))
self._sections.load(data.get('sections', dict()))
def dump(self):
print('Version Info:')
for num, entry in enumerate(self.versions):
entry.dump()
print('')
print('Section Info:')
for num, entry in enumerate(self.sections):
entry.dump()