view mamba/options.py @ 596:55e9c1b8e94c

Add --fullscreen option (that autoscales on pygame 2).
author Simon Cross <hodgestar@gmail.com>
date Sat, 14 Jan 2023 18:45:33 +0100
parents fca61cd8fc33
children
line wrap: on
line source

# global options that are set on startup

import optparse
import os
import sys
from mamba.constants import DEFAULTS


class OptionGetter(object):
    """Object grabber."""

    def __init__(self, options):
        self._options = options
        self._finalized = False

    def set_option(self, name, value):
        if self._finalized:
            raise RuntimeError("Cannot set option %r to %r after"
                               "options are finalized." % (name, value))
        self._options[name] = value

    def finalize(self):
        self._finalized = True

    def __getattr__(self, name):
        if name in self._options:
            return self._options[name]
        raise AttributeError("Unknown option %r" % (name,))

options = OptionGetter(DEFAULTS.copy())


def parse_args(args, options=options):
    options.set_option('debug', 'DEBUG' in os.environ)

    parser = optparse.OptionParser()
    parser.add_option("--no-sound", action="store_false", default=True,
                      dest="sound", help="disable sound")
    parser.add_option("--fullscreen", action="store_true", default=False,
                      dest="fullscreen", help="enable fullscreen")
    parser.add_option("--save-location", default=_get_default_save_location(),
                      dest="save_location", help="Saved game location")

    if options.debug:
        parser.add_option("--level", type="str", default=None,
                dest="level", help="Initial level")
        parser.add_option('--edit', action="store_true", default=False,
                dest="edit", help="Edit given level")
        parser.add_option("--uncurated", type="str", default=None,
                dest="uncurated", help="Load uncurated level "
                                       "from the web by name.")
        parser.add_option("--list-uncurated", action="store_true",
                          default=False, dest="list_uncurated",
                          help="List uncurated levels.")

    opts, _ = parser.parse_args(args)

    options.set_option('sound', opts.sound)
    options.set_option('fullscreen', opts.fullscreen)
    options.set_option('save_location', opts.save_location)

    if options.debug:
        options.set_option('level', opts.level)
        options.set_option('uncurated', opts.uncurated)
        options.set_option('list_uncurated', opts.list_uncurated)
        options.set_option('edit', opts.edit)

    options.finalize()


def check_args(options=options):
    """Check options and complain if they're invalid."""
    if options.edit and options.level is None:
        print("You must supply --level when using --edit.")
        return False
    return True


def _get_default_save_location():
    """Return a default save game location."""
    app = "mamba"
    if sys.platform.startswith("win"):
        if "APPDATA" in os.environ:
            return os.path.join(os.environ["APPDATA"], app)
        return os.path.join(os.path.expanduser("~"), "." + app)
    elif 'XDG_DATA_HOME' in os.environ:
        return os.path.join(os.environ["XDG_DATA_HOME"], app)
    return os.path.join(os.path.expanduser("~"), ".local", "share", app)