Python multidict.MultiDict() Examples

The following are 30 code examples of multidict.MultiDict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module multidict , or try the search function .
Example #1
Source File: formdata.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def add_fields(self, *fields: Any) -> None:
        to_add = list(fields)

        while to_add:
            rec = to_add.pop(0)

            if isinstance(rec, io.IOBase):
                k = guess_filename(rec, 'unknown')
                self.add_field(k, rec)  # type: ignore

            elif isinstance(rec, (MultiDictProxy, MultiDict)):
                to_add.extend(rec.items())

            elif isinstance(rec, (list, tuple)) and len(rec) == 2:
                k, fp = rec
                self.add_field(k, fp)  # type: ignore

            else:
                raise TypeError('Only io.IOBase, multidict and (name, file) '
                                'pairs allowed, use .add_field() for passing '
                                'more complex parameters, got {!r}'
                                .format(rec)) 
Example #2
Source File: client.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def _prepare_headers(self, headers):
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #3
Source File: formdata.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def add_fields(self, *fields):
        to_add = list(fields)

        while to_add:
            rec = to_add.pop(0)

            if isinstance(rec, io.IOBase):
                k = guess_filename(rec, 'unknown')
                self.add_field(k, rec)

            elif isinstance(rec, (MultiDictProxy, MultiDict)):
                to_add.extend(rec.items())

            elif isinstance(rec, (list, tuple)) and len(rec) == 2:
                k, fp = rec
                self.add_field(k, fp)

            else:
                raise TypeError('Only io.IOBase, multidict and (name, file) '
                                'pairs allowed, use .add_field() for passing '
                                'more complex parameters, got {!r}'
                                .format(rec)) 
Example #4
Source File: client.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def _prepare_headers(self, headers):
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #5
Source File: client.py    From grpclib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def request(
        self,
        name: str,
        cardinality: Cardinality,
        request_type: Type[_SendType],
        reply_type: Type[_RecvType],
        *,
        timeout: Optional[float] = None,
        deadline: Optional[Deadline] = None,
        metadata: Optional[_MetadataLike] = None,
    ) -> Stream[_SendType, _RecvType]:
        if timeout is not None and deadline is None:
            deadline = Deadline.from_timeout(timeout)
        elif timeout is not None and deadline is not None:
            deadline = min(Deadline.from_timeout(timeout), deadline)

        metadata = cast(_Metadata, MultiDict(metadata or ()))

        return Stream(self, name, metadata, cardinality,
                      request_type, reply_type, codec=self._codec,
                      status_details_codec=self._status_details_codec,
                      dispatch=self.__dispatch__, deadline=deadline) 
Example #6
Source File: util.py    From copra with MIT License 6 votes vote down vote up
def check_req(self, mock_req, url='', query=None, data=None, headers=None):
        if not query:
            query = {}
        if not data:
            data = {}
        if not headers:
            headers = {}
        if mock_req.method == 'GET':
            query['no-cache'] = mock_req.query['no-cache']
        self.assertEqual(mock_req.url, url)
        self.assertEqual(mock_req.query.items(), MultiDict(query).items())
        
        self.assertEqual(len(mock_req.headers), len(headers))
        for key, val in headers.items():
            self.assertIn(key, mock_req.headers)
            if not val == '*':
                self.assertEqual(mock_req.headers[key], val)
                
        self.assertEqual(mock_req.data, data) 
Example #7
Source File: formdata.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def add_fields(self, *fields):
        to_add = list(fields)

        while to_add:
            rec = to_add.pop(0)

            if isinstance(rec, io.IOBase):
                k = guess_filename(rec, 'unknown')
                self.add_field(k, rec)

            elif isinstance(rec, (MultiDictProxy, MultiDict)):
                to_add.extend(rec.items())

            elif isinstance(rec, (list, tuple)) and len(rec) == 2:
                k, fp = rec
                self.add_field(k, fp)

            else:
                raise TypeError('Only io.IOBase, multidict and (name, file) '
                                'pairs allowed, use .add_field() for passing '
                                'more complex parameters, got {!r}'
                                .format(rec)) 
