forked from NCAR/KGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkgtool.py
134 lines (104 loc) · 4.06 KB
/
kgtool.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''KGen tool base class
'''
import os
from kgconfig import Config
try:
import configparser
except:
import ConfigParser as configparser
GEN = 'general'
class KGTool(object):
def __init__(self):
pass
def run(self):
raise Exception('"%s" should implement "run" method.'%self.__class__.__name__)
class KGModelingTool(object):
def hasmodel(self, modeltype):
modelfile = '%s/%s'%(Config.path['outdir'], Config.modelfile)
if not os.path.exists(modelfile):
return False
has_general = False
has_modeltype = False
has_modelsection = False
section = ''
required_modelsections = []
model_sections = {}
with open(modelfile, 'r') as mf:
for line in mf.readlines():
if line.startswith('['):
pos = line.find(']')
if pos > 0:
section = line[1:pos].strip()
if section == 'general':
has_general = True
else:
mtype, msec = section.split('.')
if mtype not in model_sections:
model_sections[mtype] = []
model_sections[mtype].append(msec)
elif section == 'general' and line.find('=') > 0:
mtype, msections = line.split('=')
if mtype.strip() == modeltype:
required_modelsections = [s.strip() for s in msections.split(',')]
has_modeltype = True
if has_modeltype and modeltype in model_sections and all( (msec in model_sections[modeltype]) for msec in required_modelsections):
has_modelsection = True
if has_general and has_modeltype and has_modelsection:
break
# cfg = configparser.ConfigParser()
# cfg.optionxform = str
#
# with open(modelfile, 'r') as mf:
# if not cfg.read(mf):
# return False
#
# if not cfg.has_section(GEN):
# return False
#
# if not cfg.has_option(GEN, modeltype):
# return False
#
# for subsec in [ s.strip() for s in cfg.get(GEN, modeltype).split(',') ]:
# if not cfg.has_section('%s.%s'%(modeltype, subsec)):
# return False
return has_general and has_modeltype and has_modelsection
def addmodel(self, modeltype, sections):
modelfile = '%s/%s'%(Config.path['outdir'], Config.modelfile)
mode = 'r+'
if not os.path.exists(modelfile):
mode = 'w+'
with open(modelfile, mode) as mf:
mf.seek(0, os.SEEK_END)
size = mf.tell()
if size == 0:
mf.write('; KGen Model Data File\n')
cfg = configparser.ConfigParser()
cfg.optionxform = str
cfg.read(modelfile)
if not cfg.has_section(GEN):
cfg.add_section(GEN)
if not cfg.has_option(GEN, modeltype):
cfg.set(GEN, modeltype, ', '.join(sections))
#
# for sec in sections:
# secname = '%s.%s'%(modeltype, sec)
# if not cfg.has_section(secname):
# cfg.add_section(secname)
with open(modelfile, mode) as mf:
cfg.write(mf)
def addsection(self, modeltype, section, options):
modelfile = '%s/%s'%(Config.path['outdir'], Config.modelfile)
mode = 'r+'
if not os.path.exists(modelfile):
raise Exception('Modelfile does not exists: %s'%modelfile)
cfg = configparser.ConfigParser()
cfg.optionxform = str
cfg.read(modelfile)
subsec = '%s.%s'%(modeltype, section)
if cfg.has_section(subsec):
raise Exception('Section already exists: %s'%subsec)
cfg.add_section(subsec)
for opt, val in options:
cfg.set(subsec, opt, val)
with open(modelfile, mode) as mf:
cfg.write(mf)