various improvements

add print-token flag, put logs in log folder, load previous session after
aiocraft client setup, don't sleep if stopping
This commit is contained in:
əlemi 2022-04-19 01:17:37 +02:00
parent ab2170e669
commit 7b5958dde3
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 15 additions and 9 deletions

View file

@ -62,6 +62,7 @@ def main():
parser.add_argument('--code', dest='code', default='', help='login code for oauth2 flow')
parser.add_argument('--mojang', dest='mojang', action='store_const', const=True, default=False, help="use legacy Mojang authenticator")
parser.add_argument('--print-token', dest='print_token', action='store_const', const=True, default=False, help="show legacy token before stopping")
parser.add_argument('--addon-path', dest='path', default='', help='path for loading addons')
parser.add_argument('--chat-log', dest='chat_log', action='store_const', const=True, default=False, help="print (colored) chat to terminal")
@ -102,6 +103,9 @@ def main():
client.run()
if args.print_token:
logging.info("Token: %s", client.authenticator.serialize())
if __name__ == "__main__":
main()

View file

@ -27,7 +27,7 @@ def configure_logging(name:str, level=logging.INFO, color:bool = True):
logger = logging.getLogger()
logger.setLevel(level)
# create file handler which logs even debug messages
fh = RotatingFileHandler(f'data/{name}.log', maxBytes=1048576, backupCount=5) # 1MB files
fh = RotatingFileHandler(f'log/{name}.log', maxBytes=1048576, backupCount=5) # 1MB files
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()

View file

@ -151,12 +151,6 @@ class Treepuncher(
)
self.storage = Storage(self.name)
prev = self.storage.system() # if this isn't 1st time, this won't be None. Load token from there
if prev:
if self.name != prev.name:
self.logger.warning("Saved session belong to another user")
authenticator.deserialize(json.loads(prev.token))
self.logger.info("Loaded authenticated session")
self.modules = []
@ -168,6 +162,13 @@ class Treepuncher(
super().__init__(opt('server', required=True), online_mode=online_mode, authenticator=authenticator)
prev = self.storage.system() # if this isn't 1st time, this won't be None. Load token from there
if prev:
if self.name != prev.name:
self.logger.warning("Saved session belong to another user")
authenticator.deserialize(json.loads(prev.token))
self.logger.info("Loaded authenticated session")
@property
def playerName(self) -> str:
@ -177,7 +178,7 @@ class Treepuncher(
await super().authenticate()
state = SystemState(
name=self.name,
token=json.dumps(self._authenticator.serialize()),
token=json.dumps(self.authenticator.serialize()),
start_time=int(time())
)
self.storage._set_state(state)
@ -232,6 +233,7 @@ class Treepuncher(
except Exception:
self.logger.exception("Unhandled exception")
break
await asyncio.sleep(5) # TODO setting
if self._processing:
await asyncio.sleep(self.config['core'].getfloat('reconnect_delay', fallback=5))
if self._processing:
await self.stop(force=True)