forked from bertinia/CESM_xml2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machdef2html.py
executable file
·142 lines (103 loc) · 4.79 KB
/
machdef2html.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
135
136
137
138
#!/usr/bin/env python
"""Generator of html file for machines
"""
import os, sys, re, glob
import datetime
CIMEROOT = os.environ.get("CIMEROOT")
if CIMEROOT is None:
raise SystemExit("ERROR: must set CIMEROOT environment variable")
sys.path.append(os.path.join(CIMEROOT, "scripts", "Tools"))
SRCROOT = os.environ.get("SRCROOT")
if SRCROOT is None:
SRCROOT = os.path.dirname(CIMEROOT)
os.environ['SRCROOT'] = SRCROOT
from standard_script_setup import *
from CIME.utils import expect
from CIME.XML.files import Files
from CIME.XML.machines import Machines
# check for dependency module
try:
import jinja2
except:
raise SystemExit("ERROR: machdef2html.py depends on the jinja2 template module. " /
"Install using 'pip --user install jinja2'")
# global variables
_now = datetime.datetime.now().strftime('%Y-%m-%d')
logger = logging.getLogger(__name__)
###############################################################################
def commandline_options():
###############################################################################
""" Process the command line arguments.
"""
parser = argparse.ArgumentParser(
description='Read all the config_machines.xml files and generate a corresponding HTML file.')
CIME.utils.setup_standard_logging_options(parser)
parser.add_argument('--htmlfile', nargs=1, required=True,
help='Fully quailfied path to output HTML file.')
parser.add_argument('--version', nargs=1, required=True,
help='Model version (e.g. CESM2.0)')
parser.add_argument('--supported', nargs=1, required=True,
help='Comma separated list of supported' \
'machines that have passed statistical validation and ' \
'may be used for large coupled experiments.')
parser.add_argument('--tested', nargs=1, required=True,
help='Comma separated list of tested machines ' \
'but not necessarily appropriate for large coupled experiments.')
options = parser.parse_args()
CIME.utils.parse_args_and_handle_standard_logging_options(options)
return options
###############################################################################
def _main_func(options, work_dir):
###############################################################################
"""Construct machines html from an XML file."""
# Initialize a variables for the html template
mach_dict = dict()
model_version = options.version[0]
# get the machine config file
files = Files()
config_file = files.get_value("MACHINES_SPEC_FILE")
expect(os.path.isfile(config_file),
"Cannot find config_file {} on disk".format(config_file))
# instantiate a machines object and read XML values into a dictionary
machines = Machines(config_file, machine="Query")
mach_list = machines.list_available_machines()
# get all the machine values loaded into the mach_dict
mach_dict = machines.return_values()
# intialize the support keys
for machine in mach_list:
mach_dict[(machine,'support')] = "Unsupported"
# loop through the list of supported machines and flag in the dictionary
supported = options.supported[0].split(',')
for machine in supported:
mach_dict[(machine,'support')] = "Scientific"
# loop through the list of tested machines and flag in the dictionary
tested = options.tested[0].split(',')
for machine in tested:
mach_dict[(machine,'support')] = "Tested"
# load up jinja template
templateLoader = jinja2.FileSystemLoader( searchpath='{0}/templates'.format(work_dir) )
templateEnv = jinja2.Environment( loader=templateLoader )
# TODO - get the cesm_version for the CIME root
tmplFile = 'machdef2html.tmpl'
template = templateEnv.get_template( tmplFile )
templateVars = { 'mach_list' : mach_list,
'mach_dict' : mach_dict,
'today' : _now,
'model_version' : model_version }
# render the template
mach_tmpl = template.render( templateVars )
# write the output file
with open( options.htmlfile[0], 'w') as html:
html.write(mach_tmpl)
return 0
###############################################################################
if __name__ == "__main__":
options = commandline_options()
## work_dir = os.path.join(CIMEROOT,"scripts","Tools","xml2html")
work_dir = os.getcwd()
try:
status = _main_func(options, work_dir)
sys.exit(status)
except Exception as error:
print(str(error))
sys.exit(1)