Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore capability to export metadata sections (previously headers) to html #445

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 19 additions & 10 deletions scripts/metadata2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

# CCPP framework imports
from common import CCPP_INTERNAL_VARIABLE_DEFINITON_FILE
from parse_checkers import registered_fortran_ddt_names
from parse_tools import init_log, set_log_level
from metadata_table import MetadataHeader
from metadata_table import MetadataTable, parse_metadata_file
from framework_env import CCPPFrameworkEnv

###############################################################################
# Set up the command line argument parser and other global variables #
Expand All @@ -27,7 +29,7 @@

# List and order of variable attributes to output to HTML
ATTRIBUTES = [ 'local_name', 'standard_name', 'long_name', 'units',
'type', 'dimensions', 'kind', 'intent', 'optional' ]
'type', 'dimensions', 'kind', 'intent' ]

###############################################################################
# Functions and subroutines #
Expand Down Expand Up @@ -56,7 +58,7 @@ def import_config(configfile, logger):

# Get the base directory for running metadata2html.py from
# the default build directory value in the CCPP prebuild config
basedir = os.path.join(os.getcwd(), ccpp_prebuild_config.DEFAULT_BUILD_DIR)
basedir = os.path.join(os.getcwd())
logger.info('Relative path to CCPP directory from CCPP prebuild config: {}'.format(
ccpp_prebuild_config.DEFAULT_BUILD_DIR))

Expand Down Expand Up @@ -92,31 +94,38 @@ def get_output_directory_from_config(config, logger):
raise Exception("Output directory {} for converted metadata tables does not exist".format(outdir))
return outdir

def convert_to_html(filename_in, outdir, logger):
def convert_to_html(filename_in, outdir, logger, run_env):
"""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)
metadata_headers = parse_metadata_file(filename_in,
known_ddts=registered_fortran_ddt_names(),
run_env=run_env)
for metadata_header in metadata_headers:
filename_out = metadata_header.to_html(outdir, ATTRIBUTES)
if filename_out:
logger.info(" ... wrote {}".format(filename_out))
for metadata_section in metadata_header.sections():
filename_out = metadata_section.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)
run_env = CCPPFrameworkEnv(logger, ndict={'host_files':'',
'scheme_files':'',
'suites':''})

# Convert metadata file
(configfile, filename, outdir) = parse_arguments()
if configfile:
config = import_config(configfile, logger)
filenames = get_metadata_files_from_config(config, logger)
outdir = get_output_directory_from_config(config, logger)
for filename in filenames:
convert_to_html(filename, outdir, logger)
convert_to_html(filename, outdir, logger, run_env)
else:
convert_to_html(filename, outdir, logger)
convert_to_html(filename, outdir, logger, run_env)

if __name__ == '__main__':
main()
43 changes: 43 additions & 0 deletions scripts/metadata_table.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,19 @@ class MetadataSection(ParseSource):

__vref_start = re.compile(r"^\[\s*"+FORTRAN_SCALAR_REF+r"\s*\]$")

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

def __init__(self, table_name, table_type, run_env, parse_object=None,
title=None, type_in=None, module=None, process_type=None,
var_dict=None, known_ddts=None):
Expand Down Expand Up @@ -1266,6 +1279,36 @@ def is_scalar_reference(test_val):
"""Return True iff <test_val> refers to a Fortran scalar."""
return check_fortran_ref(test_val, None, False) is not None

def to_html(self, outdir, props):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would move this up to where the other active routines are, say right after write_to_file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

"""Write html file for metadata section and return filename.
Skip metadata sections without variables"""
if not self.__variables.variable_list():
return None
# Write table header
header = "<tr>"
for prop in props:
header += "<th>{}</th>".format(prop)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have been converting to f strings, this seems like a good place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have adopted f strings in this PR, thanks for the suggestion.

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

########################################################################

if __name__ == "__main__":
Expand Down