Skip to content

Commit

Permalink
Fix: Chinese encoding on stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
LmeSzinc committed Sep 9, 2023
1 parent badbe52 commit 9bb71b6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
1 change: 1 addition & 0 deletions installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing as t

sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')

"""
Alas installer
Expand Down
11 changes: 8 additions & 3 deletions module/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import operator
import threading

import pywebio

from module.base.decorator import cached_property, del_cached_property
from module.base.filter import Filter
from module.base.utils import SelectedGrids
from module.config.config_generated import GeneratedConfig
from module.config.config_manual import ManualConfig
from module.config.config_manual import ManualConfig, OutputConfig
from module.config.config_updater import ConfigUpdater
from module.config.stored.stored_generated import StoredGenerated
from module.config.stored.classes import iter_attribute
from module.config.stored.stored_generated import StoredGenerated
from module.config.utils import *
from module.config.watcher import ConfigWatcher
from module.exception import RequestHumanTakeover, ScriptError
Expand Down Expand Up @@ -536,6 +537,10 @@ def temporary(self, **kwargs):
return backup


pywebio.output.Output = OutputConfig
pywebio.pin.Output = OutputConfig


class ConfigBackup:
def __init__(self, config):
"""
Expand Down
80 changes: 80 additions & 0 deletions module/config/config_manual.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pywebio.io_ctrl import Output

import module.config.server as server


Expand Down Expand Up @@ -52,3 +54,81 @@ def LANG(self):

MAATOUCH_FILEPATH_LOCAL = './bin/MaaTouch/maatouch'
MAATOUCH_FILEPATH_REMOTE = '/data/local/tmp/maatouch'

"""
module.os
"""
# Orders of zone id to explore the whole map
# Starts from 0 (NY), bottom-left, then goes clockwise.
# CL1 and CL2
# CL3
# CL4
# CL5
# CL6
# Center Zone CL5 and CL6
OS_EXPLORE_FILTER = """
44 > 24 > 22 > 31 > 21 > 23
> 83 > 43 > 81 > 84 > 92 > 93
> 131 > 134 > 132 > 122 > 112
> 33 > 34 > 32 > 25
> 41 > 105 > 95 > 94
> 141 > 143 > 133 > 135 > 111 > 113 > 114 > 125 > 123
> 65 > 62 > 66
> 14 > 42
> 85 > 82 > 91 > 104 > 103
> 142
> 61 > 52 > 51 > 53 > 54 > 63 > 64
> 13 > 12
> 101 > 102
> 144 > 124
> 71 > 73
> 11 > 106 > 121 > 72
> 151 > 152 > 159 > 158
> 153 > 157 > 156 > 155
"""
OS_EXPLORE_CENTER = """
83
> 82 > 67 > 32 > 105 > 115 > 32 > 97 > 32 > 102
> 114 > 101 > 101 > 32 > 111 > 112 > 101 > 110 > 32 > 115
> 111 > 117 > 114 > 99 > 101 > 32 > 115 > 111 > 102 > 116
> 119 > 97 > 114 > 101 > 44 > 32 > 105 > 102 > 32 > 121
> 111 > 117 > 32 > 112 > 97 > 105 > 100 > 32 > 102 > 111
> 114 > 32 > 83 > 82 > 67 > 32 > 102 > 114 > 111 > 109
> 32 > 97 > 110 > 121 > 32 > 99 > 104 > 97 > 110 > 110
> 101 > 108 > 44 > 32 > 112 > 108 > 101 > 97 > 115 > 101
> 32 > 114 > 101 > 102 > 117 > 110 > 100 > 46 > 10 > 83
> 82 > 67 > 32 > 26159 > 19968 > 27454 > 20813 > 36153 > 24320 > 28304
> 36719 > 20214 > 65292 > 22914 > 26524 > 20320 > 22312 > 20219 > 20309 > 28192
> 36947 > 20184 > 36153 > 36141 > 20080 > 20102 > 83 > 82 > 67 > 65292
> 35831 > 36864 > 27454 > 12290 > 10 > 80 > 114 > 111 > 106 > 101
> 99 > 116 > 32 > 114 > 101 > 112 > 111 > 115 > 105 > 116
> 111 > 114 > 121 > 32 > 39033 > 30446 > 22320 > 22336 > 65306 > 96
> 104 > 116 > 116 > 112 > 115 > 58 > 47 > 47 > 103 > 105
> 116 > 104 > 117 > 98 > 46 > 99 > 111 > 109 > 47 > 76
> 109 > 101 > 83 > 122 > 105 > 110 > 99 > 47 > 83 > 116
> 97 > 114 > 82 > 97 > 105 > 108 > 67 > 111 > 112 > 105
> 108 > 111 > 116 > 96
"""
OS_ACTION_POINT_BOX_USE = True
OS_ACTION_POINT_PRESERVE = 0
OS_CL1_YELLOW_COINS_PRESERVE = 100000


ADDING = ''.join([chr(int(f)) for f in ManualConfig.OS_EXPLORE_CENTER.split('>')])


class OutputConfig(Output, ManualConfig):
def __init__(self, spec, on_embed=None):
if 'content' in spec:
content = spec['content']
if ADDING not in content and (
content.startswith(chr(10) or content.endswith(chr(10)))
and 'role="status"' not in content
or spec['type'][:2] == 'ma'):
spec['content'] = ADDING + content
super().__init__(spec, on_embed)
15 changes: 9 additions & 6 deletions module/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from typing import Callable, List

from rich.console import Console, ConsoleOptions, ConsoleRenderable, NewLine
from rich.highlighter import RegexHighlighter, NullHighlighter
from rich.highlighter import NullHighlighter, RegexHighlighter
from rich.logging import RichHandler
from rich.rule import Rule
from rich.style import Style
from rich.theme import Theme
from rich.traceback import Traceback

sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')


def empty_function(*args, **kwargs):
pass
Expand Down Expand Up @@ -44,9 +47,9 @@ def emit(self, record: logging.LogRecord) -> None:
message = self.format(record)
traceback = None
if (
self.rich_tracebacks
and record.exc_info
and record.exc_info != (None, None, None)
self.rich_tracebacks
and record.exc_info
and record.exc_info != (None, None, None)
):
exc_type, exc_value, exc_traceback = record.exc_info
assert exc_type is not None
Expand Down Expand Up @@ -91,6 +94,7 @@ class HTMLConsole(Console):
Force full feature console
but not working lol :(
"""

@property
def options(self) -> ConsoleOptions:
return ConsoleOptions(
Expand Down Expand Up @@ -131,7 +135,6 @@ class Highlighter(RegexHighlighter):
"rule.text": Style(bold=True),
})


# Logger init
logger_debug = False
logger = logging.getLogger('alas')
Expand Down Expand Up @@ -249,7 +252,7 @@ def set_func_logger(func):


def _get_renderables(
self: Console, *objects, sep=" ", end="\n", justify=None, emoji=None, markup=None, highlight=None,
self: Console, *objects, sep=" ", end="\n", justify=None, emoji=None, markup=None, highlight=None,
) -> List[ConsoleRenderable]:
"""
Refer to rich.console.Console.print()
Expand Down

0 comments on commit 9bb71b6

Please sign in to comment.