Solve the accentuation problem in Evennia (Python 3)
This commit is contained in:
parent
844b04adbb
commit
49eb2b2873
4 changed files with 40 additions and 5 deletions
|
|
@ -638,7 +638,7 @@ class AMPLauncherProtocol(amp.AMP):
|
||||||
else:
|
else:
|
||||||
status = pickle.loads(status)
|
status = pickle.loads(status)
|
||||||
callback(status)
|
callback(status)
|
||||||
return {b"status": b""}
|
return {"status": pickle.dumps(b"")}
|
||||||
|
|
||||||
|
|
||||||
def send_instruction(operation, arguments, callback=None, errback=None):
|
def send_instruction(operation, arguments, callback=None, errback=None):
|
||||||
|
|
|
||||||
|
|
@ -73,11 +73,11 @@ Content-Type: text/html
|
||||||
# Helper functions for pickling.
|
# Helper functions for pickling.
|
||||||
|
|
||||||
def dumps(data):
|
def dumps(data):
|
||||||
return to_str(pickle.dumps(to_str(data), pickle.HIGHEST_PROTOCOL))
|
return pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
|
||||||
|
|
||||||
|
|
||||||
def loads(data):
|
def loads(data):
|
||||||
return pickle.loads(to_str(data))
|
return pickle.loads(data)
|
||||||
|
|
||||||
|
|
||||||
@wraps
|
@wraps
|
||||||
|
|
|
||||||
|
|
@ -243,7 +243,7 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session):
|
||||||
line (str): Line to send.
|
line (str): Line to send.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
line = bytes(line, 'utf-8')
|
line = self.try_encode(line)
|
||||||
# escape IAC in line mode, and correctly add \r\n (the TELNET end-of-line)
|
# escape IAC in line mode, and correctly add \r\n (the TELNET end-of-line)
|
||||||
line = line.replace(IAC, IAC + IAC)
|
line = line.replace(IAC, IAC + IAC)
|
||||||
line = line.replace(b'\n', b'\r\n')
|
line = line.replace(b'\n', b'\r\n')
|
||||||
|
|
@ -343,7 +343,7 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session):
|
||||||
strip_ansi=nocolor, xterm256=xterm256)
|
strip_ansi=nocolor, xterm256=xterm256)
|
||||||
if mxp:
|
if mxp:
|
||||||
prompt = mxp_parse(prompt)
|
prompt = mxp_parse(prompt)
|
||||||
prompt = prompt.encode()
|
prompt = self.try_encode(prompt)
|
||||||
prompt = prompt.replace(IAC, IAC + IAC).replace(b'\n', b'\r\n')
|
prompt = prompt.replace(IAC, IAC + IAC).replace(b'\n', b'\r\n')
|
||||||
prompt += IAC + GA
|
prompt += IAC + GA
|
||||||
self.transport.write(mccp_compress(self, prompt))
|
self.transport.write(mccp_compress(self, prompt))
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,41 @@ class Session(object):
|
||||||
if self.account:
|
if self.account:
|
||||||
self.protocol_flags.update(self.account.attributes.get("_saved_protocol_flags", {}))
|
self.protocol_flags.update(self.account.attributes.get("_saved_protocol_flags", {}))
|
||||||
|
|
||||||
|
# helpers
|
||||||
|
|
||||||
|
def try_encode(self, text):
|
||||||
|
"""
|
||||||
|
Try to encode the given text, following the session's protocol flag.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
text (str or bytes): the text to encode to bytes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
encoded_text (bytes): the encoded text following the session's
|
||||||
|
protocol flag. If the converting fails, log the error
|
||||||
|
and send the text with "?" in place of problematic
|
||||||
|
characters. If the specified encoding cannot be found,
|
||||||
|
the protocol flag is reset to utf-8.
|
||||||
|
In any case, returns bytes.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
If the argument is bytes, return it as is.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if isinstance(text, bytes):
|
||||||
|
return text
|
||||||
|
|
||||||
|
try:
|
||||||
|
encoded = text.encode(self.protocol_flags["ENCODING"])
|
||||||
|
except LookupError:
|
||||||
|
self.protocol_flags["ENCODING"] = 'utf-8'
|
||||||
|
encoded = text.encode('utf-8')
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print("An error occurred during string encoding to {encoding}. Will remove errors and try again.".format(encoding=self.protocol_flags["ENCODING"]))
|
||||||
|
encoded = text.encode(self.protocol_flags["ENCODING"], errors="replace")
|
||||||
|
|
||||||
|
return encoded
|
||||||
|
|
||||||
# access hooks
|
# access hooks
|
||||||
|
|
||||||
def disconnect(self, reason=None):
|
def disconnect(self, reason=None):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue