Exemple #1
0
def html_quote(value, force=True):
    if not force and hasattr(value, '__html__'):
        return value.__html__()
    if value is None:
        return ''
    if not isinstance(value, basestring_):
        value = coerce_text(value)
    if sys.version >= "3" and isinstance(value, bytes):
        value = cgi.escape(value.decode('latin1'), 1)
        value = value.encode('latin1')
    else:
        value = cgi.escape(value, 1)
    if sys.version < "3":
        if is_unicode(value):
            value = value.encode('ascii', 'xmlcharrefreplace')
    return value
def html_quote(value, force=True):
    if not force and hasattr(value, '__html__'):
        return value.__html__()
    if value is None:
        return ''
    if not isinstance(value, basestring_):
        value = coerce_text(value)
    if sys.version >= "3" and isinstance(value, bytes):
        value = cgi.escape(value.decode('latin1'), 1)
        value = value.encode('latin1')
    else:
        value = cgi.escape(value, 1)
    if sys.version < "3":
        if is_unicode(value):
            value = value.encode('ascii', 'xmlcharrefreplace')
    return value
 def _eval(self, code, ns, pos):
     __traceback_hide__ = True
     try:
         try:
             value = eval(code, self.default_namespace, ns)
         except SyntaxError as e:
             raise SyntaxError(
                 'invalid syntax in expression: %s' % code)
         return value
     except:
         exc_info = sys.exc_info()
         e = exc_info[1]
         if getattr(e, 'args', None):
             arg0 = e.args[0]
         else:
             arg0 = coerce_text(e)
         e.args = (self._add_line_info(arg0, pos),)
         raise exc_info[0](e).with_traceback(exc_info[2])
 def _repr(self, value, pos):
     __traceback_hide__ = True
     try:
         if value is None:
             return ''
         if self._unicode:
             try:
                 value = str(value)
             except UnicodeDecodeError:
                 value = bytes(value)
         else:
             if not isinstance(value, basestring_):
                 value = coerce_text(value)
             if (is_unicode(value)
                 and self.default_encoding):
                 value = value.encode(self.default_encoding)
     except:
         exc_info = sys.exc_info()
         e = exc_info[1]
         e.args = (self._add_line_info(e.args[0], pos),)
         raise exc_info[0](e).with_traceback(exc_info[2])
     else:
         if self._unicode and isinstance(value, bytes):
             if not self.default_encoding:
                 raise UnicodeDecodeError(
                     'Cannot decode bytes value %r into unicode '
                     '(no default_encoding provided)' % value)
             try:
                 value = value.decode(self.default_encoding)
             except UnicodeDecodeError as e:
                 raise UnicodeDecodeError(
                     e.encoding,
                     e.object,
                     e.start,
                     e.end,
                     e.reason + ' in string %r' % value)
         elif not self._unicode and is_unicode(value):
             if not self.default_encoding:
                 raise UnicodeEncodeError(
                     'Cannot encode unicode value %r into bytes '
                     '(no default_encoding provided)' % value)
             value = value.encode(self.default_encoding)
         return value
Exemple #5
0
 def _repr(self, value, pos):
     __traceback_hide__ = True
     try:
         if value is None:
             return ''
         if self._unicode:
             try:
                 value = unicode(value)
             except UnicodeDecodeError:
                 value = bytes(value)
         else:
             if not isinstance(value, basestring_):
                 value = coerce_text(value)
             if (is_unicode(value)
                 and self.default_encoding):
                 value = value.encode(self.default_encoding)
     except:
         exc_info = sys.exc_info()
         e = exc_info[1]
         e.args = (self._add_line_info(e.args[0], pos),)
         raise exc_info[0], e, exc_info[2]
     else:
         if self._unicode and isinstance(value, bytes):
             if not self.default_encoding:
                 raise UnicodeDecodeError(
                     'Cannot decode bytes value %r into unicode '
                     '(no default_encoding provided)' % value)
             try:
                 value = value.decode(self.default_encoding)
             except UnicodeDecodeError, e:
                 raise UnicodeDecodeError(
                     e.encoding,
                     e.object,
                     e.start,
                     e.end,
                     e.reason + ' in string %r' % value)
         elif not self._unicode and is_unicode(value):
             if not self.default_encoding:
                 raise UnicodeEncodeError(
                     'Cannot encode unicode value %r into bytes '
                     '(no default_encoding provided)' % value)
             value = value.encode(self.default_encoding)
Exemple #6
0
def url(v):
    v = coerce_text(v)
    if is_unicode(v):
        v = v.encode('utf8')
    return url_quote(v)
