def register(self, func, token=None, tags=None, needs=None, provides=None, **kwargs): """Registers a new handler to this hook """ if self.deprecated: warn_deprecation('Hook {0} is deprecated!'.format(self.full_name), frame_correction=+1) new_registration = Registration(func, self, token=token, tags=tags, needs=needs, provides=provides, **kwargs) if self.group.is_strict(): self.validate_strict([new_registration]) need_sorting = self._registrations and new_registration.priority > self._registrations[ -1].priority self._get_registration_list_from_func(func).append(new_registration) if need_sorting: self._registrations.sort( key=Registration.get_priority, reverse=True ) # sort is stable, so order among registration isn't disturbed if new_registration.needs or new_registration.provides: try: self.recompute_call_order() except: self._registrations.pop() raise return new_registration
def _get_from_config_with_legacy(self, notifier_name, legacy_name, new_name): this_config = config.get_path('plugin_config.notifications') value = this_config[legacy_name] if value: warn_deprecation('{} is depreacted. use {}.{} instead'.format(legacy_name, notifier_name, new_name)) else: value = this_config[notifier_name][new_name] return value
def find(self, *predicates, **kw): if isinstance(kw.get('id', None), str): warn_deprecation(_UID_DEPRECATION_MSG) kw['uid'] = kw.pop('id') return InfiniBoxComponentQuery( self.system, self.object_type, *(_normalize_id_predicate(pred, self.object_type) for pred in predicates), **kw)
def _configure(self, plugin): cfg = plugin.get_config() if cfg is not None: warn_deprecation( 'PluginInterface.get_config() is deprecated. ' 'Please use PluginInterface.get_default_config() instead') else: cfg = plugin.get_default_config() if cfg is not None: plugin_name = plugin.get_name() config_name = self.normalize_config_name(plugin_name) config['plugin_config'].extend({config_name: cfg})
def request(self, http_method, path, assert_success=True, **kwargs): """Sends HTTP API request to the remote system """ if http_method in self._disabled_http_methods: raise MethodDisabled( "Request \"{} {}\" aborted, method is disabled".format( http_method.upper(), path)) did_interactive_confirmation = False did_login = False had_cookies = bool(self._session.cookies) auto_retries_context = self._get_auto_retries_context() while True: with auto_retries_context: returned = self._request(http_method, path, **kwargs) if returned.status_code == requests.codes.unauthorized and \ self._login_refresh_enabled and \ had_cookies and \ not did_login and \ 'login' not in path: _logger.trace( 'Performing login again due to expired cookie ({})', self._session.cookies) self.mark_not_logged_in() self.system.login() did_login = True continue if assert_success: try: returned.assert_success() except APICommandFailed as e: if self._is_approval_required(e): reason = self._get_unapproved_reason( e.response.response.json()) if self._interactive and not did_interactive_confirmation: did_interactive_confirmation = True if self._ask_approval_interactively( http_method, path, reason): path = self._with_approved(path) continue raise CommandNotApproved(e.response, reason) raise deprecation_header = returned.response.headers.get( 'x-infinidat-deprecated-api') if deprecation_header: warn_deprecation( 'Deprecation warning: {}'.format(deprecation_header), frame_correction=2) return returned assert False, "Should never get here!" # pragma: no cover
def _get_error_logging_context(self): path = config.root.log.errors_subpath if path: warn_deprecation('log.errors_subpath configuration is deprecated since 1.5.0. ' 'Please use log.highlights_subpath instead') else: path = config.root.log.highlights_subpath def _error_added_filter(record, handler): # pylint: disable=unused-argument return record.extra.get('highlight') handler, log_path = self._get_file_log_handler(path, symlink=None, bubble=True, filter=_error_added_filter) if log_path and self.session.results.current is self.session.results.global_result: self.session.results.global_result.add_extra_log_path(log_path) return handler.applicationbound()
def add_cleanup(self, _func, *args, **kwargs): """ Adds a cleanup function to the cleanup stack. Cleanups are executed in a LIFO order. Positional arguments and keywords are passed to the cleanup function when called. :param critical: If True, this cleanup will take place even when tests are interrupted by the user (Using Ctrl+C for instance) :param success_only: If True, execute this cleanup only if no errors are encountered :param scope: Scope at the end of which this cleanup will be executed :param args: positional arguments to pass to the cleanup function :param kwargs: keyword arguments to pass to the cleanup function """ scope_name = kwargs.pop('scope', None) critical = kwargs.pop('critical', False) success_only = kwargs.pop('success_only', False) new_kwargs = kwargs.pop('kwargs', {}).copy() new_args = list(kwargs.pop('args', ())) if args or kwargs: warn_deprecation( 'Passing *args/**kwargs to slash.add_cleanup is deprecated. ' 'Use args=(...) and/or kwargs={...} instead', frame_correction=+2) new_args.extend(args) new_kwargs.update(kwargs) added = _Cleanup(_func, new_args, new_kwargs, critical=critical, success_only=success_only) if scope_name is None: if not self._allow_implicit_scopes: raise CannotAddCleanup( 'Cleanup added at a stage requiring explicit scoping') scope = self._scope_stack[-1] if self._scope_stack else None else: if scope_name not in self._scopes_by_name: raise IncorrectScope( 'Incorrect scope specified: {!r}'.format(scope_name)) scope = self._scopes_by_name[scope_name][-1] if scope is None: self._pending.append(added) else: scope.cleanups.append(added) return _func
def _get_error_logging_context(self): with ExitStack() as stack: path = config.root.log.errors_subpath if path: warn_deprecation('log.errors_subpath configuration is deprecated since 1.5.0. ' 'Please use log.highlights_subpath instead') else: path = config.root.log.highlights_subpath def _error_added_filter(record, handler): # pylint: disable=unused-argument return record.extra.get('highlight') handler = stack.enter_context(self._log_file_handler_context(path, symlink=None, bubble=True, filter=_error_added_filter)) log_path = handler.stream.name if isinstance(handler, logbook.FileHandler) else None if log_path and self.session.results.current is self.session.results.global_result: self.session.results.global_result.add_extra_log_path(log_path) yield handler
def install(self, plugin, activate=False, activate_later=False, is_internal=False): """ Installs a plugin object to the plugin mechanism. ``plugin`` must be an object deriving from :class:`slash.plugins.PluginInterface`. """ if not isinstance(plugin, PluginInterface): raise IncompatiblePlugin("Invalid plugin type: {!r}".format( type(plugin))) plugin_name = plugin.get_name() if re.search(r'[^A-Za-z0-9_ -]', plugin_name): raise IllegalPluginName( "Illegal plugin name: {}".format(plugin_name)) if any(char in plugin_name for char in _DEPRECATED_CHARACTERS): warn_deprecation( "In the future, dashes and underscore will not be allowed in plugin names - " "spaces should be used instead (plugin name: {!r})".format( plugin_name)) self._configure(plugin) self._installed[plugin_name] = PluginInfo(plugin, is_internal) self._cmd_line_to_name[self.normalize_command_line_name( plugin_name)] = plugin_name self._config_to_name[self.normalize_config_name( plugin_name)] = plugin_name if not hasattr(plugin, '__toggles__'): plugin.__toggles__ = { 'session': gossip.Toggle(), } if activate: try: self.activate(plugin_name) except IncompatiblePlugin: exc_info = sys.exc_info() self.uninstall(plugin) reraise(*exc_info) if activate_later: self.activate_later(plugin_name)
def _normalize_id_predicate(predicate, component_type): if predicate.field.name == 'id' and isinstance(predicate.value, str): warn_deprecation(_UID_DEPRECATION_MSG) return FieldFilter(component_type.fields.uid, predicate.operator_name, predicate.value) return predicate