Ejemplo n.º 1
0
Archivo: wsgi.py Proyecto: jek/alfajor
 def _prep_input(self, method, data, content_type):
     """Return encoded and packed POST data."""
     if data is None or method != 'POST':
         prepped = {
             'input_stream': None,
             'content_length': None,
             'content_type': None,
             }
         if method == 'GET' and data:
             qs = MultiDict()
             for key, value in to_pairs(data):
                 qs.setlistdefault(key).append(value)
             prepped['query_string'] = url_encode(qs)
         return prepped
     else:
         if content_type == 'multipart/form-data':
             data = [(k, _wrap_file(*v)) if isinstance(v, tuple) else (k,v)
                     for k,v in data]
             boundary, payload = encode_multipart(MultiDict(to_pairs(data)))
             content_type = 'multipart/form-data; boundary=' + boundary
         else:
             payload = url_encode(MultiDict(to_pairs(data)))
             content_type = 'application/x-www-form-urlencoded'
         return {
             'input_stream': StringIO(payload),
             'content_length': len(payload),
             'content_type': content_type
             }
Ejemplo n.º 2
0
    def __init__(self, browser, mixins):
        lxml_html.HtmlElementClassLookup.__init__(self)
        mixins = list(to_pairs(mixins))

        mix_all = tuple(cls for name, cls in mixins if name == '*')

        for name in ('HtmlElement', 'HtmlComment', 'HtmlProcessingInstruction',
                     'HtmlEntity'):
            base = getattr(lxml_html, name)
            mixed = type(name, mix_all + base.__bases__, {'browser': browser})
            setattr(self, name, mixed)

        classes = self._element_classes
        mixers = {}
        for name, value in mixins:
            if name == '*':
                continue
            mixers.setdefault(name, []).append(value)

        for name, value in mixins:
            if name != '*':
                continue
            for n in classes.keys():
                mixers.setdefault(n, []).append(value)

        for name, mix_bases in mixers.items():
            cur = classes.get(name, self.HtmlElement)
            bases = tuple(mix_bases + [cur])
            classes[name] = type(cur.__name__, bases, {'browser': browser})
        self._element_classes = classes
Ejemplo n.º 3
0
Archivo: _lxml.py Proyecto: jek/alfajor
    def __init__(self, browser, mixins):
        lxml_html.HtmlElementClassLookup.__init__(self)
        mixins = list(to_pairs(mixins))

        mix_all = tuple(cls for name, cls in mixins if name == '*')

        for name in ('HtmlElement', 'HtmlComment', 'HtmlProcessingInstruction',
                     'HtmlEntity'):
            base = getattr(lxml_html, name)
            mixed = type(name,  mix_all + base.__bases__, {'browser': browser})
            setattr(self, name, mixed)

        classes = self._element_classes
        mixers = {}
        for name, value in mixins:
            if name == '*':
                continue
            mixers.setdefault(name, []).append(value)

        for name, value in mixins:
            if name != '*':
                continue
            for n in classes.keys():
                mixers.setdefault(n, []).append(value)

        for name, mix_bases in mixers.items():
            cur = classes.get(name, self.HtmlElement)
            bases = tuple(mix_bases + [cur])
            classes[name] = type(cur.__name__, bases, {'browser': browser})
        self._element_classes = classes
Ejemplo n.º 4
0
 def _prep_input(self, method, data, content_type):
     """Return encoded and packed POST data."""
     if data is None or method != 'POST':
         prepped = {
             'input_stream': None,
             'content_length': None,
             'content_type': None,
             }
         if method == 'GET' and data:
             qs = MultiDict()
             for key, value in to_pairs(data):
                 qs.setlistdefault(key).append(value)
             prepped['query_string'] = url_encode(qs)
         return prepped
     else:
         payload = url_encode(MultiDict(to_pairs(data)))
         content_type = 'application/x-www-form-urlencoded'
         return {
             'input_stream': StringIO(payload),
             'content_length': len(payload),
             'content_type': content_type
             }
Ejemplo n.º 5
0
 def _prep_input(self, method, data, content_type):
     """Return encoded and packed POST data."""
     if data is None or method != 'POST':
         prepped = {
             'input_stream': None,
             'content_length': None,
             'content_type': None,
         }
         if method == 'GET' and data:
             qs = MultiDict()
             for key, value in to_pairs(data):
                 qs.setlistdefault(key).append(value)
             prepped['query_string'] = url_encode(qs)
         return prepped
     else:
         payload = url_encode(MultiDict(to_pairs(data)))
         content_type = 'application/x-www-form-urlencoded'
         return {
             'input_stream': StringIO(payload),
             'content_length': len(payload),
             'content_type': content_type
         }
Ejemplo n.º 6
0
def _group_key_value_pairs(values, with_prefix=''):
    """Transform *values* into a sequence of ('name', ['values']) pairs.

    For use by form.fill().  Collapses repeats of a given name into a single
    list of values.  (And non-repeated names as a list of one value.)

    :param values: a mapping or sequence of name/value pairs.

    :param with_prefix: optional, a string that all form fields should
      start with.  If a supplied field name does not start with this
      prefix, it will be prepended.

    """
    grouped = defaultdict(list)
    transformed_keys = []
    for key, value in to_pairs(values):
        if with_prefix and not key.startswith(with_prefix):
            key = with_prefix + key
        grouped[key].append(value)
        if key not in transformed_keys:
            transformed_keys.append(key)
    return [(key, grouped[key]) for key in transformed_keys]
Ejemplo n.º 7
0
Archivo: _lxml.py Proyecto: jek/alfajor
def _group_key_value_pairs(values, with_prefix=''):
    """Transform *values* into a sequence of ('name', ['values']) pairs.

    For use by form.fill().  Collapses repeats of a given name into a single
    list of values.  (And non-repeated names as a list of one value.)

    :param values: a mapping or sequence of name/value pairs.

    :param with_prefix: optional, a string that all form fields should
      start with.  If a supplied field name does not start with this
      prefix, it will be prepended.

    """
    grouped = defaultdict(list)
    transformed_keys = []
    for key, value in to_pairs(values):
        if with_prefix and not key.startswith(with_prefix):
            key = with_prefix + key
        grouped[key].append(value)
        if key not in transformed_keys:
            transformed_keys.append(key)
    return [(key, grouped[key]) for key in transformed_keys]