def _get_community_platform_details(community_platform_name: str) -> Dict[str, Any]: """ Fetch community platform details Args: community_platform_name: name of community Returns: platform_details: dict of details about community platform from scrapli_community library Raises: ScrapliModuleNotFound: if scrapli_community is not importable ScrapliModuleNotFound: if provided community_platform_name package is not importable ScrapliException: if community platform is missing "SCRAPLI_PLATFORM" attribute """ try: importlib.import_module(name="scrapli_community") except ModuleNotFoundError as exc: title = "Module not found!" message = ( "Scrapli Community package is not installed!\n" "To resolve this issue, install the transport plugin. You can do this in one of " "the following ways:\n" "1: 'pip install -r requirements-community.txt'\n" "2: 'pip install scrapli[community]'" ) warning = format_user_warning(title=title, message=message) raise ScrapliModuleNotFound(warning) from exc try: # replace any underscores in platform name with "."; should support any future platforms # that dont have "child" os types -- i.e. just "cisco" instead of "cisco_iosxe" scrapli_community_platform = importlib.import_module( name=f"scrapli_community.{community_platform_name.replace('_', '.')}" ) except ModuleNotFoundError as exc: title = "Module not found!" message = ( f"Scrapli Community platform '{community_platform_name}` not found!\n" "To resolve this issue, ensure you have the correct platform name, and that a scrapli " " community platform of that name exists!" ) warning = format_user_warning(title=title, message=message) raise ScrapliModuleNotFound(warning) from exc platform_details_original = getattr(scrapli_community_platform, "SCRAPLI_PLATFORM", {}) if not platform_details_original: msg = "Community platform missing required attribute `SCRAPLI_PLATFORM`" raise ScrapliException(msg) platform_details: Dict[str, Any] = deepcopy(platform_details_original) return platform_details
def _load_core_transport_plugin( self, ) -> Tuple[Any, Type[BasePluginTransportArgs]]: """ Find non-core transport plugins and required plugin arguments Args: N/A Returns: Tuple[Any, Type[BasePluginTransportArgs]]: transport class class and TransportArgs \ dataclass Raises: ScrapliTransportPluginError: if the transport plugin is unable to be loaded """ self.logger.debug("load core transport requested") try: transport_plugin_module = importlib.import_module( f"scrapli.transport.plugins.{self.transport_name}.transport" ) except ModuleNotFoundError as exc: title = "Transport Plugin Extra Not Installed!" message = ( f"Optional transport plugin '{self.transport_name}' is not installed!\n" f"To resolve this issue, install the transport plugin. You can do this in one of " "the following ways:\n" f"1: 'pip install -r requirements-{self.transport_name}.txt'\n" f"2: 'pip install scrapli[{self.transport_name}]'" ) exception_message = format_user_warning(title=title, message=message) raise ScrapliTransportPluginError(exception_message) from exc transport_class, plugin_transport_args = self._load_transport_plugin_common( transport_plugin_module=transport_plugin_module ) self.logger.debug(f"core transport '{self.transport_name}' loaded successfully") return transport_class, plugin_transport_args
def test_format_user_warning_really_long_title(): terminal_width = get_terminal_size().columns warning_string = format_user_warning(title=("blah" * 30), message="something") assert warning_string.lstrip().startswith("*" * terminal_width)
def test_format_user_warning(): warning_string = format_user_warning(title="blah", message="something") assert "* blah *" in warning_string assert "something" in warning_string