_path_signature.py (1109B)
1 """Shared rendering for HTTP and WebSocket path signatures.""" 2 3 import re 4 5 from sphinx import addnodes 6 7 8 path_signature_param_re = re.compile( 9 r'\((?:(?P<type>[^:)]+):)?(?P<name>[\w_]+)\)', re.VERBOSE 10 ) 11 12 13 def render_path_signature(sig, signode): 14 """Render typed path parameters into *signode* and return the final path part. 15 16 The return value intentionally mirrors the historical HTTP implementation. 17 """ 18 offset = 0 19 path = None 20 for match in path_signature_param_re.finditer(sig): 21 path = sig[offset:match.start()] 22 signode += addnodes.desc_name(path, path) 23 params = addnodes.desc_parameterlist() 24 typ = match.group('type') 25 if typ: 26 typ += ': ' 27 params += addnodes.desc_annotation(typ, typ) 28 name = match.group('name') 29 params += addnodes.desc_parameter(name, name) 30 signode += params 31 offset = match.end() 32 if offset < len(sig): 33 path = sig[offset:len(sig)] 34 signode += addnodes.desc_name(path, path) 35 assert path is not None, 'no matches for sig: %s' % sig 36 return path