home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import re
- import urllib
- from routes import request_config
-
- class RoutesException(Exception):
- pass
-
-
- class MatchException(RoutesException):
- pass
-
-
- class GenerationException(RoutesException):
- pass
-
-
- def _screenargs(kargs, mapper, environ, force_explicit = False):
- encoding = mapper.encoding
- for key, val in kargs.iteritems():
- if isinstance(val, unicode):
- kargs[key] = val.encode(encoding)
- continue
-
- if mapper.explicit and mapper.sub_domains and not force_explicit:
- return _subdomain_check(kargs, mapper, environ)
- if mapper.explicit and not force_explicit:
- return kargs
- controller_name = kargs.get('controller')
- if controller_name and controller_name.startswith('/'):
- kargs['controller'] = kargs['controller'][1:]
- return kargs
- route_args = environ.get('wsgiorg.routing_args')
- if route_args:
- memory_kargs = route_args[1].copy()
- else:
- memory_kargs = { }
- for key in kargs.keys():
- if kargs[key] is None:
- continue
- _[1][key]
-
- memory_kargs.update(kargs)
- if mapper.sub_domains:
- memory_kargs = _subdomain_check(memory_kargs, mapper, environ)
-
- return memory_kargs
-
-
- def _subdomain_check(kargs, mapper, environ):
- if mapper.sub_domains:
- subdomain = kargs.pop('sub_domain', None)
- if isinstance(subdomain, unicode):
- subdomain = str(subdomain)
-
- if not environ.get('HTTP_HOST'):
- pass
- fullhost = environ.get('SERVER_NAME')
- if not fullhost:
- return kargs
- hostmatch = fullhost.split(':')
- host = hostmatch[0]
- port = ''
- if len(hostmatch) > 1:
- port += ':' + hostmatch[1]
-
- sub_match = re.compile('^.+?\\.(%s)$' % mapper.domain_match)
- domain = re.sub(sub_match, '\\1', host)
- if subdomain and not host.startswith(subdomain) and subdomain not in mapper.sub_domains_ignore:
- kargs['_host'] = subdomain + '.' + domain + port
- elif (subdomain in mapper.sub_domains_ignore or subdomain is None) and domain != host:
- kargs['_host'] = domain + port
-
- return kargs
- return kargs
-
-
- def _url_quote(string, encoding):
- if encoding:
- if isinstance(string, unicode):
- s = string.encode(encoding)
- elif isinstance(string, str):
- s = string
- else:
- s = unicode(string).encode(encoding)
- else:
- s = str(string)
- return urllib.quote(s, '/')
-
-
- def _str_encode(string, encoding):
- if encoding:
- if isinstance(string, unicode):
- s = string.encode(encoding)
- elif isinstance(string, str):
- s = string
- else:
- s = unicode(string).encode(encoding)
-
- return s
-
-
- def url_for(*args, **kargs):
- anchor = kargs.get('anchor')
- host = kargs.get('host')
- protocol = kargs.get('protocol')
- qualified = kargs.pop('qualified', None)
- for key in [
- 'anchor',
- 'host',
- 'protocol']:
- if kargs.get(key):
- del kargs[key]
- continue
-
- config = request_config()
- route = None
- static = False
- encoding = config.mapper.encoding
- url = ''
- if len(args) > 0:
- route = config.mapper._routenames.get(args[0])
- if not route:
- static = True
- url = args[0]
-
- if url.startswith('/') and hasattr(config, 'environ') and config.environ.get('SCRIPT_NAME'):
- url = config.environ.get('SCRIPT_NAME') + url
-
- if static:
- if kargs:
- url += '?'
- query_args = []
- for key, val in kargs.iteritems():
- if isinstance(val, (list, tuple)):
- for value in val:
- query_args.append('%s=%s' % (urllib.quote(unicode(key).encode(encoding)), urllib.quote(unicode(value).encode(encoding))))
-
- query_args.append('%s=%s' % (urllib.quote(unicode(key).encode(encoding)), urllib.quote(unicode(val).encode(encoding))))
-
- url += '&'.join(query_args)
-
-
-
- environ = getattr(config, 'environ', { })
- if 'wsgiorg.routing_args' not in environ:
- environ = environ.copy()
- mapper_dict = getattr(config, 'mapper_dict', None)
- if mapper_dict is not None:
- match_dict = mapper_dict.copy()
- else:
- match_dict = { }
- environ['wsgiorg.routing_args'] = ((), match_dict)
-
- if not static:
- route_args = []
- if route:
- if config.mapper.hardcode_names:
- route_args.append(route)
-
- newargs = route.defaults.copy()
- newargs.update(kargs)
- if route.filter:
- newargs = route.filter(newargs)
-
- if not route.static:
- newargs = _subdomain_check(newargs, config.mapper, environ)
-
- else:
- newargs = _screenargs(kargs, config.mapper, environ)
- if not newargs.pop('_anchor', None):
- pass
- anchor = anchor
- if not newargs.pop('_host', None):
- pass
- host = host
- if not newargs.pop('_protocol', None):
- pass
- protocol = protocol
- url = config.mapper.generate(*route_args, **newargs)
-
- if anchor is not None:
- url += '#' + _url_quote(anchor, encoding)
-
- if host and protocol or qualified:
- if not host and not qualified:
- host = config.host.split(':')[0]
- elif not host:
- host = config.host
-
- if not protocol:
- protocol = config.protocol
-
- if url is not None:
- url = protocol + '://' + host + url
-
-
- if not isinstance(url, str) and url is not None:
- raise GenerationException('url_for can only return a string, got unicode instead: %s' % url)
- url is not None
- if url is None:
- raise GenerationException('url_for could not generate URL. Called with args: %s %s' % (args, kargs))
- url is None
- return url
-
-
- class URLGenerator(object):
-
- def __init__(self, mapper, environ):
- self.mapper = mapper
- if 'SCRIPT_NAME' not in environ:
- environ['SCRIPT_NAME'] = ''
-
- self.environ = environ
-
-
- def __call__(self, *args, **kargs):
- anchor = kargs.get('anchor')
- host = kargs.get('host')
- protocol = kargs.get('protocol')
- qualified = kargs.pop('qualified', None)
- for key in [
- 'anchor',
- 'host',
- 'protocol']:
- if kargs.get(key):
- del kargs[key]
- continue
-
- route = None
- if '_use_current' in kargs:
- pass
- use_current = kargs.pop('_use_current')
- static = False
- encoding = self.mapper.encoding
- url = ''
- more_args = len(args) > 0
- if more_args:
- route = self.mapper._routenames.get(args[0])
-
- if not route and more_args:
- static = True
- url = args[0]
- if url.startswith('/') and self.environ.get('SCRIPT_NAME'):
- url = self.environ.get('SCRIPT_NAME') + url
-
- if static:
- if kargs:
- url += '?'
- query_args = []
- for key, val in kargs.iteritems():
- if isinstance(val, (list, tuple)):
- for value in val:
- query_args.append('%s=%s' % (urllib.quote(unicode(key).encode(encoding)), urllib.quote(unicode(value).encode(encoding))))
-
- query_args.append('%s=%s' % (urllib.quote(unicode(key).encode(encoding)), urllib.quote(unicode(val).encode(encoding))))
-
- url += '&'.join(query_args)
-
-
-
- if not static:
- route_args = []
- if route:
- if self.mapper.hardcode_names:
- route_args.append(route)
-
- newargs = route.defaults.copy()
- newargs.update(kargs)
- if route.filter:
- newargs = route.filter(newargs)
-
- if (not (route.static) or route.static) and not (route.external):
- sub = newargs.get('sub_domain', None)
- newargs = _subdomain_check(newargs, self.mapper, self.environ)
- if 'sub_domain' in route.defaults:
- newargs['sub_domain'] = sub
-
-
- elif use_current:
- newargs = _screenargs(kargs, self.mapper, self.environ, force_explicit = True)
- elif 'sub_domain' in kargs:
- newargs = _subdomain_check(kargs, self.mapper, self.environ)
- else:
- newargs = kargs
- if not anchor:
- pass
- anchor = newargs.pop('_anchor', None)
- if not host:
- pass
- host = newargs.pop('_host', None)
- if not protocol:
- pass
- protocol = newargs.pop('_protocol', None)
- url = self.mapper.generate(*route_args, **newargs)
-
- if anchor is not None:
- url += '#' + _url_quote(anchor, encoding)
-
- if host and protocol or qualified:
- if 'routes.cached_hostinfo' not in self.environ:
- cache_hostinfo(self.environ)
-
- hostinfo = self.environ['routes.cached_hostinfo']
- if not host and not qualified:
- host = hostinfo['host'].split(':')[0]
- elif not host:
- host = hostinfo['host']
-
- if not protocol:
- protocol = hostinfo['protocol']
-
- if url is not None:
- if host[-1] != '/':
- host += '/'
-
- url = protocol + '://' + host + url.lstrip('/')
-
-
- if not isinstance(url, str) and url is not None:
- raise GenerationException('Can only return a string, got unicode instead: %s' % url)
- url is not None
- if url is None:
- raise GenerationException('Could not generate URL. Called with args: %s %s' % (args, kargs))
- url is None
- return url
-
-
- def current(self, *args, **kwargs):
- return self(_use_current = True, *args, **kwargs)
-
-
-
- def redirect_to(*args, **kargs):
- target = url_for(*args, **kargs)
- config = request_config()
- return config.redirect(target)
-
-
- def cache_hostinfo(environ):
- hostinfo = { }
- if environ.get('HTTPS') and environ.get('wsgi.url_scheme') == 'https' or environ.get('HTTP_X_FORWARDED_PROTO') == 'https':
- hostinfo['protocol'] = 'https'
- else:
- hostinfo['protocol'] = 'http'
- if environ.get('HTTP_X_FORWARDED_HOST'):
- hostinfo['host'] = environ['HTTP_X_FORWARDED_HOST']
- elif environ.get('HTTP_HOST'):
- hostinfo['host'] = environ['HTTP_HOST']
- else:
- hostinfo['host'] = environ['SERVER_NAME']
- if environ.get('wsgi.url_scheme') == 'https':
- if environ['SERVER_PORT'] != '443':
- hostinfo['host'] += ':' + environ['SERVER_PORT']
-
- elif environ['SERVER_PORT'] != '80':
- hostinfo['host'] += ':' + environ['SERVER_PORT']
-
- environ['routes.cached_hostinfo'] = hostinfo
- return hostinfo
-
-
- def controller_scan(directory = None):
- if directory is None:
- return []
-
- def find_controllers(dirname, prefix = ('',)):
- controllers = []
- for fname in os.listdir(dirname):
- filename = os.path.join(dirname, fname)
- if os.path.isfile(filename) and re.match('^[^_]{1,1}.*\\.py$', fname):
- controllers.append(prefix + fname[:-3])
- continue
- if os.path.isdir(filename):
- controllers.extend(find_controllers(filename, prefix = prefix + fname + '/'))
- continue
-
- return controllers
-
-
- def longest_first(fst, lst):
- return cmp(len(lst), len(fst))
-
- controllers = find_controllers(directory)
- controllers.sort(longest_first)
- return controllers
-
-