bronx.syntax.parsing

Parsing tools.

Functions

bronx.syntax.parsing.str2dict(string, try_convert=None)[source]

Parse a string (of syntax key1:value1,key2=value2) to a dict.

Parameters:

try_convert – try to convert values as type try_convert, e.g. try_convert=int

bronx.syntax.parsing.xlist_strings(xlist)[source]

Some kind off handy coma-separated list (see the examples).

Examples:

>>> print(','. join(xlist_strings('host')))
host
>>> print(','. join(xlist_strings('host1,host2')))
host1,host2
>>> print(','. join(xlist_strings('host[1,2]')))
host1,host2
>>> print(','. join(xlist_strings('host[1-4]')))
host1,host2,host3,host4
>>> print(','. join(xlist_strings('fake[1-2,6],host[11,1-4,0]')))
fake1,fake2,fake6,host11,host1,host2,host3,host4,host0
>>> print(','. join(xlist_strings('fake[1-2-6]')))  
Traceback (most recent call last):
...
ValueError: Malformed xlist: fake[1-2-6]

Classes

class bronx.syntax.parsing.StringDecoder(substitution_cb=None, with_cache=True)[source]

Bases: object

Convert a string into a proper Python’s object.

This generic decoder only supports list, dictionaries and conversion to basic data types. However, it can easily be extended through inheritance.

The decoding is done simply by calling the StringDecoder object: decoded_string = DecoderObject(config_string)

A meta-language is used. Here are some examples:

  • toto will be decoded as toto

  • 1,2,3 will be decoded as a list of strings ['1', '2', '3']

  • list(1,2,3) will be decoded as a list of strings ['1', '2', '3']

  • list(1) will be decoded as a list of strings ['1', ]

  • int(1,2,3) will decoded as a list of ints [1, 2, 3]

  • dict(prod:1 assim:2) will be decoded as a dictionary of strings dict(prod='1', assim='2')

  • Dictionaries can be combined like in: dict(production:dict(0:102 12:24) assim:dict(0:6 12:6))

  • Dictionaries and lists can be mixed: dict(production:dict(0:0,96,102 12:3,6,24) assim:dict(0:0,3,6 12:0,3,6))

  • dict(production:&{prodconf} assim:&{assimconf}) will be decoded as a dictionary where &{prodconf} and &{assimconf} are replaced by entries prodconf and assimconf returned by the substitution_cb callback (see the explanation below for more details).

  • xbool(on) will be decoded as True

Multiple spaces and line breaks are ignored and removed during the decoding.

The only supported type conversion are: int, float.

The class constructor accepts a substitution_cb argument (None by default) that may be a callback function that is used to get a configuration line for a given key. This is (solely) used by the substitution mechanism. If substitution_cb is None, the substitution mechanism should not be used since it would lead to a StringDecoderSubstError exception.

A cache mechanism is used to lesser the cost of string parsing when several identical calls are made. As a consequence, the same object can be returned by two subsequent calls. This can be deactivated using the with_cache constructor’s argument.

The following exception may be raised:

remap_default(value)[source]

Convert all values: default cas. Does nothing.

remap_float(value)[source]

Convert all values to floats.

remap_int(value)[source]

Convert all values to integers.

remap_xbool(value)[source]

Convert all values to booleans.

Exceptions

exception bronx.syntax.parsing.StringDecoderRemapError(rmap)[source]

Bases: RuntimeError

Raised whenever an error occurs when re-mapping a configuration line to a given Python’s type in the StringDecoder class.

exception bronx.syntax.parsing.StringDecoderSubstError(sub, msg)[source]

Bases: RuntimeError

Raised whenever an error occurs during variable substitution in the StringDecoder class.

exception bronx.syntax.parsing.StringDecoderSyntaxError(value, msg)[source]

Bases: ValueError

Raised whenever a syntax error is detected in a configuration line (when the the StringDecoder class is used).