1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00

Auto-format the Python scripts

We have a few Python scripts which over the years ended up with a
variety of formatting.

This patch auto-formats them using `black -l 79` to make them more
uniform, and easier to read and write.
This commit is contained in:
Alberto Bertogli
2025-04-11 14:05:06 +01:00
parent b65ec36916
commit 1cf24ba94a
3 changed files with 104 additions and 85 deletions

View File

@@ -14,7 +14,7 @@ URL = "https://www.iana.org/assignments/tls-parameters/tls-parameters-4.csv"
def getCiphers(): def getCiphers():
req = urllib.request.urlopen(URL) req = urllib.request.urlopen(URL)
data = req.read().decode('utf-8') data = req.read().decode("utf-8")
ciphers = [] ciphers = []
reader = csv.DictReader(data.splitlines()) reader = csv.DictReader(data.splitlines())
@@ -37,8 +37,9 @@ def getCiphers():
ciphers = getCiphers() ciphers = getCiphers()
out = open(sys.argv[1], 'w') out = open(sys.argv[1], "w")
out.write("""\ out.write(
"""\
package tlsconst package tlsconst
// AUTOGENERATED - DO NOT EDIT // AUTOGENERATED - DO NOT EDIT
@@ -46,9 +47,10 @@ package tlsconst
// This file was autogenerated by generate-ciphers.py. // This file was autogenerated by generate-ciphers.py.
var cipherSuiteName = map[uint16]string{ var cipherSuiteName = map[uint16]string{
""") """
)
for ver, desc in ciphers: for ver, desc in ciphers:
out.write('\t%s: "%s",\n' % (ver, desc)) out.write('\t%s: "%s",\n' % (ver, desc))
out.write('}\n') out.write("}\n")

View File

@@ -21,12 +21,12 @@ import time
# Command-line flags. # Command-line flags.
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
ap.add_argument("script", type=argparse.FileType('r', encoding='utf8')) ap.add_argument("script", type=argparse.FileType("r", encoding="utf8"))
args = ap.parse_args() args = ap.parse_args()
# Make sure stdout is open in utf8 mode, as we will print our input, which is # Make sure stdout is open in utf8 mode, as we will print our input, which is
# utf8, and want it to work regardless of the environment. # utf8, and want it to work regardless of the environment.
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1) sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf8", buffering=1)
class Process(object): class Process(object):
@@ -45,6 +45,7 @@ class Process (object):
def close(self): def close(self):
return self.cmd.stdin.close() return self.cmd.stdin.close()
class Sock(object): class Sock(object):
"""A (generic) socket. """A (generic) socket.
@@ -52,6 +53,7 @@ class Sock (object):
Subclasses will implement the behaviour specific to different socket Subclasses will implement the behaviour specific to different socket
types. types.
""" """
def __init__(self, addr): def __init__(self, addr):
self.addr = addr self.addr = addr
self.sock = NotImplemented self.sock = NotImplemented
@@ -84,6 +86,7 @@ class Sock (object):
self.connw.close() self.connw.close()
self.sock.close() self.sock.close()
class UnixSock(Sock): class UnixSock(Sock):
def __init__(self, addr): def __init__(self, addr):
Sock.__init__(self, addr) Sock.__init__(self, addr)
@@ -127,6 +130,7 @@ class TLSSock (Sock):
class Interpreter(object): class Interpreter(object):
"""Interpreter for chamuyero scripts.""" """Interpreter for chamuyero scripts."""
def __init__(self): def __init__(self):
# Processes and sockets we have spawn. Indexed by the id provided by # Processes and sockets we have spawn. Indexed by the id provided by
# the user. # the user.
@@ -176,9 +180,14 @@ class Interpreter (object):
# = Launch a process. # = Launch a process.
if op == "=": if op == "=":
cmd = Process(params, shell=True, universal_newlines=True, cmd = Process(
stdin=subprocess.PIPE, stdout=subprocess.PIPE, params,
stderr=subprocess.STDOUT) shell=True,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
self.procs[proc] = cmd self.procs[proc] = cmd
# |= Launch a process, do not capture stdout. # |= Launch a process, do not capture stdout.
@@ -224,16 +233,20 @@ class Interpreter (object):
elif op == "<-": elif op == "<-":
read = self.procs[proc].readline() read = self.procs[proc].readline()
if read != params + "\n": if read != params + "\n":
self.runtime_error("data different that expected:\n" self.runtime_error(
"data different that expected:\n"
+ " expected: %s\n" % repr(params) + " expected: %s\n" % repr(params)
+ " got: %s" % repr(read)) + " got: %s" % repr(read)
)
elif op == "<~": elif op == "<~":
read = self.procs[proc].readline() read = self.procs[proc].readline()
m = re.match(params, read) m = re.match(params, read)
if m is None: if m is None:
self.runtime_error("data did not match regexp:\n" self.runtime_error(
"data did not match regexp:\n"
+ " regexp: %s\n" % repr(params) + " regexp: %s\n" % repr(params)
+ " got: %s" % repr(read)) + " got: %s" % repr(read)
)
elif op == "<...": elif op == "<...":
while True: while True:
read = self.procs[proc].readline() read = self.procs[proc].readline()
@@ -249,8 +262,10 @@ class Interpreter (object):
elif op == "wait": elif op == "wait":
retcode = self.procs[proc].wait() retcode = self.procs[proc].wait()
if params and retcode != int(params): if params and retcode != int(params):
self.runtime_error("return code did not match:\n" self.runtime_error(
+ " expected %s, got %d" % (params, retcode)) "return code did not match:\n"
+ " expected %s, got %d" % (params, retcode)
)
# close Close the process. # close Close the process.
elif op == "close": elif op == "close":

View File

@@ -24,11 +24,11 @@ def flexible_eq(expected, got):
if posG >= len(got): if posG >= len(got):
return False return False
if c == '?': if c == "?":
posG += 1 posG += 1
continue continue
if c == '*': if c == "*":
while posG < len(got) and got[posG] != '\t': while posG < len(got) and got[posG] != "\t":
posG += 1 posG += 1
continue continue
continue continue
@@ -54,7 +54,7 @@ def msg_equals(expected, msg):
diff = True diff = True
continue continue
if expected[h] == '*': if expected[h] == "*":
continue continue
if not flexible_eq(val, msg[h]): if not flexible_eq(val, msg[h]):
@@ -67,12 +67,16 @@ def msg_equals(expected, msg):
return False return False
if expected.is_multipart() != msg.is_multipart(): if expected.is_multipart() != msg.is_multipart():
print("Multipart differs, expected %s, got %s" % ( print(
expected.is_multipart(), msg.is_multipart())) "Multipart differs, expected %s, got %s"
% (expected.is_multipart(), msg.is_multipart())
)
return False return False
if expected.is_multipart(): if expected.is_multipart():
for exp, got in itertools.zip_longest(expected.get_payload(), msg.get_payload()): for exp, got in itertools.zip_longest(
expected.get_payload(), msg.get_payload()
):
if not msg_equals(exp, got): if not msg_equals(exp, got):
return False return False
else: else:
@@ -92,10 +96,8 @@ if __name__ == "__main__":
# We use a custom strict policy to do more strict content validation. # We use a custom strict policy to do more strict content validation.
policy = email.policy.EmailPolicy( policy = email.policy.EmailPolicy(
utf8=True, utf8=True, linesep="\r\n", refold_source="none", raise_on_defect=True
linesep="\r\n", )
refold_source='none',
raise_on_defect=True)
expected = email.parser.Parser(policy=policy).parse(open(f1)) expected = email.parser.Parser(policy=policy).parse(open(f1))
msg = email.parser.Parser(policy=policy).parse(open(f2)) msg = email.parser.Parser(policy=policy).parse(open(f2))