/PyQt-x11-gpl-4.9.4/pyuic/uic/objcreator.py

# · Python · 139 lines · 71 code · 26 blank · 42 comment · 18 complexity · add08f7cad5831ef815934e4264a703b MD5 · raw file

  1. #############################################################################
  2. ##
  3. ## Copyright (C) 2012 Riverbank Computing Limited.
  4. ## Copyright (C) 2006 Thorsten Marek.
  5. ## All right reserved.
  6. ##
  7. ## This file is part of PyQt.
  8. ##
  9. ## You may use this file under the terms of the GPL v2 or the revised BSD
  10. ## license as follows:
  11. ##
  12. ## "Redistribution and use in source and binary forms, with or without
  13. ## modification, are permitted provided that the following conditions are
  14. ## met:
  15. ## * Redistributions of source code must retain the above copyright
  16. ## notice, this list of conditions and the following disclaimer.
  17. ## * Redistributions in binary form must reproduce the above copyright
  18. ## notice, this list of conditions and the following disclaimer in
  19. ## the documentation and/or other materials provided with the
  20. ## distribution.
  21. ## * Neither the name of the Riverbank Computing Limited nor the names
  22. ## of its contributors may be used to endorse or promote products
  23. ## derived from this software without specific prior written
  24. ## permission.
  25. ##
  26. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ##
  38. #############################################################################
  39. import sys
  40. import os.path
  41. from PyQt4.uic.exceptions import NoSuchWidgetError, WidgetPluginError
  42. if sys.hexversion >= 0x03000000:
  43. from PyQt4.uic.port_v3.load_plugin import load_plugin
  44. else:
  45. from PyQt4.uic.port_v2.load_plugin import load_plugin
  46. # The list of directories that are searched for widget plugins. This is
  47. # exposed as part of the API.
  48. widgetPluginPath = [os.path.join(os.path.dirname(__file__), 'widget-plugins')]
  49. MATCH = True
  50. NO_MATCH = False
  51. MODULE = 0
  52. CW_FILTER = 1
  53. class QObjectCreator(object):
  54. def __init__(self, creatorPolicy):
  55. self._cpolicy = creatorPolicy
  56. self._cwFilters = []
  57. self._modules = [self._cpolicy.createQtGuiWrapper()]
  58. # Get the optional plugins.
  59. for plugindir in widgetPluginPath:
  60. try:
  61. plugins = os.listdir(plugindir)
  62. except:
  63. plugins = []
  64. for filename in plugins:
  65. if not filename.endswith('.py'):
  66. continue
  67. filename = os.path.join(plugindir, filename)
  68. plugin_globals = {
  69. "MODULE": MODULE,
  70. "CW_FILTER": CW_FILTER,
  71. "MATCH": MATCH,
  72. "NO_MATCH": NO_MATCH}
  73. plugin_locals = {}
  74. if load_plugin(open(filename, 'rU'), plugin_globals, plugin_locals):
  75. pluginType = plugin_locals["pluginType"]
  76. if pluginType == MODULE:
  77. modinfo = plugin_locals["moduleInformation"]()
  78. self._modules.append(self._cpolicy.createModuleWrapper(*modinfo))
  79. elif pluginType == CW_FILTER:
  80. self._cwFilters.append(plugin_locals["getFilter"]())
  81. else:
  82. raise WidgetPluginError("Unknown plugin type of %s" % filename)
  83. self._customWidgets = self._cpolicy.createCustomWidgetLoader()
  84. self._modules.append(self._customWidgets)
  85. def createQObject(self, classname, *args, **kwargs):
  86. # Handle scoped names, typically static factory methods.
  87. parts = classname.split('.')
  88. factory = self.findQObjectType(parts[0])
  89. if factory is not None:
  90. for part in parts[1:]:
  91. factory = getattr(factory, part, None)
  92. if factory is None:
  93. break
  94. else:
  95. return self._cpolicy.instantiate(factory, *args, **kwargs)
  96. raise NoSuchWidgetError(classname)
  97. def invoke(self, rname, method, args=()):
  98. return self._cpolicy.invoke(rname, method, args)
  99. def findQObjectType(self, classname):
  100. for module in self._modules:
  101. w = module.search(classname)
  102. if w is not None:
  103. return w
  104. return None
  105. def getSlot(self, obj, slotname):
  106. return self._cpolicy.getSlot(obj, slotname)
  107. def addCustomWidget(self, widgetClass, baseClass, module):
  108. for cwFilter in self._cwFilters:
  109. match, result = cwFilter(widgetClass, baseClass, module)
  110. if match:
  111. widgetClass, baseClass, module = result
  112. break
  113. self._customWidgets.addCustomWidget(widgetClass, baseClass, module)