fancier missing required parameter

This commit is contained in:
əlemi 2022-04-19 00:43:39 +02:00
parent 9d7badba5d
commit ab2170e669
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
2 changed files with 22 additions and 14 deletions

View file

@ -13,7 +13,7 @@ from dataclasses import dataclass, MISSING, fields
from setproctitle import setproctitle from setproctitle import setproctitle
from .treepuncher import Treepuncher, Addon, ConfigObject from .treepuncher import Treepuncher, Addon, ConfigObject, MissingParameterError
from .helpers import configure_logging from .helpers import configure_logging
def main(): def main():
@ -79,12 +79,15 @@ def main():
if args.server: if args.server:
kwargs["server"] = args.server kwargs["server"] = args.server
client = Treepuncher( try:
args.name, client = Treepuncher(
args.server, args.name,
legacy=args.mojang, args.server,
use_packet_whitelist=args.use_packet_whitelist, legacy=args.mojang,
) use_packet_whitelist=args.use_packet_whitelist,
)
except MissingParameterError as e:
return logging.error(e.args[0])
enabled_addons = set( enabled_addons = set(
a.lower() for a in ( a.lower() for a in (

View file

@ -88,6 +88,8 @@ class Addon:
async def cleanup(self): async def cleanup(self):
pass pass
class MissingParameterError(Exception):
pass
class Treepuncher( class Treepuncher(
GameState, GameState,
@ -125,23 +127,26 @@ class Treepuncher(
authenticator : AuthInterface authenticator : AuthInterface
def opt(k:str) -> Any: def opt(k:str, required=False, default=None) -> Any:
return kwargs.get(k) or self.config['Treepuncher'].get(k) v = kwargs.get(k) or self.config['Treepuncher'].get(k) or default
if not v and required:
raise MissingParameterError(f"Missing configuration parameter '{k}'")
return v
if not online_mode: if not online_mode:
authenticator = OfflineAuthenticator(self.name) authenticator = OfflineAuthenticator(self.name)
elif legacy: elif legacy:
authenticator = MojangAuthenticator( authenticator = MojangAuthenticator(
username= opt('username') or name, username= opt('username', default=name, required=True),
password= opt('password') password= opt('password')
) )
if opt('legacy_token'): if opt('legacy_token'):
authenticator.deserialize(json.loads(opt('legacy_token'))) authenticator.deserialize(json.loads(opt('legacy_token')))
else: else:
authenticator = MicrosoftAuthenticator( authenticator = MicrosoftAuthenticator(
client_id= opt('client_id'), client_id= opt('client_id', required=True),
client_secret= opt('client_secret'), client_secret= opt('client_secret', required=True),
redirect_uri= opt('redirect_uri'), redirect_uri= opt('redirect_uri', required=True),
code= opt('code'), code= opt('code'),
) )
@ -161,7 +166,7 @@ class Treepuncher(
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy
self.scheduler.start(paused=True) self.scheduler.start(paused=True)
super().__init__(opt('server'), online_mode=online_mode, authenticator=authenticator) super().__init__(opt('server', required=True), online_mode=online_mode, authenticator=authenticator)
@property @property