synth_arg_kwarg.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. # -------------------------------------------------------------------------
  7. # -------------------------------------------------------------------------
  8. # synth_arg_kwarg.py
  9. # Convenience module for a standardized attr interface for classes/objects.
  10. # -------------------------------------------------------------------------
  11. __author__ = 'HogJonny'
  12. # -------------------------------------------------------------------------
  13. from find_arg import find_arg
  14. from synth import synthesize
  15. # -------------------------------------------------------------------------
  16. def set_synth_arg_kwarg(inst, arg_pos_index=None, arg_tag=None, default_value=None,
  17. in_args=None, in_kwargs=None, remove_kwarg=True,
  18. set_anyway=True):
  19. """
  20. Uses find_arg and sets a property on a object.
  21. Special args:
  22. setAnyway <-- if the object has the property already, set it
  23. If the arg/property doesn't exist we synthesize it
  24. """
  25. found_arg = None
  26. arg_value_dict = {}
  27. # find the argument, or set to default value
  28. found_arg, in_kwargs = find_arg(arg_pos_index, arg_tag, remove_kwarg,
  29. in_args, in_kwargs,
  30. default_value)
  31. if found_arg:
  32. arg_tag = found_arg
  33. # single arg first
  34. # make sure the object doesn't arealdy have this property
  35. try:
  36. hasattr(inst, arg_tag) # check if property exists
  37. if set_anyway:
  38. try:
  39. setattr(inst, arg_tag, default_value) # try to set
  40. except Exception as e:
  41. raise e
  42. except:
  43. pass
  44. # make it a synthetic property
  45. if arg_tag:
  46. try:
  47. arg_value = synthesize(inst, arg_tag, default_value)
  48. arg_value_dict[arg_tag] = arg_value
  49. except Exception as e:
  50. raise e
  51. # multiple and/or remaining kwards next
  52. if in_kwargs:
  53. if len(in_kwargs) > 0:
  54. for k, v in in_kwargs.items():
  55. try:
  56. hasattr(inst, k) # check if property exists
  57. if set_anyway:
  58. try:
  59. setattr(inst, k, v) # try to set
  60. except Exception as e:
  61. raise e
  62. except:
  63. pass
  64. if k:
  65. try:
  66. arg_value = synthesize(inst, k, v)
  67. arg_value_dict[k] = arg_value
  68. except Exception as e:
  69. raise e
  70. return arg_value_dict
  71. # --------------------------------------------------------------------------
  72. ###########################################################################
  73. # Main Code Block, will run the tool
  74. # -------------------------------------------------------------------------
  75. if __name__ == '__main__':
  76. from test_foo import Foo
  77. # define a arg/property tag we know doesn't exist
  78. synth_arg_tag = 'synthetic_arg'
  79. # create a test object
  80. print('~ creating the test foo object...')
  81. my_foo = Foo()
  82. print('~ Starting - single synthetic arg test...')
  83. # find and set existing, or create and set
  84. arg_value_dict = set_synth_arg_kwarg(my_foo,
  85. arg_tag=synth_arg_tag,
  86. default_value='kablooey')
  87. # what was returned
  88. print('~ single value returned...')
  89. for k, v in arg_value_dict.items():
  90. print("Arg '{0}':'{1}'".format(k, v))
  91. # attempt to access the new synthetic property directly
  92. print('~ direct property access test...')
  93. try:
  94. my_foo.synthetic_arg
  95. print('myFoo.{0}: {1}'.format(synth_arg_tag, my_foo.synthetic_arg))
  96. except Exception as e:
  97. raise e
  98. # can we create a bunch of kwargs?
  99. print('~ Starting - multiple synthetic kwarg test...')
  100. newKwargs = {'fooey': 'chop suey', 'success': True}
  101. # find and set existing, or create and set
  102. arg_value_dict = set_synth_arg_kwarg(my_foo,
  103. in_kwargs=newKwargs,
  104. default_value='kablooey')
  105. # what was returned
  106. print('~ multiple values returned...')
  107. for k, v in arg_value_dict.items():
  108. print("Arg '{0}':'{1}'".format(k, v))
  109. print('~ multiple direct property access test...')
  110. try:
  111. my_foo.fooey
  112. print('myFoo.{0}: {1}'.format('fooey', my_foo.fooey))
  113. except Exception as e:
  114. raise e
  115. try:
  116. my_foo.success
  117. print('myFoo.{0}: {1}'.format('success', my_foo.success))
  118. except Exception as e:
  119. raise e
  120. print('~ Starting - known failure test...')
  121. try:
  122. my_foo.knownBad
  123. except Exception as e:
  124. print(e)
  125. print('Test failed as expected!!!')