From 7820b39f9b6b88d81e7507d9b454301ebaf31b4a Mon Sep 17 00:00:00 2001
From: Alan De Smet <alan.desmet@ssec.wisc.edu>
Date: Fri, 6 Aug 2021 16:11:24 -0500
Subject: [PATCH] Refactoring

---
 example/aitf-update-cache               | 29 +++++++++----------------
 example/{aitfancil.py => aitf/ancil.py} |  0
 example/aitf/argparseexpert.py          | 28 ++++++++++++++++++++++++
 example/{ => aitf}/argparsetools.py     | 25 ---------------------
 example/{ => aitf}/conlog.py            |  0
 5 files changed, 38 insertions(+), 44 deletions(-)
 rename example/{aitfancil.py => aitf/ancil.py} (100%)
 create mode 100644 example/aitf/argparseexpert.py
 rename example/{ => aitf}/argparsetools.py (80%)
 rename example/{ => aitf}/conlog.py (100%)

diff --git a/example/aitf-update-cache b/example/aitf-update-cache
index 3edb58c..da0755b 100755
--- a/example/aitf-update-cache
+++ b/example/aitf-update-cache
@@ -6,30 +6,22 @@ import datetime as dt
 import tempfile
 sys.path.append(os.path.abspath('..'))
 from csppfetch import DownloadStatistics
-import aitfancil
-from conlog import add_console_logging
+import aitf.ancil
+from aitf.conlog import add_console_logging
 
 
 # Environment variable that can hold the path to the cache
 CACHE_ENV="CSPP_GEO_AITF_CACHE"
 
 
-def parse_datetime_from_gfs_filename(filename):
-    return dt.datetime.strptime( filename[:-2], "%Y/%m/%d/gfs.t%Hz.pgrbf")
-
-
-
-
 def parse_args():
-    import argparsetools
-    from argparsetools import arg_duration, arg_datetime
+    from aitf.argparsetools import add_cache_dir_arg, add_verbose_quiet_args, \
+        process_verbosity, SmartFormatter, arg_duration, arg_datetime
 
     """ Parse arguments. Returns a namespace as returned by argparse.ArgumentParser.parse_args().  """
 
     import argparse
 
-    # Special case for -x/--expert, which shows help including normally hidden options
-    #expert_only = argparsetools.process_expert()
 
     default_expiration_days = 7
 
@@ -44,10 +36,10 @@ def parse_args():
             is recommended. The form "20210581450000" (YYYYjjjHHMMSSt) is also
             understood.
             """,
-        formatter_class = argparsetools.SmartFormatter
+        formatter_class = SmartFormatter
         )
 
-    argparsetools.add_cache_dir_arg(ap)
+    add_cache_dir_arg(ap)
 
     ap.add_argument('--keep-old', dest='keep_old', action='store_true',
         help='keep old files; do not remove them')
@@ -71,15 +63,14 @@ def parse_args():
         metavar='DURATION',
         help='how far before --newest to download files (default: %(default)s)')
 
-    argparsetools.add_verbose_quiet_args(ap)
-    #argparsetools.add_expert_arg(ap)
+    add_verbose_quiet_args(ap)
 
     args = ap.parse_args()
 
     if args.oldest is None:
         args.oldest = args.newest - args.download_window
 
-    argparsetools.process_verbosity(args)
+    process_verbosity(args)
 
     return args
 
@@ -93,7 +84,7 @@ def main():
     args = parse_args()
 
     sststats = DownloadStatistics()
-    aitfancil.SST.update_cache(args.dir,
+    aitf.ancil.SST.update_cache(args.dir,
             start_time=args.oldest,
             end_time=args.newest,
             download_stats = sststats,
@@ -101,7 +92,7 @@ def main():
     print_download_report("SST Download Summary", sststats)
 
     gfsstats = DownloadStatistics()
-    aitfancil.GFS.update_cache(args.dir,
+    aitf.ancil.GFS.update_cache(args.dir,
             start_time=args.oldest,
             end_time=args.newest,
             download_stats = gfsstats,
diff --git a/example/aitfancil.py b/example/aitf/ancil.py
similarity index 100%
rename from example/aitfancil.py
rename to example/aitf/ancil.py
diff --git a/example/aitf/argparseexpert.py b/example/aitf/argparseexpert.py
new file mode 100644
index 0000000..41e85bb
--- /dev/null
+++ b/example/aitf/argparseexpert.py
@@ -0,0 +1,28 @@
+# python3
+
+import argparse
+
+def expert_only_show(x): return x
+def expert_only_hide(x): return argparse.SUPPRESS
+
+def handle():
+    # Special case for -x/--expert, which shows help including normally hidden options
+    expert_opts = ["-x", "--expert"]
+    expert_help = False
+    for o in expert_opts:
+        if o in sys.argv:
+            expert_help = True
+            idx = sys.argv.index(o)
+            sys.argv[idx] = '--help'
+            break
+    if expert_help:
+        return expert_only_show
+    return expert_only_hide
+
+def add_argument(parser):
+    # --expert will never be found, as we handle it before argparse is running!
+    # We still want it for documentation, though.
+    parser.add_argument('--expert', '-x', dest='expert', action='store_true',
+        help='show all help, including advanced options, and exit'
+        )
+
diff --git a/example/argparsetools.py b/example/aitf/argparsetools.py
similarity index 80%
rename from example/argparsetools.py
rename to example/aitf/argparsetools.py
index 8e550bb..eaeeaf9 100644
--- a/example/argparsetools.py
+++ b/example/aitf/argparsetools.py
@@ -11,23 +11,6 @@ CACHE_ENV="CSPP_GEO_AITF_CACHE"
 def argerror(msg):
     raise argparse.ArgumentTypeError(msg)
 
-def expert_only_show(x): return x
-def expert_only_hide(x): return argparse.SUPPRESS
-
-def process_expert():
-    # Special case for -x/--expert, which shows help including normally hidden options
-    expert_opts = ["-x", "--expert"]
-    expert_help = False
-    for o in expert_opts:
-        if o in sys.argv:
-            expert_help = True
-            idx = sys.argv.index(o)
-            sys.argv[idx] = '--help'
-            break
-    if expert_help:
-        return expert_only_show
-    return expert_only_hide
-
 def add_verbose_quiet_args(parser):
     g = parser.add_mutually_exclusive_group()
     g.add_argument('--verbose', '-v', dest='verbosity', action='count',
@@ -50,14 +33,6 @@ def process_verbosity(args):
     logging.getLogger().setLevel(args.log_level)
 
 
-def add_expert_arg(parser):
-    # --expert will never be found, as we handle it before argparse is running!
-    # We still want it for documentation, though.
-    parser.add_argument('--expert', '-x', dest='expert', action='store_true',
-        help='show all help, including advanced options, and exit'
-        )
-
-
 CACHE_INVALID = "not specified"
 def add_cache_dir_arg(parser):
     CACHE_DEFAULT = os.environ.get(CACHE_ENV,CACHE_INVALID)
diff --git a/example/conlog.py b/example/aitf/conlog.py
similarity index 100%
rename from example/conlog.py
rename to example/aitf/conlog.py
-- 
GitLab