1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Mechanism for registering and loading ARIA extensions.
20 # pylint: disable=no-self-use
22 from .utils import collections
25 class _Registrar(object):
27 def __init__(self, registry):
28 if not isinstance(registry, (dict, list)):
29 raise RuntimeError('Unsupported registry type')
30 self._registry = registry
32 def register(self, function):
34 if isinstance(self._registry, dict):
36 if key in self._registry:
37 raise RuntimeError('Re-definition of {0} in {1}'.format(key, function.__name__))
38 self._registry.update(result)
39 elif isinstance(self._registry, list):
40 if not isinstance(result, (list, tuple, set)):
42 self._registry += list(result)
44 raise RuntimeError('Illegal state')
50 def _registrar(function):
51 function._registrar_function = True
55 class _ExtensionRegistration(object):
57 Base class for extension class decorators.
62 self._registered_classes = []
63 for attr, value in vars(self.__class__).items():
65 is_registrar_function = value._registrar_function
66 except AttributeError:
67 is_registrar_function = False
68 if is_registrar_function:
69 registrar = _Registrar(registry=getattr(self, attr)())
70 setattr(self, attr, registrar)
71 self._registrars[attr] = registrar
73 def __call__(self, cls):
74 self._registered_classes.append(cls)
79 Initialize all registrars by calling all registered functions.
81 registered_instances = [cls() for cls in self._registered_classes]
82 for name, registrar in self._registrars.items():
83 for instance in registered_instances:
84 registrating_function = getattr(instance, name, None)
85 if registrating_function:
86 registrar.register(registrating_function)
89 class _ParserExtensionRegistration(_ExtensionRegistration):
91 Parser extensions class decorator.
95 def presenter_class(self):
97 Presentation class registration.
99 Implementing functions can return a single class or a list/tuple of classes.
104 def specification_package(self):
106 Specification package registration.
108 Implementing functions can return a package name or a list/tuple of names.
113 def specification_url(self):
115 Specification URL registration.
117 Implementing functions should return a dictionary from names to URLs.
122 def uri_loader_prefix(self):
124 URI loader prefix registration.
126 Implementing functions can return a single prefix or a list/tuple of prefixes.
128 return collections.StrictList(value_class=basestring)
130 parser = _ParserExtensionRegistration()
133 class _ProcessExecutorExtensionRegistration(_ExtensionRegistration):
135 Process executor extension class decorator.
141 The operation function executed by the process executor will be decorated with the function
142 returned from ``decorate()``.
146 process_executor = _ProcessExecutorExtensionRegistration()
151 Initialize all registrars by calling all registered functions.
154 process_executor.init()