string.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. """A collection of string constants.
  2. Public module variables:
  3. whitespace -- a string containing all ASCII whitespace
  4. ascii_lowercase -- a string containing all ASCII lowercase letters
  5. ascii_uppercase -- a string containing all ASCII uppercase letters
  6. ascii_letters -- a string containing all ASCII letters
  7. digits -- a string containing all ASCII decimal digits
  8. hexdigits -- a string containing all ASCII hexadecimal digits
  9. octdigits -- a string containing all ASCII octal digits
  10. punctuation -- a string containing all ASCII punctuation characters
  11. printable -- a string containing all ASCII characters considered printable
  12. """
  13. __all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords",
  14. "digits", "hexdigits", "octdigits", "printable", "punctuation",
  15. "whitespace", "Formatter", "Template"]
  16. import _string
  17. # Some strings for ctype-style character classification
  18. whitespace = ' \t\n\r\v\f'
  19. ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
  20. ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  21. ascii_letters = ascii_lowercase + ascii_uppercase
  22. digits = '0123456789'
  23. hexdigits = digits + 'abcdef' + 'ABCDEF'
  24. octdigits = '01234567'
  25. punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
  26. printable = digits + ascii_letters + punctuation + whitespace
  27. # Functions which aren't available as string methods.
  28. # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
  29. def capwords(s, sep=None):
  30. """capwords(s [,sep]) -> string
  31. Split the argument into words using split, capitalize each
  32. word using capitalize, and join the capitalized words using
  33. join. If the optional second argument sep is absent or None,
  34. runs of whitespace characters are replaced by a single space
  35. and leading and trailing whitespace are removed, otherwise
  36. sep is used to split and join the words.
  37. """
  38. return (sep or ' ').join(map(str.capitalize, s.split(sep)))
  39. ####################################################################
  40. import re as _re
  41. from collections import ChainMap as _ChainMap
  42. _sentinel_dict = {}
  43. class Template:
  44. """A string class for supporting $-substitutions."""
  45. delimiter = '$'
  46. # r'[a-z]' matches to non-ASCII letters when used with IGNORECASE, but
  47. # without the ASCII flag. We can't add re.ASCII to flags because of
  48. # backward compatibility. So we use the ?a local flag and [a-z] pattern.
  49. # See https://bugs.python.org/issue31672
  50. idpattern = r'(?a:[_a-z][_a-z0-9]*)'
  51. braceidpattern = None
  52. flags = _re.IGNORECASE
  53. def __init_subclass__(cls):
  54. super().__init_subclass__()
  55. if 'pattern' in cls.__dict__:
  56. pattern = cls.pattern
  57. else:
  58. delim = _re.escape(cls.delimiter)
  59. id = cls.idpattern
  60. bid = cls.braceidpattern or cls.idpattern
  61. pattern = fr"""
  62. {delim}(?:
  63. (?P<escaped>{delim}) | # Escape sequence of two delimiters
  64. (?P<named>{id}) | # delimiter and a Python identifier
  65. {{(?P<braced>{bid})}} | # delimiter and a braced identifier
  66. (?P<invalid>) # Other ill-formed delimiter exprs
  67. )
  68. """
  69. cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
  70. def __init__(self, template):
  71. self.template = template
  72. # Search for $$, $identifier, ${identifier}, and any bare $'s
  73. def _invalid(self, mo):
  74. i = mo.start('invalid')
  75. lines = self.template[:i].splitlines(keepends=True)
  76. if not lines:
  77. colno = 1
  78. lineno = 1
  79. else:
  80. colno = i - len(''.join(lines[:-1]))
  81. lineno = len(lines)
  82. raise ValueError('Invalid placeholder in string: line %d, col %d' %
  83. (lineno, colno))
  84. def substitute(self, mapping=_sentinel_dict, /, **kws):
  85. if mapping is _sentinel_dict:
  86. mapping = kws
  87. elif kws:
  88. mapping = _ChainMap(kws, mapping)
  89. # Helper function for .sub()
  90. def convert(mo):
  91. # Check the most common path first.
  92. named = mo.group('named') or mo.group('braced')
  93. if named is not None:
  94. return str(mapping[named])
  95. if mo.group('escaped') is not None:
  96. return self.delimiter
  97. if mo.group('invalid') is not None:
  98. self._invalid(mo)
  99. raise ValueError('Unrecognized named group in pattern',
  100. self.pattern)
  101. return self.pattern.sub(convert, self.template)
  102. def safe_substitute(self, mapping=_sentinel_dict, /, **kws):
  103. if mapping is _sentinel_dict:
  104. mapping = kws
  105. elif kws:
  106. mapping = _ChainMap(kws, mapping)
  107. # Helper function for .sub()
  108. def convert(mo):
  109. named = mo.group('named') or mo.group('braced')
  110. if named is not None:
  111. try:
  112. return str(mapping[named])
  113. except KeyError:
  114. return mo.group()
  115. if mo.group('escaped') is not None:
  116. return self.delimiter
  117. if mo.group('invalid') is not None:
  118. return mo.group()
  119. raise ValueError('Unrecognized named group in pattern',
  120. self.pattern)
  121. return self.pattern.sub(convert, self.template)
  122. def is_valid(self):
  123. for mo in self.pattern.finditer(self.template):
  124. if mo.group('invalid') is not None:
  125. return False
  126. if (mo.group('named') is None
  127. and mo.group('braced') is None
  128. and mo.group('escaped') is None):
  129. # If all the groups are None, there must be
  130. # another group we're not expecting
  131. raise ValueError('Unrecognized named group in pattern',
  132. self.pattern)
  133. return True
  134. def get_identifiers(self):
  135. ids = []
  136. for mo in self.pattern.finditer(self.template):
  137. named = mo.group('named') or mo.group('braced')
  138. if named is not None and named not in ids:
  139. # add a named group only the first time it appears
  140. ids.append(named)
  141. elif (named is None
  142. and mo.group('invalid') is None
  143. and mo.group('escaped') is None):
  144. # If all the groups are None, there must be
  145. # another group we're not expecting
  146. raise ValueError('Unrecognized named group in pattern',
  147. self.pattern)
  148. return ids
  149. # Initialize Template.pattern. __init_subclass__() is automatically called
  150. # only for subclasses, not for the Template class itself.
  151. Template.__init_subclass__()
  152. ########################################################################
  153. # the Formatter class
  154. # see PEP 3101 for details and purpose of this class
  155. # The hard parts are reused from the C implementation. They're exposed as "_"
  156. # prefixed methods of str.
  157. # The overall parser is implemented in _string.formatter_parser.
  158. # The field name parser is implemented in _string.formatter_field_name_split
  159. class Formatter:
  160. def format(self, format_string, /, *args, **kwargs):
  161. return self.vformat(format_string, args, kwargs)
  162. def vformat(self, format_string, args, kwargs):
  163. used_args = set()
  164. result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
  165. self.check_unused_args(used_args, args, kwargs)
  166. return result
  167. def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
  168. auto_arg_index=0):
  169. if recursion_depth < 0:
  170. raise ValueError('Max string recursion exceeded')
  171. result = []
  172. for literal_text, field_name, format_spec, conversion in \
  173. self.parse(format_string):
  174. # output the literal text
  175. if literal_text:
  176. result.append(literal_text)
  177. # if there's a field, output it
  178. if field_name is not None:
  179. # this is some markup, find the object and do
  180. # the formatting
  181. # handle arg indexing when empty field_names are given.
  182. if field_name == '':
  183. if auto_arg_index is False:
  184. raise ValueError('cannot switch from manual field '
  185. 'specification to automatic field '
  186. 'numbering')
  187. field_name = str(auto_arg_index)
  188. auto_arg_index += 1
  189. elif field_name.isdigit():
  190. if auto_arg_index:
  191. raise ValueError('cannot switch from manual field '
  192. 'specification to automatic field '
  193. 'numbering')
  194. # disable auto arg incrementing, if it gets
  195. # used later on, then an exception will be raised
  196. auto_arg_index = False
  197. # given the field_name, find the object it references
  198. # and the argument it came from
  199. obj, arg_used = self.get_field(field_name, args, kwargs)
  200. used_args.add(arg_used)
  201. # do any conversion on the resulting object
  202. obj = self.convert_field(obj, conversion)
  203. # expand the format spec, if needed
  204. format_spec, auto_arg_index = self._vformat(
  205. format_spec, args, kwargs,
  206. used_args, recursion_depth-1,
  207. auto_arg_index=auto_arg_index)
  208. # format the object and append to the result
  209. result.append(self.format_field(obj, format_spec))
  210. return ''.join(result), auto_arg_index
  211. def get_value(self, key, args, kwargs):
  212. if isinstance(key, int):
  213. return args[key]
  214. else:
  215. return kwargs[key]
  216. def check_unused_args(self, used_args, args, kwargs):
  217. pass
  218. def format_field(self, value, format_spec):
  219. return format(value, format_spec)
  220. def convert_field(self, value, conversion):
  221. # do any conversion on the resulting object
  222. if conversion is None:
  223. return value
  224. elif conversion == 's':
  225. return str(value)
  226. elif conversion == 'r':
  227. return repr(value)
  228. elif conversion == 'a':
  229. return ascii(value)
  230. raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
  231. # returns an iterable that contains tuples of the form:
  232. # (literal_text, field_name, format_spec, conversion)
  233. # literal_text can be zero length
  234. # field_name can be None, in which case there's no
  235. # object to format and output
  236. # if field_name is not None, it is looked up, formatted
  237. # with format_spec and conversion and then used
  238. def parse(self, format_string):
  239. return _string.formatter_parser(format_string)
  240. # given a field_name, find the object it references.
  241. # field_name: the field being looked up, e.g. "0.name"
  242. # or "lookup[3]"
  243. # used_args: a set of which args have been used
  244. # args, kwargs: as passed in to vformat
  245. def get_field(self, field_name, args, kwargs):
  246. first, rest = _string.formatter_field_name_split(field_name)
  247. obj = self.get_value(first, args, kwargs)
  248. # loop through the rest of the field_name, doing
  249. # getattr or getitem as needed
  250. for is_attr, i in rest:
  251. if is_attr:
  252. obj = getattr(obj, i)
  253. else:
  254. obj = obj[i]
  255. return obj, first