proxy_metaclass.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #############################################################################
  2. ##
  3. ## Copyright (C) 2014 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. from .misc import Literal, moduleMember
  40. class ProxyMetaclass(type):
  41. """ ProxyMetaclass is the meta-class for proxies. """
  42. def __init__(*args):
  43. """ Initialise the meta-class. """
  44. # Initialise as normal.
  45. type.__init__(*args)
  46. # The proxy type object we have created.
  47. proxy = args[0]
  48. # Go through the proxy's attributes looking for other proxies.
  49. for sub_proxy in proxy.__dict__.values():
  50. if type(sub_proxy) is ProxyMetaclass:
  51. # Set the module name of the contained proxy to the name of the
  52. # container proxy.
  53. sub_proxy.module = proxy.__name__
  54. # Attribute hierachies are created depth first so any proxies
  55. # contained in the sub-proxy whose module we have just set will
  56. # already exist and have an incomplete module name. We need to
  57. # revisit them and prepend the new name to their module names.
  58. # Note that this should be recursive but with current usage we
  59. # know there will be only one level to revisit.
  60. for sub_sub_proxy in sub_proxy.__dict__.values():
  61. if type(sub_sub_proxy) is ProxyMetaclass:
  62. sub_sub_proxy.module = '%s.%s' % (proxy.__name__, sub_sub_proxy.module)
  63. # Makes sure there is a 'module' attribute.
  64. if not hasattr(proxy, 'module'):
  65. proxy.module = ''
  66. def __getattribute__(cls, name):
  67. try:
  68. return type.__getattribute__(cls, name)
  69. except AttributeError:
  70. # Make sure __init__()'s use of hasattr() works.
  71. if name == 'module':
  72. raise
  73. # Avoid a circular import.
  74. from .qtproxies import LiteralProxyClass
  75. return type(name, (LiteralProxyClass, ),
  76. {"module": moduleMember(type.__getattribute__(cls, "module"),
  77. type.__getattribute__(cls, "__name__"))})
  78. def __str__(cls):
  79. return moduleMember(type.__getattribute__(cls, "module"),
  80. type.__getattribute__(cls, "__name__"))
  81. def __or__(self, r_op):
  82. return Literal("%s|%s" % (self, r_op))
  83. def __eq__(self, other):
  84. return str(self) == str(other)