Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUIDv5 namespace for Kolibri ecosystem #111

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ celerybeat-schedule

# dotenv
.env
.envrc

# virtualenv
venv/
Expand Down
7 changes: 7 additions & 0 deletions js/Uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// -*- coding: utf-8 -*-
// Generated by scripts/generate_from_specs.py
// Uuid

export default {
KOLIBRI_ECOSYSTEM_UUID_NAMESPACE: "237627f4-3601-11ee-a67c-595de72bede7",
};
13 changes: 13 additions & 0 deletions le_utils/constants/uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# Generated by scripts/generate_from_specs.py
from __future__ import unicode_literals

# Uuid

KOLIBRI_ECOSYSTEM_UUID_NAMESPACE = "237627f4-3601-11ee-a67c-595de72bede7"

choices = ((KOLIBRI_ECOSYSTEM_UUID_NAMESPACE, "Kolibri Ecosystem Uuid Namespace"),)

UUIDLIST = [
KOLIBRI_ECOSYSTEM_UUID_NAMESPACE,
]
19 changes: 19 additions & 0 deletions le_utils/uuidv5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import uuid

from le_utils.constants.uuid import KOLIBRI_ECOSYSTEM_UUID_NAMESPACE

try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse


def generate_ecosystem_namespaced_uuid(hostname_or_url):
"""
Generate a UUID for a given URL, namespaced by the Kolibri ecosystem UUID namespace.
:param hostname_or_url: A URL to generate a UUID-- only the netloc/host is used
:return: A namespaced UUID
"""
_url = urlparse(hostname_or_url)

return uuid.uuid5(uuid.UUID(KOLIBRI_ECOSYSTEM_UUID_NAMESPACE), _url.netloc)
10 changes: 7 additions & 3 deletions scripts/generate_from_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ def read_constants_specs():
with open(constants_spec_file) as json_constants_spec_file:
constants_spec = json.load(json_constants_spec_file)
key = snake_to_pascal(constants_spec_file.split("-")[-1].split(".")[0])
constants_outputs[key] = OrderedDict(
[(a.upper(), a) for a in sorted(constants_spec)]
)
if isinstance(constants_spec, dict):
constants_outputs[key] = constants_spec
else:
# assume it's a list and convert to an OrderedDict
constants_outputs[key] = OrderedDict(
[(a.upper(), a) for a in sorted(constants_spec)]
)
return constants_outputs


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
setup(
name="le-utils",
packages=find_packages(),
version="0.2.0",
version="0.2.1",
description="LE-Utils contains shared constants used in Kolibri, Ricecooker, and Kolibri Studio.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
3 changes: 3 additions & 0 deletions spec/constants-uuid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"KOLIBRI_ECOSYSTEM_UUID_NAMESPACE": "237627f4-3601-11ee-a67c-595de72bede7"
}
14 changes: 14 additions & 0 deletions tests/test_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import uuid

from le_utils.constants.uuid import KOLIBRI_ECOSYSTEM_UUID_NAMESPACE
from le_utils.uuidv5 import generate_ecosystem_namespaced_uuid


def test_namespace_does_not_change():
assert KOLIBRI_ECOSYSTEM_UUID_NAMESPACE == "237627f4-3601-11ee-a67c-595de72bede7"


def test_generate_ecosystem_namespaced_uuid():
assert generate_ecosystem_namespaced_uuid(
"https://studio.learningequality.org"
) == uuid.UUID("2d04ed86-aa8c-519f-ae78-a250e03d8482")
Loading