2006-06-13 21:18:29 +02:00
|
|
|
import time
|
|
|
|
import sha
|
|
|
|
import random
|
|
|
|
import binascii
|
2006-06-15 17:18:33 +02:00
|
|
|
import string
|
2006-06-13 21:18:29 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
import gobject
|
|
|
|
|
|
|
|
class GObjectSingletonMeta(gobject.GObjectMeta):
|
|
|
|
"""GObject Singleton Metaclass"""
|
|
|
|
|
|
|
|
def __init__(klass, name, bases, dict):
|
|
|
|
gobject.GObjectMeta.__init__(klass, name, bases, dict)
|
|
|
|
klass.__instance = None
|
|
|
|
|
|
|
|
def __call__(klass, *args, **kwargs):
|
|
|
|
if klass.__instance is None:
|
|
|
|
klass.__instance = gobject.GObjectMeta.__call__(klass, *args, **kwargs)
|
|
|
|
return klass.__instance
|
|
|
|
|
2006-09-25 15:45:24 +02:00
|
|
|
def printable_hash(in_hash):
|
|
|
|
"""Convert binary hash data into printable characters."""
|
|
|
|
printable = ""
|
|
|
|
for char in in_hash:
|
|
|
|
printable = printable + binascii.b2a_hex(char)
|
|
|
|
return printable
|
2006-06-13 21:18:29 +02:00
|
|
|
|
|
|
|
def _sha_data(data):
|
|
|
|
"""sha1 hash some bytes."""
|
|
|
|
sha_hash = sha.new()
|
|
|
|
sha_hash.update(data)
|
|
|
|
return sha_hash.digest()
|
|
|
|
|
|
|
|
def unique_id(data = ''):
|
|
|
|
data_string = "%s%s%s" % (time.time(), random.randint(10000, 100000), data)
|
2006-09-25 15:45:24 +02:00
|
|
|
return printable_hash(_sha_data(data_string))
|
2006-06-14 20:42:44 +02:00
|
|
|
|
|
|
|
|
2006-06-22 20:05:38 +02:00
|
|
|
ACTIVITY_ID_LEN = 40
|
2006-06-14 20:42:44 +02:00
|
|
|
|
|
|
|
def is_hex(s):
|
|
|
|
return s.strip(string.hexdigits) == ''
|
|
|
|
|
2006-06-22 20:05:38 +02:00
|
|
|
def validate_activity_id(actid):
|
|
|
|
"""Validate an activity ID."""
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(actid, str) and not isinstance(actid, unicode):
|
2006-06-14 20:42:44 +02:00
|
|
|
return False
|
2006-06-22 20:05:38 +02:00
|
|
|
if len(actid) != ACTIVITY_ID_LEN:
|
2006-06-14 20:42:44 +02:00
|
|
|
return False
|
2006-06-22 20:05:38 +02:00
|
|
|
if not is_hex(actid):
|
2006-06-14 20:42:44 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|