skariel / webalchemy

Modern web development with Python
MIT License
346 stars 21 forks source link

Conf enhancements #131

Closed aschlapsi closed 10 years ago

aschlapsi commented 10 years ago

Created a simple configuration system.

skariel commented 10 years ago

hi,

I really like the new configuration functionality but I'm having two (small) issues.

The first - There's a problem running three_d_earth example outside of my IDE. Importing

from examples.three_d_earth.three_d_earth import ThreeDEarth gives the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-9c4ee808163d> in <module>()
----> 1 from examples.three_d_earth.three_d_earth import ThreeDEarth

c:\users\jkeselma\Desktop\examples\three_d_earth\three_d_earth.py in <module>()
    114 
    115 
--> 116 class ThreeDEarth:
    117 
    118     include = ['https://rawgithub.com/mrdoob/three.js/master/build/three.min.js']

c:\users\jkeselma\Desktop\examples\three_d_earth\three_d_earth.py in ThreeDEarth(__locals__)
    117 
    118     include = ['https://rawgithub.com/mrdoob/three.js/master/build/three.min.js']
--> 119     config = config.from_object(__name__)
    120 
    121     def initialize(self, **kwargs):

C:\Users\jkeselma\WP64\python-3.3.2.amd64\lib\site-packages\webalchemy\config.py in from_object(obj)
     24 def from_object(obj):
     25     if isinstance(obj, str):
---> 26         obj = _import_object(obj)
     27     cfg = Config()
     28     for key in dir(obj):

C:\Users\jkeselma\WP64\python-3.3.2.amd64\lib\site-packages\webalchemy\config.py in _import_object(objname)
     34     if '.' in objname:
     35         module, objname = objname.rsplit('.', 1)
---> 36         return getattr(__import__(module, None, None, [objname]), objname)
     37     else:
     38         return __import__(objname)

AttributeError: 'module' object has no attribute 'three_d_earth'

isn't some sort of recursive thing going on, I mean when I import three_d_earth the config in the app tries to dynamically import itself... I'm not sure whats going on, can you please take a look?

And second-

Sometimes it is handy to be able to override the app configuration, for e.g. in interactive sessions. Say I want to freeze the app to different names or serve the same app to different ports. How can I handle this now, should I edit the app source or some other config file? I would like to add something like this:

server.run(app, update_settings={'PORT':8081})

of course server.run would update the settings acccordingly.

what do you think?

Thanks!

EDIT:

other possible syntax:

server.run(app, update_settings=dict(PORT=8081))

or...

server.run(app, PORT=8081) using kwargs

skariel commented 10 years ago

I fixed both issues :)

Just used importlib.import_module(name) for the 1st issue; the 2nd issue was straight forward to implement

aschlapsi commented 10 years ago

Hi,

thank you for fixing the issue.

The methods from_object, from_pyfile, from_envvar, and from_dict return a Config object which has methods for updating itself from different sources.

This means that this code would update the settings accordingly:

class Settings:
    HOST = '127.0.0.1'
    PORT = 8080

class App:
    config = webalchemy.config.from_object(Settings)

...

class SpecialSettings:
    PORT = 8081

App.config.from_object(SpecialSettings)
server.run(app)
skariel commented 10 years ago

Somehow I missed that completely. It is actually very good and covers all use cases I can think of -- I've just reverted the changes to server.run, no need for a second (inferior) configuration route. Thanks for the contribution!