DirectNotify.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. DirectNotify module: this module contains the DirectNotify class
  3. """
  4. from __future__ import annotations
  5. from panda3d.core import StreamWriter
  6. from . import Notifier
  7. from . import Logger
  8. class DirectNotify:
  9. """
  10. DirectNotify class: this class contains methods for creating
  11. mulitple notify categories via a dictionary of Notifiers.
  12. """
  13. def __init__(self) -> None:
  14. """
  15. DirectNotify class keeps a dictionary of Notfiers
  16. """
  17. self.__categories: dict[str, Notifier.Notifier] = {}
  18. # create a default log file
  19. self.logger = Logger.Logger()
  20. # This will get filled in later by ShowBase.py with a
  21. # C++-level StreamWriter object for writing to standard
  22. # output.
  23. self.streamWriter: StreamWriter | None = None
  24. def __str__(self) -> str:
  25. """
  26. Print handling routine
  27. """
  28. return "DirectNotify categories: %s" % (self.__categories)
  29. #getters and setters
  30. def getCategories(self) -> list[str]:
  31. """
  32. Return list of category dictionary keys
  33. """
  34. return list(self.__categories.keys())
  35. def getCategory(self, categoryName: str) -> Notifier.Notifier | None:
  36. """getCategory(self, string)
  37. Return the category with given name if present, None otherwise
  38. """
  39. return self.__categories.get(categoryName, None)
  40. def newCategory(self, categoryName: str, logger: Logger.Logger | None = None) -> Notifier.Notifier:
  41. """newCategory(self, string)
  42. Make a new notify category named categoryName. Return new category
  43. if no such category exists, else return existing category
  44. """
  45. if categoryName not in self.__categories:
  46. self.__categories[categoryName] = Notifier.Notifier(categoryName, logger)
  47. self.setDconfigLevel(categoryName)
  48. notifier = self.getCategory(categoryName)
  49. assert notifier is not None
  50. return notifier
  51. def setDconfigLevel(self, categoryName: str) -> None:
  52. """
  53. Check to see if this category has a dconfig variable
  54. to set the notify severity and then set that level. You cannot
  55. set these until config is set.
  56. """
  57. # We use ConfigVariableString instead of base.config, in case
  58. # we're running before ShowBase has finished initializing
  59. from panda3d.core import ConfigVariableString
  60. dconfigParam = ("notify-level-" + categoryName)
  61. cvar = ConfigVariableString(dconfigParam, "")
  62. level = cvar.getValue()
  63. if not level:
  64. # see if there's an override of the default config level
  65. cvar2 = ConfigVariableString('default-directnotify-level', 'info')
  66. level = cvar2.getValue()
  67. if not level:
  68. level = 'error'
  69. category = self.getCategory(categoryName)
  70. assert category is not None, f'failed to find category: {categoryName!r}'
  71. # Note - this print statement is making it difficult to
  72. # achieve "no output unless there's an error" operation - Josh
  73. # print ("Setting DirectNotify category: " + categoryName +
  74. # " to severity: " + level)
  75. if level == "error":
  76. category.setWarning(False)
  77. category.setInfo(False)
  78. category.setDebug(False)
  79. elif level == "warning":
  80. category.setWarning(True)
  81. category.setInfo(False)
  82. category.setDebug(False)
  83. elif level == "info":
  84. category.setWarning(True)
  85. category.setInfo(True)
  86. category.setDebug(False)
  87. elif level == "debug":
  88. category.setWarning(True)
  89. category.setInfo(True)
  90. category.setDebug(True)
  91. else:
  92. print("DirectNotify: unknown notify level: " + str(level)
  93. + " for category: " + str(categoryName))
  94. def setDconfigLevels(self) -> None:
  95. for categoryName in self.getCategories():
  96. self.setDconfigLevel(categoryName)
  97. def setVerbose(self) -> None:
  98. for categoryName in self.getCategories():
  99. category = self.getCategory(categoryName)
  100. assert category is not None
  101. category.setWarning(True)
  102. category.setInfo(True)
  103. category.setDebug(True)
  104. def popupControls(self, tl = None):
  105. # Don't use a regular import, to prevent ModuleFinder from picking
  106. # it up as a dependency when building a .p3d package.
  107. import importlib
  108. NotifyPanel = importlib.import_module('direct.tkpanels.NotifyPanel')
  109. NotifyPanel.NotifyPanel(self, tl)
  110. def giveNotify(self, cls) -> None:
  111. cls.notify = self.newCategory(cls.__name__)