/ client_code / uuid.py
uuid.py
 1  # SPDX-License-Identifier: MIT
 2  #
 3  # Copyright (c) 2021 The Anvil Extras project team members listed at
 4  # https://github.com/anvilistas/anvil-extras/graphs/contributors
 5  #
 6  # This software is published at https://github.com/anvilistas/anvil-extras
 7  
 8  import anvil.js
 9  from anvil.js import window as _W
10  
11  from .utils._deprecated import deprecated
12  
13  __version__ = "3.1.0"
14  
15  try:
16      _js_uuid = _W.uuid
17      _v4, _parse, _validate = _js_uuid.v4, _js_uuid.parse, _js_uuid.validate
18  except AttributeError:
19      _js_uuid = anvil.js.import_from("https://jspm.dev/uuid@8.3.2")
20      _v4, _parse, _validate = _js_uuid.v4, _js_uuid.parse, _js_uuid.validate
21  
22  
23  class UUID(str):
24      def __init__(self, val):
25          if not _validate(val):
26              raise ValueError("badly formed hexadecimal UUID string")
27  
28      def __repr__(self):
29          return "UUID('" + self + "')"
30  
31      @property
32      def bytes(self):
33          return _parse(self)
34  
35  
36  @deprecated(
37      "anvil_extras.uuid.uuid4() should be replaced with uuid.uuid4() and will be removed in a future version"
38  )
39  def uuid4():
40      """returns a uuid"""
41      return UUID(_v4())
42  
43  
44  if __name__ == "__main__":
45      x = uuid4()
46      print(repr(x))
47      print(x)
48      print(x.bytes)
49      try:
50          UUID("foo")
51      except ValueError as e:
52          print(f"Succesfully raised - {e!r}")
53      else:
54          print("Value Error Not Raised")