A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. """
  23. Implements a hierarchy of importing classes as defined in `PEP 302
  24. <http://www.python.org/dev/peps/pep-0302/>`_ to load modules in a safe yet lazy
  25. manner, so that they can be referred to by name but are not actually loaded
  26. until they are used (i.e. their attributes are read or modified).
  27. """
  28. from imp import acquire_lock, release_lock
  29. import sys
  30. from threading import RLock
  31. from types import ModuleType
  32. __all__ = ["LazyImporter"]
  33. _real_get = ModuleType.__getattribute__
  34. def _create_failing_get(exc):
  35. def _fail(self, attr):
  36. raise exc
  37. return _fail
  38. def _mock_get(self, attr):
  39. with _real_get(self, "_lock"):
  40. if _real_get(self, "_unloaded"):
  41. type(self)._unloaded = False
  42. try:
  43. reload(self)
  44. except ImportError as exc:
  45. type(self).__getattribute__ = _create_failing_get(exc)
  46. del type(self)._lock
  47. raise
  48. type(self).__getattribute__ = _real_get
  49. del type(self)._lock
  50. return _real_get(self, attr)
  51. class _LazyModule(type):
  52. def __new__(cls, name):
  53. acquire_lock()
  54. try:
  55. if name not in sys.modules:
  56. attributes = {
  57. "__name__": name,
  58. "__getattribute__": _mock_get,
  59. "_unloaded": True,
  60. "_lock": RLock()
  61. }
  62. parents = (ModuleType,)
  63. klass = type.__new__(cls, "module", parents, attributes)
  64. sys.modules[name] = klass(name)
  65. if "." in name: # Also ensure the parent exists
  66. _LazyModule(name.rsplit(".", 1)[0])
  67. return sys.modules[name]
  68. finally:
  69. release_lock()
  70. class LazyImporter(object):
  71. """An importer for modules that are loaded lazily.
  72. This inserts itself into :py:data:`sys.meta_path`, storing a dictionary of
  73. :py:class:`_LazyModule`\ s (which is added to with :py:meth:`new`).
  74. """
  75. def __init__(self):
  76. self._modules = {}
  77. sys.meta_path.append(self)
  78. def new(self, name):
  79. module = _LazyModule(name)
  80. self._modules[name] = module
  81. return module
  82. def find_module(self, fullname, path=None):
  83. if fullname in self._modules and fullname not in sys.modules:
  84. return self
  85. def load_module(self, fullname):
  86. return self._modules.pop(fullname)