|
""" |
|
User name to file name conversion. |
|
This was taken from the UFO 3 spec. |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
illegalCharacters = { |
|
"\x00", |
|
"\x01", |
|
"\x02", |
|
"\x03", |
|
"\x04", |
|
"\x05", |
|
"\x06", |
|
"\x07", |
|
"\x08", |
|
"\t", |
|
"\n", |
|
"\x0b", |
|
"\x0c", |
|
"\r", |
|
"\x0e", |
|
"\x0f", |
|
"\x10", |
|
"\x11", |
|
"\x12", |
|
"\x13", |
|
"\x14", |
|
"\x15", |
|
"\x16", |
|
"\x17", |
|
"\x18", |
|
"\x19", |
|
"\x1a", |
|
"\x1b", |
|
"\x1c", |
|
"\x1d", |
|
"\x1e", |
|
"\x1f", |
|
'"', |
|
"*", |
|
"+", |
|
"/", |
|
":", |
|
"<", |
|
">", |
|
"?", |
|
"[", |
|
"\\", |
|
"]", |
|
"(", |
|
")", |
|
"|", |
|
"\x7f", |
|
} |
|
reservedFileNames = { |
|
"aux", |
|
"clock$", |
|
"com1", |
|
"com2", |
|
"com3", |
|
"com4", |
|
"com5", |
|
"com6", |
|
"com7", |
|
"com8", |
|
"com9", |
|
"con", |
|
"lpt1", |
|
"lpt2", |
|
"lpt3", |
|
"lpt4", |
|
"lpt5", |
|
"lpt6", |
|
"lpt7", |
|
"lpt8", |
|
"lpt9", |
|
"nul", |
|
"prn", |
|
} |
|
maxFileNameLength = 255 |
|
|
|
|
|
class NameTranslationError(Exception): |
|
pass |
|
|
|
|
|
def userNameToFileName(userName: str, existing=(), prefix="", suffix=""): |
|
""" |
|
`existing` should be a set-like object. |
|
|
|
>>> userNameToFileName("a") == "a" |
|
True |
|
>>> userNameToFileName("A") == "A_" |
|
True |
|
>>> userNameToFileName("AE") == "A_E_" |
|
True |
|
>>> userNameToFileName("Ae") == "A_e" |
|
True |
|
>>> userNameToFileName("ae") == "ae" |
|
True |
|
>>> userNameToFileName("aE") == "aE_" |
|
True |
|
>>> userNameToFileName("a.alt") == "a.alt" |
|
True |
|
>>> userNameToFileName("A.alt") == "A_.alt" |
|
True |
|
>>> userNameToFileName("A.Alt") == "A_.A_lt" |
|
True |
|
>>> userNameToFileName("A.aLt") == "A_.aL_t" |
|
True |
|
>>> userNameToFileName(u"A.alT") == "A_.alT_" |
|
True |
|
>>> userNameToFileName("T_H") == "T__H_" |
|
True |
|
>>> userNameToFileName("T_h") == "T__h" |
|
True |
|
>>> userNameToFileName("t_h") == "t_h" |
|
True |
|
>>> userNameToFileName("F_F_I") == "F__F__I_" |
|
True |
|
>>> userNameToFileName("f_f_i") == "f_f_i" |
|
True |
|
>>> userNameToFileName("Aacute_V.swash") == "A_acute_V_.swash" |
|
True |
|
>>> userNameToFileName(".notdef") == "_notdef" |
|
True |
|
>>> userNameToFileName("con") == "_con" |
|
True |
|
>>> userNameToFileName("CON") == "C_O_N_" |
|
True |
|
>>> userNameToFileName("con.alt") == "_con.alt" |
|
True |
|
>>> userNameToFileName("alt.con") == "alt._con" |
|
True |
|
""" |
|
|
|
if not isinstance(userName, str): |
|
raise ValueError("The value for userName must be a string.") |
|
|
|
prefixLength = len(prefix) |
|
suffixLength = len(suffix) |
|
|
|
|
|
if not prefix and userName[0] == ".": |
|
userName = "_" + userName[1:] |
|
|
|
filteredUserName = [] |
|
for character in userName: |
|
|
|
if character in illegalCharacters: |
|
character = "_" |
|
|
|
elif character != character.lower(): |
|
character += "_" |
|
filteredUserName.append(character) |
|
userName = "".join(filteredUserName) |
|
|
|
sliceLength = maxFileNameLength - prefixLength - suffixLength |
|
userName = userName[:sliceLength] |
|
|
|
parts = [] |
|
for part in userName.split("."): |
|
if part.lower() in reservedFileNames: |
|
part = "_" + part |
|
parts.append(part) |
|
userName = ".".join(parts) |
|
|
|
fullName = prefix + userName + suffix |
|
if fullName.lower() in existing: |
|
fullName = handleClash1(userName, existing, prefix, suffix) |
|
|
|
return fullName |
|
|
|
|
|
def handleClash1(userName, existing=[], prefix="", suffix=""): |
|
""" |
|
existing should be a case-insensitive list |
|
of all existing file names. |
|
|
|
>>> prefix = ("0" * 5) + "." |
|
>>> suffix = "." + ("0" * 10) |
|
>>> existing = ["a" * 5] |
|
|
|
>>> e = list(existing) |
|
>>> handleClash1(userName="A" * 5, existing=e, |
|
... prefix=prefix, suffix=suffix) == ( |
|
... '00000.AAAAA000000000000001.0000000000') |
|
True |
|
|
|
>>> e = list(existing) |
|
>>> e.append(prefix + "aaaaa" + "1".zfill(15) + suffix) |
|
>>> handleClash1(userName="A" * 5, existing=e, |
|
... prefix=prefix, suffix=suffix) == ( |
|
... '00000.AAAAA000000000000002.0000000000') |
|
True |
|
|
|
>>> e = list(existing) |
|
>>> e.append(prefix + "AAAAA" + "2".zfill(15) + suffix) |
|
>>> handleClash1(userName="A" * 5, existing=e, |
|
... prefix=prefix, suffix=suffix) == ( |
|
... '00000.AAAAA000000000000001.0000000000') |
|
True |
|
""" |
|
|
|
|
|
prefixLength = len(prefix) |
|
suffixLength = len(suffix) |
|
if prefixLength + len(userName) + suffixLength + 15 > maxFileNameLength: |
|
l = prefixLength + len(userName) + suffixLength + 15 |
|
sliceLength = maxFileNameLength - l |
|
userName = userName[:sliceLength] |
|
finalName = None |
|
|
|
counter = 1 |
|
while finalName is None: |
|
name = userName + str(counter).zfill(15) |
|
fullName = prefix + name + suffix |
|
if fullName.lower() not in existing: |
|
finalName = fullName |
|
break |
|
else: |
|
counter += 1 |
|
if counter >= 999999999999999: |
|
break |
|
|
|
if finalName is None: |
|
finalName = handleClash2(existing, prefix, suffix) |
|
|
|
return finalName |
|
|
|
|
|
def handleClash2(existing=[], prefix="", suffix=""): |
|
""" |
|
existing should be a case-insensitive list |
|
of all existing file names. |
|
|
|
>>> prefix = ("0" * 5) + "." |
|
>>> suffix = "." + ("0" * 10) |
|
>>> existing = [prefix + str(i) + suffix for i in range(100)] |
|
|
|
>>> e = list(existing) |
|
>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == ( |
|
... '00000.100.0000000000') |
|
True |
|
|
|
>>> e = list(existing) |
|
>>> e.remove(prefix + "1" + suffix) |
|
>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == ( |
|
... '00000.1.0000000000') |
|
True |
|
|
|
>>> e = list(existing) |
|
>>> e.remove(prefix + "2" + suffix) |
|
>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == ( |
|
... '00000.2.0000000000') |
|
True |
|
""" |
|
|
|
maxLength = maxFileNameLength - len(prefix) - len(suffix) |
|
maxValue = int("9" * maxLength) |
|
|
|
finalName = None |
|
counter = 1 |
|
while finalName is None: |
|
fullName = prefix + str(counter) + suffix |
|
if fullName.lower() not in existing: |
|
finalName = fullName |
|
break |
|
else: |
|
counter += 1 |
|
if counter >= maxValue: |
|
break |
|
|
|
if finalName is None: |
|
raise NameTranslationError("No unique name could be found.") |
|
|
|
return finalName |
|
|
|
|
|
if __name__ == "__main__": |
|
import doctest |
|
|
|
doctest.testmod() |
|
|