Example #8
Source File: server.py    From python-libmaas with GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_multipart_params(request):
    """Extract a mapping of parts sent in a multipart request.

    :rtype: MultiDict
    """

    def get_part_name(part):
        _, params = parse_content_disposition(part.headers.get(CONTENT_DISPOSITION))
        return params.get("name")

    def get_part_data(part):
        if part.filename is None:
            return part.text()
        else:
            return part.read(decode=True)

    params = MultiDict()
    async for part in await request.multipart():
        params.add(get_part_name(part), await get_part_data(part))

    return params 
Example #9
Source File: test_client_stream.py    From grpclib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_connection_error():
    class BrokenChannel:
        _calls_started = 0

        def __connect__(self):
            raise IOError('Intentionally broken connection')

    stream = Stream(BrokenChannel(), '/foo/bar', MultiDict(),
                    Cardinality.UNARY_UNARY, DummyRequest, DummyReply,
                    codec=ProtoCodec(), status_details_codec=None,
                    dispatch=_DispatchChannelEvents())

    with pytest.raises(IOError) as err:
        async with stream:
            await stream.send_request()
    err.match('Intentionally broken connection') 
Example #10
Source File: admin.py    From metaflow-service with Apache License 2.0 6 votes vote down vote up
def ping(self, request):
        """
        ---
        description: This end-point allow to test that service is up.
        tags:
        - Admin
        produces:
        - 'text/plain'
        responses:
            "202":
                description: successful operation. Return "pong" text
            "405":
                description: invalid HTTP Method
        """
        return web.Response(text="pong", headers=MultiDict(
                                {METADATA_SERVICE_HEADER: METADATA_SERVICE_VERSION})) 
Example #11
Source File: __init__.py    From yarl with Apache License 2.0 6 votes vote down vote up
def with_query(self, *args, **kwargs):
        """Return a new URL with query part replaced.

        Accepts any Mapping (e.g. dict, multidict.MultiDict instances)
        or str, autoencode the argument if needed.

        A sequence of (key, value) pairs is supported as well.

        It also can take an arbitrary number of keyword arguments.

        Clear query if None is passed.

        """
        # N.B. doesn't cleanup query/fragment

        new_query = self._get_str_query(*args, **kwargs)
        return URL(
            self._val._replace(path=self._val.path, query=new_query), encoded=True
        ) 
Example #12
Source File: __init__.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def with_query(self, *args, **kwargs):
        """Return a new URL with query part replaced.

        Accepts any Mapping (e.g. dict, multidict.MultiDict instances)
        or str, autoencode the argument if needed.

        A sequence of (key, value) pairs is supported as well.

        It also can take an arbitrary number of keyword arguments.

        Clear query if None is passed.

        """
        # N.B. doesn't cleanup query/fragment

        new_query = self._get_str_query(*args, **kwargs)
        return URL(
            self._val._replace(path=self._val.path, query=new_query), encoded=True
        ) 
Example #13
Source File: client_reqrep.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def update_headers(self, headers: Optional[LooseHeaders]) -> None:
        """Update request headers."""
        self.headers = CIMultiDict()  # type: CIMultiDict[str]

        # add host
        netloc = cast(str, self.url.raw_host)
        if helpers.is_ipv6_address(netloc):
            netloc = '[{}]'.format(netloc)
        if not self.url.is_default_port():
            netloc += ':' + str(self.url.port)
        self.headers[hdrs.HOST] = netloc

        if headers:
            if isinstance(headers, (dict, MultiDictProxy, MultiDict)):
                headers = headers.items()  # type: ignore

            for key, value in headers:
                # A special case for Host header
                if key.lower() == 'host':
                    self.headers[key] = value
                else:
                    self.headers.add(key, value) 
