Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions scripts/convert_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ def convert_file(filename_in, filename_out, metadata_filename_out, model, logger
# logger.info('Found old metadata table, {}, on line {}'.format(table_name, lindex+1))
# The header line is not modified
file.write(line+"\n")
# Write an include line for the metadata table
file.write('!! \htmlinclude {}.html\n'.format(table_name))
# Create the table start section
mdtable = MetadataTable(table_name, current_module)
mdconfig.append(mdtable)
Expand All @@ -339,6 +337,9 @@ def convert_file(filename_in, filename_out, metadata_filename_out, model, logger
dim_names = [__not_found__]*15
# Do not work on a blank table
if len(words) > 1:
# Write an include line for the metadata table
file.write('!! \htmlinclude {}.html\n'.format(table_name))
#
table_header = [x.strip() for x in words[1:-1]]
for ind in xrange(len(table_header)):
header_locs[table_header[ind]] = ind
Expand Down
5 changes: 3 additions & 2 deletions scripts/convert_metadata_schemes_using_typedef_dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ def convert_file(filename_in, filename_out, metadata_filename_out, typedef_dimen
# logger.info('Found old metadata table, {}, on line {}'.format(table_name, lindex+1))
# The header line is not modified
file.write(line+"\n")
# Write an include line for the metadata table
file.write('!! \htmlinclude {}.html\n'.format(table_name))
# Create the table start section
mdtable = MetadataTable(table_name, current_module)
mdconfig.append(mdtable)
Expand All @@ -218,6 +216,9 @@ def convert_file(filename_in, filename_out, metadata_filename_out, typedef_dimen
dim_names = [__not_found__]*15
# Do not work on a blank table
if len(words) > 1:
# Write an include line for the metadata table
file.write('!! \htmlinclude {}.html\n'.format(table_name))
#
table_header = [x.strip() for x in words[1:-1]]
for ind in xrange(len(table_header)):
header_locs[table_header[ind]] = ind
Expand Down
50 changes: 50 additions & 0 deletions scripts/metadata2html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python

import argparse
import logging
import os
import sys

# CCPP framework imports
from parse_tools import init_log, set_log_level
from metadata_table import MetadataHeader

parser = argparse.ArgumentParser()
parser.add_argument('--metafile', '-m', action='store',
help='name of metadata file to convert',
required=True)
parser.add_argument('--outputdir', '-o', action='store',
help='directory where to write the html files',
required=True)

attributes = [ 'standard_name', 'long_name', 'units', 'local_name',
Copy link
Copy Markdown
Collaborator

@grantfirl grantfirl Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which attributes should be displayed? I would argue not all of them in the scientific documentation. Maybe just standard_name, long_name, and units (intent might be useful too)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Something we need to iterate on with Ligia, Julie and Man as well. This script is a first version and I was hoping to improve it with the coming metadata conversions.

'type', 'dimensions', 'kind', 'intent', 'optional' ]

def parse_arguments():
"""Parse command line arguments."""
args = parser.parse_args()
filename = args.metafile
outdir = args.outputdir
return (filename, outdir)

def convert_to_html(filename_in, outdir, logger):
"""Convert a metadata file into html (one html file for each table)"""
if not os.path.isfile(filename_in):
raise Exception("Metadata file {} not found".format(filename_in))
logger.info("Converting file {} to HTML".format(filename_in))
metadata_headers = MetadataHeader.parse_metadata_file(filename_in)
for metadata_header in metadata_headers:
filename_out = metadata_header.to_html(outdir, attributes)
if filename_out:
logger.info(" ... wrote {}".format(filename_out))

def main():
# Initialize logging
logger = init_log('metadata2html')
set_log_level(logger, logging.INFO)
# Convert metadata file
(filename, outdir) = parse_arguments()
convert_to_html(filename, outdir, logger)

if __name__ == '__main__':
main()
44 changes: 44 additions & 0 deletions scripts/metadata_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@

# Python library imports
from __future__ import print_function
import os
import re
# CCPP framework imports
from metavar import Var, VarDictionary
Expand Down Expand Up @@ -175,6 +176,19 @@ class MetadataHeader(ParseSource):

__blank_line__ = re.compile(r"\s*[#;]")

__html_template__ = """
<html>
<head>
<title>{title}</title>
<meta charset="UTF-8">
</head>
<body>
<table>
{header}{contents}</table>
</body>
</html>
"""

def __init__(self, parse_object=None,
title=None, type_in=None, module=None, var_dict=None,
logger=None):
Expand Down Expand Up @@ -372,6 +386,36 @@ def variable_list(self):
"Return an ordered list of the header's variables"
return self._variables.variable_list()

def to_html(self, outdir, props):
"""Write html file for metadata table and return filename.
Skip metadata headers without variables"""
if not self._variables.variable_list():
return None
# Write table header
header = "<tr>"
for prop in props:
header += "<th>{}</th>".format(prop)
header += "</tr>\n"
# Write table contents, one row per variable
contents = ""
for var in self._variables.variable_list():
row = "<tr>"
for prop in props:
value = var.get_prop_value(prop)
# Pretty-print for dimensions
if prop == 'dimensions':
value = '(' + ', '.join(value) + ')'
elif value is None:
value = "n/a"
row += "<td>{}</td>".format(value)
row += "</tr>\n"
contents += row
filename = os.path.join(outdir, self.title + '.html')
with open(filename,"w") as f:
f.writelines(self.__html_template__.format(title=self.title + ' argument table',
header=header, contents=contents))
return filename

def get_var(self, standard_name=None, intent=None):
if standard_name is not None:
var = self._variables.find_variable(standard_name)
Expand Down
16 changes: 9 additions & 7 deletions scripts/metavar.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,6 @@ class Var(object):
default_fn_in=default_kind_val),
VariableProperty('state_variable', bool,
optional_in=True, default_in=False),
VariableProperty('optional', bool,
optional_in=True, default_in=False),
VariableProperty('constant', bool,
optional_in=True, default_in=False),
VariableProperty('allocatable', bool,
Expand All @@ -392,7 +390,9 @@ class Var(object):
default_in='timestep')]

# __var_props contains properties which are not in __spec_props
__var_props = [VariableProperty('intent', str,
__var_props = [VariableProperty('optional', bool,
optional_in=True, default_in=False),
VariableProperty('intent', str,
valid_values_in=['in', 'out', 'inout'])]


Expand Down Expand Up @@ -708,11 +708,13 @@ def print_debug(self):
type = {type} *
dimensions = {dimensions} *
kind = {kind} *
intent = {intent}
optional = {optional}
'''
'''
if 'intent' in self.__spec_propdict.keys():
str += ' intent = {intent}\n'
if 'optional' in self.__spec_propdict.keys():
str += ' optional = {optional}\n'
if self._context is not None:
str = str + '\n context = {}'.format(self._context)
str += ' context = {}'.format(self._context)
# End if
return str.format(**self._prop_dict)

Expand Down