From a4768df0ca2e956f245e0833331ed8abb203937a Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 30 Nov 2014 16:14:05 -0600 Subject: Context objects now use a contextmanager (and update gmg shell to use it) This means that we have a really convenient way to make sure that you have a context/request that threads its way through the application, where everything needed gets "shut down" appropriately by the end. You always get a context object via a context manager! And by the time you're done with it, things should be cleaned up. --- mediagoblin/gmg_commands/shell.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'mediagoblin/gmg_commands/shell.py') diff --git a/mediagoblin/gmg_commands/shell.py b/mediagoblin/gmg_commands/shell.py index 70e11404..4d3ec241 100644 --- a/mediagoblin/gmg_commands/shell.py +++ b/mediagoblin/gmg_commands/shell.py @@ -35,7 +35,8 @@ if DISABLE_GLOBALS: "----------------------\n" "Available vars:\n" " - app: instantiated mediagoblin application\n" - " - db: database session\n") + " - db: database session\n" + " - ctx: context object\n") else: SHELL_BANNER = ( "GNU MediaGoblin shell!\n" @@ -43,7 +44,8 @@ else: "Available vars:\n" " - app: instantiated mediagoblin application\n" " - mg_globals: mediagoblin.globals\n" - " - db: database instance\n") + " - db: database instance\n" + " - ctx: context object\n") def py_shell(**user_namespace): """ @@ -75,11 +77,12 @@ def shell(args): """ app = commands_util.setup_app(args) - def run_shell(db): + def run_shell(db, ctx): user_namespace = { 'mg_globals': mg_globals, 'app': app, - 'db': db} + 'db': db, + "ctx": ctx} if args.ipython: ipython_shell(**user_namespace) @@ -88,8 +91,6 @@ def shell(args): if not ipython_shell(**user_namespace): py_shell(**user_namespace) - if DISABLE_GLOBALS: - with app.db_manager.session_scope() as db: - run_shell(db) - else: - run_shell(mg_globals.database) + with app.gen_context() as ctx: + db = ctx.db + run_shell(db, ctx) -- cgit v1.2.3