MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / match

Method match

21-async/mojifinder/bottle.py:417–454  ·  view source on GitHub ↗

Return a (target, url_agrs) tuple or raise HTTPError(400/404/405).

(self, environ)

Source from the content-addressed store, hash-verified

415 raise RouteBuildError('Missing URL argument: %r' % _e().args[0])
416
417 def match(self, environ):
418 ''' Return a (target, url_agrs) tuple or raise HTTPError(400/404/405). '''
419 verb = environ['REQUEST_METHOD'].upper()
420 path = environ['PATH_INFO'] or '/'
421 target = None
422 if verb == 'HEAD':
423 methods = ['PROXY', verb, 'GET', 'ANY']
424 else:
425 methods = ['PROXY', verb, 'ANY']
426
427 for method in methods:
428 if method in self.static and path in self.static[method]:
429 target, getargs = self.static[method][path]
430 return target, getargs(path) if getargs else {}
431 elif method in self.dyna_regexes:
432 for combined, rules in self.dyna_regexes[method]:
433 match = combined(path)
434 if match:
435 target, getargs = rules[match.lastindex - 1]
436 return target, getargs(path) if getargs else {}
437
438 # No matching route found. Collect alternative methods for 405 response
439 allowed = set([])
440 nocheck = set(methods)
441 for method in set(self.static) - nocheck:
442 if path in self.static[method]:
443 allowed.add(method)
444 for method in set(self.dyna_regexes) - allowed - nocheck:
445 for combined, rules in self.dyna_regexes[method]:
446 match = combined(path)
447 if match:
448 allowed.add(method)
449 if allowed:
450 allow_header = ",".join(sorted(allowed))
451 raise HTTPError(405, "Method not allowed.", Allow=allow_header)
452
453 # No matching route and no alternative method found. We give up
454 raise HTTPError(404, "Not found: " + repr(path))
455
456
457

Callers 5

next_tokenMethod · 0.45
matchMethod · 0.45
_handleMethod · 0.45
numerics_demo.pyFile · 0.45

Calls 2

HTTPErrorClass · 0.85
addMethod · 0.80

Tested by

no test coverage detected