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

Function path_shift

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

Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa. :return: The modified paths. :param script_name: The SCRIPT_NAME path. :param script_name: The PATH_INFO path. :param shift: The number of path fragments to shift. May be negative to change

(script_name, path_info, shift=1)

Source from the content-addressed store, hash-verified

2655
2656
2657def path_shift(script_name, path_info, shift=1):
2658 ''' Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa.
2659
2660 :return: The modified paths.
2661 :param script_name: The SCRIPT_NAME path.
2662 :param script_name: The PATH_INFO path.
2663 :param shift: The number of path fragments to shift. May be negative to
2664 change the shift direction. (default: 1)
2665 '''
2666 if shift == 0: return script_name, path_info
2667 pathlist = path_info.strip('/').split('/')
2668 scriptlist = script_name.strip('/').split('/')
2669 if pathlist and pathlist[0] == '': pathlist = []
2670 if scriptlist and scriptlist[0] == '': scriptlist = []
2671 if shift > 0 and shift <= len(pathlist):
2672 moved = pathlist[:shift]
2673 scriptlist = scriptlist + moved
2674 pathlist = pathlist[shift:]
2675 elif shift < 0 and shift >= -len(scriptlist):
2676 moved = scriptlist[shift:]
2677 pathlist = moved + pathlist
2678 scriptlist = scriptlist[:shift]
2679 else:
2680 empty = 'SCRIPT_NAME' if shift < 0 else 'PATH_INFO'
2681 raise AssertionError("Cannot shift. Nothing left from %s" % empty)
2682 new_script_name = '/' + '/'.join(scriptlist)
2683 new_path_info = '/' + '/'.join(pathlist)
2684 if path_info.endswith('/') and pathlist: new_path_info += '/'
2685 return new_script_name, new_path_info
2686
2687
2688def auth_basic(check, realm="private", text="Access denied"):

Callers 1

path_shiftMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected