60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
class Appoxy:
|
|
|
|
"""
|
|
Singleton app proxy object. Used in Ooze submodules to avoid the need of
|
|
passing the app object into functions whose overrides in projects would
|
|
have automatic access to the app object through the parent scope anyhow.
|
|
|
|
To further illustrate, take this example:
|
|
|
|
# foo.py
|
|
import ooze
|
|
|
|
app = ooze.Application()
|
|
|
|
class Foo(ooze.rendering.Renderable):
|
|
|
|
def render(self, app, format='html', mode='full'):
|
|
return app.renderer.render_template('non-default.template', content=self)
|
|
|
|
The function signature of render including 'app' has been used in the past,
|
|
but as projects instantiate the app object and thus have easy access to it,
|
|
this only serves to add boilerplate.
|
|
|
|
Thus this singleton was created so it can be imported without dependencies
|
|
by submodules within Ooze, later have its .app set and proxy attribute
|
|
access during runtime.
|
|
"""
|
|
|
|
app = None
|
|
_cached = None
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if not cls._cached:
|
|
|
|
obj = super().__new__(cls)
|
|
cls._cached = obj
|
|
|
|
return cls._cached
|
|
|
|
def set_app(self, app):
|
|
super().__setattr__('app', app)
|
|
|
|
def __getattribute__(self, name):
|
|
|
|
if name in ['app', 'set_app'] or name.startswith('__'):
|
|
# non-magic access for:
|
|
# * app: needed to proxy through to its attributes
|
|
# * set_app: needed to set the app object after instantiation
|
|
# * dunder (__) things needed for pythons data model
|
|
|
|
return super().__getattribute__(name)
|
|
|
|
return getattr(self.app, name)
|
|
|
|
def __setattr__(self, name, value):
|
|
setattr(self.app, name, value)
|
|
|
|
app = Appoxy()
|