mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-07 08:18:41 +09:00
import FIREFOX_52_6_0esr_RELEASE from mozilla-esr52 hg repo
This commit is contained in:
commit
dcd9973243
150858 changed files with 23884658 additions and 0 deletions
43
python/slugid/slugid/__init__.py
Normal file
43
python/slugid/slugid/__init__.py
Normal file
|
|
@ -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
|
||||
43
python/slugid/slugid/slugid.py
Normal file
43
python/slugid/slugid/slugid.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue