# HG changeset patch # User Simon Cross # Date 1316210626 -7200 # Node ID cc8be536a7fc241c5655f2c33a3411bec8c5c415 # Parent d4062344d8f446167c1994b1e1a49e6e4318e0f7 Add ability to play uncurated levels via the command line. diff -r d4062344d8f4 -r cc8be536a7fc mamba/__main__.py --- a/mamba/__main__.py Fri Sep 16 23:53:34 2011 +0200 +++ b/mamba/__main__.py Sat Sep 17 00:03:46 2011 +0200 @@ -11,6 +11,7 @@ from mamba.sound import SoundSystem from mamba.habitats.mainmenu import MainMenu from mamba.habitats.levelmenu import LevelMenu +from mamba.habitats.userlevelmenu import UserLevelApi from mamba.habitats.level import LevelHabitat from mamba.habitats.editor import EditorHabitat from mamba.level import Level @@ -36,6 +37,10 @@ elif options.level is not None: start = LevelHabitat(Level(options.level, 'official'), LevelMenu.go_menu) + elif options.uncurated is not None: + api = UserLevelApi("uncurated") + start = LevelHabitat(api.get_level(options.uncurated), + sys.exit) else: start = MainMenu() diff -r d4062344d8f4 -r cc8be536a7fc mamba/constants.py --- a/mamba/constants.py Fri Sep 16 23:53:34 2011 +0200 +++ b/mamba/constants.py Sat Sep 17 00:03:46 2011 +0200 @@ -39,6 +39,7 @@ 'debug': False, 'sound': True, 'level': None, + 'uncurated': None, 'edit': False, 'save_location': None, # Determined by a function in options } diff -r d4062344d8f4 -r cc8be536a7fc mamba/habitats/userlevelmenu.py --- a/mamba/habitats/userlevelmenu.py Fri Sep 16 23:53:34 2011 +0200 +++ b/mamba/habitats/userlevelmenu.py Sat Sep 17 00:03:46 2011 +0200 @@ -7,40 +7,58 @@ import urllib2 -class UserLevelMenu(LevelMenu): +class UserLevelApi(object): - level_namespace = 'curated' - LEVEL_SERVER_URL = LEVEL_SERVER + "curated/" - TIMEOUT = 5.0 # in seconds - CACHE = {} + def __init__(self, ctype, url=LEVEL_SERVER, timeout=5): + assert ctype in ("curated", "uncurated") + assert url.endswith("/") + self.ctype = ctype + self.level_namespace = ctype + self.url = url + self.timeout = timeout + self.cache = {} - @classmethod - def _url_data(cls, route): - url = "%s%s" % (cls.LEVEL_SERVER_URL, route) - return urllib2.urlopen(url, timeout=cls.TIMEOUT).read() + def _url_data(self, route): + url = "%s%s/%s" % (self.url, self.ctype, route) + return urllib2.urlopen(url, timeout=self.timeout).read() - @classmethod - def _populate_cache(cls): + def _populate_level(self, name): try: - data = cls._url_data("index") + source = self._url_data("level/%s" % name) + level = Level(name, self.level_namespace, source) + except: + print "Failed to download online level %r" % name + return + self.cache[name] = level + + def _populate_cache(self): + try: + data = self._url_data("index") except: print "Failed to download online level index." return levels = [x.strip() for x in data.splitlines()] for name in levels: - try: - source = cls._url_data("level/%s" % name) - level = Level(name, cls.level_namespace, source) - except: - print "Failed to download online level %r" % name - continue - cls.CACHE[level] = level + self._populate_level(name) def list_levels(self): - if not self.CACHE: + if not self.cache: self._populate_cache() - return self.CACHE.keys() + return self.cache.keys() def get_level(self, name): - return self.CACHE[name] + if name not in self.cache: + self._populate_level(name) + return self.cache[name] + + +class UserLevelMenu(LevelMenu): + + API = UserLevelApi("curated") + + def list_levels(self): + return self.API.list_levels() + + def get_level(self, name): + return self.API.get_level(name) diff -r d4062344d8f4 -r cc8be536a7fc mamba/options.py --- a/mamba/options.py Fri Sep 16 23:53:34 2011 +0200 +++ b/mamba/options.py Sat Sep 17 00:03:46 2011 +0200 @@ -44,6 +44,9 @@ 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.") opts, _ = parser.parse_args(args) @@ -52,6 +55,7 @@ if options.debug: options.set_option('level', opts.level) + options.set_option('uncurated', opts.uncurated) options.set_option('edit', opts.edit) options.finalize()