changeset 231:6d8443878a00

Use argparse in genpwd.py now. As side effect there is now a help and version flag. And the help message are slightly better. Also added a genpwd.py driver program.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 07 Feb 2025 13:30:33 +0100
parents ccbb6905914e
children 7ac8a2537bc9
files cutils/genpwd.py genpwd.py
diffstat 2 files changed, 57 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/genpwd.py	Fri Feb 07 12:58:55 2025 +0100
+++ b/cutils/genpwd.py	Fri Feb 07 13:30:33 2025 +0100
@@ -18,57 +18,61 @@
 
 from __future__ import (division, absolute_import, print_function)
 
-import getopt
+import argparse
+import base64
+import os
 import sys
-import os
-import base64
 
 from . import (__version__, __revision__)
 
 
-WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz0123456789.,-_;!()[]{}*"
-WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz23456789.,-_;!"
-WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS
+WEB_CHARS = b"ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz" \
+            b"0123456789.,-_;!()[]{}*"
+WEB_SAFE_CHARS = b"ABCDEFGHJKLMNPQRSTUVWYXZabcdefghijkmnopqrstuvwxyz" \
+                 b"23456789.,-_;!"
+WEB_SAFE2_CHARS = b".,-_;!" + WEB_SAFE_CHARS   # prefer punctionation chars
 
 PY2 = sys.version_info[0] <= 2
 
 
-def main():
-    opt_type = "web-safe2"
-    opts, args = getopt.getopt(sys.argv[1:],
-                               "t:",
-                               ["type="])
-    for opt, val in opts:
-        if opt in ("-t", "--type"):
-            if val not in ("web", "web-safe", "web-safe2",
-                           "base64", "base32", "ascii85", ):
-                raise getopt.GetoptError("invalid type: %s" % val, "")
-            opt_type = val
-        else:
-            raise RuntimeError("inconsistent getopt handling")
+def main(argv=None):
+    aparser = argparse.ArgumentParser(
+        description="A simple password generator for password of a given"
+                    " length within a character repertoire",
+        fromfile_prefix_chars='@')
+    aparser.add_argument(
+        "--repertoire", "--type", "-t",
+        choices=("web", "web-safe", "web-safe2",
+                 "base64", "base32", "ascii85", ),
+        default="web-safe2",
+        help="Select the character repertoire. Default: web-safe2")
+    aparser.add_argument(
+        "--version", "-v", action="version",
+        version="%s (rv:%s)" % (__version__, __revision__))
+    aparser.add_argument(
+        "req_length", metavar="OUTPUT-LENGTH", type=int,
+        help="The required length of the generated password")
 
-    try:
-        req_length = int(args[0], 10)
-    except IndexError:
-        raise getopt.GetoptError("no length given")
+    opts = aparser.parse_args(args=argv)
 
-    if opt_type == "web":
-        pwd = gen_web(req_length, WEB_CHARS)
-    elif opt_type == "web-safe":
-        pwd = gen_web(req_length, WEB_SAFE_CHARS)
-    elif opt_type == "web-safe2":
-        pwd = gen_web(req_length, WEB_SAFE2_CHARS)
-    elif opt_type == "base64":
+    if opts.repertoire == "web":
+        pwd = gen_web(opts.req_length, WEB_CHARS)
+    elif opts.repertoire == "web-safe":
+        pwd = gen_web(opts.req_length, WEB_SAFE_CHARS)
+    elif opts.repertoire == "web-safe2":
+        pwd = gen_web(opts.req_length, WEB_SAFE2_CHARS)
+    elif opts.repertoire == "base64":
         encoder = base64.b64encode
-        pwd = gen_bin(req_length, encoder)
-    elif opt_type == "base32":
+        pwd = gen_bin(opts.req_length, encoder)
+    elif opts.repertoire == "base32":
         encoder = base64.b32encode
-        pwd = gen_bin(req_length, encoder)
-    elif opt_type == "ascii85":
+        pwd = gen_bin(opts.req_length, encoder)
+    elif opts.repertoire == "ascii85":
         encoder = base64.a85encode
-        pwd = gen_bin(req_length, encoder)
+        pwd = gen_bin(opts.req_length, encoder)
     else:
-        raise NotImplementedError("type not yet implemented: %s" % opt_type)
+        raise NotImplementedError("type not yet implemented: %s"
+                                  % opts.repertoire)
     print(pwd)
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/genpwd.py	Fri Feb 07 13:30:33 2025 +0100
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+# :-
+# :Copyright: (c) 2025 Franz Glasner
+# :License:   BSD-3-Clause
+# :-
+r"""Pure Python implementation of a directory tree checksum.
+
+"""
+
+from __future__ import absolute_import
+
+import sys
+
+import cutils.genpwd
+
+
+sys.exit(cutils.genpwd.main())