1"""Functions for escaping and unescaping strings (for URLs, filenames, etc). 2 3The underscore escaping functions are useful when generating directory names 4which should be reasonably human-readable, and not contain characters which are 5likely to confuse programs (e.g. ":" doesn't work on Windows, "#" confuses 6cmake, "=" confuses Java, [:;,] confuse anything that uses these as list item 7separators, etc. 8 9See also L{zeroinstall.injector.model.escape} for an older escaping scheme.1011@since: 1.1312"""1314importre1516# Copyright (C) 2012, Thomas Leonard17# See the README file for details, or visit http://0install.net.18
38"""Escape troublesome characters in 'src'.39 The result is a valid file leaf name (i.e. does not contain / etc).40 Letters, digits and characters > 127 are copied unmodified.41 '/' becomes '__'. Other characters become '_code_', where code is the42 lowercase hex value of the character in Unicode.43 @param src: the string to escape44 @type src: str45 @return: the escaped string46 @rtype: str"""47escaped=re.sub(_troublesome_re,_under_escape,src)4849# Avoid creating hidden files, or specials (. and ..)50ifescaped.startswith("."):51return"_2e_"+escaped[1:]5253returnescaped