def _get_impl(): """Delay import of rpc_backend until configuration is loaded.""" global _RPCIMPL if _RPCIMPL is None: try: _RPCIMPL = importutils.import_module(CONF.rpc_backend) except ImportError: # For backwards compatibility with older nova config. impl = CONF.rpc_backend.replace('nova.rpc', 'nova.openstack.common.rpc') _RPCIMPL = importutils.import_module(impl) return _RPCIMPL
def process_object_spec(obj_name, obj_spec_map, context): if obj_name in context: return context[obj_name] obj_spec = obj_spec_map[obj_name] if obj_name not in obj_spec_map: raise AttributeError( "Object with name {} is not found".format( obj_name ) ) args = obj_spec.get("args") res_args = [] if args: for param_value in args: if (isinstance(param_value, basestring) and len(param_value) > 1 and param_value[0] == "@"): param_value = param_value[1:] if param_value[0] != "@": param_value = process_object_spec( param_value, obj_spec_map, context ) res_args.append(param_value) kwargs = obj_spec.get("kwargs") res_kwargs = {} if kwargs: for param_name, param_value in kwargs.iteritems(): if (isinstance(param_value, basestring) and len(param_value) > 1 and param_value[0] == "@"): param_value = param_value[1:] if param_value[0] != "@": param_value = process_object_spec( param_value, obj_spec_map, context ) res_kwargs[param_name] = param_value type_str = obj_spec["type"] mod_str, _sep, class_str = type_str.rpartition('.') if mod_str: module = importutils.import_module(mod_str) cls = getattr(module, class_str) else: cls = eval(type_str) obj = cls(*res_args, **res_kwargs) context[obj_name] = obj return obj
def _get_drivers(): """Instantiate, cache, and return drivers based on the CONF.""" global _drivers if _drivers is None: _drivers = {} for notification_driver in CONF.notification_driver: try: driver = importutils.import_module(notification_driver) _drivers[notification_driver] = driver except ImportError: LOG.exception(_LE("Failed to load notifier %s. " "These notifications will not be sent.") % notification_driver) return _drivers.values()
def add_driver(notification_driver): """Add a notification driver at runtime.""" # Make sure the driver list is initialized. _get_drivers() if isinstance(notification_driver, basestring): # Load and add try: driver = importutils.import_module(notification_driver) _drivers[notification_driver] = driver except ImportError: LOG.exception(_("Failed to load notifier %s. " "These notifications will not be sent.") % notification_driver) else: # Driver is already loaded; just add the object. _drivers[notification_driver] = notification_driver
def process_object_spec(obj_name, obj_spec_map, context): if obj_name in context: return context[obj_name] obj_spec = obj_spec_map[obj_name] if obj_name not in obj_spec_map: raise AttributeError( "Object with name {} is not found".format(obj_name)) args = obj_spec.get("args") res_args = [] if args: for param_value in args: if (isinstance(param_value, basestring) and len(param_value) > 1 and param_value[0] == "@"): param_value = param_value[1:] if param_value[0] != "@": param_value = process_object_spec(param_value, obj_spec_map, context) res_args.append(param_value) kwargs = obj_spec.get("kwargs") res_kwargs = {} if kwargs: for param_name, param_value in kwargs.iteritems(): if (isinstance(param_value, basestring) and len(param_value) > 1 and param_value[0] == "@"): param_value = param_value[1:] if param_value[0] != "@": param_value = process_object_spec(param_value, obj_spec_map, context) res_kwargs[param_name] = param_value type_str = obj_spec["type"] mod_str, _sep, class_str = type_str.rpartition('.') if mod_str: module = importutils.import_module(mod_str) cls = getattr(module, class_str) else: cls = eval(type_str) obj = cls(*res_args, **res_kwargs) context[obj_name] = obj return obj
def deserialize_remote_exception(conf, data): failure = jsonutils.loads(str(data)) trace = failure.get('tb', []) message = failure.get('message', "") + "\n" + "\n".join(trace) name = failure.get('class') module = failure.get('module') # NOTE(ameade): We DO NOT want to allow just any module to be imported, in # order to prevent arbitrary code execution. if module not in conf.allowed_rpc_exception_modules: return RemoteError(name, failure.get('message'), trace) try: mod = importutils.import_module(module) klass = getattr(mod, name) if not issubclass(klass, Exception): raise TypeError("Can only deserialize Exceptions") failure = klass(*failure.get('args', []), **failure.get('kwargs', {})) except (AttributeError, TypeError, ImportError): return RemoteError(name, failure.get('message'), trace) ex_type = type(failure) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + _REMOTE_POSTFIX, (ex_type, ), { '__str__': str_override, '__unicode__': str_override }) new_ex_type.__module__ = '%s%s' % (module, _REMOTE_POSTFIX) try: # NOTE(ameade): Dynamically create a new exception type and swap it in # as the new type for the exception. This only works on user defined # Exceptions and not core python exceptions. This is important because # we cannot necessarily change an exception message so we must override # the __str__ method. failure.__class__ = new_ex_type except TypeError: # NOTE(ameade): If a core exception then just add the traceback to the # first exception argument. failure.args = (message, ) + failure.args[1:] return failure
def deserialize_remote_exception(conf, data): failure = jsonutils.loads(str(data)) trace = failure.get('tb', []) message = failure.get('message', "") + "\n" + "\n".join(trace) name = failure.get('class') module = failure.get('module') # NOTE(ameade): We DO NOT want to allow just any module to be imported, in # order to prevent arbitrary code execution. if module not in conf.allowed_rpc_exception_modules: return RemoteError(name, failure.get('message'), trace) try: mod = importutils.import_module(module) klass = getattr(mod, name) if not issubclass(klass, Exception): raise TypeError("Can only deserialize Exceptions") failure = klass(*failure.get('args', []), **failure.get('kwargs', {})) except (AttributeError, TypeError, ImportError): return RemoteError(name, failure.get('message'), trace) ex_type = type(failure) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + _REMOTE_POSTFIX, (ex_type,), {'__str__': str_override, '__unicode__': str_override}) new_ex_type.__module__ = '%s%s' % (module, _REMOTE_POSTFIX) try: # NOTE(ameade): Dynamically create a new exception type and swap it in # as the new type for the exception. This only works on user defined # Exceptions and not core python exceptions. This is important because # we cannot necessarily change an exception message so we must override # the __str__ method. failure.__class__ = new_ex_type except TypeError: # NOTE(ameade): If a core exception then just add the traceback to the # first exception argument. failure.args = (message,) + failure.args[1:] return failure
# not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from magnetodb.common import config from magnetodb.openstack.common import importutils CONF = config.CONF STORAGE_IMPL = importutils.import_module(CONF.storage_impl) def create_table(context, table_schema): """ Creates table @param context: current request context @param table_schema: TableSchema instance which define table to create @raise BackendInteractionException """ STORAGE_IMPL.create_table(context, table_schema) def delete_table(context, table_name):
def __init__(self, hashtype='SHA256'): self.hashfn = importutils.import_module('Crypto.Hash.' + hashtype) self.max_okm_length = 255 * self.hashfn.digest_size
def __init__(self, enctype='AES', hashtype='SHA256'): self.cipher = importutils.import_module('Crypto.Cipher.' + enctype) self.hashfn = importutils.import_module('Crypto.Hash.' + hashtype)