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

View file

@ -88,6 +88,8 @@ class Addon:
async def cleanup(self):
pass
class MissingParameterError(Exception):
pass
class Treepuncher(
GameState,
@ -125,23 +127,26 @@ class Treepuncher(
authenticator : AuthInterface
def opt(k:str) -> Any:
return kwargs.get(k) or self.config['Treepuncher'].get(k)
def opt(k:str, required=False, default=None) -> Any:
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:
authenticator = OfflineAuthenticator(self.name)
elif legacy:
authenticator = MojangAuthenticator(
username= opt('username') or name,
username= opt('username', default=name, required=True),
password= opt('password')
)
if opt('legacy_token'):
authenticator.deserialize(json.loads(opt('legacy_token')))
else:
authenticator = MicrosoftAuthenticator(
client_id= opt('client_id'),
client_secret= opt('client_secret'),
redirect_uri= opt('redirect_uri'),
client_id= opt('client_id', required=True),
client_secret= opt('client_secret', required=True),
redirect_uri= opt('redirect_uri', required=True),
code= opt('code'),
)
@ -161,7 +166,7 @@ class Treepuncher(
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy
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