diff options
author | achhina <aman.s.chhina@gmail.com> | 2023-01-22 06:59:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-22 06:59:31 (GMT) |
commit | 95f5b05a8cad61b296807c14e50896075c4dc1de (patch) | |
tree | 165d16ec14680f38e0a8631603190125351de2b0 /Lib/uuid.py | |
parent | c1c5882359a2899b74c1685a0d4e61d6e232161f (diff) | |
download | cpython-95f5b05a8cad61b296807c14e50896075c4dc1de.zip cpython-95f5b05a8cad61b296807c14e50896075c4dc1de.tar.gz cpython-95f5b05a8cad61b296807c14e50896075c4dc1de.tar.bz2 |
GH-88597: Added command line interface to UUID module. (#99463)
The `uuid` module now supports command line usage.
```python
❯ ./python.exe -m uuid
5f2d57b1-90e8-417c-ba5d-69b9b6f74289
❯ ./python.exe -m uuid -h
usage: uuid.py [-h] [-u {uuid1,uuid3,uuid4,uuid5}] [-ns NAMESPACE] [-n NAME]
...
```
Diffstat (limited to 'Lib/uuid.py')
-rw-r--r-- | Lib/uuid.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py index e863b63..2904b9c 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -728,9 +728,58 @@ def uuid5(namespace, name): hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest() return UUID(bytes=hash[:16], version=5) + +def main(): + """Run the uuid command line interface.""" + uuid_funcs = {"uuid1": uuid1, + "uuid3": uuid3, + "uuid4": uuid4, + "uuid5": uuid5} + uuid_namespace_funcs = ("uuid3", "uuid5") + namespaces = { + "NAMESPACE_DNS": NAMESPACE_DNS, + "NAMESPACE_URL": NAMESPACE_URL, + "NAMESPACE_OID": NAMESPACE_OID, + "NAMESPACE_X500": NAMESPACE_X500 + } + + import argparse + parser = argparse.ArgumentParser( + description="Generates a uuid using the selected uuid function.") + parser.add_argument("-u", "--uuid", choices=uuid_funcs.keys(), default="uuid4", + help="The function to use to generate the uuid. " + "By default uuid4 function is used.") + parser.add_argument("-ns", "--namespace", + help="The namespace used as part of generating the uuid. " + "Only required for uuid3/uuid5 functions.") + parser.add_argument("-n", "--name", + help="The name used as part of generating the uuid. " + "Only required for uuid3/uuid5 functions.") + + args = parser.parse_args() + uuid_func = uuid_funcs[args.uuid] + namespace = args.namespace + name = args.name + + if args.uuid in uuid_namespace_funcs: + if not namespace or not name: + parser.error( + "Incorrect number of arguments. " + f"{args.uuid} requires a namespace and a name. " + "Run 'python -m uuid -h' for more information." + ) + namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace) + print(uuid_func(namespace, name)) + else: + print(uuid_func()) + + # The following standard UUIDs are for use with uuid3() or uuid5(). NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8') NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8') + +if __name__ == "__main__": + main() |