Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommonClient: abstract url handling so it's importable #4068

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
30 changes: 19 additions & 11 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

if typing.TYPE_CHECKING:
import kvui
import argparse

logger = logging.getLogger("Client")

Expand Down Expand Up @@ -1028,6 +1029,23 @@ def get_base_parser(description: typing.Optional[str] = None):
return parser


def handle_url_arg(args: "argparse.Namespace", parser: "typing.Optional[argparse.parser]" = None) -> "argparse.Namespace":
qwint marked this conversation as resolved.
Show resolved Hide resolved
# handle if text client is launched using the "archipelago://name:pass@host:port" url from webhost
qwint marked this conversation as resolved.
Show resolved Hide resolved
if args.url:
url = urllib.parse.urlparse(args.url)
if url.scheme == "archipelago":
args.connect = url.netloc
if url.username:
args.name = urllib.parse.unquote(url.username)
if url.password:
args.password = urllib.parse.unquote(url.password)
else:
if not parser:
parser = get_base_parser()
parser.error(f"bad url, found {args.url}, expected url in form of archipelago://archipelago.gg:38281")
return args


def run_as_textclient(*args):
class TextContext(CommonContext):
# Text Mode to use !hint and such with games that have no text entry
Expand Down Expand Up @@ -1069,17 +1087,7 @@ async def main(args):
parser.add_argument("url", nargs="?", help="Archipelago connection url")
args = parser.parse_args(args)

# handle if text client is launched using the "archipelago://name:pass@host:port" url from webhost
if args.url:
url = urllib.parse.urlparse(args.url)
if url.scheme == "archipelago":
args.connect = url.netloc
if url.username:
args.name = urllib.parse.unquote(url.username)
if url.password:
args.password = urllib.parse.unquote(url.password)
else:
parser.error(f"bad url, found {args.url}, expected url in form of archipelago://archipelago.gg:38281")
args = handle_url_arg(args)

# use colorama to display colored text highlighting on windows
colorama.init()
Expand Down
Loading