Ejemplo n.º 1
0
    def set_options(self, options, sort=False):
        """(options: [objects:any], sort=False)
         or
           (options: [(object:any, description:any)], sort=False)
         or
           (options: [(object:any, description:any, key:any)], sort=False)
        """
        """
        Set the options list.  The list of options can be a list of objects, in
        which case the descriptions default to map(htmlescape, objects)
        applying htmlescape() to each description and
        key.
        If keys are provided they must be distinct.  If the sort keyword
        argument is true, sort the options by case-insensitive lexicographic
        order of descriptions, except that options with value None appear
        before others.
        """
        if options:
            first = options[0]
            values = []
            descriptions = []
            keys = []
            if isinstance(first, tuple):
                if len(first) == 2:
                    for value, description in options:
                        values.append(value)
                        descriptions.append(description)
                elif len(first) == 3:
                    for value, description, key in options:
                        values.append(value)
                        descriptions.append(description)
                        keys.append(stringify(key))
                else:
                    raise ValueError, 'invalid options %r' % options
            else:
                values = descriptions = options

            if not keys:
                keys = self._generate_keys(values, descriptions)

            options = zip(values, descriptions, keys)

            if sort:

                def make_sort_key(option):
                    value, description, key = option
                    if value is None:
                        return ('', option)
                    else:
                        return (stringify(description).lower(), option)

                doptions = map(make_sort_key, options)
                doptions.sort()
                options = [item[1] for item in doptions]
        self.options = options
Ejemplo n.º 2
0
    def set_options(self, options, sort=False):
        """(options: [objects:any], sort=False)
         or
           (options: [(object:any, description:any)], sort=False)
         or
           (options: [(object:any, description:any, key:any)], sort=False)
        """

        """
        Set the options list.  The list of options can be a list of objects, in
        which case the descriptions default to map(htmlescape, objects)
        applying htmlescape() to each description and
        key.
        If keys are provided they must be distinct.  If the sort keyword
        argument is true, sort the options by case-insensitive lexicographic
        order of descriptions, except that options with value None appear
        before others.
        """
        if options:
            first = options[0]
            values = []
            descriptions = []
            keys = []
            if isinstance(first, tuple):
                if len(first) == 2:
                    for value, description in options:
                        values.append(value)
                        descriptions.append(description)
                elif len(first) == 3:
                    for value, description, key in options:
                        values.append(value)
                        descriptions.append(description)
                        keys.append(stringify(key))
                else:
                    raise ValueError, 'invalid options %r' % options
            else:
                values = descriptions = options

            if not keys:
                keys = self._generate_keys(values, descriptions)

            options = zip(values, descriptions, keys)

            if sort:
                def make_sort_key(option):
                    value, description, key = option
                    if value is None:
                        return ('', option)
                    else:
                        return (stringify(description).lower(), option)
                doptions = map(make_sort_key, options)
                doptions.sort()
                options = [item[1] for item in doptions]
        self.options = options
Ejemplo n.º 3
0
    def set_body(self, body, compress=False):
        """(body : any, compress : bool = False)

        Sets the response body equal to the argument 'body'.  If 'compress'
        is true then the body may be compressed using 'gzip'.
        """
        if not isinstance(body, Stream):
            body = self._encode_chunk(stringify(body))
            if compress and self.content_type not in _GZIP_EXCLUDE:
                body = self._compress_body(body)
        self.body = body
Ejemplo n.º 4
0
    def set_body(self, body, compress=False):
        """(body : any, compress : bool = False)

        Sets the response body equal to the argument 'body'.  If 'compress'
        is true then the body may be compressed using 'gzip'.
        """
        if not isinstance(body, Stream):
            body = self._encode_chunk(stringify(body))
            if compress and self.content_type not in _GZIP_EXCLUDE:
                body = self._compress_body(body)
        self.body = body
Ejemplo n.º 5
0
 def parse(self, request=None):
     if not self._parsed:
         self._parsed = True
         if request is None:
             request = get_request()
         if request.form or request.get_method() == 'POST':
             try:
                 self._parse(request)
             except WidgetValueError, exc:
                 self.set_error(stringify(exc))
             if (self.required and self.value is None
                     and not self.has_error()):
                 self.set_error(self.REQUIRED_ERROR)
Ejemplo n.º 6
0
 def parse(self, request=None):
     if not self._parsed:
         self._parsed = True
         if request is None:
             request = get_request()
         if request.form or request.get_method() == 'POST':
             try:
                 self._parse(request)
             except WidgetValueError, exc:
                 self.set_error(stringify(exc))
             if (self.required and self.value is None and
                 not self.has_error()):
                 self.set_error(self.REQUIRED_ERROR)
