diff --git a/kivy/app.py b/kivy/app.py index f31d8f2fb..1a296abd9 100644 --- a/kivy/app.py +++ b/kivy/app.py @@ -281,6 +281,7 @@ class App(EventDispatcher): _running_app = None def __init__(self, **kwargs): + App._running_app = self self._app_directory = None self._app_name = None self._app_settings = None @@ -537,7 +538,6 @@ class App(EventDispatcher): window.set_icon(icon) self._install_settings_keys(window) - App._running_app = self self.dispatch('on_start') runTouchApp() self.dispatch('on_stop') diff --git a/kivy/lang.py b/kivy/lang.py index fbde40844..10cdd2157 100644 --- a/kivy/lang.py +++ b/kivy/lang.py @@ -475,6 +475,51 @@ lang_key = re.compile('([a-zA-Z_]+)') lang_keyvalue = re.compile('([a-zA-Z_][a-zA-Z0-9_.]*\.[a-zA-Z0-9_.]+)') +class ProxyApp(object): + # proxy app object + # taken from http://code.activestate.com/recipes/496741-object-proxying/ + + __slots__ = ['_obj'] + + def __init__(self): + object.__init__(self) + object.__setattr__(self, '_obj', None) + + def _ensure_app(self): + app = object.__getattribute__(self, '_obj') + if app is None: + from kivy.app import App + app = App.get_running_app() + object.__setattr__(self, '_obj', app) + return app + + def __getattribute__(self, name): + object.__getattribute__(self, '_ensure_app')() + return getattr(object.__getattribute__(self, '_obj'), name) + + def __delattr__(self, name): + object.__getattribute__(self, '_ensure_app')() + delattr(object.__getattribute__(self, '_obj'), name) + + def __setattr__(self, name, value): + object.__getattribute__(self, '_ensure_app')() + setattr(object.__getattribute__(self, '_obj'), name, value) + + def __nonzero__(self): + object.__getattribute__(self, '_ensure_app')() + return bool(object.__getattribute__(self, '_obj')) + + def __str__(self): + object.__getattribute__(self, '_ensure_app')() + return str(object.__getattribute__(self, '_obj')) + + def __repr__(self): + object.__getattribute__(self, '_ensure_app')() + return repr(object.__getattribute__(self, '_obj')) + +global_idmap['app'] = ProxyApp() + + class ParserException(Exception): '''Exception raised when something wrong happened in a kv file. '''