librelist archives

« back to archive

Proposal for an extensions dict

Proposal for an extensions dict

From:
Dag Odenhall
Date:
2010-07-27 @ 04:14
A common pattern for extensions is to attach the instance of themselves
to the Flask instance. I propose a dict or similar on the app object,
called something like "extensions", where extensions can attach
themselves in a less "monkey patchy" way.

app.extensions['genshi'] = self

For backwards compatibility, extensions can for a while do:

if not hasattr(app, 'extensions'):
    app.extensions = {}

Potentially a module function could also be added such as this:

def flaskext(name):
    return current_app.extensions[name]

Preferably extensions should attach themselves by a lowercase version of
their extension name without the "Flask-" initial.

Re: [flask] Proposal for an extensions dict

From:
Ali Afshar
Date:
2010-07-27 @ 08:09
On 27 July 2010 05:14, Dag Odenhall <dag.odenhall@gmail.com> wrote:
> A common pattern for extensions is to attach the instance of themselves
> to the Flask instance. I propose a dict or similar on the app object,
> called something like "extensions", where extensions can attach
> themselves in a less "monkey patchy" way.
>
> app.extensions['genshi'] = self
>
> For backwards compatibility, extensions can for a while do:
>
> if not hasattr(app, 'extensions'):
>    app.extensions = {}
>
> Potentially a module function could also be added such as this:
>
> def flaskext(name):
>    return current_app.extensions[name]
>
> Preferably extensions should attach themselves by a lowercase version of
> their extension name without the "Flask-" initial.
>
>

In general, I think there should be a way for extensions to add
Flask-instance-variables without monkey-patching (so +1ish). I am not
sure we need to keep a copy of extension instances themselves. I am
also not sure that a simple dict like that is the best option. For
example, an extension may want to register more than one
Flask-instance-variable.

Re: [flask] Proposal for an extensions dict

From:
Dag Odenhall
Date:
2010-07-27 @ 08:29
tis 2010-07-27 klockan 09:09 +0100 skrev Ali Afshar:
> On 27 July 2010 05:14, Dag Odenhall <dag.odenhall@gmail.com> wrote:
> > A common pattern for extensions is to attach the instance of themselves
> > to the Flask instance. I propose a dict or similar on the app object,
> > called something like "extensions", where extensions can attach
> > themselves in a less "monkey patchy" way.
> >
> > app.extensions['genshi'] = self
> >
> > For backwards compatibility, extensions can for a while do:
> >
> > if not hasattr(app, 'extensions'):
> >    app.extensions = {}
> >
> > Potentially a module function could also be added such as this:
> >
> > def flaskext(name):
> >    return current_app.extensions[name]
> >
> > Preferably extensions should attach themselves by a lowercase version of
> > their extension name without the "Flask-" initial.
> >
> >
> 
> In general, I think there should be a way for extensions to add
> Flask-instance-variables without monkey-patching (so +1ish). I am not
> sure we need to keep a copy of extension instances themselves. I am
> also not sure that a simple dict like that is the best option. For
> example, an extension may want to register more than one
> Flask-instance-variable.

I think you're complicating things. Extension instance variables belong
in your extension and it's easy to access if you attach your extension
instance.

Like I showed at IRC:

app.extensions['creole'].attribute
app.extensions['creole']['item']

flaskext('creole').attribute
flaskext('creole')['item']

# And usually you'll have a creole object in your namespace anyway
creole.attribute
creole['item']

This way, it is predictable and consistent for extension users, and
extension developers are free to do whatever they like with their
extension instances and it's also clear from API docs what you can
access from an extension instance.

Re: [flask] Proposal for an extensions dict

From:
Dan Jacob
Date:
2010-07-27 @ 08:13
Something like:

app.extensions.set("genshi", template_dir="/foo")

"genshi" being the extension name, with **kwargs as the variables you
want to register.

Then:

app.extensions.get("genshi", "template_dir", [default])

Alternatively:

app.extensions.genshi.template_dir


On 27 July 2010 09:09, Ali Afshar <aafshar@gmail.com> wrote:
> On 27 July 2010 05:14, Dag Odenhall <dag.odenhall@gmail.com> wrote:
>> A common pattern for extensions is to attach the instance of themselves
>> to the Flask instance. I propose a dict or similar on the app object,
>> called something like "extensions", where extensions can attach
>> themselves in a less "monkey patchy" way.
>>
>> app.extensions['genshi'] = self
>>
>> For backwards compatibility, extensions can for a while do:
>>
>> if not hasattr(app, 'extensions'):
>>    app.extensions = {}
>>
>> Potentially a module function could also be added such as this:
>>
>> def flaskext(name):
>>    return current_app.extensions[name]
>>
>> Preferably extensions should attach themselves by a lowercase version of
>> their extension name without the "Flask-" initial.
>>
>>
>
> In general, I think there should be a way for extensions to add
> Flask-instance-variables without monkey-patching (so +1ish). I am not
> sure we need to keep a copy of extension instances themselves. I am
> also not sure that a simple dict like that is the best option. For
> example, an extension may want to register more than one
> Flask-instance-variable.
>

Re: [flask] Proposal for an extensions dict

From:
Dag Odenhall
Date:
2010-07-27 @ 08:30
tis 2010-07-27 klockan 09:13 +0100 skrev Dan Jacob:
> Something like:
> 
> app.extensions.set("genshi", template_dir="/foo")
> 
> "genshi" being the extension name, with **kwargs as the variables you
> want to register.
> 
> Then:
> 
> app.extensions.get("genshi", "template_dir", [default])
> 
> Alternatively:
> 
> app.extensions.genshi.template_dir

Way more complicated than need be I think, see my reply to Ali.