changeset 249:f161448d673e

genpwd: allow with "--repertoire REPERTOIRE" to select from given character REPERTOIRE
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Feb 2025 13:26:39 +0100
parents 4796e5da04ee
children 48f89d312309
files cutils/genpwd.py
diffstat 1 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/genpwd.py	Sat Feb 08 12:57:14 2025 +0100
+++ b/cutils/genpwd.py	Sat Feb 08 13:26:39 2025 +0100
@@ -73,7 +73,8 @@
     aparser.add_argument(
         "--version", "-v", action="version",
         version="%s (rv:%s)" % (__version__, __revision__))
-    aparser.add_argument(
+    group = aparser.add_mutually_exclusive_group()
+    group.add_argument(
         "--algorithm", "-a",
         choices=("web", "safe-web", "safe-web-2",
                  "uri", "safe-uri", "safe-uri-2",
@@ -89,9 +90,17 @@
 All repertoires that end with `-safe' or `safe-2' do not contain visually
 similar characters (currently `0O' or `Il1').
 All repertoires that end with `-2' are variants with a bias to punctuation
-characters.        
-Default: safe-ascii
-""")
+characters.
+This is incompatible with option `--repertoire'.
+Default: safe-ascii""")
+    group.add_argument(
+        "--repertoire", "-r",
+        action="store", metavar="REPERTOIRE",
+        help="""
+Select from given character repertoire.
+The repertoire must be characters from the ISO-8859-15 character set.
+An empty REPERTOIRE selects implicitly the default algorithm.
+This is incompatible with option `--algorithm'.""")
     aparser.add_argument(
         "-E", dest="use_bin_length", action="store_true",
         help="For some repertoires make OUTPUT-LENGTH the number of bytes"
@@ -102,7 +111,15 @@
 
     opts = aparser.parse_args(args=argv)
 
-    if opts.algorithm == "web":
+    if opts.repertoire:
+        try:
+            repertoire = (opts.repertoire
+                          if PY2
+                          else opts.repertoire.encode("iso-8859-15"))
+        except UnicodeError:
+            raise ValueError("non ISO-8859-15 character in given repertoire")
+        pwd = gen_from_repertoire(opts.req_length, repertoire)
+    elif opts.algorithm == "web":
         pwd = gen_from_repertoire(opts.req_length, WEB_CHARS)
     elif opts.algorithm == "safe-web":
         pwd = gen_from_repertoire(opts.req_length, SAFE_WEB_CHARS)
@@ -150,7 +167,7 @@
         if len(pwd) != opts.req_length:
             raise AssertionError("internal length mismatch")
     if not PY2:
-        pwd = pwd.decode("ascii")
+        pwd = pwd.decode("iso-8859-15")
     print(pwd)