Done. I'm using os.path.walk to find and import view files in my
application.py:
def find_and_import(arg, dirname, fnames):
for file_name in fnames:
if file_name == arg + '.py':
module_name = os.path.join(dirname, arg)[2:].replace('/',
'.')
__import__(module_name)
break
os.path.walk('.', find_and_import, 'views')
Ty for your patience :)
On 8 dez, 21:08, Khaoz <[EMAIL PROTECTED]> wrote:
> the problem was due to the fact that i need to import the views.
>
> Still trying to decouple the url_map using decorators or something
> like django does: one urls files per app mapped in a central place
> throug include()
>
> On 6 dez, 01:53, Khaoz <[EMAIL PROTECTED]> wrote:
>
> > So, trying to use some convention over configuration i have changed my
> > code where my end points will be:
>
> > module/action
> > def action():
>
> > or
>
> > module1/module2/action
> > def action():
>
> > and in my utils.expose i will try to add to my views {}:
>
> > views{'modules1/action': 'myapp.module1.views.action'}
>
> > or
>
> > views{'modules1/module2/action': 'myapp.module1.module2.views.action'}
>
> > but i cant get out of 404 erro. The code:
>
> > ************
> > * utils.py
> > ************
>
> > from os import path
> > from urlparse import urlparse
>
> > from sqlalchemy import MetaData
> > from sqlalchemy.orm import create_session, scoped_session
> > from werkzeug import Local, LocalManager, Response
> > from werkzeug.routing import Map, Rule
> > from mako.lookup import TemplateLookup
>
> > ALLOWED_SCHEMES = frozenset(['http', 'https', 'ftp', 'ftps'])
> > TEMPLATE_PATH = path.join(path.dirname(__file__), 'templates')
> > mako_lookup = TemplateLookup(directories=[TEMPLATE_PATH],
> > input_encoding='utf-8')
>
> > local = Local()
> > local_manager = LocalManager([local])
> > application = local('application')
>
> > metadata = MetaData()
> > session = scoped_session(lambda: create_session
> > (application.database_engine,
> > autocommit=True),
> > local_manager.get_ident)
>
> > url_map = Map()
> > views = {}
> > def expose(url_rule, url_endpoint, **kwargs):
> > def decorate(f):
> > url_map.add(Rule(url_rule, endpoint=url_endpoint, **kwargs))
>
> > endpoint_parts = url_endpoint.split('/')
> > endpoint_length = len(endpoint_parts)
> > module_string = ''
> > # ignore the last item (method name) wich will be added later
> > using f.__name__
> > for idx in range(endpoint_length - 1):
> > module_string = ''.join([module_string, endpoint_parts
> > [idx], '.'])
>
> > views[url_endpoint] = 'myapp.%s%s%s' \
> > % (module_string, 'views.', f.__name__)
>
> > return f
> > return decorate
>
> > def url_for(endpoint, _external=False, **values):
> > return local.url_adapter.build(endpoint, values,
> > force_external=_external)
>
> > def render_template(template, **context):
> > return Response(mako_lookup.get_template(template).render
> > (**context),
> > mimetype='text/html')
>
> > def validate_url(url):
> > return urlparse(url)[0] in ALLOWED_SCHEMES
>
> > *****************
> > * application.py
> > *****************
>
> > from sqlalchemy import create_engine
> > from werkzeug import Request, import_string, ClosingIterator
> > from werkzeug.exceptions import HTTPException
>
> > from myapp.utils \
> > import session, views, metadata, local, local_manager, url_map
> > import myapp.models
>
> > class Database(object):
>
> > def __init__(self, db_uri):
> > local.application = self
> > self.database_engine = create_engine(db_uri, encoding='utf-8')
>
> > def init_database(self):
> > metadata.create_all(self.database_engine)
>
> > def __call__(self, environ, start_response):
> > local.application = self
> > request = Request(environ)
> > local.url_adapter = adapter = url_map.bind_to_environ(environ)
> > try:
> > endpoint, values = adapter.match()
> > view = import_string(views[endpoint]) # here, i'm trying
> > to import myapp.views.listar
> > response = view(request, **values)
> > except HTTPException, e:
> > response = e
>
> > return ClosingIterator(response(environ, start_response),
> > [session.remove,
> > local_manager.cleanup])
>
> > ***********
> > * views.py
> > ***********
>
> > from werkzeug.exceptions import NotFound
> > from myapp.utils import session, expose, render_template
> > from myapp.models import Contact
>
> > @expose('/', 'listar')
> > def listar(request):
> > contacts = session.query(Contact).all()
> > if not contacts:
> > raise NotFound()
> > return render_template('list.html', contacts=contacts)
>
> > On 5 dez, 11:52, Khaoz <[EMAIL PROTECTED]> wrote:
>
> > > My views['database/list'] receive the decorated function not the '/',
> > > look:
>
> > > url_map = Map()
> > > views = {}
> > > def expose(rule, endpoint, **kwargs):
> > > def decorate(f):
> > > epoint = endpoint
> > > views[epoint] = f
> > > url_map.add(Rule(rule, endpoint=epoint, **kwargs))
> > > return f
> > > return decorate
>
> > > epoin == endpoint == 'database/list'
>
> > > views[epoint] = f (wich is the decorated function to be called on
> > > application.py):
>
> > > view = views[endpoint]
> > > response = view(request, **values)
>
> > > On 5 dez, 08:54, "Rodrigo Moraes" <[EMAIL PROTECTED]> wrote:
>
> > > > hey,
> > > > apparently, you are not mapping 'database/list' to any valid module.
> > > > views['database/list'] = '/'?
>
> > > > -- rodrigo
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pocoo-libs" 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/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---