Ok, here's what I'm trying to do. I have a dir:
app/resources/*.py
Suppose a resource looks like this:
class Resource:
uri = "/api/resources/(\d+)"
def GET(self, id):
return "You're trying to query for Resource[%s]!" % id
Then, in bin/serve.py I have:
from __future__ import with_statement
import sys, os.path, types, glob, itertools, web
base = os.path.abspath(os.path.dirname(sys.argv[0]))
sys.path.append(os.path.join(base, os.path.pardir))
sys.path.append(os.path.join(base, os.path.pardir, 'lib'))
def load_resources():
routes = {}
base = os.path.abspath(os.path.dirname(sys.argv[0]))
resources_path = os.path.join(base, os.path.pardir, 'resources',
'*.py')
for resource_path in glob.glob(resources_path):
resource_file = os.path.split(resource_path)[1]
resource_name = os.path.splitext(resource_file)[0]
module = __import__('resources', fromlist=[resource_name])
for name in dir(getattr(module, resource_name)):
ref = getattr(getattr(module, resource_name), name)
if type(ref) == types.ClassType and ref.__dict__.get('uris'):
for uri in ref.uris:
routes[uri] = 'resources.%s.%s' % (resource_name, name)
routes = itertools.chain(*zip(routes.keys(), routes.values()))
print(list(routes)) # looks ok
return web.application(list(routes), globals())
app = load_resources()
if __name__ == '__main__':
app.run()
Then I get that cryptic error message. Any clues?
--Jonas Galvez
On Nov 23, 11:21 pm, Jonas Galvez <[EMAIL PROTECTED]> wrote:
> Ha! Thanks a lot.
>
> --Jonas Galvez
>
> On Nov 23, 10:58 pm, Khaoz <[EMAIL PROTECTED]> wrote:
>
> > yout urls need to be:
>
> > urls = ('/', 'resources.someresource.hellew')
>
> > or
>
> > urls = ('/', 'app.bin.resources.someresource.hellew')
>
> > On 23 nov, 22:21, Jonas Galvez <[EMAIL PROTECTED]> wrote:
>
> > > I have the following directory structure:
>
> > > app/
> > > bin/
> > > serve.py
> > > resources/
> > > __init__.py
> > > someresource.py
>
> > > Where, serve.py is:
>
> > > import os, sys, web
> > > base = os.path.abspath(os.path.dirname(sys.argv[0]))
> > > sys.path.append(os.path.join(base, os.path.pardir))
> > > urls = ('/', 'hellew')
> > > from resources.someresource import hellew
> > > app = web.application(urls, globals())
>
> > > and someresource.py is:
>
> > > class hellew:
> > > def GET(self):
> > > return "Hellew World!"
>
> > > and when I tri to accesshttp://localhost:8080/, I get:
>
> > > Traceback (most recent call last):
> > > File "c:\home\Dev\Python25\Lib\site-packages\web\application.py",
> > > line 180, in process
> > > return p(lambda: process(processors))
> > > File "c:\home\Dev\Python25\Lib\site-packages\web\application.py",
> > > line 501, in processor
> > > h()
> > > File "c:\home\Dev\Python25\Lib\site-packages\web\application.py",
> > > line 67, in reload_mapping
> > > self.mapping = getattr(mod, mapping_name)
> > > TypeError: getattr(): attribute name must be string
>
> > > Ouch. Am I doing anything wrong?
>
> > > --Jonas Galvez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---