Commits

rkruppe committed cf20f97

Kill .config and .util.path, they turned out unused and useless
Also kill a bit of other dead code I found in the process

Comments (0)

Files changed (4)

ttgx/config.py

-import os
-import logging
-from ttgx.meta import to_yaml, from_yaml, model
-# We have a circular dependency here, as ttgx_path is used to find the config
-# file to load, but ttgx_path also needs ttgx.config for some functions
-# (nothing we access from here, so no real problem).
-# Thus we import it in load() where it is needed.
-#import ttgx.util.path as ttgx_path
-from ttgx.meta import validate
-
-__all__ = ['get', 'set', 'load', 'save']
-
-log = logging.getLogger(__name__)
-
-
-class Config(model.Model):
-    is_value_type = True
-    # These paths are intended to be cross-platform. Both / and \ are accepted
-    # as seperator, and on use they are transformed to use the one the
-    # underlying platform uses.
-    gameset_dir = model.String()
-    savegame_dir = model.String()
-    language = model.String()
-
-_CONFIG_OBJ = None
-
-
-def get(attr):
-    return getattr(_CONFIG_OBJ, attr)
-
-
-def set(attr, val):
-    setattr(_CONFIG_OBJ, attr, val)
-
-
-def load():
-    # global state, ugh. But then again, YAGNI...
-    global _CONFIG_OBJ
-    import ttgx.util.path as ttgx_path
-    config_path = ttgx_path.below_home('config.yaml')
-    with open(config_path) as f:
-        obj, errors = from_yaml.load(Config, f)
-    if errors:
-        log.error('Configuration file is invalid:')
-        for line in validate.format_errors(errors):
-            log.error('%s', line)
-        raise RuntimeError('config.yaml is invalid')
-    _CONFIG_OBJ = obj
-
-
-def save():
-    config_path = ttgx_path.below_home('config.yaml')
-    with open(config_path, 'w') as f:
-        to_yaml.YAMLWriter().dump(_CONFIG_OBJ, f)
-
 import ttgx.gui.driver
 import ttgx.gui.window
 import ttgx.gui.model
-import ttgx.util.path as ttgx_path
-import ttgx.config
 import ttgx.assets
 from ttgx.model import gamestate
 from ttgx.meta import from_yaml
 
 
 def init():
-    if args.home is None:
-        ttgx_path.init_from_module(__name__)
-    else:
-        ttgx_path.init_from_directory(args.home)
-    ttgx.config.load()
-    gsc = gamestate.GameStateController(args.gameset)
+    gsdir = os.path.abspath(args.gameset)
+    gsc = gamestate.GameStateController(gsdir)
     return gsc
 
 
     gsc.gui_driver = driver
 
 
-def exit(gsc):
-    if gsc.game_active:
-        try:
-            gsc.save('autosave')
-        except Exception:
-            log.error('error during saving:', exc_info=True)
-
-
 def main():
     try:
         gsc = init()
     #TODO fix our timestep
     pyglet.clock.schedule_interval(lambda _: gsc.universe.step(), 0.1)
     pyglet.app.run()
-    exit(gsc)
 
 if __name__ == '__main__':
     main()

ttgx/model/gamestate.py

 import pyglet
 
 from ttgx.meta import model, from_yaml, validate
-import ttgx.util.path as ttgx_path
 from ttgx.util import event
 import ttgx.toolutil
 import ttgx.assets
 import ttgx.util
 import ttgx.dodecs
 
-from .date import Date
 from .gameset import GameSet
 
 log = logging.getLogger(__name__)
 
 
-@ttgx.util.split.simple_splitting
-class GameState(model.Model):
-    date = model.Value(Date)
-    # only store the gameset directory, to allow GS modifications
-    gameset = model.String()
-
-
-#XXX are we sure we need those exceptions?
-
-
-class LoadingError(Exception):
-    pass
-
-
-class ValidationFailure(Exception):
-    pass
-
-
 @event.source
 class GameStateController:
     EVENTS = ('StateReset', 'NewGame')
 
-    def __init__(self, gameset_name, state=None):
-        self._state = state
-        self.gameset, self.gameset_assets = _load_gameset(gameset_name)
-        self.gameset_name = gameset_name
+    def __init__(self, gameset_path):
+        self.gameset_path = gameset_path
+        self.gameset, self.gameset_assets = _load_gameset(gameset_path)
         self.gui_driver = None
         self.universe = ttgx.dodecs.Universe()
 
             exc_info = (type(err), err, err.__traceback__)
             log.error('Error in gameset entry point', exc_info=exc_info)
 
