def legalize_path(path, replacements, length, extension, fragment):
 if fragment:
  extension = extension.decode('utf-8', 'ignore')
 (first_stage_path, _) = _legalize_stage(path, replacements, length, extension, fragment)
 (first_stage_path, _) = os.path.splitext(displayable_path(first_stage_path))
 (second_stage_path, retruncated) = _legalize_stage(first_stage_path, replacements, length, extension, fragment)
 if retruncated:
  (second_stage_path, _) = _legalize_stage(first_stage_path, None, length, extension, fragment)
 return (second_stage_path, retruncated)

def legalize_path ( path , replacements , length , extension , fragment ) : if fragment : extension = extension . decode ( 'utf-8' , 'ignore' ) ( first_stage_path , _ ) = _legalize_stage ( path , replacements , length , extension , fragment ) ( first_stage_path , _ ) = os . path . splitext ( displayable_path ( first_stage_path ) ) ( second_stage_path , retruncated ) = _legalize_stage ( first_stage_path , replacements , length , extension , fragment ) if retruncated : ( second_stage_path , _ ) = _legalize_stage ( first_stage_path , None , length , extension , fragment ) return ( second_stage_path , retruncated )
given a path-like unicode string .