Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1 | """A collection of string constants. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 2 | |
| 3 | Public module variables: |
| 4 | |
Georg Brandl | 5076740 | 2008-11-22 08:31:09 +0000 | [diff] [blame] | 5 | whitespace -- a string containing all ASCII whitespace |
| 6 | ascii_lowercase -- a string containing all ASCII lowercase letters |
| 7 | ascii_uppercase -- a string containing all ASCII uppercase letters |
| 8 | ascii_letters -- a string containing all ASCII letters |
| 9 | digits -- a string containing all ASCII decimal digits |
| 10 | hexdigits -- a string containing all ASCII hexadecimal digits |
| 11 | octdigits -- a string containing all ASCII octal digits |
| 12 | punctuation -- a string containing all ASCII punctuation characters |
| 13 | printable -- a string containing all ASCII characters considered printable |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 14 | |
| 15 | """ |
| 16 | |
Zachary Ware | c17a0b8 | 2016-06-04 14:35:05 -0500 | [diff] [blame] | 17 | __all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords", |
| 18 | "digits", "hexdigits", "octdigits", "printable", "punctuation", |
| 19 | "whitespace", "Formatter", "Template"] |
| 20 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 21 | import _string |
| 22 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 23 | # Some strings for ctype-style character classification |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 24 | whitespace = ' \t\n\r\v\f' |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 25 | ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 26 | ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
Fred Drake | 960fdf9 | 2001-07-20 18:38:26 +0000 | [diff] [blame] | 27 | ascii_letters = ascii_lowercase + ascii_uppercase |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 28 | digits = '0123456789' |
| 29 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 30 | octdigits = '01234567' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 31 | punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 32 | printable = digits + ascii_letters + punctuation + whitespace |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 34 | # Functions which aren't available as string methods. |
| 35 | |
| 36 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 37 | def capwords(s, sep=None): |
Ezio Melotti | a40bdda | 2009-09-26 12:33:22 +0000 | [diff] [blame] | 38 | """capwords(s [,sep]) -> string |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 39 | |
| 40 | Split the argument into words using split, capitalize each |
| 41 | word using capitalize, and join the capitalized words using |
Ezio Melotti | a40bdda | 2009-09-26 12:33:22 +0000 | [diff] [blame] | 42 | join. If the optional second argument sep is absent or None, |
| 43 | runs of whitespace characters are replaced by a single space |
| 44 | and leading and trailing whitespace are removed, otherwise |
| 45 | sep is used to split and join the words. |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 46 | |
| 47 | """ |
Ezio Melotti | a40bdda | 2009-09-26 12:33:22 +0000 | [diff] [blame] | 48 | return (sep or ' ').join(x.capitalize() for x in s.split(sep)) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 49 | |
| 50 | |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 51 | #################################################################### |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 52 | import re as _re |
Zachary Ware | c17a0b8 | 2016-06-04 14:35:05 -0500 | [diff] [blame] | 53 | from collections import ChainMap as _ChainMap |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 55 | class _TemplateMetaclass(type): |
| 56 | pattern = r""" |
Raymond Hettinger | 55593c3 | 2004-09-26 18:56:44 +0000 | [diff] [blame] | 57 | %(delim)s(?: |
| 58 | (?P<escaped>%(delim)s) | # Escape sequence of two delimiters |
| 59 | (?P<named>%(id)s) | # delimiter and a Python identifier |
Barry Warsaw | ba42796 | 2017-09-04 16:32:10 -0400 | [diff] [blame] | 60 | {(?P<braced>%(bid)s)} | # delimiter and a braced identifier |
Raymond Hettinger | 55593c3 | 2004-09-26 18:56:44 +0000 | [diff] [blame] | 61 | (?P<invalid>) # Other ill-formed delimiter exprs |
| 62 | ) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 63 | """ |
| 64 | |
| 65 | def __init__(cls, name, bases, dct): |
| 66 | super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
| 67 | if 'pattern' in dct: |
| 68 | pattern = cls.pattern |
| 69 | else: |
| 70 | pattern = _TemplateMetaclass.pattern % { |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 71 | 'delim' : _re.escape(cls.delimiter), |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 72 | 'id' : cls.idpattern, |
Barry Warsaw | ba42796 | 2017-09-04 16:32:10 -0400 | [diff] [blame] | 73 | 'bid' : cls.braceidpattern or cls.idpattern, |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 74 | } |
Georg Brandl | 056cb93 | 2010-07-29 17:16:10 +0000 | [diff] [blame] | 75 | cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 76 | |
| 77 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 78 | class Template(metaclass=_TemplateMetaclass): |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 79 | """A string class for supporting $-substitutions.""" |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 81 | delimiter = '$' |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 82 | idpattern = r'[_a-z][_a-z0-9]*' |
Barry Warsaw | ba42796 | 2017-09-04 16:32:10 -0400 | [diff] [blame] | 83 | braceidpattern = None |
Georg Brandl | 056cb93 | 2010-07-29 17:16:10 +0000 | [diff] [blame] | 84 | flags = _re.IGNORECASE |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 85 | |
| 86 | def __init__(self, template): |
| 87 | self.template = template |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 88 | |
| 89 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 90 | |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 91 | def _invalid(self, mo): |
| 92 | i = mo.start('invalid') |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 93 | lines = self.template[:i].splitlines(keepends=True) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 94 | if not lines: |
| 95 | colno = 1 |
| 96 | lineno = 1 |
| 97 | else: |
| 98 | colno = i - len(''.join(lines[:-1])) |
| 99 | lineno = len(lines) |
| 100 | raise ValueError('Invalid placeholder in string: line %d, col %d' % |
| 101 | (lineno, colno)) |
| 102 | |
Serhiy Storchaka | 8ffe917 | 2015-03-24 22:28:43 +0200 | [diff] [blame] | 103 | def substitute(*args, **kws): |
| 104 | if not args: |
| 105 | raise TypeError("descriptor 'substitute' of 'Template' object " |
| 106 | "needs an argument") |
| 107 | self, *args = args # allow the "self" keyword be passed |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 108 | if len(args) > 1: |
| 109 | raise TypeError('Too many positional arguments') |
| 110 | if not args: |
| 111 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 112 | elif kws: |
Zachary Ware | c17a0b8 | 2016-06-04 14:35:05 -0500 | [diff] [blame] | 113 | mapping = _ChainMap(kws, args[0]) |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 114 | else: |
| 115 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 116 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 117 | def convert(mo): |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 118 | # Check the most common path first. |
| 119 | named = mo.group('named') or mo.group('braced') |
| 120 | if named is not None: |
Serhiy Storchaka | 6e6883f | 2015-05-28 20:45:29 +0300 | [diff] [blame] | 121 | return str(mapping[named]) |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 122 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 123 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 124 | if mo.group('invalid') is not None: |
| 125 | self._invalid(mo) |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 126 | raise ValueError('Unrecognized named group in pattern', |
| 127 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 128 | return self.pattern.sub(convert, self.template) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 129 | |
Serhiy Storchaka | 8ffe917 | 2015-03-24 22:28:43 +0200 | [diff] [blame] | 130 | def safe_substitute(*args, **kws): |
| 131 | if not args: |
| 132 | raise TypeError("descriptor 'safe_substitute' of 'Template' object " |
| 133 | "needs an argument") |
| 134 | self, *args = args # allow the "self" keyword be passed |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 135 | if len(args) > 1: |
| 136 | raise TypeError('Too many positional arguments') |
| 137 | if not args: |
| 138 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 139 | elif kws: |
Zachary Ware | c17a0b8 | 2016-06-04 14:35:05 -0500 | [diff] [blame] | 140 | mapping = _ChainMap(kws, args[0]) |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 141 | else: |
| 142 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 143 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 144 | def convert(mo): |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 145 | named = mo.group('named') or mo.group('braced') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 146 | if named is not None: |
| 147 | try: |
Serhiy Storchaka | 6e6883f | 2015-05-28 20:45:29 +0300 | [diff] [blame] | 148 | return str(mapping[named]) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 149 | except KeyError: |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 150 | return mo.group() |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 151 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 152 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 153 | if mo.group('invalid') is not None: |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 154 | return mo.group() |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 155 | raise ValueError('Unrecognized named group in pattern', |
| 156 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 157 | return self.pattern.sub(convert, self.template) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | |
| 161 | ######################################################################## |
| 162 | # the Formatter class |
| 163 | # see PEP 3101 for details and purpose of this class |
| 164 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 165 | # The hard parts are reused from the C implementation. They're exposed as "_" |
Florent Xicluna | 7b2a771 | 2010-09-06 20:27:55 +0000 | [diff] [blame] | 166 | # prefixed methods of str. |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 168 | # The overall parser is implemented in _string.formatter_parser. |
| 169 | # The field name parser is implemented in _string.formatter_field_name_split |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 170 | |
| 171 | class Formatter: |
Serhiy Storchaka | 8ffe917 | 2015-03-24 22:28:43 +0200 | [diff] [blame] | 172 | def format(*args, **kwargs): |
| 173 | if not args: |
| 174 | raise TypeError("descriptor 'format' of 'Formatter' object " |
| 175 | "needs an argument") |
| 176 | self, *args = args # allow the "self" keyword be passed |
| 177 | try: |
| 178 | format_string, *args = args # allow the "format_string" keyword be passed |
| 179 | except ValueError: |
Serhiy Storchaka | 009b0a1 | 2017-01-13 09:10:51 +0200 | [diff] [blame] | 180 | raise TypeError("format() missing 1 required positional " |
| 181 | "argument: 'format_string'") from None |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 182 | return self.vformat(format_string, args, kwargs) |
| 183 | |
| 184 | def vformat(self, format_string, args, kwargs): |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 185 | used_args = set() |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 186 | result, _ = self._vformat(format_string, args, kwargs, used_args, 2) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 187 | self.check_unused_args(used_args, args, kwargs) |
| 188 | return result |
| 189 | |
Eric V. Smith | 7ce9074 | 2014-04-14 16:43:50 -0400 | [diff] [blame] | 190 | def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, |
| 191 | auto_arg_index=0): |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 192 | if recursion_depth < 0: |
| 193 | raise ValueError('Max string recursion exceeded') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 194 | result = [] |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 195 | for literal_text, field_name, format_spec, conversion in \ |
| 196 | self.parse(format_string): |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 197 | |
| 198 | # output the literal text |
| 199 | if literal_text: |
| 200 | result.append(literal_text) |
| 201 | |
| 202 | # if there's a field, output it |
| 203 | if field_name is not None: |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 204 | # this is some markup, find the object and do |
| 205 | # the formatting |
| 206 | |
Eric V. Smith | 7ce9074 | 2014-04-14 16:43:50 -0400 | [diff] [blame] | 207 | # handle arg indexing when empty field_names are given. |
| 208 | if field_name == '': |
| 209 | if auto_arg_index is False: |
| 210 | raise ValueError('cannot switch from manual field ' |
| 211 | 'specification to automatic field ' |
| 212 | 'numbering') |
| 213 | field_name = str(auto_arg_index) |
| 214 | auto_arg_index += 1 |
| 215 | elif field_name.isdigit(): |
| 216 | if auto_arg_index: |
| 217 | raise ValueError('cannot switch from manual field ' |
| 218 | 'specification to automatic field ' |
| 219 | 'numbering') |
| 220 | # disable auto arg incrementing, if it gets |
| 221 | # used later on, then an exception will be raised |
| 222 | auto_arg_index = False |
| 223 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 224 | # given the field_name, find the object it references |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 225 | # and the argument it came from |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 226 | obj, arg_used = self.get_field(field_name, args, kwargs) |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 227 | used_args.add(arg_used) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 228 | |
| 229 | # do any conversion on the resulting object |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 230 | obj = self.convert_field(obj, conversion) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 231 | |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 232 | # expand the format spec, if needed |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 233 | format_spec, auto_arg_index = self._vformat( |
| 234 | format_spec, args, kwargs, |
| 235 | used_args, recursion_depth-1, |
| 236 | auto_arg_index=auto_arg_index) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 237 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 238 | # format the object and append to the result |
| 239 | result.append(self.format_field(obj, format_spec)) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 240 | |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 241 | return ''.join(result), auto_arg_index |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 242 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 243 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 244 | def get_value(self, key, args, kwargs): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 245 | if isinstance(key, int): |
| 246 | return args[key] |
| 247 | else: |
| 248 | return kwargs[key] |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 249 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 250 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 251 | def check_unused_args(self, used_args, args, kwargs): |
| 252 | pass |
| 253 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 254 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 255 | def format_field(self, value, format_spec): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 256 | return format(value, format_spec) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 257 | |
| 258 | |
| 259 | def convert_field(self, value, conversion): |
| 260 | # do any conversion on the resulting object |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 261 | if conversion is None: |
| 262 | return value |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 263 | elif conversion == 's': |
| 264 | return str(value) |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 265 | elif conversion == 'r': |
| 266 | return repr(value) |
| 267 | elif conversion == 'a': |
| 268 | return ascii(value) |
Florent Xicluna | 7b2a771 | 2010-09-06 20:27:55 +0000 | [diff] [blame] | 269 | raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | # returns an iterable that contains tuples of the form: |
| 273 | # (literal_text, field_name, format_spec, conversion) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 274 | # literal_text can be zero length |
| 275 | # field_name can be None, in which case there's no |
| 276 | # object to format and output |
| 277 | # if field_name is not None, it is looked up, formatted |
| 278 | # with format_spec and conversion and then used |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 279 | def parse(self, format_string): |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 280 | return _string.formatter_parser(format_string) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 281 | |
| 282 | |
| 283 | # given a field_name, find the object it references. |
| 284 | # field_name: the field being looked up, e.g. "0.name" |
| 285 | # or "lookup[3]" |
| 286 | # used_args: a set of which args have been used |
| 287 | # args, kwargs: as passed in to vformat |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 288 | def get_field(self, field_name, args, kwargs): |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 289 | first, rest = _string.formatter_field_name_split(field_name) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 290 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 291 | obj = self.get_value(first, args, kwargs) |
| 292 | |
| 293 | # loop through the rest of the field_name, doing |
| 294 | # getattr or getitem as needed |
| 295 | for is_attr, i in rest: |
| 296 | if is_attr: |
| 297 | obj = getattr(obj, i) |
| 298 | else: |
| 299 | obj = obj[i] |
| 300 | |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 301 | return obj, first |