-    def ecs_step(self):
-        pass
+    def gameset_gui_path(self):
+        return os.path.join(self.gameset_path, self.gameset.gui)
 
-    def savegame_path(self, name):
-        """
-        Return the path to savegame (a directory) called ``name``.
-        If it does not exist yet, create it before returning.
-        """
-        path = ttgx_path.savegame_path(name)
-        log.debug("Savegame path: %r", path)
-        if not os.path.exists(path):
-            os.makedirs(path)
-        return path
 
-    def gameset_gui_path(self):
-        gsdir = ttgx_path.gameset_path(self.gameset_name)
-        return os.path.join(gsdir, self.gameset.gui)
-
-    @property
-    def game_active(self):
-        return False
-
-
-def _load_gameset(name):
-    gsdir = ttgx_path.gameset_path(name)
+def _load_gameset(gsdir):
     #TODO better error handling
     gs, _, vctx = ttgx.toolutil.load_with_context(GameSet, gsdir)
     if vctx.errors:
         log.error('Gameset %r (in %r) is invalid:', name, gsdir)
         for err_line in validate.format_errors(vctx.errors):
             log.error('%s', err_line)
-        raise ValidationFailure
+        raise RuntimeError('Gameset data invalid')
     gs = gs[0]
     assets_dir = os.path.join(gsdir, gs.assets)
     gameset_assets = ttgx.assets.AssetCollection(assets_dir)

ttgx/util/path.py

-"""
-Determine paths for program files, gamesets, assets, etc.
-"""
-import os
-import sys
-import glob
-import re
-import ttgx.config
-
-_game_home = None
-
-
-def translate_config_path(path):
-    """
-    Return a path for the underlying system that mirrors ``path``.
-    ``path`` is a platform-agnostic path (e.g. for configuration files) in
-    that it always uses ``..`` for the parent directory and may use both
-    forward and backwards slashes as path seperators.
-    """
-    parts = re.split(r'/|\\', path)
-    for i, part in enumerate(parts):
-        if part == '..':
-            parts[i] = os.pardir
-    return os.path.join(*parts)
-
-
-def game_home():
-    """
-    Return the toplevel directory containing the game.
-    """
-    return _game_home
-
-
-def below_home(*parts):
-    """
-    Shortcut for os.path.join-ing the game home with the supplied parts.
-    """
-    return os.path.normpath(os.path.join(game_home(), *parts))
-
-
-def savegame_path(name=None):
-    """
-    Get the full path for the savegame of the given ``name``, or the full path
-    of the directory which contains all savegames if not given.
-    The path may not exist yet.
-    """
-    save_dir = translate_config_path(ttgx.config.get('savegame_dir'))
-    if name is None:
-        return below_home(save_dir)
-    return below_home(save_dir, name)
-
-
-def gameset_path(name=None):
-    """
-    Get the full path for the gameset of the given name, or the full path
-    of the directory which contains all gamesets if not given.
-    The path may not exist, this function merely computes where it *would* be.
-    """
-    gs_dir = translate_config_path(ttgx.config.get('gameset_dir'))
-    if name is None:
-        return below_home(gs_dir)
-    return below_home(gs_dir, name)
-
-
-def iter_model_data_in_dir(root):
-    """
-    Yield the names of all directories immediately below path which appear
-    to contain model data serialized by ``meta.DataSplit``.
-    May yield false positives -- the only way to know if it's actually
-    a valid object of the expected type is to try and load it.
-    """
-    if not os.path.exists(root):
-        return
-    for name in os.listdir(root):
-        path = os.path.join(root, name)
-        if os.path.isfile(path):
-            continue
-        # Several destructive tools generate backups by default, which are
-        # signified by a .bak suffix -- we ignore them
-        if name.endswith('.bak'):
-            continue
-        has_yaml = glob.glob(os.path.join(path, '*.yaml'))
-        if has_yaml:
-            yield name
-
-
-def init_from_module(name):
-    """
-    Initialize the module and determine several paths, passing the __name__
-    of the entry module. You should NOT pass '__main__' to play nice with
-    (for instance) profiling.
-    """
-    global _game_home
-    mod = sys.modules[name]
-    game_home, _ = os.path.split(mod.__file__)
-    #XXX this assumes program files are in a subdirectory
-    _game_home = os.path.abspath(os.path.join(game_home, os.pardir))
-
-
-def init_from_directory(path):
-    """
-    Accept a game home directory path, instead of guessing it from a module.
-    """
-    global _game_home
-    _game_home = os.path.abspath(path)
-