views:

111

answers:

1

Hi there,

How can I make my static-file root directories relative to my application root folder (instead of a hard-coded path)?

In accordance with CP instructions (http://www.cherrypy.org/wiki/StaticContent) I have tried the following in my configuration file:

tree.cpapp = cherrypy.Application(cpapp.Root())
tools.staticdir.root = cpapp.current_dir

but when I run cherrpy.quickstart(rootclass, script_name='/', config=config_file) I get the following error

builtins.ValueError: ("Config error in section: 'global', option: 'tree.cpapp', value: 'cherrypy.Application(cpapp.Root())'. Config values must be valid Python.", 'TypeError', ("unrepr could not resolve the name 'cpapp'",))

I know I can do configuration from within the main.py file just before quickstart is called (eg. using os.path.abspath(os.path.dirname(file))), but I prefer using the idea of a separate configuration file if possible.

Any help would be appreciated (in case it is relevant, I am using CP 3.2 with Python 3.1)

TIA Alan

+1  A: 

When you refer to a module inside configuration entries, CherryPy first looks for that module in sys.modules. So one solution would be to import cpapp just before you call quickstart.

But if that lookup in sys.modules fails, CherryPy tries to __import__ the module. Since that is also failing, you might need to investigate whether your cpapp.py module is indeed importable at all.

See the lib/reprconf.py module for all the gory details.

fumanchu
Hi fumanchu - thanks for the reply. As far as I can see there is no cpapp.py module to import, so I can't try that option. I think on reflection that the best solution may be to have a separate configuration file for each application (which I guess was the intended designed use), hard-code tools.staticdir.root, then set all the other sub-folders (css, js, etc.) relative to the root.
Alan Harris-Reid
cpapp.py is wherever you have your Root class declared. if your Root class is found in myapp/main.py, for example, then your config needs to say "tree.cpapp = cherrypy.Application(myapp.main.Root())"
fumanchu