MCPcopy Index your code
hub / github.com/python/cpython / parse_spec

Function parse_spec

Tools/i18n/pygettext.py:297–376  ·  view source on GitHub ↗

Parse a keyword spec string into a dictionary. The keyword spec format defines the name of the gettext function and the positions of the arguments that correspond to msgid, msgid_plural, and msgctxt. The format is as follows: name - the name of the gettext function, assumed to

(spec)

Source from the content-addressed store, hash-verified

295
296
297def parse_spec(spec):
298 """Parse a keyword spec string into a dictionary.
299
300 The keyword spec format defines the name of the gettext function and the
301 positions of the arguments that correspond to msgid, msgid_plural, and
302 msgctxt. The format is as follows:
303
304 name - the name of the gettext function, assumed to
305 have a single argument that is the msgid.
306 name:pos1 - the name of the gettext function and the position
307 of the msgid argument.
308 name:pos1,pos2 - the name of the gettext function and the positions
309 of the msgid and msgid_plural arguments.
310 name:pos1,pos2c - the name of the gettext function and the positions
311 of the msgid and msgctxt arguments.
312 name:pos1,pos2,pos3c - the name of the gettext function and the
313 positions of the msgid, msgid_plural, and
314 msgctxt arguments.
315
316 As an example, the spec 'foo:1,2,3c' means that the function foo has three
317 arguments, the first one is the msgid, the second one is the msgid_plural,
318 and the third one is the msgctxt. The positions are 1-based.
319
320 The msgctxt argument can appear in any position, but it can only appear
321 once. For example, the keyword specs 'foo:3c,1,2' and 'foo:1,2,3c' are
322 equivalent.
323
324 See https://www.gnu.org/software/gettext/manual/gettext.html
325 for more information.
326 """
327 parts = spec.strip().split(':', 1)
328 if len(parts) == 1:
329 name = parts[0]
330 return name, {'msgid': 0}
331
332 name, args = parts
333 if not args:
334 raise ValueError(f'Invalid keyword spec {spec!r}: '
335 'missing argument positions')
336
337 result = {}
338 for arg in args.split(','):
339 arg = arg.strip()
340 is_context = False
341 if arg.endswith('c'):
342 is_context = True
343 arg = arg[:-1]
344
345 try:
346 pos = int(arg) - 1
347 except ValueError as e:
348 raise ValueError(f'Invalid keyword spec {spec!r}: '
349 'position is not an integer') from e
350
351 if pos < 0:
352 raise ValueError(f'Invalid keyword spec {spec!r}: '
353 'argument positions must be strictly positive')
354

Callers 2

process_keywordsFunction · 0.85

Calls 4

splitMethod · 0.45
stripMethod · 0.45
endswithMethod · 0.45
valuesMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…