-
Notifications
You must be signed in to change notification settings - Fork 67
gmtb/develop: metadata2html batch processing #223
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
Changes from all commits
d9f538e
7f3c188
258bc8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,26 +6,84 @@ | |
| import sys | ||
|
|
||
| # CCPP framework imports | ||
| from common import CCPP_INTERNAL_VARIABLE_DEFINITON_FILE | ||
| from parse_tools import init_log, set_log_level | ||
| from metadata_table import MetadataHeader | ||
|
|
||
| ############################################################################### | ||
| # Set up the command line argument parser and other global variables # | ||
| ############################################################################### | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--metafile', '-m', action='store', | ||
| help='name of metadata file to convert', | ||
| required=True) | ||
| method = parser.add_mutually_exclusive_group(required=True) | ||
| method.add_argument('--config', '-c', action='store', | ||
| help='path to CCPP prebuild configuration file') | ||
| method.add_argument('--metafile', '-m', action='store', | ||
| help='name of metadata file to convert (requires -o)') | ||
| parser.add_argument('--outputdir', '-o', action='store', | ||
| help='directory where to write the html files', | ||
| required=True) | ||
| required='--metafile' in sys.argv or '-m' in sys.argv) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smart.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... Prof. Google ... |
||
|
|
||
| attributes = [ 'local_name', 'standard_name', 'long_name', 'units', | ||
| # List and order of variable attributes to output to HTML | ||
| ATTRIBUTES = [ 'local_name', 'standard_name', 'long_name', 'units', | ||
| 'type', 'dimensions', 'kind', 'intent', 'optional' ] | ||
|
|
||
| ############################################################################### | ||
| # Functions and subroutines # | ||
| ############################################################################### | ||
|
|
||
| def parse_arguments(): | ||
| """Parse command line arguments.""" | ||
| args = parser.parse_args() | ||
| config = args.config | ||
| filename = args.metafile | ||
| outdir = args.outputdir | ||
| return (filename, outdir) | ||
| return (config, filename, outdir) | ||
|
|
||
| def import_config(configfile, logger): | ||
| """Import the configuration from a given configuration file""" | ||
|
|
||
| if not os.path.isfile(configfile): | ||
| raise Exception("Configuration file {0} not found".format(configfile)) | ||
|
|
||
| # Import the host-model specific CCPP prebuild config; | ||
| # split into path and module name for import | ||
| configpath = os.path.abspath(os.path.split(configfile)[0]) | ||
| configmodule = os.path.split(configfile)[1].rstrip('.py') | ||
| sys.path.append(configpath) | ||
| ccpp_prebuild_config = __import__(configmodule) | ||
|
|
||
| config = {} | ||
| # Definitions in host-model dependent CCPP prebuild config script | ||
| config['variable_definition_files'] = ccpp_prebuild_config.VARIABLE_DEFINITION_FILES | ||
| config['scheme_files'] = ccpp_prebuild_config.SCHEME_FILES | ||
| # Add model-independent, CCPP-internal variable definition files | ||
| config['variable_definition_files'].append(CCPP_INTERNAL_VARIABLE_DEFINITON_FILE) | ||
| # Output directory for converted metadata tables | ||
| config['metadata_html_output_dir'] = ccpp_prebuild_config.METADATA_HTML_OUTPUT_DIR | ||
|
|
||
| return config | ||
|
|
||
| def get_metadata_files_from_config(config, logger): | ||
| """Create a list of metadata filenames for a CCPP prebuild configuration""" | ||
| filenames = [] | ||
| for sourcefile in config['variable_definition_files'] + config['scheme_files'].keys(): | ||
| metafile = os.path.splitext(sourcefile)[0]+'.meta' | ||
| if os.path.isfile(metafile): | ||
| filenames.append(metafile) | ||
| else: | ||
| # DH* Warn for now, raise exception later when | ||
| # old metadata format is no longer supported | ||
| logger.warn("Metadata file {} for source file {} not found, assuming old metadata format".format( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic is that if something is still using the old metadata format, there is no need to generate a HTML table because it already exists in Doxygen-readable format, right?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
| metafile, sourcefile)) | ||
| return filenames | ||
|
|
||
| def get_output_directory_from_config(config, logger): | ||
| """Return the html output directory for a CCPP prebuild configuration""" | ||
| outdir = config['metadata_html_output_dir'] | ||
| if not os.path.isdir(outdir): | ||
| raise Exception("Output directory {} for converted metadata tables does not exist".format(outdir)) | ||
| return outdir | ||
|
|
||
| def convert_to_html(filename_in, outdir, logger): | ||
| """Convert a metadata file into html (one html file for each table)""" | ||
|
|
@@ -34,7 +92,7 @@ def convert_to_html(filename_in, outdir, logger): | |
| 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) | ||
| filename_out = metadata_header.to_html(outdir, ATTRIBUTES) | ||
| if filename_out: | ||
| logger.info(" ... wrote {}".format(filename_out)) | ||
|
|
||
|
|
@@ -43,8 +101,15 @@ def main(): | |
| logger = init_log('metadata2html') | ||
| set_log_level(logger, logging.INFO) | ||
| # Convert metadata file | ||
| (filename, outdir) = parse_arguments() | ||
| convert_to_html(filename, outdir, logger) | ||
| (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) | ||
| else: | ||
| convert_to_html(filename, outdir, logger) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love argparse. Nice writeup of using the script.