Exemple #7
0
def fill_command(args=None):
    import sys
    import optparse
    import pkg_resources
    import os
    if args is None:
        args = sys.argv[1:]
    dist = pkg_resources.get_distribution('Paste')
    parser = optparse.OptionParser(
        version=coerce_text(dist),
        usage=_fill_command_usage)
    parser.add_option(
        '-o', '--output',
        dest='output',
        metavar="FILENAME",
        help="File to write output to (default stdout)")
    parser.add_option(
        '--html',
        dest='use_html',
        action='store_true',
        help="Use HTML style filling (including automatic HTML quoting)")
    parser.add_option(
        '--env',
        dest='use_env',
        action='store_true',
        help="Put the environment in as top-level variables")
    options, args = parser.parse_args(args)
    if len(args) < 1:
        print('You must give a template filename')
        sys.exit(2)
    template_name = args[0]
    args = args[1:]
    vars = {}
    if options.use_env:
        vars.update(os.environ)
    for value in args:
        if '=' not in value:
            print('Bad argument: %r' % value)
            sys.exit(2)
        name, value = value.split('=', 1)
        if name.startswith('py:'):
            name = name[:3]
            value = eval(value)
        vars[name] = value
    if template_name == '-':
        template_content = sys.stdin.read()
        template_name = '<stdin>'
    else:
        f = open(template_name, 'rb')
        template_content = f.read()
        f.close()
    if options.use_html:
        TemplateClass = HTMLTemplate
    else:
        TemplateClass = Template
    template = TemplateClass(template_content, name=template_name)
    result = template.substitute(vars)
    if options.output:
        f = open(options.output, 'wb')
        f.write(result)
        f.close()
    else:
        sys.stdout.write(result)
Exemple #8
0
    def _eval(self, code, ns, pos):
        __traceback_hide__ = True
        try:
            try:
                value = eval(code, self.default_namespace, ns)
            except SyntaxError, e:
                raise SyntaxError(
                    'invalid syntax in expression: %s' % code)
            return value
        except:
            exc_info = sys.exc_info()
            e = exc_info[1]
            if getattr(e, 'args', None):
                arg0 = e.args[0]
            else:
                arg0 = coerce_text(e)
            e.args = (self._add_line_info(arg0, pos),)
            raise exc_info[0], e, exc_info[2]

    def _exec(self, code, ns, pos):
        __traceback_hide__ = True
        try:
            exec code in self.default_namespace, ns
        except:
            exc_info = sys.exc_info()
            e = exc_info[1]
            if e.args:
                e.args = (self._add_line_info(e.args[0], pos),)
            else:
                e.args = (self._add_line_info(None, pos),)
            raise exc_info[0], e, exc_info[2]
def url(v):
    v = coerce_text(v)
    if is_unicode(v):
        v = v.encode('utf8')
    return url_quote(v)
Exemple #10
0
def fill_command(args=None):
    import sys
    import optparse
    import pkg_resources
    import os
    if args is None:
        args = sys.argv[1:]
    dist = pkg_resources.get_distribution('Paste')
    parser = optparse.OptionParser(
        version=coerce_text(dist),
        usage=_fill_command_usage)
    parser.add_option(
        '-o', '--output',
        dest='output',
        metavar="FILENAME",
        help="File to write output to (default stdout)")
    parser.add_option(
        '--html',
        dest='use_html',
        action='store_true',
        help="Use HTML style filling (including automatic HTML quoting)")
    parser.add_option(
        '--env',
        dest='use_env',
        action='store_true',
        help="Put the environment in as top-level variables")
    options, args = parser.parse_args(args)
    if len(args) < 1:
        print('You must give a template filename')
        sys.exit(2)
    template_name = args[0]
    args = args[1:]
    vars = {}
    if options.use_env:
        vars.update(os.environ)
    for value in args:
        if '=' not in value:
            print(('Bad argument: %r' % value))
            sys.exit(2)
        name, value = value.split('=', 1)
        if name.startswith('py:'):
            name = name[:3]
            value = eval(value)
        vars[name] = value
    if template_name == '-':
        template_content = sys.stdin.read()
        template_name = '<stdin>'
    else:
        f = open(template_name, 'rb')
        template_content = f.read()
        f.close()
    if options.use_html:
        TemplateClass = HTMLTemplate
    else:
        TemplateClass = Template
    template = TemplateClass(template_content, name=template_name)
    result = template.substitute(vars)
    if options.output:
        f = open(options.output, 'wb')
        f.write(result)
        f.close()
    else:
        sys.stdout.write(result)
Exemple #11
0
    def _eval(self, code, ns, pos):
        __traceback_hide__ = True
        try:
            try:
                value = eval(code, self.default_namespace, ns)
            except SyntaxError, e:
                raise SyntaxError(
                    'invalid syntax in expression: %s' % code)
            return value
        except:
            exc_info = sys.exc_info()
            e = exc_info[1]
            if getattr(e, 'args', None):
                arg0 = e.args[0]
            else:
                arg0 = coerce_text(e)
            e.args = (self._add_line_info(arg0, pos),)
            raise exc_info[0], e, exc_info[2]

    def _exec(self, code, ns, pos):
        __traceback_hide__ = True
        try:
            exec code in self.default_namespace, ns
        except:
            exc_info = sys.exc_info()
            e = exc_info[1]
            if e.args:
                e.args = (self._add_line_info(e.args[0], pos),)
            else:
                e.args = (self._add_line_info(None, pos),)
            raise exc_info[0], e, exc_info[2]