From e0f1e269bcc8dba9594b1ec4cb509ce52b5af685 Mon Sep 17 00:00:00 2001 From: zaihuaji Date: Tue, 24 Mar 2026 15:54:45 -0500 Subject: [PATCH 1/3] save change --- pyproject.toml | 2 +- src/rda_python_miscs/pg_rst.py | 84 ++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5d8633e..c7ba334 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" [project] name = "rda_python_miscs" -version = "2.0.2" +version = "2.0.3" authors = [ { name="Zaihua Ji", email="zji@ucar.edu" }, ] diff --git a/src/rda_python_miscs/pg_rst.py b/src/rda_python_miscs/pg_rst.py index 0196d03..898ab0c 100755 --- a/src/rda_python_miscs/pg_rst.py +++ b/src/rda_python_miscs/pg_rst.py @@ -19,7 +19,6 @@ import argparse import importlib from os import path as op -import PgLOG from rda_python_common.pg_file import PgFile from rda_python_common.pg_util import PgUtil @@ -778,7 +777,7 @@ def create_description(self, desc, secid, dtype): str: RST-formatted description content. """ if desc == "\n": return '' - ptype = 0 # paragraph type: 0 - normal, 1 - table, 2 - synopsis + ptype = 0 # paragraph type: 0 - normal, 1 - table, 2 = synopsis content = '' cnt = 0 alllines = re.split(r'\n', desc) @@ -1195,41 +1194,46 @@ def _load_opts_alias(docname): return opts, alias, origin -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description=( - "Generate RST documentation from a structured .usg source document.\n\n" - "OPTS and ALIAS are loaded from rda_python_/.py: " - "the module is searched first for module-level OPTS/ALIAS variables, " - "then for a class defined in that module that carries both as class " - "attributes." - ), - formatter_class=argparse.RawDescriptionHelpFormatter, - ) - parser.add_argument( - 'docname', - help=( - "Short document name, e.g. 'dsarch' or 'dsupdt'. " - "The module rda_python_/.py must be importable " - "and must define OPTS (and optionally ALIAS) either at module " - "level or as class attributes." - ), - ) - parser.add_argument( - '--docdir', - default=None, - metavar='DIR', - help=( - "Root directory under which the per-document RST output directory " - "is created (default: current working directory). " - "The final output lands in //." - ), - ) - args = parser.parse_args() - - opts, alias, origin = _load_opts_alias(args.docname) - pg = PgRST() - pg.DOCS['ORIGIN'] = origin - if args.docdir is not None: - pg.DOCS['DOCDIR'] = args.docdir - pg.process_docs(args.docname, opts, alias) +def main(): + """Entry point for command-line usage of pg_rst.py.""" + import argparse + parser = argparse.ArgumentParser( + description=( + "Convert a .usg help document to reStructuredText (.rst) using RST templates. " + "OPTS and ALIAS are loaded from rda_python_/.py: " + "the module is searched first for module-level OPTS/ALIAS variables, " + "then for a class defined in that module that carries both as class " + "attributes." + ), + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + parser.add_argument( + 'docname', + help=( + "Short document name, e.g. 'dsarch' or 'dsupdt'. " + "The module rda_python_/.py must be importable " + "and must define OPTS (and optionally ALIAS) either at module " + "level or as class attributes." + ), + ) + parser.add_argument( + '--docdir', + default=None, + metavar='DIR', + help=( + "Root directory under which the per-document RST output directory " + "is created (default: current working directory). " + "The final output lands in //." + ), + ) + args = parser.parse_args() + + opts, alias, origin = _load_opts_alias(args.docname) + pg = PgRST() + pg.DOCS['ORIGIN'] = origin + if args.docdir is not None: + pg.DOCS['DOCDIR'] = args.docdir + pg.process_docs(args.docname, opts, alias) + +if __name__ == "__main__": + main() \ No newline at end of file From faa38a6c42e156c58c4de7a746c987f8dc7c57ff Mon Sep 17 00:00:00 2001 From: zaihuaji Date: Tue, 24 Mar 2026 16:04:38 -0500 Subject: [PATCH 2/3] PgLOG. => self. --- src/rda_python_miscs/pg_rst.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/rda_python_miscs/pg_rst.py b/src/rda_python_miscs/pg_rst.py index 898ab0c..0c7c4ad 100755 --- a/src/rda_python_miscs/pg_rst.py +++ b/src/rda_python_miscs/pg_rst.py @@ -141,17 +141,17 @@ def process_docs(self, docname, opts, alias): self.ALIAS = alias self.parse_docs(docname) - if not self.sections: PgLOG.pglog(docname + ": empty document", PgLOG.LGWNEX) + if not self.sections: self.pglog(docname + ": empty document", self.LGWNEX) self.DOCS['DOCNAM'] = docname if docname in self.LINKS: self.LINKS.remove(docname) self.DOCS['DOCLNK'] = r"({})".format('|'.join(self.LINKS)) self.DOCS['DOCTIT'] = docname.upper() - self.change_local_directory(self.DOCS['DOCDIR'], PgLOG.LGWNEX) - PgLOG.pglog("Write rst document '{}' under {}".format(docname, self.DOCS['DOCDIR']), PgLOG.LOGWRN) + self.change_local_directory(self.DOCS['DOCDIR'], self.LGWNEX) + self.pglog("Write rst document '{}' under {}".format(docname, self.DOCS['DOCDIR']), self.LOGWRN) if op.exists("index.rst"): # write index file once - PgLOG.pglog("index.rst exists already, delete first if needs to be regenerated", PgLOG.LOGWRN) + self.pglog("index.rst exists already, delete first if needs to be regenerated", self.LOGWRN) else: self.write_index(self.sections[0]) @@ -175,7 +175,7 @@ def parse_docs(self, docname): docname (str): Short document name used to locate ``/.usg``. """ docfile = "{}/{}.usg".format(self.DOCS['ORIGIN'], docname) - PgLOG.pglog("Parsing info for Document '{}'".format(docname), PgLOG.LOGWRN) + self.pglog("Parsing info for Document '{}'".format(docname), self.LOGWRN) section = self.init_section('0', "Preface") option = example = None fh = open(docfile, 'r') @@ -226,11 +226,11 @@ def parse_docs(self, docname): # check completion of options for opt in self.OPTS: if opt not in self.options: - PgLOG.pglog("Missing option Entry -{} (-{}) in Document '{}'".format(opt, self.OPTS[opt][1], docname), PgLOG.LOGWRN) + self.pglog("Missing option Entry -{} (-{}) in Document '{}'".format(opt, self.OPTS[opt][1], docname), self.LOGWRN) if self.sections: cnt = len(self.sections) s = 's' if cnt > 1 else '' - PgLOG.pglog("{} Section{} gathered for '{}'".format(cnt, s, docname), PgLOG.LOGWRN) + self.pglog("{} Section{} gathered for '{}'".format(cnt, s, docname), self.LOGWRN) # # cache section information @@ -368,7 +368,7 @@ def init_option(self, secid, opt, desc): types = ("Mode", "Info", "Info", "Action") if opt not in self.OPTS: - PgLOG.pglog("{} -- option not defined for {}".format(opt, self.DOCS['DOCNAM']), PgLOG.LGWNEX) + self.pglog("{} -- option not defined for {}".format(opt, self.DOCS['DOCNAM']), self.LGWNEX) option['secid'] = secid option['opt'] = opt ms = re.match(r'^(, | \(Alias: .*\), )(.*)', desc) @@ -488,15 +488,15 @@ def template_to_rst(self, template, hash, extra=None): matches = re.findall(r'__([A-Z]+)__', line) if matches: for key in matches: - if key not in hash: PgLOG.pglog("{}: not defined at {}({}) {}".format(key, line, idx, tempfile), PgLOG.LGWNEX) - if not hash[key]: PgLOG.pglog(key + ": empty content", PgLOG.LGWNEX) + if key not in hash: self.pglog("{}: not defined at {}({}) {}".format(key, line, idx, tempfile), self.LGWNEX) + if not hash[key]: self.pglog(key + ": empty content", self.LGWNEX) line = line.replace("__{}__".format(key), hash[key]) rf.write(line + "\n") line = tf.readline() tf.close() rf.close() - PgLOG.pglog("{}{}.rst created from {}.rst.temp".format(template, extra, template), PgLOG.LOGWRN) + self.pglog("{}{}.rst created from {}.rst.temp".format(template, extra, template), self.LOGWRN) # # create rst content for table of contents @@ -1079,7 +1079,7 @@ def get_short_option(self, p): for alias in self.ALIAS[opt]: if re.match(r'^{}$'.format(alias), p, re.I): return opt - PgLOG.pglog("{} - unknown option for {}".format(p, self.DOCS['DOCNAM']), PgLOG.LGWNEX) + self.pglog("{} - unknown option for {}".format(p, self.DOCS['DOCNAM']), self.LGWNEX) # # replace with rst link for a given section title @@ -1117,7 +1117,7 @@ def get_section(self, secid): for section in self.sections: if section['secid'] == secid: return section - PgLOG.pglog("Unknown Section ID {}".format(secid), PgLOG.LGWNEX) + self.pglog("Unknown Section ID {}".format(secid), self.LGWNEX) # --------------------------------------------------------------------------- @@ -1157,9 +1157,9 @@ def _load_opts_alias(docname): try: mod = importlib.import_module(modname) except ImportError as exc: - PgLOG.pglog( + self.pglog( "Cannot import module '{}': {}".format(modname, exc), - PgLOG.LGWNEX, + self.LGWNEX, ) # Derive ORIGIN from the module's own file path. @@ -1181,10 +1181,10 @@ def _load_opts_alias(docname): break if opts is None: - PgLOG.pglog( + self.pglog( "Module '{}' does not define OPTS (checked module level and " "all classes defined in the module)".format(modname), - PgLOG.LGWNEX, + self.LGWNEX, ) # ALIAS is optional; default to empty dict. From 7f43944ccc85796719543c6e134405d662b41fe5 Mon Sep 17 00:00:00 2001 From: Zaihua Ji Date: Tue, 24 Mar 2026 16:32:10 -0500 Subject: [PATCH 3/3] Update pg_rst.py --- src/rda_python_miscs/pg_rst.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/rda_python_miscs/pg_rst.py b/src/rda_python_miscs/pg_rst.py index 0c7c4ad..715b256 100755 --- a/src/rda_python_miscs/pg_rst.py +++ b/src/rda_python_miscs/pg_rst.py @@ -19,6 +19,7 @@ import argparse import importlib from os import path as op +from rda_python_common.pg_LOG import PgLOG from rda_python_common.pg_file import PgFile from rda_python_common.pg_util import PgUtil @@ -1157,9 +1158,9 @@ def _load_opts_alias(docname): try: mod = importlib.import_module(modname) except ImportError as exc: - self.pglog( + PgLOG.pglog( "Cannot import module '{}': {}".format(modname, exc), - self.LGWNEX, + PgLOG.LGWNEX, ) # Derive ORIGIN from the module's own file path. @@ -1181,10 +1182,10 @@ def _load_opts_alias(docname): break if opts is None: - self.pglog( + PgLOG.pglog( "Module '{}' does not define OPTS (checked module level and " "all classes defined in the module)".format(modname), - self.LGWNEX, + PgLOG.LGWNEX, ) # ALIAS is optional; default to empty dict. @@ -1236,4 +1237,4 @@ def main(): pg.process_docs(args.docname, opts, alias) if __name__ == "__main__": - main() \ No newline at end of file + main()