Ejemplo n.º 7
0
 def _generate_compressed(self, body):
     co = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS,
                           zlib.DEF_MEM_LEVEL, 0)
     crc = zlib.crc32(b'') & 0xffffffff
     n = 0
     yield _GZIP_HEADER
     for chunk in body:
         if not isinstance(chunk, bytes):
             chunk = self._encode_chunk(stringify(chunk))
         crc = zlib.crc32(chunk, crc) & 0xffffffff
         n += len(chunk)
         yield co.compress(chunk)
     crc = struct.pack("<LL", _LOWU32(crc), _LOWU32(n))
     yield co.flush() + crc
Ejemplo n.º 8
0
 def _generate_compressed(self, body):
     co = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS,
                               zlib.DEF_MEM_LEVEL, 0)
     crc = zlib.crc32('') & 0xffffffffL
     n = 0
     yield _GZIP_HEADER
     for chunk in body:
         if not isinstance(chunk, str):
             chunk = self._encode_chunk(stringify(chunk))
         crc = zlib.crc32(chunk, crc) & 0xffffffffL
         n += len(chunk)
         yield co.compress(chunk)
     crc = struct.pack("<LL", _LOWU32(crc), _LOWU32(n))
     yield co.flush() + crc
Ejemplo n.º 9
0
    def set_body(self, body, compress=False):
        """(body : any, compress : bool = False)

        Sets the response body equal to the argument 'body'.  If 'compress'
        is true then the body may be compressed.
        """
        if not isinstance(body, Stream):
            body = self._encode_chunk(stringify(body))
            if compress and self.content_type not in _GZIP_EXCLUDE:
                body = self._compress_body(body)
        else:
            if compress and self.content_type not in _GZIP_EXCLUDE:
                self.set_header("Content-Encoding", "gzip")
                body = Stream(self._generate_compressed(body))
        self.body = body
Ejemplo n.º 10
0
    def set_body(self, body, compress=False):
        """(body : any, compress : bool = False)

        Sets the response body equal to the argument 'body'.  If 'compress'
        is true then the body may be compressed.
        """
        if not isinstance(body, Stream):
            if not isinstance(body, bytes):
                body = self._encode_chunk(stringify(body))
            if compress and self.content_type not in _GZIP_EXCLUDE:
                body = self._compress_body(body)
        else:
            if compress and self.content_type not in _GZIP_EXCLUDE:
                self.set_header("Content-Encoding", "gzip")
                body = Stream(self._generate_compressed(body))
        self.body = body
Ejemplo n.º 11
0
 def parse(self, request=None):
     if not self._parsed:
         self._parsed = True
         if request is None:
             request = get_request()
         if self._form is not None:
             # use the form to determine if form data was submitted.  It
             # is possible that there is a query string, the request method
             # is GET and the form method is POST.  In that case the form
             # should not be considered submitted.
             submitted = self._form.is_submitted()
         else:
             submitted = request.form or request.get_method() == 'POST'
         if submitted:
             try:
                 self._parse(request)
             except WidgetValueError, exc:
                 self.set_error(stringify(exc))
             if (self.required and self.value is None and
                 not self.has_error()):
                 self.set_error(self.REQUIRED_ERROR)
Ejemplo n.º 12
0
 def parse(self, request=None):
     if not self._parsed:
         self._parsed = True
         if request is None:
             request = get_request()
         if self._form is not None:
             # use the form to determine if form data was submitted.  It
             # is possible that there is a query string, the request method
             # is GET and the form method is POST.  In that case the form
             # should not be considered submitted.
             submitted = self._form.is_submitted()
         else:
             submitted = request.form or request.get_method() == 'POST'
         if submitted:
             try:
                 self._parse(request)
             except WidgetValueError, exc:
                 self.set_error(stringify(exc))
             if (self.required and self.value is None
                     and not self.has_error()):
                 self.set_error(self.REQUIRED_ERROR)
Ejemplo n.º 13
0
 def make_sort_key(option):
     value, description, key = option
     if value is None:
         return ('', option)
     else:
         return (stringify(description).lower(), option)
Ejemplo n.º 14
0
 def __str__(self):
     return stringify(self.msg)
Ejemplo n.º 15
0
 def make_sort_key(option):
     value, description, key = option
     if value is None:
         return ('', option)
     else:
         return (stringify(description).lower(), option)
Ejemplo n.º 16
0
 def __str__(self):
     return stringify(self.msg)