From b88ca698dd4dc4eb486b08bdb10be9123ef01e8e Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 30 Nov 2014 12:49:26 -0600 Subject: [PATCH] An environment variable to transition towards removing global variables --- mediagoblin/app.py | 21 ++++++++++++++------- mediagoblin/db/base.py | 14 +++++++++----- mediagoblin/tools/transition.py | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 mediagoblin/tools/transition.py diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 8b231163..e5442010 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -42,6 +42,8 @@ from mediagoblin.tools.pluginapi import PluginManager, hook_transform from mediagoblin.tools.crypto import setup_crypto from mediagoblin.auth.tools import check_auth_enabled, no_auth_logout +from mediagoblin.tools.transition import DISABLE_GLOBALS + _log = logging.getLogger(__name__) @@ -150,9 +152,14 @@ class MediaGoblinApp(object): # certain properties need to be accessed globally eg from # validators, etc, which might not access to the request # object. + # + # Note, we are trying to transition this out; + # run with environment variable DISABLE_GLOBALS=true + # to work on it ####################################################### - setup_globals(app=self) + if not DISABLE_GLOBALS: + setup_globals(app=self) # Workbench *currently* only used by celery, so this only # matters in always eager mode :) @@ -174,12 +181,7 @@ class MediaGoblinApp(object): # -------------- # Is a context provided? - if ctx is not None: - # Do special things if this is a request - if isinstance(ctx, Request): - ctx = self._request_only_gen_context(ctx) - - else: + if ctx is None: ctx = Context() # Attach utilities @@ -192,6 +194,11 @@ class MediaGoblinApp(object): ctx.db = self.db ctx.staticdirect = self.staticdirector + # Do special things if this is a request + # -------------------------------------- + if isinstance(ctx, Request): + ctx = self._request_only_gen_context(ctx) + return ctx def _request_only_gen_context(self, request): diff --git a/mediagoblin/db/base.py b/mediagoblin/db/base.py index e254e810..e594bd95 100644 --- a/mediagoblin/db/base.py +++ b/mediagoblin/db/base.py @@ -19,8 +19,10 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session, sessionmaker, object_session from sqlalchemy import inspect -Session = scoped_session(sessionmaker()) +from mediagoblin.tools.transition import DISABLE_GLOBALS +if not DISABLE_GLOBALS: + Session = scoped_session(sessionmaker()) class GMGTableBase(object): @@ -28,7 +30,8 @@ class GMGTableBase(object): def _session(self): return inspect(self).session - query = Session.query_property() + if not DISABLE_GLOBALS: + query = Session.query_property() def get(self, key): return getattr(self, key) @@ -38,9 +41,10 @@ class GMGTableBase(object): return getattr(self, key) def save(self, commit=True): - sess = object_session(self) - if sess is None: + sess = self._session + if sess is None and not DISABLE_GLOBALS: sess = Session() + assert sess is not None, "Can't save, %r has a detached session" % self sess.add(self) if commit: sess.commit() @@ -49,7 +53,7 @@ class GMGTableBase(object): def delete(self, commit=True): """Delete the object and commit the change immediately by default""" - sess = object_session(self) + sess = self._session assert sess is not None, "Not going to delete detached %r" % self sess.delete(self) if commit: diff --git a/mediagoblin/tools/transition.py b/mediagoblin/tools/transition.py new file mode 100644 index 00000000..a8041b89 --- /dev/null +++ b/mediagoblin/tools/transition.py @@ -0,0 +1,21 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import os + +# one global to disable them all +DISABLE_GLOBALS = os.environ.get("DISABLE_GLOBALS", "false").lower() == "true" -- 2.25.1