Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def http(
self, path, args=[], params={}, anchor=None,
sign=None, scheme=None, host=None, language=None
):
if not isinstance(args, (list, tuple)):
args = [args]
# allow user to use url('static', 'file')
if path == 'static':
path = '/static'
# routes urls with 'dot' notation
if '/' not in path:
# urls like 'function' refers to same module
if '.' not in path:
namespace = current.app.config.url_default_namespace or \
current.app.name
path = namespace + "." + path
# urls like '.function' refers to main app module
elif path.startswith('.'):
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) '
'without current request'
)
module = current.request.name.rsplit('.', 1)[0]
path = module + path
# find correct route
try:
url_components = \
current.app._router_http.routes_out[path]['path']
url_host = current.app._router_http.routes_out[path]['host']
builder = HttpUrlBuilder(url_components)
current.app.name
path = namespace + "." + path
# urls like '.function' refers to main app module
elif path.startswith('.'):
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) '
'without current request'
)
module = current.request.name.rsplit('.', 1)[0]
path = module + path
# find correct route
try:
url_components = \
current.app._router_ws.routes_out[path]['path']
url_host = current.app._router_ws.routes_out[path]['host']
builder = UrlBuilder(url_components)
# try to use the correct hostname
if url_host is not None:
# TODO: remap host
try:
if current.request.host != url_host:
scheme = \
self.http_to_ws_schemes[current.request.scheme]
host = url_host
except Exception:
pass
except KeyError:
raise RuntimeError(f'invalid url("{path}",...)')
# handle classic urls
else:
builder = UrlBuilder([path])
def ws(
self, path, args=[], params={}, scheme=None, host=None, language=None
):
if not isinstance(args, (list, tuple)):
args = [args]
# routes urls with 'dot' notation
if '/' not in path:
# urls like 'function' refers to same module
if '.' not in path:
namespace = current.app.config.url_default_namespace or \
current.app.name
path = namespace + "." + path
# urls like '.function' refers to main app module
elif path.startswith('.'):
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) '
'without current request'
)
module = current.request.name.rsplit('.', 1)[0]
path = module + path
# find correct route
try:
url_components = \
current.app._router_ws.routes_out[path]['path']
url_host = current.app._router_ws.routes_out[path]['host']
namespace = current.app.config.url_default_namespace or \
current.app.name
path = namespace + "." + path
# urls like '.function' refers to main app module
elif path.startswith('.'):
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) '
'without current request'
)
module = current.request.name.rsplit('.', 1)[0]
path = module + path
# find correct route
try:
url_components = \
current.app._router_http.routes_out[path]['path']
url_host = current.app._router_http.routes_out[path]['host']
builder = HttpUrlBuilder(url_components)
# try to use the correct hostname
if url_host is not None:
try:
if current.request.host != url_host:
scheme = current.request.scheme
host = url_host
except Exception:
pass
except KeyError:
raise RuntimeError(f'invalid url("{path}",...)')
# handle classic urls
else:
builder = HttpUrlBuilder([path])
# add language
self._logger = None
self.logger_name = self.import_name
#: init extensions
self.ext = sdict()
self._extensions_env = sdict()
self._extensions_listeners = {key: [] for key in Extension._signals_}
#: init templater
self.templater = Templater(
path=self.template_path,
encoding=self.config.templates_encoding,
escape=self.config.templates_escape,
prettify=self.config.templates_prettify,
reload=self.config.templates_auto_reload
)
#: store app in current
current.app = self
def add_prefix(self, args):
if current.app._router_http._prefix_main:
self.components.insert(0, '{}')
args.insert(0, current.app._router_http._prefix_main)
# try to use the correct hostname
if url_host is not None:
try:
if current.request.host != url_host:
scheme = current.request.scheme
host = url_host
except Exception:
pass
except KeyError:
raise RuntimeError(f'invalid url("{path}",...)')
# handle classic urls
else:
builder = HttpUrlBuilder([path])
# add language
lang = None
if current.app.language_force_on_url:
if language:
#: use the given language if is enabled in application
if language in current.app.languages:
lang = language
else:
#: try to use the request language if context exists
if hasattr(current, 'request'):
lang = current.request.language
if lang == current.app.language_default:
lang = None
# # add extension (useless??)
# if extension:
# url = url + '.' + extension
# scheme=True means to use current scheme
if scheme is True:
if not hasattr(current, 'request'):
raise RuntimeError(f'invalid url("{path}",...)')
# handle classic urls
else:
builder = HttpUrlBuilder([path])
# add language
lang = None
if current.app.language_force_on_url:
if language:
#: use the given language if is enabled in application
if language in current.app.languages:
lang = language
else:
#: try to use the request language if context exists
if hasattr(current, 'request'):
lang = current.request.language
if lang == current.app.language_default:
lang = None
# # add extension (useless??)
# if extension:
# url = url + '.' + extension
# scheme=True means to use current scheme
if scheme is True:
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) without current request'
)
scheme = current.request.scheme
# add scheme and host
if scheme:
if host is None:
if not hasattr(current, 'request'):
raise RuntimeError(
scheme = \
self.http_to_ws_schemes[current.request.scheme]
host = url_host
except Exception:
pass
except KeyError:
raise RuntimeError(f'invalid url("{path}",...)')
# handle classic urls
else:
builder = UrlBuilder([path])
# add language
lang = None
if current.app.language_force_on_url:
if language:
#: use the given language if is enabled in application
if language in current.app.languages:
lang = language
else:
#: try to use the request language if context exists
if hasattr(current, 'request'):
lang = current.request.language
if lang == current.app.language_default:
lang = None
# scheme=True means to use current scheme
if scheme is True:
if not hasattr(current, 'request'):
raise RuntimeError(
f'cannot build url("{path}",...) without current request'
)
scheme = self.http_to_ws_schemes[current.request.scheme]
# add scheme and host
if scheme:
def process(self, ctx, value):
for helper in self.helpers:
ctx.html(helper.format(current.app._router_http._prefix_main))