Example #14
Source File: client.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def _prepare_headers(
            self,
            headers: Optional[LooseHeaders]) -> 'CIMultiDict[str]':
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()  # type: Set[str]
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #15
Source File: formdata.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def add_field(self, name, value, *, content_type=None, filename=None,
                  content_transfer_encoding=None):

        if isinstance(value, io.IOBase):
            self._is_multipart = True
        elif isinstance(value, (bytes, bytearray, memoryview)):
            if filename is None and content_transfer_encoding is None:
                filename = name

        type_options = MultiDict({'name': name})
        if filename is not None and not isinstance(filename, str):
            raise TypeError('filename must be an instance of str. '
                            'Got: %s' % filename)
        if filename is None and isinstance(value, io.IOBase):
            filename = guess_filename(value, name)
        if filename is not None:
            type_options['filename'] = filename
            self._is_multipart = True

        headers = {}
        if content_type is not None:
            if not isinstance(content_type, str):
                raise TypeError('content_type must be an instance of str. '
                                'Got: %s' % content_type)
            headers[hdrs.CONTENT_TYPE] = content_type
            self._is_multipart = True
        if content_transfer_encoding is not None:
            if not isinstance(content_transfer_encoding, str):
                raise TypeError('content_transfer_encoding must be an instance'
                                ' of str. Got: %s' % content_transfer_encoding)
            headers[hdrs.CONTENT_TRANSFER_ENCODING] = content_transfer_encoding
            self._is_multipart = True

        self._fields.append((type_options, headers, value)) 
Example #16
Source File: __init__.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def query(self):
        """A MultiDictProxy representing parsed query parameters in decoded
        representation.

        Empty value if URL has no query part.

        """
        ret = MultiDict(parse_qsl(self.raw_query_string,
                                  keep_blank_values=True))
        return MultiDictProxy(ret) 
Example #17
Source File: server.py    From python-libmaas with GNU Affero General Public License v3.0 5 votes vote down vote up
def _wrap_handler(handler):
        """Wrap `handler` in some conveniences.

        These are:

        * Setting `request.params` to a `MultiDict` instance of POSTed form
          parameters, or `None` if the body content was not a multipart form.

        * Passing `request.match_info` as keyword arguments into the handler.
          For example, if a route like "/foo/{bar}" is matched by a path
          "/foo/thing", the handler will be called with bar="thing".

        * Objects returned from `handler` that are not proper responses are
          rendered as JSON.

        """

        async def wrapper(request):
            # For convenience, read in all multipart parameters.
            assert not hasattr(request, "params")
            if request.content_type == "multipart/form-data":
                request.params = await _get_multipart_params(request)
            else:
                request.params = None
            response = await handler(request, **request.match_info)
            # For convenience, assume non-Responses are meant as JSON.
            if not isinstance(response, aiohttp.web.Response):
                response = aiohttp.web.json_response(response)
            return response

        return wrapper 
Example #18
Source File: formdata.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def add_field(self, name, value, *, content_type=None, filename=None,
                  content_transfer_encoding=None):

        if isinstance(value, io.IOBase):
            self._is_multipart = True
        elif isinstance(value, (bytes, bytearray, memoryview)):
            if filename is None and content_transfer_encoding is None:
                filename = name

        type_options = MultiDict({'name': name})
        if filename is not None and not isinstance(filename, str):
            raise TypeError('filename must be an instance of str. '
                            'Got: %s' % filename)
        if filename is None and isinstance(value, io.IOBase):
            filename = guess_filename(value, name)
        if filename is not None:
            type_options['filename'] = filename
            self._is_multipart = True

        headers = {}
        if content_type is not None:
            if not isinstance(content_type, str):
                raise TypeError('content_type must be an instance of str. '
                                'Got: %s' % content_type)
            headers[hdrs.CONTENT_TYPE] = content_type
            self._is_multipart = True
        if content_transfer_encoding is not None:
            if not isinstance(content_transfer_encoding, str):
                raise TypeError('content_transfer_encoding must be an instance'
                                ' of str. Got: %s' % content_transfer_encoding)
            headers[hdrs.CONTENT_TRANSFER_ENCODING] = content_transfer_encoding
            self._is_multipart = True

        self._fields.append((type_options, headers, value)) 
