mirror of
				https://github.com/checktheroads/hyperglass
				synced 2024-05-11 05:55:08 +00:00 
			
		
		
		
	Fix string output type checks
This commit is contained in:
		@@ -1,17 +1,18 @@
 | 
				
			|||||||
"""Base Connection Class."""
 | 
					"""Base Connection Class."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Standard Library
 | 
					# Standard Library
 | 
				
			||||||
 | 
					import typing as t
 | 
				
			||||||
from abc import ABC, abstractmethod
 | 
					from abc import ABC, abstractmethod
 | 
				
			||||||
from typing import TYPE_CHECKING, Union, Sequence
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Project
 | 
					# Project
 | 
				
			||||||
from hyperglass.log import log
 | 
					from hyperglass.log import log
 | 
				
			||||||
 | 
					from hyperglass.types import Series
 | 
				
			||||||
from hyperglass.plugins import OutputPluginManager
 | 
					from hyperglass.plugins import OutputPluginManager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Local
 | 
					# Local
 | 
				
			||||||
from ._construct import Construct
 | 
					from ._construct import Construct
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if TYPE_CHECKING:
 | 
					if t.TYPE_CHECKING:
 | 
				
			||||||
    # Project
 | 
					    # Project
 | 
				
			||||||
    from hyperglass.models.api import Query
 | 
					    from hyperglass.models.api import Query
 | 
				
			||||||
    from hyperglass.models.data import OutputDataModel
 | 
					    from hyperglass.models.data import OutputDataModel
 | 
				
			||||||
@@ -37,7 +38,7 @@ class Connection(ABC):
 | 
				
			|||||||
        """Return a preconfigured sshtunnel.SSHTunnelForwarder instance."""
 | 
					        """Return a preconfigured sshtunnel.SSHTunnelForwarder instance."""
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def response(self, output: Sequence[str]) -> Union["OutputDataModel", str]:
 | 
					    async def response(self, output: Series[str]) -> t.Union["OutputDataModel", str]:
 | 
				
			||||||
        """Send output through common parsers."""
 | 
					        """Send output through common parsers."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        log.debug("Pre-parsed responses:\n{}", output)
 | 
					        log.debug("Pre-parsed responses:\n{}", output)
 | 
				
			||||||
@@ -47,7 +48,7 @@ class Connection(ABC):
 | 
				
			|||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if response is None:
 | 
					        if response is None:
 | 
				
			||||||
            response = "\n\n".join(output)
 | 
					            response = ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        log.debug("Post-parsed responses:\n{}", response)
 | 
					        log.debug("Post-parsed responses:\n{}", response)
 | 
				
			||||||
        return response
 | 
					        return response
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Any, Dict, Union, Callable
 | 
				
			|||||||
# Project
 | 
					# Project
 | 
				
			||||||
from hyperglass.log import log
 | 
					from hyperglass.log import log
 | 
				
			||||||
from hyperglass.state import use_state
 | 
					from hyperglass.state import use_state
 | 
				
			||||||
 | 
					from hyperglass.util.typing import is_series
 | 
				
			||||||
from hyperglass.exceptions.public import DeviceTimeout, ResponseEmpty
 | 
					from hyperglass.exceptions.public import DeviceTimeout, ResponseEmpty
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if TYPE_CHECKING:
 | 
					if TYPE_CHECKING:
 | 
				
			||||||
@@ -71,7 +72,12 @@ async def execute(query: "Query") -> Union["OutputDataModel", str]:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    output = await driver.response(response)
 | 
					    output = await driver.response(response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if isinstance(output, str):
 | 
					    if is_series(output):
 | 
				
			||||||
 | 
					        if len(output) == 0:
 | 
				
			||||||
 | 
					            raise ResponseEmpty(query=query)
 | 
				
			||||||
 | 
					        output = "\n\n".join(output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    elif isinstance(output, str):
 | 
				
			||||||
        # If the output is a string (not structured) and is empty,
 | 
					        # If the output is a string (not structured) and is empty,
 | 
				
			||||||
        # produce an error.
 | 
					        # produce an error.
 | 
				
			||||||
        if output == "" or output == "\n":
 | 
					        if output == "" or output == "\n":
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,7 @@ from typing import TYPE_CHECKING, Sequence
 | 
				
			|||||||
from pydantic import PrivateAttr
 | 
					from pydantic import PrivateAttr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Project
 | 
					# Project
 | 
				
			||||||
from hyperglass.util.typing import is_type
 | 
					from hyperglass.util.typing import is_series
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Local
 | 
					# Local
 | 
				
			||||||
from .._output import OutputType, OutputPlugin
 | 
					from .._output import OutputType, OutputPlugin
 | 
				
			||||||
@@ -36,7 +36,7 @@ class RemoveCommand(OutputPlugin):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            return "\n".join(output_out)
 | 
					            return "\n".join(output_out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if is_type(device_output, str):
 | 
					        if is_series(device_output):
 | 
				
			||||||
            return tuple(_remove_command(o) for o in device_output)
 | 
					            return tuple(_remove_command(o) for o in device_output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return device_output
 | 
					        return device_output
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,10 +1,11 @@
 | 
				
			|||||||
"""Device output plugins."""
 | 
					"""Device output plugins."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Standard Library
 | 
					# Standard Library
 | 
				
			||||||
from typing import TYPE_CHECKING, Union, Sequence
 | 
					from typing import TYPE_CHECKING, Union
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Project
 | 
					# Project
 | 
				
			||||||
from hyperglass.log import log
 | 
					from hyperglass.log import log
 | 
				
			||||||
 | 
					from hyperglass.types import Series
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Local
 | 
					# Local
 | 
				
			||||||
from ._base import DirectivePlugin, DeviceTypePlugin, HyperglassPlugin
 | 
					from ._base import DirectivePlugin, DeviceTypePlugin, HyperglassPlugin
 | 
				
			||||||
@@ -14,7 +15,7 @@ if TYPE_CHECKING:
 | 
				
			|||||||
    from hyperglass.models.data import OutputDataModel
 | 
					    from hyperglass.models.data import OutputDataModel
 | 
				
			||||||
    from hyperglass.models.config.devices import Device
 | 
					    from hyperglass.models.config.devices import Device
 | 
				
			||||||
 | 
					
 | 
				
			||||||
OutputType = Union["OutputDataModel", Sequence[str]]
 | 
					OutputType = Union["OutputDataModel", Series[str]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class OutputPlugin(HyperglassPlugin, DirectivePlugin, DeviceTypePlugin):
 | 
					class OutputPlugin(HyperglassPlugin, DirectivePlugin, DeviceTypePlugin):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user