Best Python code snippet using localstack_python
op_router.py
Source:op_router.py  
...162        request_uri_variable = request_uri_variable.strip("+")163    # replace forbidden chars (not allowed in Werkzeug rule variable names) with their placeholder164    escaped_request_uri_variable = request_uri_variable.translate(_rule_replacement_table)165    return f"<{greedy_prefix}{escaped_request_uri_variable}>"166def _post_process_arg_name(arg_key: str) -> str:167    """168    Reverses previous manipulations to the path parameters names (like replacing forbidden characters with169    placeholders).170    :param arg_key: Path param key name extracted using Werkzeug rules171    :return: Post-processed ("un-sanitized") path param key172    """173    result = arg_key174    for original, substitution in _rule_replacements.items():175        result = result.replace(substitution, original)176    return result177def _create_service_map(service: ServiceModel) -> Map:178    """179    Creates a Werkzeug Map object with all rules necessary for the specific service.180    :param service: botocore service model to create the rules for181    :return: a Map instance which is used to perform the in-service operation routing182    """183    ops = [service.operation_model(op_name) for op_name in service.operation_names]184    rules = []185    # group all operations by their path and method186    path_index: Dict[(str, str), List[_HttpOperation]] = defaultdict(list)187    for op in ops:188        http_op = _HttpOperation.from_operation(op)189        path_index[(http_op.path, http_op.method)].append(http_op)190    # create a matching rule for each (path, method) combination191    for (path, method), ops in path_index.items():192        # translate the requestUri to a Werkzeug rule string193        rule_string = _path_param_regex.sub(_transform_path_params_to_rule_vars, path)194        if len(ops) == 1:195            # if there is only a single operation for a (path, method) combination,196            # the default Werkzeug rule can be used directly (this is the case for most rules)197            op = ops[0]198            rules.append(_StrictMethodRule(string=rule_string, method=method, endpoint=op.operation))  # type: ignore199        else:200            # if there is an ambiguity with only the (path, method) combination,201            # a custom rule - which can use additional request metadata - needs to be used202            rules.append(_RequestMatchingRule(string=rule_string, method=method, operations=ops))203    return Map(204        rules=rules,205        strict_slashes=False,206        merge_slashes=False,207        converters={"path": GreedyPathConverter},208    )209class RestServiceOperationRouter:210    """211    A router implementation which abstracts the (quite complex) routing of incoming HTTP requests to a specific212    operation within a "REST" service (rest-xml, rest-json).213    """214    _map: Map215    def __init__(self, service: ServiceModel):216        self._map = _create_service_map(service)217    def match(self, request: Request) -> Tuple[OperationModel, Mapping[str, Any]]:218        """219        Matches the given request to the operation it targets (or raises an exception if no operation matches).220        :param request: The request of which the targeting operation needs to be found221        :return: A tuple with the matched operation and the (already parsed) path params222        :raises: Werkzeug's NotFound exception in case the given request does not match any operation223        """224        # bind the map to get the actual matcher225        matcher: MapAdapter = self._map.bind(request.host)226        # perform the matching227        rule, args = matcher.match(get_raw_path(request), method=request.method, return_rule=True)228        # if the found rule is a _RequestMatchingRule, the multi rule matching needs to be invoked to perform the229        # fine-grained matching based on the whole request230        if isinstance(rule, _RequestMatchingRule):231            rule = rule.match_request(request)232        # post process the arg keys and values233        # - the path param keys need to be "un-sanitized", i.e. sanitized rule variable names need to be reverted234        # - the path param values might still be url-encoded235        args = {_post_process_arg_name(k): unquote(v) for k, v in args.items()}236        # extract the operation model from the rule237        operation: OperationModel = rule.endpoint...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