Example #19
Source File: client_reqrep.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def update_headers(self, headers):
        """Update request headers."""
        self.headers = CIMultiDict()
        if headers:
            if isinstance(headers, (dict, MultiDictProxy, MultiDict)):
                headers = headers.items()

            for key, value in headers:
                self.headers.add(key, value) 
Example #20
Source File: compat.py    From aioresponses with MIT License 5 votes vote down vote up
def merge_params(url: 'Union[URL, str]', params: 'Dict' = None) -> 'URL':
    url = URL(url)
    if params:
        query_params = MultiDict(url.query)
        query_params.extend(url.with_query(params).query)
        return url.with_query(query_params)
    return url 
Example #21
Source File: base_view.py    From slim with zlib License 5 votes vote down vote up
def params(self) -> "MultiDict[str]":
        if self._params_cache is None:
            self._params_cache = MultiDict(self._request.query)
        return self._params_cache 
Example #22
Source File: utils.py    From metaflow-service with Apache License 2.0 5 votes vote down vote up
def format_response(func):
    """handle formatting"""

    @wraps(func)
    async def wrapper(*args, **kwargs):
        db_response = await func(*args, **kwargs)
        return web.Response(status=db_response.response_code,
                            body=json.dumps(db_response.body),
                            headers=MultiDict(
                                {METADATA_SERVICE_HEADER: METADATA_SERVICE_VERSION}))

    return wrapper 
Example #23
Source File: sqlquery.py    From slim with zlib License 5 votes vote down vote up
def parse(self, post_data: MultiDict):
        self.clear()
        if isinstance(post_data, dict):
            post_data = MultiDict(post_data)

        for k, v in post_data.items():
            # 提交多个相同值,等价于提交一个数组(用于formdata和urlencode形式)
            v_all = post_data.getall(k)
            if len(v_all) > 1:
                v = v_all

            if k.startswith('$'):
                continue
            elif k == 'returning':
                self.returning = True
                continue
            elif '.' in k:
                # TODO: 不允许 incr 和普通赋值同时出现
                k, op = k.rsplit('.', 1)
                if op == 'incr':
                    self.incr_fields.add(k)
                elif op == 'decr':
                    self.decr_fields.add(k)
                elif op == 'set_add':
                    self.set_add_fields.add(k)
                elif op == 'set_remove':
                    self.set_remove_fields.add(k)
                # elif op == 'array_append':
                #     self.array_append.add(k)
                # elif op == 'array_remove':
                #    self.array_remove.add(k)

            self[k] = v 
Example #24
Source File: __init__.py    From Galaxy_Plugin_Bethesda with MIT License 5 votes vote down vote up
def update_query(self, *args, **kwargs):
        """Return a new URL with query part updated."""
        s = self._get_str_query(*args, **kwargs)
        new_query = MultiDict(parse_qsl(s, keep_blank_values=True))
        query = MultiDict(self.query)
        query.update(new_query)

        return URL(self._val._replace(query=self._get_str_query(query)), encoded=True) 
Example #25
Source File: __init__.py    From Galaxy_Plugin_Bethesda with MIT License 5 votes vote down vote up
def query(self):
        """A MultiDictProxy representing parsed query parameters in decoded
        representation.

        Empty value if URL has no query part.

        """
        ret = MultiDict(parse_qsl(self.raw_query_string, keep_blank_values=True))
        return MultiDictProxy(ret) 
Example #26
Source File: client_reqrep.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def update_headers(self, headers):
        """Update request headers."""
        self.headers = CIMultiDict()
        if headers:
            if isinstance(headers, (dict, MultiDictProxy, MultiDict)):
                headers = headers.items()

            for key, value in headers:
                self.headers.add(key, value) 
