1"""
2User name to file name conversion.
3This was taken form the UFO 3 spec.
4"""
5from __future__ import absolute_import, unicode_literals
6from fontTools.misc.py23 import basestring, unicode
7
8
9illegalCharacters = "\" * + / : < > ? [ \ ] | \0".split(" ")
10illegalCharacters += [chr(i) for i in range(1, 32)]
11illegalCharacters += [chr(0x7F)]
12reservedFileNames = "CON PRN AUX CLOCK$ NUL A:-Z: COM1".lower().split(" ")
13reservedFileNames += "LPT1 LPT2 LPT3 COM2 COM3 COM4".lower().split(" ")
14maxFileNameLength = 255
15
16
17class NameTranslationError(Exception):
18	pass
19
20
21def userNameToFileName(userName, existing=[], prefix="", suffix=""):
22	"""
23	existing should be a case-insensitive list
24	of all existing file names.
25
26	>>> userNameToFileName("a") == "a"
27	True
28	>>> userNameToFileName("A") == "A_"
29	True
30	>>> userNameToFileName("AE") == "A_E_"
31	True
32	>>> userNameToFileName("Ae") == "A_e"
33	True
34	>>> userNameToFileName("ae") == "ae"
35	True
36	>>> userNameToFileName("aE") == "aE_"
37	True
38	>>> userNameToFileName("a.alt") == "a.alt"
39	True
40	>>> userNameToFileName("A.alt") == "A_.alt"
41	True
42	>>> userNameToFileName("A.Alt") == "A_.A_lt"
43	True
44	>>> userNameToFileName("A.aLt") == "A_.aL_t"
45	True
46	>>> userNameToFileName(u"A.alT") == "A_.alT_"
47	True
48	>>> userNameToFileName("T_H") == "T__H_"
49	True
50	>>> userNameToFileName("T_h") == "T__h"
51	True
52	>>> userNameToFileName("t_h") == "t_h"
53	True
54	>>> userNameToFileName("F_F_I") == "F__F__I_"
55	True
56	>>> userNameToFileName("f_f_i") == "f_f_i"
57	True
58	>>> userNameToFileName("Aacute_V.swash") == "A_acute_V_.swash"
59	True
60	>>> userNameToFileName(".notdef") == "_notdef"
61	True
62	>>> userNameToFileName("con") == "_con"
63	True
64	>>> userNameToFileName("CON") == "C_O_N_"
65	True
66	>>> userNameToFileName("con.alt") == "_con.alt"
67	True
68	>>> userNameToFileName("alt.con") == "alt._con"
69	True
70	"""
71	# the incoming name must be a unicode string
72	if not isinstance(userName, unicode):
73		raise ValueError("The value for userName must be a unicode string.")
74	# establish the prefix and suffix lengths
75	prefixLength = len(prefix)
76	suffixLength = len(suffix)
77	# replace an initial period with an _
78	# if no prefix is to be added
79	if not prefix and userName[0] == ".":
80		userName = "_" + userName[1:]
81	# filter the user name
82	filteredUserName = []
83	for character in userName:
84		# replace illegal characters with _
85		if character in illegalCharacters:
86			character = "_"
87		# add _ to all non-lower characters
88		elif character != character.lower():
89			character += "_"
90		filteredUserName.append(character)
91	userName = "".join(filteredUserName)
92	# clip to 255
93	sliceLength = maxFileNameLength - prefixLength - suffixLength
94	userName = userName[:sliceLength]
95	# test for illegal files names
96	parts = []
97	for part in userName.split("."):
98		if part.lower() in reservedFileNames:
99			part = "_" + part
100		parts.append(part)
101	userName = ".".join(parts)
102	# test for clash
103	fullName = prefix + userName + suffix
104	if fullName.lower() in existing:
105		fullName = handleClash1(userName, existing, prefix, suffix)
106	# finished
107	return fullName
108
109def handleClash1(userName, existing=[], prefix="", suffix=""):
110	"""
111	existing should be a case-insensitive list
112	of all existing file names.
113
114	>>> prefix = ("0" * 5) + "."
115	>>> suffix = "." + ("0" * 10)
116	>>> existing = ["a" * 5]
117
118	>>> e = list(existing)
119	>>> handleClash1(userName="A" * 5, existing=e,
120	...		prefix=prefix, suffix=suffix) == (
121	... 	'00000.AAAAA000000000000001.0000000000')
122	True
123
124	>>> e = list(existing)
125	>>> e.append(prefix + "aaaaa" + "1".zfill(15) + suffix)
126	>>> handleClash1(userName="A" * 5, existing=e,
127	...		prefix=prefix, suffix=suffix) == (
128	... 	'00000.AAAAA000000000000002.0000000000')
129	True
130
131	>>> e = list(existing)
132	>>> e.append(prefix + "AAAAA" + "2".zfill(15) + suffix)
133	>>> handleClash1(userName="A" * 5, existing=e,
134	...		prefix=prefix, suffix=suffix) == (
135	... 	'00000.AAAAA000000000000001.0000000000')
136	True
137	"""
138	# if the prefix length + user name length + suffix length + 15 is at
139	# or past the maximum length, silce 15 characters off of the user name
140	prefixLength = len(prefix)
141	suffixLength = len(suffix)
142	if prefixLength + len(userName) + suffixLength + 15 > maxFileNameLength:
143		l = (prefixLength + len(userName) + suffixLength + 15)
144		sliceLength = maxFileNameLength - l
145		userName = userName[:sliceLength]
146	finalName = None
147	# try to add numbers to create a unique name
148	counter = 1
149	while finalName is None:
150		name = userName + str(counter).zfill(15)
151		fullName = prefix + name + suffix
152		if fullName.lower() not in existing:
153			finalName = fullName
154			break
155		else:
156			counter += 1
157		if counter >= 999999999999999:
158			break
159	# if there is a clash, go to the next fallback
160	if finalName is None:
161		finalName = handleClash2(existing, prefix, suffix)
162	# finished
163	return finalName
164
165def handleClash2(existing=[], prefix="", suffix=""):
166	"""
167	existing should be a case-insensitive list
168	of all existing file names.
169
170	>>> prefix = ("0" * 5) + "."
171	>>> suffix = "." + ("0" * 10)
172	>>> existing = [prefix + str(i) + suffix for i in range(100)]
173
174	>>> e = list(existing)
175	>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
176	... 	'00000.100.0000000000')
177	True
178
179	>>> e = list(existing)
180	>>> e.remove(prefix + "1" + suffix)
181	>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
182	... 	'00000.1.0000000000')
183	True
184
185	>>> e = list(existing)
186	>>> e.remove(prefix + "2" + suffix)
187	>>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
188	... 	'00000.2.0000000000')
189	True
190	"""
191	# calculate the longest possible string
192	maxLength = maxFileNameLength - len(prefix) - len(suffix)
193	maxValue = int("9" * maxLength)
194	# try to find a number
195	finalName = None
196	counter = 1
197	while finalName is None:
198		fullName = prefix + str(counter) + suffix
199		if fullName.lower() not in existing:
200			finalName = fullName
201			break
202		else:
203			counter += 1
204		if counter >= maxValue:
205			break
206	# raise an error if nothing has been found
207	if finalName is None:
208		raise NameTranslationError("No unique name could be found.")
209	# finished
210	return finalName
211
212if __name__ == "__main__":
213	import doctest
214	doctest.testmod()
215