python3.6 fixes

This commit is contained in:
Jack Robison 2018-10-18 00:09:19 -04:00
parent 5aa03900a1
commit 2215222e0b
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 20 additions and 8 deletions

View file

@ -7,7 +7,7 @@ class SOAPCommands:
@staticmethod
async def AddPortMapping(NewRemoteHost: str, NewExternalPort: int, NewProtocol: str, NewInternalPort: int,
NewInternalClient: str, NewEnabled: int, NewPortMappingDescription: str,
NewLeaseDuration: str = '0') -> None:
NewLeaseDuration: str) -> None:
"""Returns None"""
raise NotImplementedError()
@ -17,7 +17,7 @@ class SOAPCommands:
raise NotImplementedError()
@staticmethod
async def GetGenericPortMappingEntry(NewPortMappingIndex: int) -> Tuple[none_or_str, int, str, int, str,
async def GetGenericPortMappingEntry(NewPortMappingIndex: int) -> Tuple[str, int, str, int, str,
bool, str, int]:
"""
Returns (NewRemoteHost, NewExternalPort, NewProtocol, NewInternalPort, NewInternalClient, NewEnabled,

View file

@ -2,7 +2,7 @@ import logging
import socket
import asyncio
from collections import OrderedDict
from typing import Dict, List, Union, Type, Set
from typing import Dict, List, Union, Type
from aioupnp.util import get_dict_val_case_insensitive, BASE_PORT_REGEX, BASE_ADDRESS_REGEX
from aioupnp.constants import SPEC_VERSION, SERVICE
from aioupnp.commands import SOAPCommands
@ -221,10 +221,10 @@ class Gateway:
annotations = current.__annotations__
return_types = annotations.get('return', None)
if return_types:
if isinstance(return_types, type):
return_types = (return_types, )
else:
if hasattr(return_types, '__args__'):
return_types = tuple([return_type_lambas.get(a, a) for a in return_types.__args__])
elif isinstance(return_types, type):
return_types = (return_types, )
return_types = {r: t for r, t in zip(outputs, return_types)}
param_types = {}
for param_name, param_type in annotations.items():

View file

@ -8,6 +8,18 @@ from aioupnp.fault import UPnPError
log = logging.getLogger(__name__)
def safe_type(t):
if t is typing.Tuple:
return tuple
if t is typing.List:
return list
if t is typing.Dict:
return dict
if t is typing.Set:
return set
return t
class SOAPCommand:
def __init__(self, gateway_address: str, service_port: int, control_url: str, service_id: bytes, method: str,
param_types: dict, return_types: dict, param_order: list, return_order: list,
@ -28,7 +40,7 @@ class SOAPCommand:
if set(kwargs.keys()) != set(self.param_types.keys()):
raise Exception("argument mismatch: %s vs %s" % (kwargs.keys(), self.param_types.keys()))
close_after_send = not self.return_types or self.return_types == [None]
soap_kwargs = {n: self.param_types[n](kwargs[n]) for n in self.param_types.keys()}
soap_kwargs = {n: safe_type(self.param_types[n])(kwargs[n]) for n in self.param_types.keys()}
try:
response, xml_bytes = await scpd_post(
self.control_url, self.gateway_address, self.service_port, self.method, self.param_order, self.service_id,
@ -40,7 +52,7 @@ class SOAPCommand:
self._requests.append((soap_kwargs, xml_bytes))
if not response:
return None
result = tuple([self.return_types[n](response.get(n)) for n in self.return_order])
result = tuple([safe_type(self.return_types[n])(response.get(n)) for n in self.return_order])
if len(result) == 1:
return result[0]
return result