ptpb / pb

pb is a formerly-lightweight pastebin and url shortener
Other
549 stars 52 forks source link

Curious configuration file #123

Closed pypingou closed 9 years ago

pypingou commented 9 years ago

Hi,

I was just curious if there is any special reason you went with a yaml configuration file instead of using the approach flasks support: http://flask.pocoo.org/docs/0.10/config/#configuring-from-files

Thanks

buhman commented 9 years ago

instead of using the approach flasks support

pb uses app.config.from_mapping, which is new in 1.0--the main point of from_mapping is to help use-cases just like this. So I'm not sure what you mean by "support".

any special reason

Yes. pyfile syntax doesn't support nested objects. A good example of how pb exploited this was back when we used MySQL, the configuration looked like this.

Notice that the dictionary keys within MYSQL exactly match they keyword arguments used by mysql.connector.connect, which results in fun sugar like this.

The other cool thing about that is you're able to pass arbitrary keyword arguments (other than the required/mandatory/documented arguments) to connect() if you need to without pb having explicit logic for said arguments.

This is still used with Mongo as well.

buhman commented 9 years ago

The alternative is less pretty; something like:

FOO_SPAM=heh
FOO_EGGS=wut
foo(spam=config['FOO_SPAM'], eggs=config['FOO_EGGS'])

yaml

Standardized, easy to read/write, and extremely flexible.

pypingou commented 9 years ago

the pyfile syntax allows you to use plain python dictionary, which will end up to be the same thing as your yaml structure.

It also gives you the benefit of actually embedding python code in your configuration file (for example see how to configure a session time-out, which directly calls datetime.timedelta: http://flask.pocoo.org/docs/0.10/api/#flask.session.permanent)

Anyway, I was merely curious :)

buhman commented 9 years ago

actually embedding python code in your configuration file

I consider this to be a solid anti-feature. I'd completely forgotten that from_pyfile actually does exec().