forked from rc/aircox
code quality
This commit is contained in:
@ -1,24 +1,22 @@
|
||||
import socket
|
||||
import re
|
||||
import json
|
||||
import re
|
||||
import socket
|
||||
|
||||
|
||||
response_re = re.compile(r'(.*)\s+END\s*$')
|
||||
response_re = re.compile(r"(.*)\s+END\s*$")
|
||||
key_val_re = re.compile(r'(?P<key>[^=]+)="?(?P<value>([^"]|\\")+)"?')
|
||||
|
||||
|
||||
class Connector:
|
||||
"""Connection to AF_UNIX or AF_INET, get and send data.
|
||||
|
||||
Received data can be parsed from list of `key=value` or JSON.
|
||||
"""
|
||||
Connection to AF_UNIX or AF_INET, get and send data. Received
|
||||
data can be parsed from list of `key=value` or JSON.
|
||||
"""
|
||||
|
||||
socket = None
|
||||
""" The socket """
|
||||
"""The socket."""
|
||||
address = None
|
||||
"""
|
||||
String to a Unix domain socket file, or a tuple (host, port) for
|
||||
TCP/IP connection
|
||||
"""
|
||||
"""String to a Unix domain socket file, or a tuple (host, port) for TCP/IP
|
||||
connection."""
|
||||
|
||||
@property
|
||||
def is_open(self):
|
||||
@ -32,12 +30,13 @@ class Connector:
|
||||
if self.is_open:
|
||||
return
|
||||
|
||||
family = socket.AF_UNIX if isinstance(self.address, str) else \
|
||||
socket.AF_INET
|
||||
family = (
|
||||
socket.AF_UNIX if isinstance(self.address, str) else socket.AF_INET
|
||||
)
|
||||
try:
|
||||
self.socket = socket.socket(family, socket.SOCK_STREAM)
|
||||
self.socket.connect(self.address)
|
||||
except:
|
||||
except Exception:
|
||||
self.close()
|
||||
return -1
|
||||
|
||||
@ -50,27 +49,32 @@ class Connector:
|
||||
if self.open():
|
||||
return None
|
||||
|
||||
data = bytes(''.join([str(d) for d in data]) + '\n', encoding='utf-8')
|
||||
data = bytes("".join([str(d) for d in data]) + "\n", encoding="utf-8")
|
||||
try:
|
||||
self.socket.sendall(data)
|
||||
data = ''
|
||||
data = ""
|
||||
while not response_re.search(data):
|
||||
data += self.socket.recv(1024).decode('utf-8')
|
||||
data += self.socket.recv(1024).decode("utf-8")
|
||||
|
||||
if data:
|
||||
data = response_re.sub(r'\1', data).strip()
|
||||
data = self.parse(data) if parse else \
|
||||
self.parse_json(data) if parse_json else data
|
||||
data = response_re.sub(r"\1", data).strip()
|
||||
data = (
|
||||
self.parse(data)
|
||||
if parse
|
||||
else self.parse_json(data)
|
||||
if parse_json
|
||||
else data
|
||||
)
|
||||
return data
|
||||
except:
|
||||
except Exception:
|
||||
self.close()
|
||||
if try_count > 0:
|
||||
return self.send(data, try_count - 1)
|
||||
|
||||
def parse(self, value):
|
||||
return {
|
||||
line.groupdict()['key']: line.groupdict()['value']
|
||||
for line in (key_val_re.search(line) for line in value.split('\n'))
|
||||
line.groupdict()["key"]: line.groupdict()["value"]
|
||||
for line in (key_val_re.search(line) for line in value.split("\n"))
|
||||
if line
|
||||
}
|
||||
|
||||
@ -79,5 +83,5 @@ class Connector:
|
||||
if value[0] == '"' and value[-1] == '"':
|
||||
value = value[1:-1]
|
||||
return json.loads(value) if value else None
|
||||
except:
|
||||
except Exception:
|
||||
return None
|
||||
|
Reference in New Issue
Block a user