Example #27
Source File: __init__.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def query(self):
        """A MultiDictProxy representing parsed query parameters in decoded
        representation.

        Empty value if URL has no query part.

        """
        ret = MultiDict(parse_qsl(self.raw_query_string,
                                  keep_blank_values=True))
        return MultiDictProxy(ret) 
Example #28
Source File: helpers.py    From Galaxy_Plugin_Bethesda with MIT License 5 votes vote down vote up
def parse_mimetype(mimetype: str) -> MimeType:
    """Parses a MIME type into its components.

    mimetype is a MIME type string.

    Returns a MimeType object.

    Example:

    >>> parse_mimetype('text/html; charset=utf-8')
    MimeType(type='text', subtype='html', suffix='',
             parameters={'charset': 'utf-8'})

    """
    if not mimetype:
        return MimeType(type='', subtype='', suffix='',
                        parameters=MultiDictProxy(MultiDict()))

    parts = mimetype.split(';')
    params = MultiDict()  # type: MultiDict[str]
    for item in parts[1:]:
        if not item:
            continue
        key, value = cast(Tuple[str, str],
                          item.split('=', 1) if '=' in item else (item, ''))
        params.add(key.lower().strip(), value.strip(' "'))

    fulltype = parts[0].strip().lower()
    if fulltype == '*':
        fulltype = '*/*'

    mtype, stype = (cast(Tuple[str, str], fulltype.split('/', 1))
                    if '/' in fulltype else (fulltype, ''))
    stype, suffix = (cast(Tuple[str, str], stype.split('+', 1))
                     if '+' in stype else (stype, ''))

    return MimeType(type=mtype, subtype=stype, suffix=suffix,
                    parameters=MultiDictProxy(params)) 
Example #29
Source File: vfolder.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def download(request: web.Request, params: Any, row: VFolderRow) -> web.Response:
    folder_name = request.match_info['name']
    access_key = request['keypair']['access_key']
    files = params['files']
    log.info('VFOLDER.DOWNLOAD (ak:{}, vf:{}, path:{})', access_key, folder_name, files[0])
    folder_path = get_folder_hostpath(row, request.app)
    for file in files:
        try:
            file_path = (folder_path / file).resolve()
            file_path.relative_to(folder_path)
        except ValueError:
            raise InvalidAPIParameters('The requested path is out of the folder')
        if not file_path.is_file():
            raise InvalidAPIParameters(
                f'You cannot download "{file}" because it is not a regular file.')
    with aiohttp.MultipartWriter('mixed') as mpwriter:
        total_payloads_length = 0
        headers = multidict.MultiDict({'Content-Encoding': 'identity'})
        try:
            for file in files:
                data = open(folder_path / file, 'rb')
                payload = mpwriter.append(data, headers)
                if payload.size is not None:
                    total_payloads_length += payload.size
        except FileNotFoundError:
            return web.Response(status=404, reason='File not found')
        mpwriter._headers['X-TOTAL-PAYLOADS-LENGTH'] = str(total_payloads_length)
        return web.Response(body=mpwriter, status=200) 
Example #30
Source File: containers.py    From aiodocker with Apache License 2.0 5 votes vote down vote up
def attach(
        self,
        *,
        stdout: bool = False,
        stderr: bool = False,
        stdin: bool = False,
        detach_keys: Optional[str] = None,
        logs: bool = False,
    ) -> Stream:
        async def setup() -> Tuple[URL, bytes]:
            params = MultiDict()
            if detach_keys:
                params.add("detachKeys", detach_keys)
            else:
                params.add("detachKeys", "")
            params.add("logs", int(logs))
            params.add("stdin", int(stdin))
            params.add("stdout", int(stdout))
            params.add("stderr", int(stderr))
            params.add("stream", 1)
            inspect_info = await self.show()
            return (
                URL(f"containers/{self._id}/attach").with_query(params),
                None,
                inspect_info["Config"]["Tty"],
            )

        return Stream(self.docker, setup, None)