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 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 55 | _sentinel_dict = {} |
| 56 | |
Serhiy Storchaka | 919f0bc | 2019-10-21 09:36:21 +0300 | [diff] [blame] | 57 | class Template: |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 58 | """A string class for supporting $-substitutions.""" |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 59 | |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 60 | delimiter = '$' |
Barry Warsaw | e256b40 | 2017-11-21 10:28:13 -0500 | [diff] [blame] | 61 | # r'[a-z]' matches to non-ASCII letters when used with IGNORECASE, but |
| 62 | # without the ASCII flag. We can't add re.ASCII to flags because of |
| 63 | # backward compatibility. So we use the ?a local flag and [a-z] pattern. |
INADA Naoki | b22273e | 2017-10-13 16:02:23 +0900 | [diff] [blame] | 64 | # See https://bugs.python.org/issue31672 |
Serhiy Storchaka | 87be28f | 2018-01-04 19:20:11 +0200 | [diff] [blame] | 65 | idpattern = r'(?a:[_a-z][_a-z0-9]*)' |
Barry Warsaw | ba42796 | 2017-09-04 16:32:10 -0400 | [diff] [blame] | 66 | braceidpattern = None |
Georg Brandl | 056cb93 | 2010-07-29 17:16:10 +0000 | [diff] [blame] | 67 | flags = _re.IGNORECASE |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 68 | |
Serhiy Storchaka | 919f0bc | 2019-10-21 09:36:21 +0300 | [diff] [blame] | 69 | def __init_subclass__(cls): |
| 70 | super().__init_subclass__() |
| 71 | if 'pattern' in cls.__dict__: |
| 72 | pattern = cls.pattern |
| 73 | else: |
| 74 | delim = _re.escape(cls.delimiter) |
| 75 | id = cls.idpattern |
| 76 | bid = cls.braceidpattern or cls.idpattern |
| 77 | pattern = fr""" |
| 78 | {delim}(?: |
| 79 | (?P<escaped>{delim}) | # Escape sequence of two delimiters |
| 80 | (?P<named>{id}) | # delimiter and a Python identifier |
| 81 | {{(?P<braced>{bid})}} | # delimiter and a braced identifier |
| 82 | (?P<invalid>) # Other ill-formed delimiter exprs |
| 83 | ) |
| 84 | """ |
| 85 | cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE) |
| 86 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 87 | def __init__(self, template): |
| 88 | self.template = template |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 89 | |
| 90 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 91 | |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 92 | def _invalid(self, mo): |
| 93 | i = mo.start('invalid') |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 94 | lines = self.template[:i].splitlines(keepends=True) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 95 | if not lines: |
| 96 | colno = 1 |
| 97 | lineno = 1 |
| 98 | else: |
| 99 | colno = i - len(''.join(lines[:-1])) |
| 100 | lineno = len(lines) |
| 101 | raise ValueError('Invalid placeholder in string: line %d, col %d' % |
| 102 | (lineno, colno)) |
| 103 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 104 | def substitute(self, mapping=_sentinel_dict, /, **kws): |
| 105 | if mapping is _sentinel_dict: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 106 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 107 | elif kws: |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 108 | mapping = _ChainMap(kws, mapping) |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 109 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 110 | def convert(mo): |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 111 | # Check the most common path first. |
| 112 | named = mo.group('named') or mo.group('braced') |
| 113 | if named is not None: |
Serhiy Storchaka | 6e6883f | 2015-05-28 20:45:29 +0300 | [diff] [blame] | 114 | return str(mapping[named]) |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 115 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 116 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 117 | if mo.group('invalid') is not None: |
| 118 | self._invalid(mo) |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 119 | raise ValueError('Unrecognized named group in pattern', |
| 120 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 121 | return self.pattern.sub(convert, self.template) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 122 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 123 | def safe_substitute(self, mapping=_sentinel_dict, /, **kws): |
| 124 | if mapping is _sentinel_dict: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 125 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 126 | elif kws: |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 127 | mapping = _ChainMap(kws, mapping) |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 128 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 129 | def convert(mo): |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 130 | named = mo.group('named') or mo.group('braced') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 131 | if named is not None: |
| 132 | try: |
Serhiy Storchaka | 6e6883f | 2015-05-28 20:45:29 +0300 | [diff] [blame] | 133 | return str(mapping[named]) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 134 | except KeyError: |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 135 | return mo.group() |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 136 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 137 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 138 | if mo.group('invalid') is not None: |
Florent Xicluna | eb19dce | 2010-09-18 23:34:07 +0000 | [diff] [blame] | 139 | return mo.group() |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 140 | raise ValueError('Unrecognized named group in pattern', |
| 141 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 142 | return self.pattern.sub(convert, self.template) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 143 | |
Serhiy Storchaka | 919f0bc | 2019-10-21 09:36:21 +0300 | [diff] [blame] | 144 | # Initialize Template.pattern. __init_subclass__() is automatically called |
| 145 | # only for subclasses, not for the Template class itself. |
| 146 | Template.__init_subclass__() |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | ######################################################################## |
| 150 | # the Formatter class |
| 151 | # see PEP 3101 for details and purpose of this class |
| 152 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 153 | # 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] | 154 | # prefixed methods of str. |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 155 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 156 | # The overall parser is implemented in _string.formatter_parser. |
| 157 | # The field name parser is implemented in _string.formatter_field_name_split |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 158 | |
| 159 | class Formatter: |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 160 | def format(self, format_string, /, *args, **kwargs): |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 161 | return self.vformat(format_string, args, kwargs) |
| 162 | |
| 163 | def vformat(self, format_string, args, kwargs): |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 164 | used_args = set() |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 165 | result, _ = self._vformat(format_string, args, kwargs, used_args, 2) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 166 | self.check_unused_args(used_args, args, kwargs) |
| 167 | return result |
| 168 | |
Eric V. Smith | 7ce9074 | 2014-04-14 16:43:50 -0400 | [diff] [blame] | 169 | def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, |
| 170 | auto_arg_index=0): |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 171 | if recursion_depth < 0: |
| 172 | raise ValueError('Max string recursion exceeded') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 173 | result = [] |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 174 | for literal_text, field_name, format_spec, conversion in \ |
| 175 | self.parse(format_string): |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 176 | |
| 177 | # output the literal text |
| 178 | if literal_text: |
| 179 | result.append(literal_text) |
| 180 | |
| 181 | # if there's a field, output it |
| 182 | if field_name is not None: |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 183 | # this is some markup, find the object and do |
| 184 | # the formatting |
| 185 | |
Eric V. Smith | 7ce9074 | 2014-04-14 16:43:50 -0400 | [diff] [blame] | 186 | # handle arg indexing when empty field_names are given. |
| 187 | if field_name == '': |
| 188 | if auto_arg_index is False: |
| 189 | raise ValueError('cannot switch from manual field ' |
| 190 | 'specification to automatic field ' |
| 191 | 'numbering') |
| 192 | field_name = str(auto_arg_index) |
| 193 | auto_arg_index += 1 |
| 194 | elif field_name.isdigit(): |
| 195 | if auto_arg_index: |
| 196 | raise ValueError('cannot switch from manual field ' |
| 197 | 'specification to automatic field ' |
| 198 | 'numbering') |
| 199 | # disable auto arg incrementing, if it gets |
| 200 | # used later on, then an exception will be raised |
| 201 | auto_arg_index = False |
| 202 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 203 | # given the field_name, find the object it references |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 204 | # and the argument it came from |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 205 | obj, arg_used = self.get_field(field_name, args, kwargs) |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 206 | used_args.add(arg_used) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 207 | |
| 208 | # do any conversion on the resulting object |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 209 | obj = self.convert_field(obj, conversion) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 210 | |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 211 | # expand the format spec, if needed |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 212 | format_spec, auto_arg_index = self._vformat( |
| 213 | format_spec, args, kwargs, |
| 214 | used_args, recursion_depth-1, |
| 215 | auto_arg_index=auto_arg_index) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 216 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 217 | # format the object and append to the result |
| 218 | result.append(self.format_field(obj, format_spec)) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 219 | |
Eric V. Smith | 85976b1 | 2015-09-29 10:27:38 -0400 | [diff] [blame] | 220 | return ''.join(result), auto_arg_index |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 221 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 222 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 223 | def get_value(self, key, args, kwargs): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 224 | if isinstance(key, int): |
| 225 | return args[key] |
| 226 | else: |
| 227 | return kwargs[key] |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 228 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 229 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 230 | def check_unused_args(self, used_args, args, kwargs): |
| 231 | pass |
| 232 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 233 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 234 | def format_field(self, value, format_spec): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 235 | return format(value, format_spec) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | def convert_field(self, value, conversion): |
| 239 | # do any conversion on the resulting object |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 240 | if conversion is None: |
| 241 | return value |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 242 | elif conversion == 's': |
| 243 | return str(value) |
R David Murray | e56bf97 | 2012-08-19 17:26:34 -0400 | [diff] [blame] | 244 | elif conversion == 'r': |
| 245 | return repr(value) |
| 246 | elif conversion == 'a': |
| 247 | return ascii(value) |
Florent Xicluna | 7b2a771 | 2010-09-06 20:27:55 +0000 | [diff] [blame] | 248 | raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | # returns an iterable that contains tuples of the form: |
| 252 | # (literal_text, field_name, format_spec, conversion) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 253 | # literal_text can be zero length |
| 254 | # field_name can be None, in which case there's no |
| 255 | # object to format and output |
| 256 | # if field_name is not None, it is looked up, formatted |
| 257 | # with format_spec and conversion and then used |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 258 | def parse(self, format_string): |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 259 | return _string.formatter_parser(format_string) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | # given a field_name, find the object it references. |
| 263 | # field_name: the field being looked up, e.g. "0.name" |
| 264 | # or "lookup[3]" |
| 265 | # used_args: a set of which args have been used |
| 266 | # args, kwargs: as passed in to vformat |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 267 | def get_field(self, field_name, args, kwargs): |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 268 | first, rest = _string.formatter_field_name_split(field_name) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 269 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 270 | obj = self.get_value(first, args, kwargs) |
| 271 | |
| 272 | # loop through the rest of the field_name, doing |
| 273 | # getattr or getitem as needed |
| 274 | for is_attr, i in rest: |
| 275 | if is_attr: |
| 276 | obj = getattr(obj, i) |
| 277 | else: |
| 278 | obj = obj[i] |
| 279 | |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 280 | return obj, first |