custom_uuid.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # vim:fileencoding=utf-8
  2. # Go Working - Controle das Mesas
  3. #
  4. # Copyright (C) 2019-2020 Fábrica do Futuro
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ## Padroniza utilização local de UUID
  20. import uuid
  21. ## NIL
  22. nil_uuid = uuid.UUID('{00000000-0000-0000-0000-000000000000}')
  23. default_string = 'fabricadofuturo'
  24. default_uuid = nil_uuid
  25. ## UUID('6137e891-6c26-5e69-9676-4a00550ad64f')
  26. custom_uuid = uuid.uuid5(default_uuid, default_string)
  27. def uuid4_to_string():
  28. return str(uuid.uuid4())
  29. def uuid5_to_string(namespace=custom_uuid, string=default_string):
  30. return str(uuid.uuid5(namespace, string))
  31. ## Retorna um UUID pseudo aleatório
  32. def random_uuid():
  33. return uuid4_to_string()
  34. ## Gera um UUID replicável a partir de duas strings, sendo a primeira utilizada
  35. ## para gerar um namespace usando o namespace NIL
  36. def uuid_string(namespace=default_string, string=default_string):
  37. return uuid5_to_string(uuid.uuid5(default_uuid, namespace), string)
  38. ## Como o método anterior, mas recebe um UUID em forma de string
  39. def uuid_namespace(namespace=default_uuid, string=default_string):
  40. return uuid5_to_string(uuid.UUID('{%s}' % (namespace)), string)
  41. ## Retorna um UUID replicável com o namespace customizado, ou o namespace
  42. ## customizado em caso de ausência de parâmetros.
  43. def custom_namespace():
  44. return custom_uuid
  45. def custom_namespace(string):
  46. return uuid5_to_string(custom_uuid, string)