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('--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('--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('--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") 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() client.run()
if args.print_token:
logging.info("Token: %s", client.authenticator.serialize())
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -27,7 +27,7 @@ def configure_logging(name:str, level=logging.INFO, color:bool = True):
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(level) logger.setLevel(level)
# create file handler which logs even debug messages # 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) fh.setLevel(logging.DEBUG)
# create console handler with a higher log level # create console handler with a higher log level
ch = logging.StreamHandler() ch = logging.StreamHandler()

View file

@ -151,12 +151,6 @@ class Treepuncher(
) )
self.storage = Storage(self.name) 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 = [] self.modules = []
@ -168,6 +162,13 @@ class Treepuncher(
super().__init__(opt('server', required=True), online_mode=online_mode, authenticator=authenticator) 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 @property
def playerName(self) -> str: def playerName(self) -> str:
@ -177,7 +178,7 @@ class Treepuncher(
await super().authenticate() await super().authenticate()
state = SystemState( state = SystemState(
name=self.name, name=self.name,
token=json.dumps(self._authenticator.serialize()), token=json.dumps(self.authenticator.serialize()),
start_time=int(time()) start_time=int(time())
) )
self.storage._set_state(state) self.storage._set_state(state)
@ -232,6 +233,7 @@ class Treepuncher(
except Exception: except Exception:
self.logger.exception("Unhandled exception") self.logger.exception("Unhandled exception")
break break
await asyncio.sleep(5) # TODO setting if self._processing:
await asyncio.sleep(self.config['core'].getfloat('reconnect_delay', fallback=5))
if self._processing: if self._processing:
await self.stop(force=True) await self.stop(force=True)