We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Restore the Registry(*args, **kw) API
In the course of #2891, the Registry API was changed in a backwards-incompatible way. While the author of the change (PR 2893) couldn't imagine anyone using the keywords API, I immediately ran into the incompatibility with a number of customer projects. Since the change back to the old API shouldn't introduce another backwards incompatibility, I suggest reverting that change as soon as possible.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pyramid/registry.py`
Content:
```
1 import operator
2 import threading
3 
4 from zope.interface import implementer
5 
6 from zope.interface.registry import Components
7 
8 from pyramid.compat import text_
9 from pyramid.decorator import reify
10 
11 from pyramid.interfaces import (
12     IIntrospector,
13     IIntrospectable,
14     ISettings,
15     )
16 
17 from pyramid.path import (
18     CALLER_PACKAGE,
19     caller_package,
20 )
21 
22 empty = text_('')
23 
24 class Registry(Components, dict):
25     """ A registry object is an :term:`application registry`.
26 
27     It is used by the framework itself to perform mappings of URLs to view
28     callables, as well as servicing other various framework duties. A registry
29     has its own internal API, but this API is rarely used by Pyramid
30     application developers (it's usually only used by developers of the
31     Pyramid framework and Pyramid addons).  But it has a number of attributes
32     that may be useful to application developers within application code,
33     such as ``settings``, which is a dictionary containing application
34     deployment settings.
35 
36     For information about the purpose and usage of the application registry,
37     see :ref:`zca_chapter`.
38 
39     The registry may be used both as an :class:`pyramid.interfaces.IDict` and
40     as a Zope component registry.
41     These two ways of storing configuration are independent.
42     Applications will tend to prefer to store information as key-values
43     whereas addons may prefer to use the component registry to avoid naming
44     conflicts and to provide more complex lookup mechanisms.
45 
46     The application registry is usually accessed as ``request.registry`` in
47     application code. By the time a registry is used to handle requests it
48     should be considered frozen and read-only. Any changes to its internal
49     state should be done with caution and concern for thread-safety.
50 
51     """
52 
53     # for optimization purposes, if no listeners are listening, don't try
54     # to notify them
55     has_listeners = False
56 
57     _settings = None
58 
59     def __init__(self, package_name=CALLER_PACKAGE):
60         # add a registry-instance-specific lock, which is used when the lookup
61         # cache is mutated
62         self._lock = threading.Lock()
63         # add a view lookup cache
64         self._clear_view_lookup_cache()
65         if package_name is CALLER_PACKAGE:
66             package_name = caller_package().__name__
67         Components.__init__(self, package_name)
68         dict.__init__(self)
69 
70     def _clear_view_lookup_cache(self):
71         self._view_lookup_cache = {}
72 
73     def __nonzero__(self):
74         # defeat bool determination via dict.__len__
75         return True
76 
77     @reify
78     def package_name(self):
79         return self.__name__
80 
81     def registerSubscriptionAdapter(self, *arg, **kw):
82         result = Components.registerSubscriptionAdapter(self, *arg, **kw)
83         self.has_listeners = True
84         return result
85 
86     def registerSelfAdapter(self, required=None, provided=None, name=empty,
87                             info=empty, event=True):
88         # registerAdapter analogue which always returns the object itself
89         # when required is matched
90         return self.registerAdapter(lambda x: x, required=required,
91                                     provided=provided, name=name,
92                                     info=info, event=event)
93 
94     def queryAdapterOrSelf(self, object, interface, default=None):
95         # queryAdapter analogue which returns the object if it implements
96         # the interface, otherwise it will return an adaptation to the
97         # interface
98         if not interface.providedBy(object):
99             return self.queryAdapter(object, interface, default=default)
100         return object
101 
102     def registerHandler(self, *arg, **kw):
103         result = Components.registerHandler(self, *arg, **kw)
104         self.has_listeners = True
105         return result
106 
107     def notify(self, *events):
108         if self.has_listeners:
109             # iterating over subscribers assures they get executed
110             [ _ for _ in self.subscribers(events, None) ]
111 
112     # backwards compatibility for code that wants to look up a settings
113     # object via ``registry.getUtility(ISettings)``
114     def _get_settings(self):
115         return self._settings
116 
117     def _set_settings(self, settings):
118         self.registerUtility(settings, ISettings)
119         self._settings = settings
120 
121     settings = property(_get_settings, _set_settings)
122 
123 @implementer(IIntrospector)
124 class Introspector(object):
125     def __init__(self):
126         self._refs = {}
127         self._categories = {}
128         self._counter = 0
129 
130     def add(self, intr):
131         category = self._categories.setdefault(intr.category_name, {})
132         category[intr.discriminator] = intr
133         category[intr.discriminator_hash] = intr
134         intr.order = self._counter
135         self._counter += 1
136 
137     def get(self, category_name, discriminator, default=None):
138         category = self._categories.setdefault(category_name, {})
139         intr = category.get(discriminator, default)
140         return intr
141 
142     def get_category(self, category_name, default=None, sort_key=None):
143         if sort_key is None:
144             sort_key = operator.attrgetter('order')
145         category = self._categories.get(category_name)
146         if category is None:
147             return default
148         values = category.values()
149         values = sorted(set(values), key=sort_key)
150         return [
151             {'introspectable': intr,
152              'related': self.related(intr)}
153             for intr in values
154         ]
155 
156     def categorized(self, sort_key=None):
157         L = []
158         for category_name in self.categories():
159             L.append((category_name, self.get_category(category_name,
160                                                        sort_key=sort_key)))
161         return L
162 
163     def categories(self):
164         return sorted(self._categories.keys())
165 
166     def remove(self, category_name, discriminator):
167         intr = self.get(category_name, discriminator)
168         if intr is None:
169             return
170         L = self._refs.pop(intr, [])
171         for d in L:
172             L2 = self._refs[d]
173             L2.remove(intr)
174         category = self._categories[intr.category_name]
175         del category[intr.discriminator]
176         del category[intr.discriminator_hash]
177 
178     def _get_intrs_by_pairs(self, pairs):
179         introspectables = []
180         for pair in pairs:
181             category_name, discriminator = pair
182             intr = self._categories.get(category_name, {}).get(discriminator)
183             if intr is None:
184                 raise KeyError((category_name, discriminator))
185             introspectables.append(intr)
186         return introspectables
187 
188     def relate(self, *pairs):
189         introspectables = self._get_intrs_by_pairs(pairs)
190         relatable = ((x,y) for x in introspectables for y in introspectables)
191         for x, y in relatable:
192             L = self._refs.setdefault(x, [])
193             if x is not y and y not in L:
194                 L.append(y)
195 
196     def unrelate(self, *pairs):
197         introspectables = self._get_intrs_by_pairs(pairs)
198         relatable = ((x,y) for x in introspectables for y in introspectables)
199         for x, y in relatable:
200             L = self._refs.get(x, [])
201             if y in L:
202                 L.remove(y)
203 
204     def related(self, intr):
205         category_name, discriminator = intr.category_name, intr.discriminator
206         intr = self._categories.get(category_name, {}).get(discriminator)
207         if intr is None:
208             raise KeyError((category_name, discriminator))
209         return self._refs.get(intr, [])
210 
211 @implementer(IIntrospectable)
212 class Introspectable(dict):
213 
214     order = 0 # mutated by introspector.add
215     action_info = None # mutated by self.register
216 
217     def __init__(self, category_name, discriminator, title, type_name):
218         self.category_name = category_name
219         self.discriminator = discriminator
220         self.title = title
221         self.type_name = type_name
222         self._relations = []
223 
224     def relate(self, category_name, discriminator):
225         self._relations.append((True, category_name, discriminator))
226 
227     def unrelate(self, category_name, discriminator):
228         self._relations.append((False, category_name, discriminator))
229 
230     def _assert_resolved(self):
231         assert undefer(self.discriminator) is self.discriminator
232 
233     @property
234     def discriminator_hash(self):
235         self._assert_resolved()
236         return hash(self.discriminator)
237 
238     def __hash__(self):
239         self._assert_resolved()
240         return hash((self.category_name,) + (self.discriminator,))
241 
242     def __repr__(self):
243         self._assert_resolved()
244         return '<%s category %r, discriminator %r>' % (self.__class__.__name__,
245                                                        self.category_name,
246                                                        self.discriminator)
247 
248     def __nonzero__(self):
249         return True
250 
251     __bool__ = __nonzero__ # py3
252 
253     def register(self, introspector, action_info):
254         self.discriminator = undefer(self.discriminator)
255         self.action_info = action_info
256         introspector.add(self)
257         for relate, category_name, discriminator in self._relations:
258             discriminator = undefer(discriminator)
259             if relate:
260                 method = introspector.relate
261             else:
262                 method = introspector.unrelate
263             method(
264                 (self.category_name, self.discriminator),
265                 (category_name, discriminator)
266                 )
267 
268 class Deferred(object):
269     """ Can be used by a third-party configuration extender to wrap a
270     :term:`discriminator` during configuration if an immediately hashable
271     discriminator cannot be computed because it relies on unresolved values.
272     The function should accept no arguments and should return a hashable
273     discriminator."""
274     def __init__(self, func):
275         self.func = func
276 
277     @reify
278     def value(self):
279         return self.func()
280 
281     def resolve(self):
282         return self.value
283 
284 def undefer(v):
285     """ Function which accepts an object and returns it unless it is a
286     :class:`pyramid.registry.Deferred` instance.  If it is an instance of
287     that class, its ``resolve`` method is called, and the result of the
288     method is returned."""
289     if isinstance(v, Deferred):
290         v = v.resolve()
291     return v
292 
293 class predvalseq(tuple):
294     """ A subtype of tuple used to represent a sequence of predicate values """
295     pass
296 
297 global_registry = Registry('global')
298
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pyramid/registry.py b/pyramid/registry.py
--- a/pyramid/registry.py
+++ b/pyramid/registry.py
@@ -56,7 +56,7 @@
 
     _settings = None
 
-    def __init__(self, package_name=CALLER_PACKAGE):
+    def __init__(self, package_name=CALLER_PACKAGE, *args, **kw):
         # add a registry-instance-specific lock, which is used when the lookup
         # cache is mutated
         self._lock = threading.Lock()
@@ -64,7 +64,7 @@
         self._clear_view_lookup_cache()
         if package_name is CALLER_PACKAGE:
             package_name = caller_package().__name__
-        Components.__init__(self, package_name)
+        Components.__init__(self, package_name, *args, **kw)
         dict.__init__(self)
 
     def _clear_view_lookup_cache(self):

{"golden_diff": "diff --git a/pyramid/registry.py b/pyramid/registry.py\n--- a/pyramid/registry.py\n+++ b/pyramid/registry.py\n@@ -56,7 +56,7 @@\n \n     _settings = None\n \n-    def __init__(self, package_name=CALLER_PACKAGE):\n+    def __init__(self, package_name=CALLER_PACKAGE, *args, **kw):\n         # add a registry-instance-specific lock, which is used when the lookup\n         # cache is mutated\n         self._lock = threading.Lock()\n@@ -64,7 +64,7 @@\n         self._clear_view_lookup_cache()\n         if package_name is CALLER_PACKAGE:\n             package_name = caller_package().__name__\n-        Components.__init__(self, package_name)\n+        Components.__init__(self, package_name, *args, **kw)\n         dict.__init__(self)\n \n     def _clear_view_lookup_cache(self):\n", "issue": "Restore the Registry(*args, **kw) API\nIn the course of #2891, the Registry API was changed in a backwards-incompatible way. While the author of the change (PR 2893) couldn't imagine anyone using the keywords API, I immediately ran into the incompatibility with a number of customer projects. Since the change back to the old API shouldn't introduce another backwards incompatibility, I suggest reverting that change as soon as possible.\n", "before_files": [{"content": "import operator\nimport threading\n\nfrom zope.interface import implementer\n\nfrom zope.interface.registry import Components\n\nfrom pyramid.compat import text_\nfrom pyramid.decorator import reify\n\nfrom pyramid.interfaces import (\n    IIntrospector,\n    IIntrospectable,\n    ISettings,\n    )\n\nfrom pyramid.path import (\n    CALLER_PACKAGE,\n    caller_package,\n)\n\nempty = text_('')\n\nclass Registry(Components, dict):\n    \"\"\" A registry object is an :term:`application registry`.\n\n    It is used by the framework itself to perform mappings of URLs to view\n    callables, as well as servicing other various framework duties. A registry\n    has its own internal API, but this API is rarely used by Pyramid\n    application developers (it's usually only used by developers of the\n    Pyramid framework and Pyramid addons).  But it has a number of attributes\n    that may be useful to application developers within application code,\n    such as ``settings``, which is a dictionary containing application\n    deployment settings.\n\n    For information about the purpose and usage of the application registry,\n    see :ref:`zca_chapter`.\n\n    The registry may be used both as an :class:`pyramid.interfaces.IDict` and\n    as a Zope component registry.\n    These two ways of storing configuration are independent.\n    Applications will tend to prefer to store information as key-values\n    whereas addons may prefer to use the component registry to avoid naming\n    conflicts and to provide more complex lookup mechanisms.\n\n    The application registry is usually accessed as ``request.registry`` in\n    application code. By the time a registry is used to handle requests it\n    should be considered frozen and read-only. Any changes to its internal\n    state should be done with caution and concern for thread-safety.\n\n    \"\"\"\n\n    # for optimization purposes, if no listeners are listening, don't try\n    # to notify them\n    has_listeners = False\n\n    _settings = None\n\n    def __init__(self, package_name=CALLER_PACKAGE):\n        # add a registry-instance-specific lock, which is used when the lookup\n        # cache is mutated\n        self._lock = threading.Lock()\n        # add a view lookup cache\n        self._clear_view_lookup_cache()\n        if package_name is CALLER_PACKAGE:\n            package_name = caller_package().__name__\n        Components.__init__(self, package_name)\n        dict.__init__(self)\n\n    def _clear_view_lookup_cache(self):\n        self._view_lookup_cache = {}\n\n    def __nonzero__(self):\n        # defeat bool determination via dict.__len__\n        return True\n\n    @reify\n    def package_name(self):\n        return self.__name__\n\n    def registerSubscriptionAdapter(self, *arg, **kw):\n        result = Components.registerSubscriptionAdapter(self, *arg, **kw)\n        self.has_listeners = True\n        return result\n\n    def registerSelfAdapter(self, required=None, provided=None, name=empty,\n                            info=empty, event=True):\n        # registerAdapter analogue which always returns the object itself\n        # when required is matched\n        return self.registerAdapter(lambda x: x, required=required,\n                                    provided=provided, name=name,\n                                    info=info, event=event)\n\n    def queryAdapterOrSelf(self, object, interface, default=None):\n        # queryAdapter analogue which returns the object if it implements\n        # the interface, otherwise it will return an adaptation to the\n        # interface\n        if not interface.providedBy(object):\n            return self.queryAdapter(object, interface, default=default)\n        return object\n\n    def registerHandler(self, *arg, **kw):\n        result = Components.registerHandler(self, *arg, **kw)\n        self.has_listeners = True\n        return result\n\n    def notify(self, *events):\n        if self.has_listeners:\n            # iterating over subscribers assures they get executed\n            [ _ for _ in self.subscribers(events, None) ]\n\n    # backwards compatibility for code that wants to look up a settings\n    # object via ``registry.getUtility(ISettings)``\n    def _get_settings(self):\n        return self._settings\n\n    def _set_settings(self, settings):\n        self.registerUtility(settings, ISettings)\n        self._settings = settings\n\n    settings = property(_get_settings, _set_settings)\n\n@implementer(IIntrospector)\nclass Introspector(object):\n    def __init__(self):\n        self._refs = {}\n        self._categories = {}\n        self._counter = 0\n\n    def add(self, intr):\n        category = self._categories.setdefault(intr.category_name, {})\n        category[intr.discriminator] = intr\n        category[intr.discriminator_hash] = intr\n        intr.order = self._counter\n        self._counter += 1\n\n    def get(self, category_name, discriminator, default=None):\n        category = self._categories.setdefault(category_name, {})\n        intr = category.get(discriminator, default)\n        return intr\n\n    def get_category(self, category_name, default=None, sort_key=None):\n        if sort_key is None:\n            sort_key = operator.attrgetter('order')\n        category = self._categories.get(category_name)\n        if category is None:\n            return default\n        values = category.values()\n        values = sorted(set(values), key=sort_key)\n        return [\n            {'introspectable': intr,\n             'related': self.related(intr)}\n            for intr in values\n        ]\n\n    def categorized(self, sort_key=None):\n        L = []\n        for category_name in self.categories():\n            L.append((category_name, self.get_category(category_name,\n                                                       sort_key=sort_key)))\n        return L\n\n    def categories(self):\n        return sorted(self._categories.keys())\n\n    def remove(self, category_name, discriminator):\n        intr = self.get(category_name, discriminator)\n        if intr is None:\n            return\n        L = self._refs.pop(intr, [])\n        for d in L:\n            L2 = self._refs[d]\n            L2.remove(intr)\n        category = self._categories[intr.category_name]\n        del category[intr.discriminator]\n        del category[intr.discriminator_hash]\n\n    def _get_intrs_by_pairs(self, pairs):\n        introspectables = []\n        for pair in pairs:\n            category_name, discriminator = pair\n            intr = self._categories.get(category_name, {}).get(discriminator)\n            if intr is None:\n                raise KeyError((category_name, discriminator))\n            introspectables.append(intr)\n        return introspectables\n\n    def relate(self, *pairs):\n        introspectables = self._get_intrs_by_pairs(pairs)\n        relatable = ((x,y) for x in introspectables for y in introspectables)\n        for x, y in relatable:\n            L = self._refs.setdefault(x, [])\n            if x is not y and y not in L:\n                L.append(y)\n\n    def unrelate(self, *pairs):\n        introspectables = self._get_intrs_by_pairs(pairs)\n        relatable = ((x,y) for x in introspectables for y in introspectables)\n        for x, y in relatable:\n            L = self._refs.get(x, [])\n            if y in L:\n                L.remove(y)\n\n    def related(self, intr):\n        category_name, discriminator = intr.category_name, intr.discriminator\n        intr = self._categories.get(category_name, {}).get(discriminator)\n        if intr is None:\n            raise KeyError((category_name, discriminator))\n        return self._refs.get(intr, [])\n\n@implementer(IIntrospectable)\nclass Introspectable(dict):\n\n    order = 0 # mutated by introspector.add\n    action_info = None # mutated by self.register\n\n    def __init__(self, category_name, discriminator, title, type_name):\n        self.category_name = category_name\n        self.discriminator = discriminator\n        self.title = title\n        self.type_name = type_name\n        self._relations = []\n\n    def relate(self, category_name, discriminator):\n        self._relations.append((True, category_name, discriminator))\n\n    def unrelate(self, category_name, discriminator):\n        self._relations.append((False, category_name, discriminator))\n\n    def _assert_resolved(self):\n        assert undefer(self.discriminator) is self.discriminator\n\n    @property\n    def discriminator_hash(self):\n        self._assert_resolved()\n        return hash(self.discriminator)\n\n    def __hash__(self):\n        self._assert_resolved()\n        return hash((self.category_name,) + (self.discriminator,))\n\n    def __repr__(self):\n        self._assert_resolved()\n        return '<%s category %r, discriminator %r>' % (self.__class__.__name__,\n                                                       self.category_name,\n                                                       self.discriminator)\n\n    def __nonzero__(self):\n        return True\n\n    __bool__ = __nonzero__ # py3\n\n    def register(self, introspector, action_info):\n        self.discriminator = undefer(self.discriminator)\n        self.action_info = action_info\n        introspector.add(self)\n        for relate, category_name, discriminator in self._relations:\n            discriminator = undefer(discriminator)\n            if relate:\n                method = introspector.relate\n            else:\n                method = introspector.unrelate\n            method(\n                (self.category_name, self.discriminator),\n                (category_name, discriminator)\n                )\n\nclass Deferred(object):\n    \"\"\" Can be used by a third-party configuration extender to wrap a\n    :term:`discriminator` during configuration if an immediately hashable\n    discriminator cannot be computed because it relies on unresolved values.\n    The function should accept no arguments and should return a hashable\n    discriminator.\"\"\"\n    def __init__(self, func):\n        self.func = func\n\n    @reify\n    def value(self):\n        return self.func()\n\n    def resolve(self):\n        return self.value\n\ndef undefer(v):\n    \"\"\" Function which accepts an object and returns it unless it is a\n    :class:`pyramid.registry.Deferred` instance.  If it is an instance of\n    that class, its ``resolve`` method is called, and the result of the\n    method is returned.\"\"\"\n    if isinstance(v, Deferred):\n        v = v.resolve()\n    return v\n\nclass predvalseq(tuple):\n    \"\"\" A subtype of tuple used to represent a sequence of predicate values \"\"\"\n    pass\n\nglobal_registry = Registry('global')\n", "path": "pyramid/registry.py"}], "after_files": [{"content": "import operator\nimport threading\n\nfrom zope.interface import implementer\n\nfrom zope.interface.registry import Components\n\nfrom pyramid.compat import text_\nfrom pyramid.decorator import reify\n\nfrom pyramid.interfaces import (\n    IIntrospector,\n    IIntrospectable,\n    ISettings,\n    )\n\nfrom pyramid.path import (\n    CALLER_PACKAGE,\n    caller_package,\n)\n\nempty = text_('')\n\nclass Registry(Components, dict):\n    \"\"\" A registry object is an :term:`application registry`.\n\n    It is used by the framework itself to perform mappings of URLs to view\n    callables, as well as servicing other various framework duties. A registry\n    has its own internal API, but this API is rarely used by Pyramid\n    application developers (it's usually only used by developers of the\n    Pyramid framework and Pyramid addons).  But it has a number of attributes\n    that may be useful to application developers within application code,\n    such as ``settings``, which is a dictionary containing application\n    deployment settings.\n\n    For information about the purpose and usage of the application registry,\n    see :ref:`zca_chapter`.\n\n    The registry may be used both as an :class:`pyramid.interfaces.IDict` and\n    as a Zope component registry.\n    These two ways of storing configuration are independent.\n    Applications will tend to prefer to store information as key-values\n    whereas addons may prefer to use the component registry to avoid naming\n    conflicts and to provide more complex lookup mechanisms.\n\n    The application registry is usually accessed as ``request.registry`` in\n    application code. By the time a registry is used to handle requests it\n    should be considered frozen and read-only. Any changes to its internal\n    state should be done with caution and concern for thread-safety.\n\n    \"\"\"\n\n    # for optimization purposes, if no listeners are listening, don't try\n    # to notify them\n    has_listeners = False\n\n    _settings = None\n\n    def __init__(self, package_name=CALLER_PACKAGE, *args, **kw):\n        # add a registry-instance-specific lock, which is used when the lookup\n        # cache is mutated\n        self._lock = threading.Lock()\n        # add a view lookup cache\n        self._clear_view_lookup_cache()\n        if package_name is CALLER_PACKAGE:\n            package_name = caller_package().__name__\n        Components.__init__(self, package_name, *args, **kw)\n        dict.__init__(self)\n\n    def _clear_view_lookup_cache(self):\n        self._view_lookup_cache = {}\n\n    def __nonzero__(self):\n        # defeat bool determination via dict.__len__\n        return True\n\n    @reify\n    def package_name(self):\n        return self.__name__\n\n    def registerSubscriptionAdapter(self, *arg, **kw):\n        result = Components.registerSubscriptionAdapter(self, *arg, **kw)\n        self.has_listeners = True\n        return result\n\n    def registerSelfAdapter(self, required=None, provided=None, name=empty,\n                            info=empty, event=True):\n        # registerAdapter analogue which always returns the object itself\n        # when required is matched\n        return self.registerAdapter(lambda x: x, required=required,\n                                    provided=provided, name=name,\n                                    info=info, event=event)\n\n    def queryAdapterOrSelf(self, object, interface, default=None):\n        # queryAdapter analogue which returns the object if it implements\n        # the interface, otherwise it will return an adaptation to the\n        # interface\n        if not interface.providedBy(object):\n            return self.queryAdapter(object, interface, default=default)\n        return object\n\n    def registerHandler(self, *arg, **kw):\n        result = Components.registerHandler(self, *arg, **kw)\n        self.has_listeners = True\n        return result\n\n    def notify(self, *events):\n        if self.has_listeners:\n            # iterating over subscribers assures they get executed\n            [ _ for _ in self.subscribers(events, None) ]\n\n    # backwards compatibility for code that wants to look up a settings\n    # object via ``registry.getUtility(ISettings)``\n    def _get_settings(self):\n        return self._settings\n\n    def _set_settings(self, settings):\n        self.registerUtility(settings, ISettings)\n        self._settings = settings\n\n    settings = property(_get_settings, _set_settings)\n\n@implementer(IIntrospector)\nclass Introspector(object):\n    def __init__(self):\n        self._refs = {}\n        self._categories = {}\n        self._counter = 0\n\n    def add(self, intr):\n        category = self._categories.setdefault(intr.category_name, {})\n        category[intr.discriminator] = intr\n        category[intr.discriminator_hash] = intr\n        intr.order = self._counter\n        self._counter += 1\n\n    def get(self, category_name, discriminator, default=None):\n        category = self._categories.setdefault(category_name, {})\n        intr = category.get(discriminator, default)\n        return intr\n\n    def get_category(self, category_name, default=None, sort_key=None):\n        if sort_key is None:\n            sort_key = operator.attrgetter('order')\n        category = self._categories.get(category_name)\n        if category is None:\n            return default\n        values = category.values()\n        values = sorted(set(values), key=sort_key)\n        return [\n            {'introspectable': intr,\n             'related': self.related(intr)}\n            for intr in values\n        ]\n\n    def categorized(self, sort_key=None):\n        L = []\n        for category_name in self.categories():\n            L.append((category_name, self.get_category(category_name,\n                                                       sort_key=sort_key)))\n        return L\n\n    def categories(self):\n        return sorted(self._categories.keys())\n\n    def remove(self, category_name, discriminator):\n        intr = self.get(category_name, discriminator)\n        if intr is None:\n            return\n        L = self._refs.pop(intr, [])\n        for d in L:\n            L2 = self._refs[d]\n            L2.remove(intr)\n        category = self._categories[intr.category_name]\n        del category[intr.discriminator]\n        del category[intr.discriminator_hash]\n\n    def _get_intrs_by_pairs(self, pairs):\n        introspectables = []\n        for pair in pairs:\n            category_name, discriminator = pair\n            intr = self._categories.get(category_name, {}).get(discriminator)\n            if intr is None:\n                raise KeyError((category_name, discriminator))\n            introspectables.append(intr)\n        return introspectables\n\n    def relate(self, *pairs):\n        introspectables = self._get_intrs_by_pairs(pairs)\n        relatable = ((x,y) for x in introspectables for y in introspectables)\n        for x, y in relatable:\n            L = self._refs.setdefault(x, [])\n            if x is not y and y not in L:\n                L.append(y)\n\n    def unrelate(self, *pairs):\n        introspectables = self._get_intrs_by_pairs(pairs)\n        relatable = ((x,y) for x in introspectables for y in introspectables)\n        for x, y in relatable:\n            L = self._refs.get(x, [])\n            if y in L:\n                L.remove(y)\n\n    def related(self, intr):\n        category_name, discriminator = intr.category_name, intr.discriminator\n        intr = self._categories.get(category_name, {}).get(discriminator)\n        if intr is None:\n            raise KeyError((category_name, discriminator))\n        return self._refs.get(intr, [])\n\n@implementer(IIntrospectable)\nclass Introspectable(dict):\n\n    order = 0 # mutated by introspector.add\n    action_info = None # mutated by self.register\n\n    def __init__(self, category_name, discriminator, title, type_name):\n        self.category_name = category_name\n        self.discriminator = discriminator\n        self.title = title\n        self.type_name = type_name\n        self._relations = []\n\n    def relate(self, category_name, discriminator):\n        self._relations.append((True, category_name, discriminator))\n\n    def unrelate(self, category_name, discriminator):\n        self._relations.append((False, category_name, discriminator))\n\n    def _assert_resolved(self):\n        assert undefer(self.discriminator) is self.discriminator\n\n    @property\n    def discriminator_hash(self):\n        self._assert_resolved()\n        return hash(self.discriminator)\n\n    def __hash__(self):\n        self._assert_resolved()\n        return hash((self.category_name,) + (self.discriminator,))\n\n    def __repr__(self):\n        self._assert_resolved()\n        return '<%s category %r, discriminator %r>' % (self.__class__.__name__,\n                                                       self.category_name,\n                                                       self.discriminator)\n\n    def __nonzero__(self):\n        return True\n\n    __bool__ = __nonzero__ # py3\n\n    def register(self, introspector, action_info):\n        self.discriminator = undefer(self.discriminator)\n        self.action_info = action_info\n        introspector.add(self)\n        for relate, category_name, discriminator in self._relations:\n            discriminator = undefer(discriminator)\n            if relate:\n                method = introspector.relate\n            else:\n                method = introspector.unrelate\n            method(\n                (self.category_name, self.discriminator),\n                (category_name, discriminator)\n                )\n\nclass Deferred(object):\n    \"\"\" Can be used by a third-party configuration extender to wrap a\n    :term:`discriminator` during configuration if an immediately hashable\n    discriminator cannot be computed because it relies on unresolved values.\n    The function should accept no arguments and should return a hashable\n    discriminator.\"\"\"\n    def __init__(self, func):\n        self.func = func\n\n    @reify\n    def value(self):\n        return self.func()\n\n    def resolve(self):\n        return self.value\n\ndef undefer(v):\n    \"\"\" Function which accepts an object and returns it unless it is a\n    :class:`pyramid.registry.Deferred` instance.  If it is an instance of\n    that class, its ``resolve`` method is called, and the result of the\n    method is returned.\"\"\"\n    if isinstance(v, Deferred):\n        v = v.resolve()\n    return v\n\nclass predvalseq(tuple):\n    \"\"\" A subtype of tuple used to represent a sequence of predicate values \"\"\"\n    pass\n\nglobal_registry = Registry('global')\n", "path": "pyramid/registry.py"}]}