Skip to content

Commit

Permalink
fix: corrected type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelm committed Feb 5, 2022
1 parent b0cc9ab commit 7fef38e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
48 changes: 23 additions & 25 deletions broadworks_ocip/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import socket
import sys
import uuid
from typing import Callable
from typing import Dict
from typing import Type

import attr
from lxml import etree
Expand Down Expand Up @@ -59,9 +59,11 @@ class BroadworksAPI:
command_timeout: int = attr.ib(default=30)
socket = attr.ib(default=None)
session_id: str = attr.ib(default=None)
_despatch_table: Dict[str, Callable] = attr.ib(default=None)
_despatch_table: Dict[str, Type[broadworks_ocip.base.OCIType]] = attr.ib(
default=None,
)

def __attrs_post_init__(self):
def __attrs_post_init__(self) -> None:
"""
Initialise the API object.
Expand All @@ -76,7 +78,7 @@ def __attrs_post_init__(self):
self.build_despatch_table()
self.authenticated = False

def build_despatch_table(self):
def build_despatch_table(self) -> None:
"""
Create a despatch table of commands and types used
"""
Expand Down Expand Up @@ -109,7 +111,7 @@ def build_despatch_table(self):
self._despatch_table = despatch_table
self.logger.debug("Built Broadworks despatch table")

def configure_logger(self):
def configure_logger(self) -> None:
"""
Create and configure a logging object
Expand All @@ -123,7 +125,7 @@ def configure_logger(self):
logger.addHandler(console_handler)
self.logger = logger

def get_type_class(self, command: str):
def get_type_class(self, command: str) -> Type[broadworks_ocip.base.OCIType]:
"""
Given a name (Request/Response/Type) name, return a class object for it
Expand All @@ -143,7 +145,7 @@ def get_type_class(self, command: str):
raise e
return cls

def get_type_object(self, command, **kwargs):
def get_type_object(self, command, **kwargs) -> broadworks_ocip.base.OCIType:
"""
Build the OCIType object instance for a type and parameters
Expand All @@ -165,7 +167,7 @@ def get_type_object(self, command, **kwargs):
cmd = cls(**kwargs)
return cmd

def get_command_object(self, command, **kwargs):
def get_command_object(self, command, **kwargs) -> broadworks_ocip.base.OCICommand:
"""
Build the OCICommand object instance for a command and parameter
Expand All @@ -185,9 +187,9 @@ def get_command_object(self, command, **kwargs):
"""
cls = self.get_type_class(command)
cmd = cls(session_id=self.session_id, **kwargs)
return cmd
return cmd # type: ignore

def get_command_xml(self, command, **kwargs):
def get_command_xml(self, command, **kwargs) -> bytes:
"""
Build the XML for a command and parameter
Expand All @@ -204,7 +206,7 @@ def get_command_xml(self, command, **kwargs):
cmd = self.get_command_object(command, **kwargs)
return cmd.build_xml_()

def send_command(self, command, **kwargs):
def send_command(self, command, **kwargs) -> None:
"""
Build the XML for a command and parameter and send it to the server
Expand All @@ -223,12 +225,10 @@ def send_command(self, command, **kwargs):
self.logger.log(VERBOSE_DEBUG, f"SEND: {str(xml)}")
self.socket.sendall(xml + b"\n")

def receive_response(self):
def receive_response(self) -> broadworks_ocip.base.OCICommand:
"""
Wait and receive response XML from server, and decode it
Arguments:
Raises:
OCIErrorResponse: An error was returned from the server
OCIErrorTimeOut: The client timed out waiting for the server
Expand Down Expand Up @@ -257,7 +257,7 @@ def receive_response(self):
self.logger.log(VERBOSE_DEBUG, f"RECV: {str(content)}")
return self.decode_xml(content)

def decode_xml(self, xml):
def decode_xml(self, xml) -> broadworks_ocip.base.OCICommand:
"""
Decode XML into an OCICommand based object instance
Expand Down Expand Up @@ -286,12 +286,10 @@ def decode_xml(self, xml):
return result
raise OCIErrorUnknown(message="Unknown XML decode", object=root)

def connect(self):
def connect(self) -> None:
"""
Open the connection to the OCI-P server
Arguments:
Raises:
IOError: Communications failure
Expand All @@ -314,12 +312,10 @@ def connect(self):
self.logger.error("Connection timed out")
raise e

def authenticate(self):
def authenticate(self) -> broadworks_ocip.base.OCICommand:
"""
Authenticate the connection to the OCI-P server
Arguments:
Raises:
OCIErrorResponse: An error was returned from the server
Expand All @@ -330,7 +326,9 @@ def authenticate(self):
resp = self.receive_response()
authhash = hashlib.sha1(self.password.encode()).hexdigest().lower()
signed_password = (
hashlib.md5(":".join([resp.nonce, authhash]).encode()).hexdigest().lower()
hashlib.md5(":".join([resp.nonce, authhash]).encode()) # type: ignore
.hexdigest()
.lower()
)
self.send_command(
"LoginRequest14sp4",
Expand All @@ -344,7 +342,7 @@ def authenticate(self):
self.authenticated = True
return resp

def command(self, command, **kwargs):
def command(self, command, **kwargs) -> broadworks_ocip.base.OCICommand:
"""
Send a command and parameters to the server, receive and decode a response
Expand All @@ -367,7 +365,7 @@ def command(self, command, **kwargs):
self.send_command(command, **kwargs)
return self.receive_response()

def close(self, no_log=False):
def close(self, no_log=False) -> None:
"""
Close the connection to the OCI-P server
"""
Expand All @@ -389,7 +387,7 @@ def close(self, no_log=False):
self.logger.info(f"Disconnected from host={self.host} port={self.port}")
self.socket = None

def __del__(self):
def __del__(self) -> None:
self.close(no_log=True)


Expand Down
13 changes: 5 additions & 8 deletions broadworks_ocip/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Tuple

import attr
Expand Down Expand Up @@ -50,8 +51,6 @@ class OCIType:
"""
OCIType - Base type for all the OCI-P component classes
Attributes:
There are no attributes of this base class (the `_frozen` attribute is
used as a flag to lock the instance). The attributes are added in the
various subclasses.
Expand Down Expand Up @@ -223,7 +222,7 @@ def etree_sub_element_(
elem.text = value

@classmethod
def column_header_snake_case_(cls, header):
def column_header_snake_case_(cls, header:str)->str:
"""
Converts an XML name into a pythonic snake case name
Expand All @@ -235,7 +234,7 @@ def column_header_snake_case_(cls, header):
"""
return re.sub("[ _]+", r"_", header).lower()

def snake_case_to_column_header(self, snake_str):
def snake_case_to_column_header(self, snake_str:str)->str:
"""
Converts a pythonic snake case name into a column header name
Expand All @@ -251,7 +250,7 @@ def snake_case_to_column_header(self, snake_str):
return " ".join(x.title() for x in components)

@classmethod
def decode_table_(cls, element: "etree._Element"):
def decode_table_(cls, element: "etree._Element")->List[NamedTuple]:
"""
Decode a table (used in a OCIResponse) into a list of named tuples
Expand Down Expand Up @@ -279,7 +278,7 @@ def build_from_etree_non_parameters_(
cls,
element: "etree._Element",
initialiser: dict,
):
)->None:
"""
Handle any items outside the parameter set
Expand Down Expand Up @@ -395,8 +394,6 @@ def build_xml_(self):
"""
Build an XML document of the current Command (Request/Response)
Parameters:
Returns:
xml: string containing XML document
"""
Expand Down

0 comments on commit 7fef38e

Please sign in to comment.