| |
| """ |
| Generate README-{UD_VER} from templates/README.tmpl using metadata-{UD_VER}.json. |
| """ |
|
|
| import json |
| import argparse |
| import logging |
| from os import getenv |
| from pathlib import Path |
|
|
| import jinja2 |
| from dotenv import load_dotenv |
|
|
|
|
| TOOLS_DIR = Path(__file__).resolve().parent |
|
|
| load_dotenv(TOOLS_DIR / ".env") |
| UD_VER = getenv('UD_VER', "2.17") |
|
|
| |
| parser = argparse.ArgumentParser(description=__doc__, |
| formatter_class=argparse.RawDescriptionHelpFormatter) |
| parser.add_argument('-o', '--override', action='store_true', |
| help='override output file if it already exists') |
| parser.add_argument('--ud-ver', default=UD_VER, |
| help='UD version to process (default: UD_VER from env or 2.17)') |
| parser.add_argument('-v', '--verbose', action='count', default=0, |
| help='increase verbosity level') |
| args = parser.parse_args() |
| UD_VER = args.ud_ver |
|
|
| |
| logging.basicConfig( |
| level = max(logging.DEBUG, logging.INFO - args.verbose * 10), |
| format='%(asctime)s [%(levelname)s] %(message)s', |
| datefmt='%Y-%m-%d %H:%M:%S' |
| ) |
|
|
|
|
| if __name__ == '__main__': |
| |
| with Path(TOOLS_DIR, f"metadata-{UD_VER}.json").open(mode='r') as fh: |
| metadata = json.load(fh) |
|
|
| with Path(TOOLS_DIR, "etc", f"description-{UD_VER}").open(mode='r') as fh: |
| description = (fh.read()).strip() |
|
|
| with Path(TOOLS_DIR, "etc", f"citation-{UD_VER}").open(mode='r') as fh: |
| citation = (fh.read()).strip() |
|
|
| |
| template_fn = Path(TOOLS_DIR, "templates", "README.tmpl") |
| output_fn = Path(TOOLS_DIR, f"README-{UD_VER}") |
|
|
| if args.override or not output_fn.exists(): |
| with template_fn.open(mode='r') as fh: |
| template = jinja2.Template(fh.read()) |
|
|
| with output_fn.open(mode='w') as fh: |
| output = template.render(citation=citation, |
| description=description, |
| data=metadata, ud_ver=UD_VER,) |
| fh.write(output) |
| print(f"{output_fn.name} written") |
| else: |
| logging.info("Output %s already exists: Not overriding.", output_fn.name) |
|
|