-
Notifications
You must be signed in to change notification settings - Fork 2
/
weathermap_parser.py
executable file
·79 lines (63 loc) · 2.83 KB
/
weathermap_parser.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
#!/usr/bin/env python3
# Copyright 2014-2016 Science & Technology Facilities Council
#
# 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 collections import defaultdict
from re import compile as re_compile
class WeathermapParser:
RE_LINE = re_compile(r'^(?P<indent>\s*)(?P<command>[A-Z0-9]+)\s(?P<parameters>.*)$')
def load(self, filename = 'example-header.conf'):
with open(filename) as headerfile:
template = defaultdict(dict)
previous_object = ''
previous_section = ''
for line in headerfile:
is_command = self.RE_LINE.match(line)
if is_command:
groups = is_command.groupdict()
command = groups['command']
parameters = groups['parameters'].strip()
object_data = "%s %s" % (command, parameters)
if not groups['indent']: # New object, just add to tree
object_type = object_data.split(' ', 1)[0]
section = '%sS' % object_type
if object_type not in ('NODE', 'LINK'):
section = 'GLOBALS'
if 'OUTPUTFILE' in object_type.upper():
template[section][object_type] = object_name
continue
previous_object = object
previous_section = section
template[section][object] = {}
else: # extra parameters for previous object
template[previous_section][previous_object][command] = parameters
return template
return False
def render(self, data, indent=0):
result = ''
for k, v in sorted(data.iteritems()):
if isinstance(v, dict) and len(v) > 0:
result += "%s\n" % k
result += "%s\n" % self.render(v, indent+1)
else:
result += "%s%s" % (' '*indent, k)
if len(v) > 0:
result += " %s" % (v)
result += "\n"
return result
def dump(self, data):
s = ''
s += self.render(data['GLOBALS'])
s += self.render(data['NODES'])
s += self.render(data['LINKS'])
return s