mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 04:01:32 -06:00
move networks commands to [str, Optional[dict]] across the board
and some other updates PrintHTML is an experiment and is unlikely the solution I'll go with
This commit is contained in:
62
NetUtils.py
62
NetUtils.py
@@ -2,10 +2,12 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import logging
|
||||
import typing
|
||||
from html.parser import HTMLParser
|
||||
from json import loads, dumps
|
||||
|
||||
import websockets
|
||||
|
||||
|
||||
class Node:
|
||||
endpoints: typing.List
|
||||
dumper = staticmethod(dumps)
|
||||
@@ -20,7 +22,7 @@ class Node:
|
||||
for endpoint in self.endpoints:
|
||||
asyncio.create_task(self.send_encoded_msgs(endpoint, msgs))
|
||||
|
||||
async def send_msgs(self, endpoint: Endpoint, msgs):
|
||||
async def send_msgs(self, endpoint: Endpoint, msgs: typing.Iterable[typing.Sequence[str, typing.Optional[dict]]]):
|
||||
if not endpoint.socket or not endpoint.socket.open or endpoint.socket.closed:
|
||||
return
|
||||
try:
|
||||
@@ -51,3 +53,61 @@ class Endpoint:
|
||||
|
||||
async def disconnect(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class HTMLtoColoramaParser(HTMLParser):
|
||||
def get_colorama_text(self, input_text: str) -> str:
|
||||
self.feed(input_text)
|
||||
self.close()
|
||||
data = self.data
|
||||
self.reset()
|
||||
return data
|
||||
|
||||
def handle_data(self, data):
|
||||
self.data += data
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
if tag in {"span", "div", "p"}:
|
||||
for attr in attrs:
|
||||
subtag, data = attr
|
||||
if subtag == "style":
|
||||
for subdata in data.split(";"):
|
||||
if subdata.startswith("color"):
|
||||
color = subdata.split(":", 1)[-1].strip()
|
||||
if color in color_codes:
|
||||
self.data += color_code(color)
|
||||
self.colored = tag
|
||||
|
||||
def handle_endtag(self, tag):
|
||||
if tag == self.colored:
|
||||
self.colored = False
|
||||
self.data += color_code("reset")
|
||||
|
||||
def reset(self):
|
||||
super(HTMLtoColoramaParser, self).reset()
|
||||
self.data = ""
|
||||
self.colored = False
|
||||
|
||||
def close(self):
|
||||
super(HTMLtoColoramaParser, self).close()
|
||||
if self.colored:
|
||||
self.handle_endtag(self.colored)
|
||||
|
||||
|
||||
color_codes = {'reset': 0, 'bold': 1, 'underline': 4, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34,
|
||||
'magenta': 35, 'cyan': 36, 'white': 37, 'black_bg': 40, 'red_bg': 41, 'green_bg': 42, 'yellow_bg': 43,
|
||||
'blue_bg': 44, 'purple_bg': 45, 'cyan_bg': 46, 'white_bg': 47}
|
||||
|
||||
|
||||
def color_code(*args):
|
||||
return '\033[' + ';'.join([str(color_codes[arg]) for arg in args]) + 'm'
|
||||
|
||||
|
||||
def color(text, *args):
|
||||
return color_code(*args) + text + color_code('reset')
|
||||
|
||||
|
||||
CLIENT_UNKNOWN = 0
|
||||
CLIENT_READY = 10
|
||||
CLIENT_PLAYING = 20
|
||||
CLIENT_GOAL = 30
|
Reference in New Issue
Block a user