Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
Improve the newtroy_nbuuid.py filter module
Browse files Browse the repository at this point in the history
- Use argparse for better help
- Add url_nbuuid()
- Add dns_nbuuid()
  • Loading branch information
mrled committed May 2, 2018
1 parent 9bdcc25 commit 80365fb
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions filter_plugins/newtroy_nbuuid.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# A UUID filter that works with namespaces

import argparse
import sys
import uuid

Expand All @@ -17,6 +18,8 @@ def nbuuid(name, namespace):
Per RFC4122, a name-based UUID is generated from a 'name' (any input) and a 'namespace'
(itself a UUID which uniquely represents the namespace).
Given the same name and namespace, will always return the same value.
name Any input
namespace A UUID representing the namespace
Must be either a valid uuid.UUID object
Expand All @@ -27,11 +30,20 @@ def nbuuid(name, namespace):
return uuid.uuid5(namespace, str(name))

@staticmethod
def test_nbuuid(name, namespace=None):
"""Show a simple example result of using the nbuuid() static method
def url_nbuuid(name):
"""Generate a name-based UUID in the URL namespace
"""
return uuid.uuid5(uuid.NAMESPACE_URL, name)

Intended only for demo/testing purposes.
@staticmethod
def dns_nbuuid(name):
"""Generate a name-based UUID in the DNS namespace
"""
return uuid.uuid5(uuid.NAMESPACE_DNS, name)

@staticmethod
def test_nbuuid(name, namespace=None):

if namespace == None:
namespace = uuid.uuid4()
print("Example namespace (randomly generated): {}".format(namespace))
Expand All @@ -44,8 +56,39 @@ def test_nbuuid(name, namespace=None):
def filters(self):
return {
'newtroy_nbuuid': self.nbuuid,
'newtroy_url_nbuuid': self.url_nbuuid,
'newtroy_dns_nbuuid': self.dns_nbuuid,
}


def main(*args, **kwargs):
"""Show a simple example result of using the nbuuid() static method
Intended only for demo/testing purposes.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'name', help="Input for a new name-based UUID")
parser.add_argument(
'namespace', nargs='?', default=None, type=uuid.UUID,
help=(
"An optional namespace (which itself must be a UUID) for a name-based UUID. "
"If omitted, a randomly generated namespace will be used. "
"Note that this program will return the same output UUID "
"for the same input name and namespace UUIDs"))
parsed = parser.parse_args()

if not parsed.namespace:
namespace = uuid.uuid4()
print("Example namespace (randomly generated): {}".format(namespace))
else:
namespace = parsed.namespace
print("Attempting to use passed-in namespace: {}".format(namespace))

namebased_uuid = FilterModule.nbuuid(parsed.name, namespace)
print("UUID for input '{}' in namespace '{}': {}".format(
parsed.name, namespace, namebased_uuid))


if __name__ == '__main__':
FilterModule.test_nbuuid(*sys.argv[1:])
main(*sys.argv[1:])

0 comments on commit 80365fb

Please sign in to comment.