summaryrefslogtreecommitdiff
path: root/python/slugid
diff options
context:
space:
mode:
Diffstat (limited to 'python/slugid')
-rw-r--r--python/slugid/.gitignore57
-rw-r--r--python/slugid/.travis.yml27
-rw-r--r--python/slugid/LICENSE363
-rw-r--r--python/slugid/README.rst121
-rw-r--r--python/slugid/requirements.txt2
-rw-r--r--python/slugid/setup.py39
-rw-r--r--python/slugid/slugid/__init__.py43
-rw-r--r--python/slugid/slugid/slugid.py43
-rw-r--r--python/slugid/test.py167
-rw-r--r--python/slugid/tox.ini26
10 files changed, 888 insertions, 0 deletions
diff --git a/python/slugid/.gitignore b/python/slugid/.gitignore
new file mode 100644
index 0000000000..ba74660506
--- /dev/null
+++ b/python/slugid/.gitignore
@@ -0,0 +1,57 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+# Usually these files are written by a python script from a template
+# before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*,cover
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
diff --git a/python/slugid/.travis.yml b/python/slugid/.travis.yml
new file mode 100644
index 0000000000..dcc430347f
--- /dev/null
+++ b/python/slugid/.travis.yml
@@ -0,0 +1,27 @@
+language: python
+python:
+ - 2.7
+
+install:
+ - pip install -r requirements.txt
+
+script:
+ - tox
+
+after_script:
+ - tox -e coveralls
+
+# currently cannot customise per user fork, see:
+# https://github.com/travis-ci/travis-ci/issues/1094
+# please comment out this section in your personal fork!
+notifications:
+ irc:
+ channels:
+ - "irc.mozilla.org#taskcluster-bots"
+ on_success: always
+ on_failure: always
+ template:
+ - "\x02%{repository}\x0314#%{build_number}\x03\x02 (%{branch} - %{commit} : %{author}): \x02\x0312%{message}\x02\x03"
+ - "\x02Change view\x02 : \x0314%{compare_url}\x03"
+ - "\x02Build details\x02 : \x0314%{build_url}\x03"
+ - "\x02Commit message\x02 : \x0314%{commit_message}\x03"
diff --git a/python/slugid/LICENSE b/python/slugid/LICENSE
new file mode 100644
index 0000000000..e87a115e46
--- /dev/null
+++ b/python/slugid/LICENSE
@@ -0,0 +1,363 @@
+Mozilla Public License, version 2.0
+
+1. Definitions
+
+1.1. "Contributor"
+
+ means each individual or legal entity that creates, contributes to the
+ creation of, or owns Covered Software.
+
+1.2. "Contributor Version"
+
+ means the combination of the Contributions of others (if any) used by a
+ Contributor and that particular Contributor's Contribution.
+
+1.3. "Contribution"
+
+ means Covered Software of a particular Contributor.
+
+1.4. "Covered Software"
+
+ means Source Code Form to which the initial Contributor has attached the
+ notice in Exhibit A, the Executable Form of such Source Code Form, and
+ Modifications of such Source Code Form, in each case including portions
+ thereof.
+
+1.5. "Incompatible With Secondary Licenses"
+ means
+
+ a. that the initial Contributor has attached the notice described in
+ Exhibit B to the Covered Software; or
+
+ b. that the Covered Software was made available under the terms of
+ version 1.1 or earlier of the License, but not also under the terms of
+ a Secondary License.
+
+1.6. "Executable Form"
+
+ means any form of the work other than Source Code Form.
+
+1.7. "Larger Work"
+
+ means a work that combines Covered Software with other material, in a
+ separate file or files, that is not Covered Software.
+
+1.8. "License"
+
+ means this document.
+
+1.9. "Licensable"
+
+ means having the right to grant, to the maximum extent possible, whether
+ at the time of the initial grant or subsequently, any and all of the
+ rights conveyed by this License.
+
+1.10. "Modifications"
+
+ means any of the following:
+
+ a. any file in Source Code Form that results from an addition to,
+ deletion from, or modification of the contents of Covered Software; or
+
+ b. any new file in Source Code Form that contains any Covered Software.
+
+1.11. "Patent Claims" of a Contributor
+
+ means any patent claim(s), including without limitation, method,
+ process, and apparatus claims, in any patent Licensable by such
+ Contributor that would be infringed, but for the grant of the License,
+ by the making, using, selling, offering for sale, having made, import,
+ or transfer of either its Contributions or its Contributor Version.
+
+1.12. "Secondary License"
+
+ means either the GNU General Public License, Version 2.0, the GNU Lesser
+ General Public License, Version 2.1, the GNU Affero General Public
+ License, Version 3.0, or any later versions of those licenses.
+
+1.13. "Source Code Form"
+
+ means the form of the work preferred for making modifications.
+
+1.14. "You" (or "Your")
+
+ means an individual or a legal entity exercising rights under this
+ License. For legal entities, "You" includes any entity that controls, is
+ controlled by, or is under common control with You. For purposes of this
+ definition, "control" means (a) the power, direct or indirect, to cause
+ the direction or management of such entity, whether by contract or
+ otherwise, or (b) ownership of more than fifty percent (50%) of the
+ outstanding shares or beneficial ownership of such entity.
+
+
+2. License Grants and Conditions
+
+2.1. Grants
+
+ Each Contributor hereby grants You a world-wide, royalty-free,
+ non-exclusive license:
+
+ a. under intellectual property rights (other than patent or trademark)
+ Licensable by such Contributor to use, reproduce, make available,
+ modify, display, perform, distribute, and otherwise exploit its
+ Contributions, either on an unmodified basis, with Modifications, or
+ as part of a Larger Work; and
+
+ b. under Patent Claims of such Contributor to make, use, sell, offer for
+ sale, have made, import, and otherwise transfer either its
+ Contributions or its Contributor Version.
+
+2.2. Effective Date
+
+ The licenses granted in Section 2.1 with respect to any Contribution
+ become effective for each Contribution on the date the Contributor first
+ distributes such Contribution.
+
+2.3. Limitations on Grant Scope
+
+ The licenses granted in this Section 2 are the only rights granted under
+ this License. No additional rights or licenses will be implied from the
+ distribution or licensing of Covered Software under this License.
+ Notwithstanding Section 2.1(b) above, no patent license is granted by a
+ Contributor:
+
+ a. for any code that a Contributor has removed from Covered Software; or
+
+ b. for infringements caused by: (i) Your and any other third party's
+ modifications of Covered Software, or (ii) the combination of its
+ Contributions with other software (except as part of its Contributor
+ Version); or
+
+ c. under Patent Claims infringed by Covered Software in the absence of
+ its Contributions.
+
+ This License does not grant any rights in the trademarks, service marks,
+ or logos of any Contributor (except as may be necessary to comply with
+ the notice requirements in Section 3.4).
+
+2.4. Subsequent Licenses
+
+ No Contributor makes additional grants as a result of Your choice to
+ distribute the Covered Software under a subsequent version of this
+ License (see Section 10.2) or under the terms of a Secondary License (if
+ permitted under the terms of Section 3.3).
+
+2.5. Representation
+
+ Each Contributor represents that the Contributor believes its
+ Contributions are its original creation(s) or it has sufficient rights to
+ grant the rights to its Contributions conveyed by this License.
+
+2.6. Fair Use
+
+ This License is not intended to limit any rights You have under
+ applicable copyright doctrines of fair use, fair dealing, or other
+ equivalents.
+
+2.7. Conditions
+
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
+ Section 2.1.
+
+
+3. Responsibilities
+
+3.1. Distribution of Source Form
+
+ All distribution of Covered Software in Source Code Form, including any
+ Modifications that You create or to which You contribute, must be under
+ the terms of this License. You must inform recipients that the Source
+ Code Form of the Covered Software is governed by the terms of this
+ License, and how they can obtain a copy of this License. You may not
+ attempt to alter or restrict the recipients' rights in the Source Code
+ Form.
+
+3.2. Distribution of Executable Form
+
+ If You distribute Covered Software in Executable Form then:
+
+ a. such Covered Software must also be made available in Source Code Form,
+ as described in Section 3.1, and You must inform recipients of the
+ Executable Form how they can obtain a copy of such Source Code Form by
+ reasonable means in a timely manner, at a charge no more than the cost
+ of distribution to the recipient; and
+
+ b. You may distribute such Executable Form under the terms of this
+ License, or sublicense it under different terms, provided that the
+ license for the Executable Form does not attempt to limit or alter the
+ recipients' rights in the Source Code Form under this License.
+
+3.3. Distribution of a Larger Work
+
+ You may create and distribute a Larger Work under terms of Your choice,
+ provided that You also comply with the requirements of this License for
+ the Covered Software. If the Larger Work is a combination of Covered
+ Software with a work governed by one or more Secondary Licenses, and the
+ Covered Software is not Incompatible With Secondary Licenses, this
+ License permits You to additionally distribute such Covered Software
+ under the terms of such Secondary License(s), so that the recipient of
+ the Larger Work may, at their option, further distribute the Covered
+ Software under the terms of either this License or such Secondary
+ License(s).
+
+3.4. Notices
+
+ You may not remove or alter the substance of any license notices
+ (including copyright notices, patent notices, disclaimers of warranty, or
+ limitations of liability) contained within the Source Code Form of the
+ Covered Software, except that You may alter any license notices to the
+ extent required to remedy known factual inaccuracies.
+
+3.5. Application of Additional Terms
+
+ You may choose to offer, and to charge a fee for, warranty, support,
+ indemnity or liability obligations to one or more recipients of Covered
+ Software. However, You may do so only on Your own behalf, and not on
+ behalf of any Contributor. You must make it absolutely clear that any
+ such warranty, support, indemnity, or liability obligation is offered by
+ You alone, and You hereby agree to indemnify every Contributor for any
+ liability incurred by such Contributor as a result of warranty, support,
+ indemnity or liability terms You offer. You may include additional
+ disclaimers of warranty and limitations of liability specific to any
+ jurisdiction.
+
+4. Inability to Comply Due to Statute or Regulation
+
+ If it is impossible for You to comply with any of the terms of this License
+ with respect to some or all of the Covered Software due to statute,
+ judicial order, or regulation then You must: (a) comply with the terms of
+ this License to the maximum extent possible; and (b) describe the
+ limitations and the code they affect. Such description must be placed in a
+ text file included with all distributions of the Covered Software under
+ this License. Except to the extent prohibited by statute or regulation,
+ such description must be sufficiently detailed for a recipient of ordinary
+ skill to be able to understand it.
+
+5. Termination
+
+5.1. The rights granted under this License will terminate automatically if You
+ fail to comply with any of its terms. However, if You become compliant,
+ then the rights granted under this License from a particular Contributor
+ are reinstated (a) provisionally, unless and until such Contributor
+ explicitly and finally terminates Your grants, and (b) on an ongoing
+ basis, if such Contributor fails to notify You of the non-compliance by
+ some reasonable means prior to 60 days after You have come back into
+ compliance. Moreover, Your grants from a particular Contributor are
+ reinstated on an ongoing basis if such Contributor notifies You of the
+ non-compliance by some reasonable means, this is the first time You have
+ received notice of non-compliance with this License from such
+ Contributor, and You become compliant prior to 30 days after Your receipt
+ of the notice.
+
+5.2. If You initiate litigation against any entity by asserting a patent
+ infringement claim (excluding declaratory judgment actions,
+ counter-claims, and cross-claims) alleging that a Contributor Version
+ directly or indirectly infringes any patent, then the rights granted to
+ You by any and all Contributors for the Covered Software under Section
+ 2.1 of this License shall terminate.
+
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
+ license agreements (excluding distributors and resellers) which have been
+ validly granted by You or Your distributors under this License prior to
+ termination shall survive termination.
+
+6. Disclaimer of Warranty
+
+ Covered Software is provided under this License on an "as is" basis,
+ without warranty of any kind, either expressed, implied, or statutory,
+ including, without limitation, warranties that the Covered Software is free
+ of defects, merchantable, fit for a particular purpose or non-infringing.
+ The entire risk as to the quality and performance of the Covered Software
+ is with You. Should any Covered Software prove defective in any respect,
+ You (not any Contributor) assume the cost of any necessary servicing,
+ repair, or correction. This disclaimer of warranty constitutes an essential
+ part of this License. No use of any Covered Software is authorized under
+ this License except under this disclaimer.
+
+7. Limitation of Liability
+
+ Under no circumstances and under no legal theory, whether tort (including
+ negligence), contract, or otherwise, shall any Contributor, or anyone who
+ distributes Covered Software as permitted above, be liable to You for any
+ direct, indirect, special, incidental, or consequential damages of any
+ character including, without limitation, damages for lost profits, loss of
+ goodwill, work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses, even if such party shall have been
+ informed of the possibility of such damages. This limitation of liability
+ shall not apply to liability for death or personal injury resulting from
+ such party's negligence to the extent applicable law prohibits such
+ limitation. Some jurisdictions do not allow the exclusion or limitation of
+ incidental or consequential damages, so this exclusion and limitation may
+ not apply to You.
+
+8. Litigation
+
+ Any litigation relating to this License may be brought only in the courts
+ of a jurisdiction where the defendant maintains its principal place of
+ business and such litigation shall be governed by laws of that
+ jurisdiction, without reference to its conflict-of-law provisions. Nothing
+ in this Section shall prevent a party's ability to bring cross-claims or
+ counter-claims.
+
+9. Miscellaneous
+
+ This License represents the complete agreement concerning the subject
+ matter hereof. If any provision of this License is held to be
+ unenforceable, such provision shall be reformed only to the extent
+ necessary to make it enforceable. Any law or regulation which provides that
+ the language of a contract shall be construed against the drafter shall not
+ be used to construe this License against a Contributor.
+
+
+10. Versions of the License
+
+10.1. New Versions
+
+ Mozilla Foundation is the license steward. Except as provided in Section
+ 10.3, no one other than the license steward has the right to modify or
+ publish new versions of this License. Each version will be given a
+ distinguishing version number.
+
+10.2. Effect of New Versions
+
+ You may distribute the Covered Software under the terms of the version
+ of the License under which You originally received the Covered Software,
+ or under the terms of any subsequent version published by the license
+ steward.
+
+10.3. Modified Versions
+
+ If you create software not governed by this License, and you want to
+ create a new license for such software, you may create and use a
+ modified version of this License if you rename the license and remove
+ any references to the name of the license steward (except to note that
+ such modified license differs from this License).
+
+10.4. Distributing Source Code Form that is Incompatible With Secondary
+ Licenses If You choose to distribute Source Code Form that is
+ Incompatible With Secondary Licenses under the terms of this version of
+ the License, the notice described in Exhibit B of this License must be
+ attached.
+
+Exhibit A - Source Code Form License Notice
+
+ This Source Code Form is subject to the
+ terms of the Mozilla Public License, v.
+ 2.0. If a copy of the MPL was not
+ distributed with this file, You can
+ obtain one at
+ http://mozilla.org/MPL/2.0/.
+
+If it is not possible or desirable to put the notice in a particular file,
+then You may include the notice in a location (such as a LICENSE file in a
+relevant directory) where a recipient would be likely to look for such a
+notice.
+
+You may add additional accurate notices of copyright ownership.
+
+Exhibit B - "Incompatible With Secondary Licenses" Notice
+
+ This Source Code Form is "Incompatible
+ With Secondary Licenses", as defined by
+ the Mozilla Public License, v. 2.0.
+
diff --git a/python/slugid/README.rst b/python/slugid/README.rst
new file mode 100644
index 0000000000..587cd70045
--- /dev/null
+++ b/python/slugid/README.rst
@@ -0,0 +1,121 @@
+slugid.py - Compressed UUIDs for python
+=======================================
+
+.. image:: https://tools.taskcluster.net/lib/assets/taskcluster-120.png
+
+|Build Status| |Coverage Status| |License| |pypi Version| |Downloads|
+
+A python module for generating v4 UUIDs and encoding them into 22 character
+URL-safe base64 slug representation (see `RFC 4648 sec. 5`_).
+
+Slugs are url-safe base64 encoded v4 uuids, stripped of base64 ``=`` padding.
+
+There are two methods for generating slugs - ``slugid.v4()`` and
+``slugid.nice()``.
+
+- The ``slugid.v4()`` method returns a slug from a randomly generated v4 uuid.
+- The ``slugid.nice()`` method returns a v4 slug which conforms to a set of
+ "nice" properties. At the moment the only "nice" property is that the slug
+ starts with ``[A-Za-f]``, which in turn implies that the first (most
+ significant) bit of its associated uuid is set to 0.
+
+The purpose of the ``slugid.nice()`` method is to support having slugids which
+can be used in more contexts safely. Regular slugids can safely be used in
+urls, and for example in AMQP routing keys. However, slugs beginning with ``-``
+may cause problems when used as command line parameters.
+
+In contrast, slugids generated by the ``slugid.nice()`` method can safely be
+used as command line parameters. This comes at a cost to entropy (121 bits vs
+122 bits for regular v4 slugs).
+
+Slug consumers should consider carefully which of these two slug generation
+methods to call. Is it more important to have maximum entropy, or to have
+slugids that do not need special treatment when used as command line
+parameters? This is especially important if you are providing a service which
+supplies slugs to unexpecting tool developers downstream, who may not realise
+the risks of using your regular v4 slugs as command line parameters, especially
+since this would arise only as an intermittent issue (one time in 64).
+
+Generated slugs take the form ``[A-Za-z0-9_-]{22}``, or more precisely:
+
+- ``slugid.v4()`` slugs conform to
+ ``[A-Za-z0-9_-]{8}[Q-T][A-Za-z0-9_-][CGKOSWaeimquy26-][A-Za-z0-9_-]{10}[AQgw]``
+
+- ``slugid.nice()`` slugs conform to
+ ``[A-Za-f][A-Za-z0-9_-]{7}[Q-T][A-Za-z0-9_-][CGKOSWaeimquy26-][A-Za-z0-9_-]{10}[AQgw]``
+
+RFC 4122 defines the setting of 6 bits of the v4 UUID which implies v4 slugs
+provide 128 - 6 = 122 bits entropy. Due to the (un)setting of the first bit
+of "nice" slugs, nice slugs provide therefore 121 bits entropy.
+
+
+Usage
+-----
+
+.. code-block:: python
+
+ import slugid
+
+ # Generate "nice" URL-safe base64 encoded UUID version 4 (random)
+ slug = slugid.nice() # a8_YezW8T7e1jLxG7evy-A
+
+ # Alternative, if slugs will not be used as command line parameters
+ slug = slugid.v4() # -9OpXaCORAaFh4sJRk7PUA
+
+ # Get python uuid.UUID object
+ uuid = slugid.decode(slug)
+
+ # Compress to slug again
+ assert(slug == slugid.encode(uuid))
+
+
+RNG Characteristics
+-------------------
+UUID generation is performed by the built-in python `uuid library`_ which does
+not document its randomness, but falls back to system uuid-generation libraries
+where available, then urandom, then random. Therefore generated slugids match
+these rng characteristics.
+
+License
+-------
+The ``slugid`` library is released on the MPL 2.0 license, see the ``LICENSE``
+for complete license.
+
+Testing
+-------
+
+.. code-block:: bash
+
+ pip install -r requirements.txt
+ tox
+
+Publishing
+----------
+To republish this library to pypi.python.org, update the version number in
+``slugid/__init__.py``, commit it, push to github, and then run:
+
+.. code-block:: bash
+
+ # delete stale versions
+ rm -rf dist
+
+ # build source package
+ python setup.py sdist
+
+ # publish it
+ twine upload -s dist/*
+
+
+.. _RFC 4648 sec. 5: http://tools.ietf.org/html/rfc4648#section-5
+.. _uuid library: https://docs.python.org/2/library/uuid.html
+
+.. |Build Status| image:: https://travis-ci.org/taskcluster/slugid.py.svg?branch=master
+ :target: http://travis-ci.org/taskcluster/slugid.py
+.. |Coverage Status| image:: https://coveralls.io/repos/taskcluster/slugid.py/badge.svg?branch=master&service=github
+ :target: https://coveralls.io/github/taskcluster/slugid.py?branch=master
+.. |License| image:: https://img.shields.io/badge/license-MPL%202.0-orange.svg
+ :target: https://github.com/taskcluster/slugid.py/blob/master/LICENSE
+.. |pypi Version| image:: https://img.shields.io/pypi/v/slugid.svg
+ :target: https://pypi.python.org/pypi/slugid
+.. |Downloads| image:: https://img.shields.io/pypi/dm/slugid.svg
+ :target: https://pypi.python.org/pypi/slugid
diff --git a/python/slugid/requirements.txt b/python/slugid/requirements.txt
new file mode 100644
index 0000000000..16caa8d626
--- /dev/null
+++ b/python/slugid/requirements.txt
@@ -0,0 +1,2 @@
+tox
+twine
diff --git a/python/slugid/setup.py b/python/slugid/setup.py
new file mode 100644
index 0000000000..d7c8b328bd
--- /dev/null
+++ b/python/slugid/setup.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+import re
+
+from codecs import open
+
+try:
+ from setuptools import setup
+except ImportError:
+ from distutils.core import setup
+
+packages = [
+ 'slugid',
+]
+
+version = ''
+with open('slugid/__init__.py', 'r') as fd:
+ version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
+ fd.read(), re.MULTILINE).group(1)
+
+if not version:
+ raise RuntimeError('Cannot find version information')
+
+setup(
+ name='slugid',
+ version=version,
+ description='Base64 encoded uuid v4 slugs',
+ author='Pete Moore',
+ author_email='pmoore@mozilla.com',
+ url='http://taskcluster.github.io/slugid.py',
+ packages=packages,
+ package_data={'': ['LICENSE', 'README.md']},
+ license='MPL 2.0',
+ classifiers=(
+ 'Intended Audience :: Developers',
+ 'Natural Language :: English',
+ 'Programming Language :: Python :: 2.7',
+ ),
+)
diff --git a/python/slugid/slugid/__init__.py b/python/slugid/slugid/__init__.py
new file mode 100644
index 0000000000..ca7de07e24
--- /dev/null
+++ b/python/slugid/slugid/__init__.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+
+# **************
+# * Slugid API *
+# **************
+#
+# @)@)
+# _|_| ( )
+# _(___,`\ _,--------------._ (( /`, ))
+# `==` `*-_,' O `~._ ( ( _/ | ) )
+# `, : o } `~._.~` * ',
+# \ - _ O - ,'
+# | ; - - " ; o /
+# | O o ,-`
+# \ _,-:""""""'`:-._ - . O /
+# `""""""~'` `._ _,-`
+# """"""
+
+"""
+SlugID: Base 64 encoded v4 UUIDs
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Usage:
+
+ >>> import slugid
+ >>> s = slugid.nice()
+ >>> s
+ eWIgwMgxSfeXQ36iPbOxiQ
+ >>> u = slugid.decode(s)
+ >>> u
+ UUID('796220c0-c831-49f7-9743-7ea23db3b189')
+ >>> slugid.encode(u)
+ eWIgwMgxSfeXQ36iPbOxiQ
+ >>> slugid.v4()
+ -9OpXaCORAaFh4sJRk7PUA
+"""
+
+__title__ = 'slugid'
+__version__ = '1.0.6'
+__author__ = 'Peter Moore'
+__license__ = 'MPL 2.0'
+
+from .slugid import decode, encode, nice, v4
diff --git a/python/slugid/slugid/slugid.py b/python/slugid/slugid/slugid.py
new file mode 100644
index 0000000000..cd7dc9ab97
--- /dev/null
+++ b/python/slugid/slugid/slugid.py
@@ -0,0 +1,43 @@
+# Licensed under the Mozilla Public Licence 2.0.
+# https://www.mozilla.org/en-US/MPL/2.0
+
+import uuid
+import base64
+
+def encode(uuid_):
+ """
+ Returns the given uuid.UUID object as a 22 character slug. This can be a
+ regular v4 slug or a "nice" slug.
+ """
+ return base64.urlsafe_b64encode(uuid_.bytes)[:-2] # Drop '==' padding
+
+
+def decode(slug):
+ """
+ Returns the uuid.UUID object represented by the given v4 or "nice" slug
+ """
+ return uuid.UUID(bytes=base64.urlsafe_b64decode(slug + '==')) # b64 padding
+
+
+def v4():
+ """
+ Returns a randomly generated uuid v4 compliant slug
+ """
+ return base64.urlsafe_b64encode(uuid.uuid4().bytes)[:-2] # Drop '==' padding
+
+
+def nice():
+ """
+ Returns a randomly generated uuid v4 compliant slug which conforms to a set
+ of "nice" properties, at the cost of some entropy. Currently this means one
+ extra fixed bit (the first bit of the uuid is set to 0) which guarantees the
+ slug will begin with [A-Za-f]. For example such slugs don't require special
+ handling when used as command line parameters (whereas non-nice slugs may
+ start with `-` which can confuse command line tools).
+
+ Potentially other "nice" properties may be added in future to further
+ restrict the range of potential uuids that may be generated.
+ """
+ rawBytes = uuid.uuid4().bytes
+ rawBytes = chr(ord(rawBytes[0]) & 0x7f) + rawBytes[1:] # Ensure slug starts with [A-Za-f]
+ return base64.urlsafe_b64encode(rawBytes)[:-2] # Drop '==' padding
diff --git a/python/slugid/test.py b/python/slugid/test.py
new file mode 100644
index 0000000000..55103453a3
--- /dev/null
+++ b/python/slugid/test.py
@@ -0,0 +1,167 @@
+# Licensed under the Mozilla Public Licence 2.0.
+# https://www.mozilla.org/en-US/MPL/2.0
+
+import uuid
+import slugid
+
+
+def testEncode():
+ """ Test that we can correctly encode a "non-nice" uuid (with first bit
+ set) to its known slug. The specific uuid was chosen since it has a slug
+ which contains both `-` and `_` characters."""
+
+ # 10000000010011110011111111001000110111111100101101001011000001101000100111111011101011101111101011010101111000011000011101010100....
+ # <8 ><0 ><4 ><f ><3 ><f ><c ><8 ><d ><f ><c ><b ><4 ><b ><0 ><6 ><8 ><9 ><f ><b ><a ><e ><f ><a ><d ><5 ><e ><1 ><8 ><7 ><5 ><4 >
+ # < g >< E >< 8 >< _ >< y >< N >< _ >< L >< S >< w >< a >< J >< - >< 6 >< 7 >< 6 >< 1 >< e >< G >< H >< V >< A >
+ uuid_ = uuid.UUID('{804f3fc8-dfcb-4b06-89fb-aefad5e18754}')
+ expectedSlug = 'gE8_yN_LSwaJ-6761eGHVA'
+ actualSlug = slugid.encode(uuid_)
+
+ assert expectedSlug == actualSlug, "UUID not correctly encoded into slug: '" + expectedSlug + "' != '" + actualSlug + "'"
+
+
+def testDecode():
+ """ Test that we can decode a "non-nice" slug (first bit of uuid is set)
+ that begins with `-`"""
+
+ # 11111011111011111011111011111011111011111011111001000011111011111011111111111111111111111111111111111111111111111111111111111101....
+ # <f ><b ><e ><f ><b ><e ><f ><b ><e ><f ><b ><e ><4 ><3 ><e ><f ><b ><f ><f ><f ><f ><f ><f ><f ><f ><f ><f ><f ><f ><f ><f ><d >
+ # < - >< - >< - >< - >< - >< - >< - >< - >< Q >< - >< - >< - >< _ >< _ >< _ >< _ >< _ >< _ >< _ >< _ >< _ >< Q >
+ slug = '--------Q--__________Q'
+ expectedUuid = uuid.UUID('{fbefbefb-efbe-43ef-bfff-fffffffffffd}')
+ actualUuid = slugid.decode(slug)
+
+ assert expectedUuid == actualUuid, "Slug not correctly decoded into uuid: '" + str(expectedUuid) + "' != '" + str(actualUuid) + "'"
+
+
+def testUuidEncodeDecode():
+ """ Test that 10000 v4 uuids are unchanged after encoding and then decoding them"""
+
+ for i in range(0, 10000):
+ uuid1 = uuid.uuid4()
+ slug = slugid.encode(uuid1)
+ uuid2 = slugid.decode(slug)
+
+ assert uuid1 == uuid2, "Encode and decode isn't identity: '" + str(uuid1) + "' != '" + str(uuid2) + "'"
+
+
+def testSlugDecodeEncode():
+ """ Test that 10000 v4 slugs are unchanged after decoding and then encoding them."""
+
+ for i in range(0, 10000):
+ slug1 = slugid.v4()
+ uuid_ = slugid.decode(slug1)
+ slug2 = slugid.encode(uuid_)
+
+ assert slug1 == slug2, "Decode and encode isn't identity"
+
+
+def testSpreadNice():
+ """ Make sure that all allowed characters can appear in all allowed
+ positions within the "nice" slug. In this test we generate over a thousand
+ slugids, and make sure that every possible allowed character per position
+ appears at least once in the sample of all slugids generated. We also make
+ sure that no other characters appear in positions in which they are not
+ allowed.
+
+ base 64 encoding char -> value:
+ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
+ 0 1 2 3 4 5 6
+ 0123456789012345678901234567890123456789012345678901234567890123
+
+ e.g. from this we can see 'j' represents 35 in base64
+
+ The following comments show the 128 bits of the v4 uuid in binary, hex and
+ base 64 encodings. The 6 fixed bits (`0`/`1`) according to RFC 4122, plus
+ the first (most significant) fixed bit (`0`) are shown among the 121
+ arbitrary value bits (`.`/`x`). The `x` means the same as `.` but just
+ highlights which bits are grouped together for the respective encoding.
+
+ schema:
+ <..........time_low............><...time_mid...><time_hi_+_vers><clk_hi><clk_lo><.....................node.....................>
+
+ bin: 0xxx............................................0100............10xx............................................................
+ hex: $A <01><02><03><04><05><06><07><08><09><10><11> 4 <13><14><15> $B <17><18><19><20><21><22><23><24><25><26><27><28><29><30><31>
+
+ => $A in {0, 1, 2, 3, 4, 5, 6, 7} (0b0xxx)
+ => $B in {8, 9, A, B} (0b10xx)
+
+ bin: 0xxxxx..........................................0100xx......xxxx10............................................................xx0000
+ b64: $C < 01 >< 02 >< 03 >< 04 >< 05 >< 06 >< 07 > $D < 09 > $E < 11 >< 12 >< 13 >< 14 >< 15 >< 16 >< 17 >< 18 >< 19 >< 20 > $F
+
+ => $C in {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f} (0b0xxxxx)
+ => $D in {Q, R, S, T} (0b0100xx)
+ => $E in {C, G, K, O, S, W, a, e, i, m, q, u, y, 2, 6, -} (0bxxxx10)
+ => $F in {A, Q, g, w} (0bxx0000)"""
+
+ charsAll = ''.join(sorted('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'))
+ # 0 - 31: 0b0xxxxx
+ charsC = ''.join(sorted('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef'))
+ # 16, 17, 18, 19: 0b0100xx
+ charsD = ''.join(sorted('QRST'))
+ # 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62: 0bxxxx10
+ charsE = ''.join(sorted('CGKOSWaeimquy26-'))
+ # 0, 16, 32, 48: 0bxx0000
+ charsF = ''.join(sorted('AQgw'))
+ expected = [charsC, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsD, charsAll, charsE, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsF]
+ spreadTest(slugid.nice, expected)
+
+
+def testSpreadV4():
+ """ This test is the same as niceSpreadTest but for slugid.v4() rather than
+ slugid.nice(). The only difference is that a v4() slug can start with any of
+ the base64 characters since the first six bits of the uuid are random."""
+
+ charsAll = ''.join(sorted('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'))
+ # 16, 17, 18, 19: 0b0100xx
+ charsD = ''.join(sorted('QRST'))
+ # 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62: 0bxxxx10
+ charsE = ''.join(sorted('CGKOSWaeimquy26-'))
+ # 0, 16, 32, 48: 0bxx0000
+ charsF = ''.join(sorted('AQgw'))
+ expected = [charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsD, charsAll, charsE, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsAll, charsF]
+ spreadTest(slugid.v4, expected)
+
+
+def spreadTest(generator, expected):
+ """ `spreadTest` runs a test against the `generator` function, to check that
+ when calling it 64*40 times, the range of characters per string position it
+ returns matches the array `expected`, where each entry in `expected` is a
+ string of all possible characters that should appear in that position in the
+ string, at least once in the sample of 64*40 responses from the `generator`
+ function"""
+ # k is an array which stores which characters were found at which
+ # positions. It has one entry per slugid character, therefore 22 entries.
+ # Each entry is a dict with a key for each character found, and its value
+ # as the number of times that character appeared at that position in the
+ # slugid in the large sample of slugids generated in this test.
+ k = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
+
+ # Generate a large sample of slugids, and record what characters appeared
+ # where... A monte-carlo test has demonstrated that with 64 * 20
+ # iterations, no failure occurred in 1000 simulations, so 64 * 40 should be
+ # suitably large to rule out false positives.
+ for i in range(0, 64 * 40):
+ slug = generator()
+ assert len(slug) == 22
+ for j in range(0, 22):
+ if slug[j] in k[j]:
+ k[j][slug[j]] = k[j][slug[j]] + 1
+ else:
+ k[j][slug[j]] = 1
+
+ # Compose results into an array `actual`, for comparison with `expected`
+ actual = []
+ for j in range(0, len(k)):
+ actual.append('')
+ for a in k[j].keys():
+ if k[j][a] > 0:
+ actual[j] += a
+ # sort for easy comparison
+ actual[j] = ''.join(sorted(actual[j]))
+
+ assert arraysEqual(expected, actual), "In a large sample of generated slugids, the range of characters found per character position in the sample did not match expected results.\n\nExpected: " + str(expected) + "\n\nActual: " + str(actual)
+
+def arraysEqual(a, b):
+ """ returns True if arrays a and b are equal"""
+ return cmp(a, b) == 0
diff --git a/python/slugid/tox.ini b/python/slugid/tox.ini
new file mode 100644
index 0000000000..87326e4d43
--- /dev/null
+++ b/python/slugid/tox.ini
@@ -0,0 +1,26 @@
+[tox]
+envlist = py27
+
+
+[base]
+deps =
+ coverage
+ nose
+ rednose
+commands =
+ coverage run --source slugid --branch {envbindir}/nosetests -v --with-xunit --rednose --force-color
+
+
+[testenv:py27]
+deps=
+ {[base]deps}
+basepython = python2.7
+commands =
+ {[base]commands}
+
+
+[testenv:coveralls]
+deps=
+ python-coveralls
+commands=
+ coveralls