Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Assorted fixes: Regex, reproducibility, and more. #97

Open
wants to merge 12 commits into
base: next
Choose a base branch
from
Prev Previous commit
Next Next commit
Remove Python 2.5 compatibility code + privacy fix
Really old versions of Python are no longer supported by PyXB anyway, so there's no need to keep complicated code just to support them.

Also use UUID4 rather than UUID1 for generating UUIDs, because there are privacy issues with people's MAC address being embedded in UUIDs in documents.
jonfoster committed Mar 26, 2018
commit 9298678691eb1ecf3c747d0c30dbf15539f90312
34 changes: 4 additions & 30 deletions pyxb/utils/utility.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@
import re
import os
import errno
import uuid
import hashlib
import pyxb
from pyxb.utils.six.moves.urllib import parse as urlparse
import time
@@ -829,15 +831,6 @@ def OpenOrCreate (file_name, tag=None, preserve_contents=False):
fp.seek(2) # os.SEEK_END
return fp

# hashlib didn't show up until 2.5, and sha is deprecated in 2.6.
__Hasher = None
try:
import hashlib
__Hasher = hashlib.sha1
except ImportError:
import sha
__Hasher = sha.new

def HashForText (text):
"""Calculate a cryptographic hash of the given string.

@@ -850,26 +843,7 @@ def HashForText (text):
"""
if isinstance(text, six.text_type):
text = text.encode('utf-8')
return __Hasher(text).hexdigest()

# uuid didn't show up until 2.5
__HaveUUID = False
try:
import uuid
__HaveUUID = True
except ImportError:
import random
def _NewUUIDString ():
"""Obtain a UUID using the best available method. On a version of
python that does not incorporate the C{uuid} class, this creates a
string combining the current date and time (to the second) with a
random number.

@rtype: C{str}
"""
if __HaveUUID:
return uuid.uuid1().urn
return '%s:%08.8x' % (time.strftime('%Y%m%d%H%M%S'), random.randint(0, 0xFFFFFFFF))
return hashlib.sha1(text).hexdigest()

class UniqueIdentifier (object):
"""Records a unique identifier, generally associated with a
@@ -908,7 +882,7 @@ def __setstate__ (self, state):
# Singleton-like
def __new__ (cls, *args):
if 0 == len(args):
uid = _NewUUIDString()
uid = uuid.uuid4().urn
else:
uid = args[0]
if isinstance(uid, UniqueIdentifier):