MetaData Sharing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
1.4 KiB

  1. import base64
  2. import datetime
  3. import pasn1
  4. import sys
  5. import uuid
  6. _real_stderr = sys.stderr
  7. DEBUG = True
  8. def enable_debug():
  9. global DEBUG
  10. DEBUG = True
  11. def disable_debug():
  12. global DEBUG
  13. DEBUG = False
  14. def _debprint(*args): # pragma: no cover
  15. if not DEBUG:
  16. return
  17. import traceback, sys, os.path
  18. st = traceback.extract_stack(limit=2)[0]
  19. sep = ''
  20. if args:
  21. sep = ':'
  22. print('%s:%d%s' % (os.path.basename(st.filename), st.lineno, sep),
  23. *args, file=_real_stderr)
  24. sys.stderr.flush()
  25. def _makeuuid(s):
  26. if isinstance(s, uuid.UUID):
  27. return s
  28. if isinstance(s, bytes):
  29. return uuid.UUID(bytes=s)
  30. else:
  31. return uuid.UUID(s)
  32. def _makedatetime(s):
  33. if isinstance(s, datetime.datetime):
  34. return s
  35. return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ').replace(
  36. tzinfo=datetime.timezone.utc)
  37. def _makebytes(s):
  38. if isinstance(s, bytes):
  39. return s
  40. return base64.urlsafe_b64decode(s)
  41. def _trytodict(o):
  42. if isinstance(o, uuid.UUID):
  43. return 'bytes', o.bytes
  44. if isinstance(o, tuple):
  45. return 'list', o
  46. try:
  47. return 'dict', o.__to_dict__()
  48. except Exception: # pragma: no cover
  49. raise TypeError('unable to find __to_dict__ on %s: %s' %
  50. (type(o), repr(o)))
  51. class CanonicalCoder(pasn1.ASN1DictCoder):
  52. def enc_dict(self, obj, **kwargs):
  53. class FakeIter:
  54. def items(self):
  55. return iter(sorted(obj.items()))
  56. return pasn1.ASN1DictCoder.enc_dict(self, FakeIter(), **kwargs)
  57. _asn1coder = CanonicalCoder(coerce=_trytodict)