lluuid.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # file: lluuid.py
  2. # author: Phoenix
  3. #
  4. # $LicenseInfo:firstyear=2004&license=mit$
  5. #
  6. # Copyright (c) 2004-2009, Linden Research, Inc.
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in
  16. # all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. # THE SOFTWARE.
  25. # $/LicenseInfo$
  26. """
  27. Python UUID helpers.
  28. """
  29. import uuid
  30. from hashlib import md5
  31. # Regular expression string to slap into your code when you need to
  32. # find a regular expression somewhere.
  33. REGEX_STR = r'\b(?:[a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{12})\b'
  34. # Either make one yourself or use this one when you need a null uuid.
  35. NULL = uuid.UUID(int=0)
  36. def generate():
  37. """
  38. Return an md5 hash of a newly generated random uuid. This matches
  39. the behavior of Linden Lab's c++ implementation of
  40. LLUUID::generate() so we provide it here.
  41. :returns: Returns a fully random and anonymized uuid.UUID instance.
  42. """
  43. m = md5()
  44. m.update(uuid.uuid1().bytes)
  45. return uuid.UUID(bytes = m.digest())
  46. def is_str_uuid(id_str):
  47. """
  48. Check if a passed in string can be interpreted as a UUID. Returns
  49. True on success and False otherwise.
  50. This function essentially strips the hypens so both
  51. 589EF487-197B-4822-911A-811BB011716A and
  52. 589EF487197B4822911A811BB011716A will be treated as valid uuids.
  53. This function is also tolerant of case change, so
  54. 589ef487-197b-4822-911a-811bb011716a will also evalute as a uuid.
  55. This function is not tolerant of leading or trailing garbage so
  56. leading or trailing whitespace will fail to evaluate as a uuid.
  57. :param uuid_str: The string to test
  58. :returns: Returns True if the input string can be converted to a
  59. uuid. Otherwise returns False.
  60. """
  61. try:
  62. the_id = uuid.UUID(id_str)
  63. except ValueError:
  64. return False
  65. return True