示例#1
0
文件: web.py 项目: buhtigexa/Nerit
def normalize_result(result, headers):
    if hasattr(result, 'read'): content = result  # file-like object
    else: content = tostring(result)
    headers = dict([ (name.replace('_', '-').title(), str(value))
                     for name, value in headers.items() ])
    media_type = headers.pop('Type', None)
    charset = headers.pop('Charset', None)
    content_type = headers.get('Content-Type')
    if content_type:
        media_type, type_params = cgi.parse_header(content_type)
        charset = type_params.get('charset', 'iso-8859-1')
    else:
        if media_type is None: media_type = getattr(result, 'media_type', None)
        if media_type is None:
            if isinstance(content, (Html, StrHtml)): media_type = 'text/html'
            else: media_type = 'text/plain'
        if charset is None: charset = getattr(result, 'charset', 'UTF-8')
        content_type = '%s; charset=%s' % (media_type, charset)
        headers['Content-Type'] = content_type
    if hasattr(content, 'read') \
       or media_type != 'text/html' \
       or isinstance(content, (Html, StrHtml)): pass
    elif isinstance(content, unicode): content = Html(content)
    elif isinstance(content, str): content = StrHtml(content)
    else: assert False  # pragma: no cover
    return content, headers
示例#2
0
文件: web.py 项目: buhtigexa/Nerit
def normalize_result(result, headers):
    if hasattr(result, 'read'): content = result  # file-like object
    else: content = tostring(result)
    headers = dict([(name.replace('_', '-').title(), str(value))
                    for name, value in headers.items()])
    media_type = headers.pop('Type', None)
    charset = headers.pop('Charset', None)
    content_type = headers.get('Content-Type')
    if content_type:
        media_type, type_params = cgi.parse_header(content_type)
        charset = type_params.get('charset', 'iso-8859-1')
    else:
        if media_type is None: media_type = getattr(result, 'media_type', None)
        if media_type is None:
            if isinstance(content, (Html, StrHtml)): media_type = 'text/html'
            else: media_type = 'text/plain'
        if charset is None: charset = getattr(result, 'charset', 'UTF-8')
        content_type = '%s; charset=%s' % (media_type, charset)
        headers['Content-Type'] = content_type
    if hasattr(content, 'read') \
       or media_type != 'text/html' \
       or isinstance(content, (Html, StrHtml)):
        pass
    elif isinstance(content, unicode):
        content = Html(content)
    elif isinstance(content, str):
        content = StrHtml(content)
    else:
        assert False  # pragma: no cover
    return content, headers
示例#3
0
def link(*args, **kwargs):
    if not args:
        raise TypeError(
            'link() function requires at least one positional argument')
    attrs = None

    last = args[-1]
    if isinstance(last, BoundMarkup):
        description = last()
        args = (description, ) + args[:-1]

    first = args[0]
    if hasattr(first, 'routes'):
        func = first
        args = args[1:]
        if func.__doc__ is None: description = func.__name__
        else: description = Html(func.__doc__.split('\n', 1)[0])
    elif len(args) > 1 and hasattr(args[1], 'routes'):
        description = tostring(first)
        func = args[1]
        args = args[2:]
    elif len(args) > 2 and hasattr(args[2], 'routes'):
        attrs = args[1]
        if isinstance(attrs, basestring): attrs = {'class': attrs}
        elif not hasattr(attrs, 'items'):
            raise TypeError('Invalid second argument of link() function: %r' %
                            second)
        description = tostring(first)
        func = args[2]
        args = args[3:]
    elif isinstance(first, basestring):
        func = link_funcs.get(first)
        if func is not None: return func(*args[1:], **kwargs)
        if first.endswith('.css'):
            if kwargs: raise TypeError('Unexpected key arguments')
            return css_link(args)
        if first.endswith('.js'):
            if len(args) > 1:
                raise TypeError('Unexpected positional arguments')
            if kwargs: raise TypeError('Unexpected key arguments')
            return script_link(first)
        raise TypeError('Invalid arguments of link() function')

    href = url(func, *args, **kwargs)
    return htmljoin(
        [htmltag('a', attrs, href=href), description,
         Html('</a>')])
示例#4
0
def link(*args, **kwargs):
    if not args: raise TypeError('link() function requires at least one positional argument')
    attrs = None

    last = args[-1]
    if isinstance(last, BoundMarkup):
        description = last()
        args = (description,) + args[:-1]

    first = args[0]
    if hasattr(first, 'routes'):
        func = first
        args = args[1:]
        if func.__doc__ is None: description = func.__name__
        else: description = Html(func.__doc__.split('\n', 1)[0])
    elif len(args) > 1 and hasattr(args[1], 'routes'):
        description = tostring(first)
        func = args[1]
        args = args[2:]
    elif len(args) > 2 and hasattr(args[2], 'routes'):
        attrs = args[1]
        if isinstance(attrs, basestring): attrs = {'class': attrs}
        elif not hasattr(attrs, 'items'):
            raise TypeError('Invalid second argument of link() function: %r' % second)
        description = tostring(first)
        func = args[2]
        args = args[3:]
    elif isinstance(first, basestring):
        func = link_funcs.get(first)
        if func is not None: return func(*args[1:], **kwargs)
        if first.endswith('.css'):
            if kwargs: raise TypeError('Unexpected key arguments')
            return css_link(args)
        if first.endswith('.js'):
            if len(args) > 1: raise TypeError('Unexpected positional arguments')
            if kwargs: raise TypeError('Unexpected key arguments')
            return script_link(first)
        raise TypeError('Invalid arguments of link() function')

    href = url(func, *args, **kwargs)
    return htmljoin([htmltag('a', attrs, href=href), description, Html('</a>')])
def restore_escapes(s):
    s = tostring(s)
    if not options.DEBUGGING_RESTORE_ESCAPES: return s
    return utils.restore_escapes(s, 'utf-8').decode('utf-8')
示例#6
0
def restore_escapes(s):
    s = tostring(s)
    if not options.DEBUGGING_RESTORE_ESCAPES: return s
    return utils.restore_escapes(s, 'utf-8').decode('utf-8')