filenames.py 8.0 KB

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