def create_app(options): subpath = options['subpath'] app = tornado.web.Application( [ urlspec(path('', subpath), RootHandler, dict(options=options), name='home'), urlspec(path('page', subpath), PageHandler, dict(options=options), name='page'), urlspec(path('search', subpath), SearchHandler, dict(options=options), name='search'), urlspec(path('tree', subpath), TreeHandler, dict(options=options), name='tree'), # (r"/options", OptionsHandler, dict(options=options)), ], template_path=os.path.join(base_dir, 'templates'), static_path=os.path.join(base_dir, 'static'), xheaders=True) return app
def include(pattern, handlers, prefix_name=None): try: if prefix_name: new_name = '%s-%s' % (prefix_name, '%s') else: new_name = '%s' warnings.warn( "you should give a 'prefix_name' for include urls,to avoid naming conflicts" ) if handlers and isinstance(handlers, str): handlers = import_object(handlers) else: handlers = handlers urlspecs = [] if not pattern.endswith('/'): pattern += '/' if handlers and isinstance(handlers, (tuple, list)): for handler in handlers: if handler and isinstance(handler, urlspec): patt = pattern + handler.repr_pattern \ .lstrip('^').lstrip('//').lstrip('/') urlspecs.append( urlspec( patt, handler.handler_class, kwargs=handler.kwargs, name=new_name % handler.name if handler.name else handler.name)) return urlspecs else: raise UrlError('url error,it is must be an tuple or list') except Exception: raise
def urlhelper(self, *urllist): """ 路由列表list """ urls = [] for u in urllist: handler_path = '.'.join([self.path_prefix, u.get('handler_path')]) pattern = u.get('pattern') if pattern.endswith('/'): pattern += '?' else: pattern += '/?' path = u.get('path', None) if path: if path != '/': pattern = path + pattern else: pattern = self.path + pattern kw = dict(u.get('kwargs', {})) kw['app_name'] = self.app_name url_name = self.app_name + '-' + u.get('name') urls.append( urlspec(pattern, import_object(handler_path), kwargs=kw, name=url_name)) return urls
def urlhelper(self, prefix='', *urllist): """ 路由列表list """ urls = [] for u in urllist: handler = u.get('handler_module', '') if isinstance(handler, basestring): handler_module = '.'.join([prefix, u.get('handler_module', '')]) handler_module = import_object(handler_module) else: handler_module = handler pattern = u.get('pattern') pattern += '?' if pattern.endswith('/') else '/?' path = u.get('path', None) if path: if path != '/': pattern = path + pattern else: pattern = self.path + pattern kw = dict(u.get('kwargs', {})) kw['subapp_name'] = self.subapp_name url_name = ''.join([self.subapp_name, '-', u.get('name')]) url = urlspec(pattern, handler_module, kwargs=kw, name=url_name) urls.append(url) return urls
def include(pattern, handlers, prefix_name=None): try: if prefix_name: new_name = '%s-%s' % (prefix_name, '%s') else: new_name = '%s' warnings.warn("you should give a 'prefix_name' for include urls,to avoid naming conflicts") if handlers and isinstance(handlers, str): handlers = import_object(handlers) else: handlers = handlers urlspecs = [] if not pattern.endswith('/'): pattern += '/' if handlers and isinstance(handlers, (tuple, list)): for handler in handlers: if handler and isinstance(handler, urlspec): patt = pattern + handler.repr_pattern \ .lstrip('^').lstrip('//').lstrip('/') urlspecs.append(urlspec(patt, handler.handler_class, kwargs=handler.kwargs, name=new_name % handler.name if handler.name else handler.name)) return urlspecs else: raise UrlError('url error,it is must be an tuple or list') except Exception: raise
def urlhelper(self, prefix='', *urllist): """ 路由列表list """ urls = [] for u in urllist: handler = u.get('handler', '') if isinstance(handler, basestring): handler_module = '.'.join([prefix, u.get('handler', '')]) handler_module = import_object(handler_module) else: handler_module = handler pattern = u.get('pattern') # pattern += '?' if pattern.endswith('/') else '/?' path = u.get('path', None) if path: if path != '/': pattern = path + pattern else: pattern = self.path + pattern kw = dict(u.get('kwargs', {})) # url_name = ''.join([self.subapp_name, '-', u.get('name')]) url = urlspec(pattern, handler_module, kwargs=kw, name=u.get('name')) urls.append(url) return urls
def include(pattern, handlers, prefix_name=None): """ 当你有多组url分布在不同的文件,或者你有多组url前缀不同,include可以帮你组织路由 eg: url1.py: url=Url('handler_abc') URLS = route( url(r'/add/','add.AddHandler'), ... ) url2.py: url=Url() URLS = route( url(r'/list/','handler_efg.list.ListHandler'), ..., include('/admin','url1.URLS') ) 最终的url可能类似于这样: url = [ r'/list/','handler_efg.list.ListHandler', ..., r'/admin/add/' , 'handler_abc.add.AddHandler', ... ] app = Application(url) """ try: if prefix_name: new_name = '%s-%s' % (prefix_name, '%s') else: new_name = '%s' if handlers and isinstance(handlers, str): handlers = import_object(handlers) else: handlers = handlers urlspecs = [] if not pattern.endswith('/'): pattern += '/' if handlers and isinstance(handlers, (tuple, list)): for handler in handlers: if handler and isinstance(handler, urlspec): patt = pattern + handler.repr_pattern \ .lstrip('^').lstrip('//').lstrip('/') urlsp = urlspec( patt, handler.handler_class, kwargs=handler.kwargs, name=new_name % handler.name if handler.name else handler.name) urlsp.repr_pattern = patt urlspecs.append(urlsp) return urlspecs else: raise UrlError('url error,it is must be an tuple or list') except Exception: raise
def include(pattern, handlers, prefix_name=None): """ 当你有多组url分布在不同的文件,或者你有多组url前缀不同,include可以帮你组织路由 eg: url1.py: url=Url('handler_abc') URLS = route( url(r'/add/','add.AddHandler'), ... ) url2.py: url=Url() URLS = route( url(r'/list/','handler_efg.list.ListHandler'), ..., include('/admin','url1.URLS') ) 最终的url可能类似于这样: url = [ r'/list/','handler_efg.list.ListHandler', ..., r'/admin/add/' , 'handler_abc.add.AddHandler', ... ] app = Application(url) """ try: if prefix_name: new_name = '%s-%s' % (prefix_name, '%s') else: new_name = '%s' if handlers and isinstance(handlers, str): handlers = import_object(handlers) else: handlers = handlers urlspecs = [] if not pattern.endswith('/'): pattern += '/' if handlers and isinstance(handlers, (tuple, list)): for handler in handlers: if handler and isinstance(handler, urlspec): patt = pattern + handler.repr_pattern \ .lstrip('^').lstrip('//').lstrip('/') urlsp = urlspec(patt, handler.handler_class, kwargs=handler.kwargs, name=new_name % handler.name if handler.name else handler.name) urlsp.repr_pattern = patt urlspecs.append(urlsp) return urlspecs else: raise UrlError('url error,it is must be an tuple or list') except Exception: raise
def __call__(self, pattern='', handler='', kwargs=None, name=None): kw = {} if self.overall_kw: kw.update(self.overall_kw) if kwargs: kw.update(kwargs) if self.prefix and isinstance(handler, str): handler = "%s.%s" % (self.prefix, handler) urlsp = urlspec(pattern, handler, kwargs=kw, name=name) urlsp.repr_pattern = pattern return urlsp
def __call__(self, pattern, handler, kwargs=None, name=None): kw = {} if self.overall_kw: kw.update(self.overall_kw) if kwargs: kw.update(kwargs) if self.prefix and isinstance(handler, str): handler = "%s.%s" % (self.prefix, handler) urlsp = urlspec(pattern, handler, kwargs=kw, name=name) urlsp.repr_pattern = pattern return urlsp
def urlhelper(self, *urllist): """ 路由列表list """ urls = [] for u in urllist: handler_path = '.'.join([self.path_prefix, u.get('handler_path')]) pattern = u.get('pattern') if pattern.endswith('/'): pattern += '?' else: pattern += '/?' path = u.get('path', None) if path: if path != '/': pattern = path + pattern else: pattern = self.path + pattern kw = dict(u.get('kwargs', {})) kw['app_name'] = self.app_name url_name = self.app_name + '-' + u.get('name') urls.append(urlspec(pattern, import_object(handler_path), kwargs=kw, name=url_name)) return urls