globals.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. # -------------------------------------------------------------------------
  9. """! dccsi global state module
  10. :file: < DCCsi >/globals.py
  11. :Status: Prototype
  12. :Version: 0.0.1
  13. """
  14. # -------------------------------------------------------------------------
  15. # standard imports
  16. import os
  17. import sys
  18. import site
  19. from pathlib import Path
  20. import logging as _logging
  21. # -------------------------------------------------------------------------
  22. # global scope
  23. from DccScriptingInterface import _PACKAGENAME, STR_CROSSBAR
  24. _MODULENAME = f'{_PACKAGENAME}.globals'
  25. _LOGGER = _logging.getLogger(_MODULENAME)
  26. _LOGGER.debug('Initializing: {0}.'.format({_MODULENAME}))
  27. _MODULE_PATH = Path(__file__) # this module should not be used as an entry
  28. # -------------------------------------------------------------------------
  29. # global state to be shared
  30. from DccScriptingInterface import ENVAR_PATH_DCCSIG
  31. # the default folder path location for the dccsi gem
  32. # pull the default path
  33. from DccScriptingInterface import PATH_DCCSIG
  34. # this allows env to override the folder path location externally
  35. PATH_DCCSIG = Path(os.getenv(ENVAR_PATH_DCCSIG, PATH_DCCSIG)).resolve()
  36. sys.path.append(PATH_DCCSIG.as_posix())
  37. site.addsitedir(PATH_DCCSIG.as_posix())
  38. _LOGGER.debug(f'{ENVAR_PATH_DCCSIG}: {PATH_DCCSIG.as_posix()}') # debug tracking
  39. # propagate the lib bootstrap folder path
  40. # ensure package dependencies are accessible, by python sys version
  41. # other pkgs and modules may depend on them
  42. # this is transient (per-session) so we don't override in env
  43. from DccScriptingInterface import PATH_DCCSI_PYTHON_LIB
  44. PATH_DCCSI_PYTHON_LIB = Path(PATH_DCCSI_PYTHON_LIB).resolve()
  45. site.addsitedir(PATH_DCCSI_PYTHON_LIB.as_posix())
  46. # propagate the o3de engine root
  47. from DccScriptingInterface import ENVAR_O3DE_DEV
  48. from DccScriptingInterface import O3DE_DEV
  49. # this allows env to override the folder path location externally
  50. O3DE_DEV = Path(os.getenv(ENVAR_O3DE_DEV, O3DE_DEV)).resolve()
  51. sys.path.append(O3DE_DEV.as_posix())
  52. site.addsitedir(O3DE_DEV.as_posix())
  53. _LOGGER.debug(f'{ENVAR_O3DE_DEV}: {O3DE_DEV.as_posix()}') # debug tracking
  54. from DccScriptingInterface import DCCSI_STRICT
  55. from DccScriptingInterface.constants import ENVAR_DCCSI_GDEBUG
  56. from DccScriptingInterface.constants import ENVAR_DCCSI_DEV_MODE
  57. from DccScriptingInterface.constants import ENVAR_DCCSI_GDEBUGGER
  58. from DccScriptingInterface.constants import ENVAR_DCCSI_LOGLEVEL
  59. from DccScriptingInterface.constants import ENVAR_DCCSI_TESTS
  60. from DccScriptingInterface.constants import DCCSI_SETTINGS_LOCAL_FILENAME
  61. from DccScriptingInterface.constants import PATH_DCCSI_LOG_PATH
  62. from DccScriptingInterface.constants import STR_CROSSBAR
  63. from DccScriptingInterface.constants import FRMT_LOG_LONG
  64. # sub pkg dccsi imports
  65. # in next iteration these should be refactored to:
  66. # from DccScriptingInterface.azpy import foo
  67. from DccScriptingInterface.azpy.env_bool import env_bool
  68. # global state init and storage, can be overridden from external env
  69. # retrieve the dccsi global debug flag
  70. # this adds additional debug behavior and inspection, more verbose
  71. DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  72. # suggestion, it might be an improvement to pack all of these into
  73. # a single map/dict using a dot-notation Box so that you could do this:
  74. #
  75. # from DccScriptingInterface.globals import global_state
  76. # if global_state.DCCSI_DEV_MODE: print('foo')
  77. #
  78. # instead of this:
  79. #
  80. # from DccScriptingInterface.globals import *
  81. # if DCCSI_DEV_MODE: print('foo')
  82. # retrieve the dccsi global developer mode flag
  83. # enables early auto-debugger attachment
  84. DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  85. # retrieve the dccsi global ide debugger type
  86. # Wing Pro 8 is currently the only one set up
  87. DCCSI_GDEBUGGER = env_bool(ENVAR_DCCSI_GDEBUGGER, 'WING')
  88. # retrieve the dccsi global log level
  89. # you can for example enable debug logging without global debug or dev mode flags
  90. DCCSI_LOGLEVEL = int(env_bool(ENVAR_DCCSI_LOGLEVEL, _logging.INFO))
  91. # retrieve the dccsi global test mode flag
  92. # when enabled additional internal tests are run, like forcing cascaded imports
  93. DCCSI_TESTS = env_bool(ENVAR_DCCSI_TESTS, False)
  94. if DCCSI_GDEBUG or DCCSI_DEV_MODE:
  95. DCCSI_LOGLEVEL = _logging.DEBUG
  96. _LOGGER.setLevel(DCCSI_LOGLEVEL) # throttle up help
  97. # configure basic logger
  98. # note: not using a common logger to reduce cyclical imports
  99. _logging.basicConfig(level=DCCSI_LOGLEVEL,
  100. format=FRMT_LOG_LONG,
  101. datefmt='%m-%d %H:%M')
  102. _LOGGER = _logging.getLogger(_MODULENAME)
  103. # default settings file path
  104. DCCSI_SETTINGS_LOCAL_PATH = Path(DCCSI_SETTINGS_LOCAL_FILENAME).resolve()
  105. # default / temp log path
  106. DCCSI_O3DE_USER_HOME_LOG = Path(PATH_DCCSI_LOG_PATH).resolve()
  107. # putting these here, allows us to pull them from globals
  108. # and reduce boilerplate in other modules
  109. _LOGGER.debug(f'This MODULE_PATH: {_MODULE_PATH}')
  110. _LOGGER.debug(f'Default {ENVAR_PATH_DCCSIG}: {PATH_DCCSIG}') # debug tracking
  111. _LOGGER.debug(f'Default {ENVAR_O3DE_DEV}: {O3DE_DEV}')
  112. _LOGGER.debug(f'{ENVAR_DCCSI_GDEBUG}: {DCCSI_GDEBUG}')
  113. _LOGGER.debug(f'{ENVAR_DCCSI_DEV_MODE}: {DCCSI_DEV_MODE}')
  114. _LOGGER.debug(f'{ENVAR_DCCSI_GDEBUGGER}: {DCCSI_GDEBUGGER}')
  115. _LOGGER.debug(f'{ENVAR_DCCSI_LOGLEVEL}: {DCCSI_LOGLEVEL}')
  116. _LOGGER.debug(f'{ENVAR_DCCSI_TESTS}: {DCCSI_TESTS}')
  117. _LOGGER.debug(STR_CROSSBAR)
  118. ###########################################################################
  119. # Main Code Block, runs this script as main (testing)
  120. # -------------------------------------------------------------------------
  121. if __name__ == '__main__':
  122. """Run as main, perform additional debug and module tests"""
  123. # some simple logger tests
  124. # evoke the filehandlers and test writing to the log file
  125. if DCCSI_GDEBUG:
  126. _LOGGER.debug('Forced Info! for {0}.'.format({_MODULENAME}))
  127. _LOGGER.error('Forced ERROR! for {0}.'.format({_MODULENAME}))
  128. pass