anyconfig.parser

Misc parsers

anyconfig.parser.parse_single(str_)

Very simple parser to parse expressions represent some single values.

Parameters:str – a string to parse
Returns:Int | Bool | String
>>> parse_single(None)
''
>>> parse_single("0")
0
>>> parse_single("123")
123
>>> parse_single("True")
True
>>> parse_single("a string")
'a string'
>>> parse_single('"a string"')
'a string'
>>> parse_single("'a string'")
'a string'
>>> parse_single("0.1")
'0.1'
>>> parse_single("    a string contains extra whitespaces     ")
'a string contains extra whitespaces'
anyconfig.parser.parse_list(str_, sep=', ')

Simple parser to parse expressions reprensent some list values.

Parameters:
  • str – a string to parse
  • sep – Char to separate items of list
Returns:

[Int | Bool | String]

>>> parse_list("")
[]
>>> parse_list("1")
[1]
>>> parse_list("a,b")
['a', 'b']
>>> parse_list("1,2")
[1, 2]
>>> parse_list("a,b,")
['a', 'b']
anyconfig.parser.attr_val_itr(str_, avs_sep=':', vs_sep=', ', as_sep=';')

Atrribute and value pair parser.

Parameters:
  • str – String represents a list of pairs of attribute and value
  • avs_sep – char to separate attribute and values
  • vs_sep – char to separate values
  • as_sep – char to separate attributes
anyconfig.parser.parse_attrlist_0(str_, avs_sep=':', vs_sep=', ', as_sep=';')

Simple parser to parse expressions in the form of [ATTR1:VAL0,VAL1,...;ATTR2:VAL0,VAL2,..].

Parameters:
  • str – input string
  • avs_sep – char to separate attribute and values
  • vs_sep – char to separate values
  • as_sep – char to separate attributes
Returns:

a list of tuples of (key, value | [value])

where key = (Int | String | ...), value = (Int | Bool | String | ...) | [Int | Bool | String | ...]

>>> parse_attrlist_0("a:1")
[('a', 1)]
>>> parse_attrlist_0("a:1;b:xyz")
[('a', 1), ('b', 'xyz')]
>>> parse_attrlist_0("requires:bash,zsh")
[('requires', ['bash', 'zsh'])]
>>> parse_attrlist_0("obsoletes:sysdata;conflicts:sysdata-old")
[('obsoletes', 'sysdata'), ('conflicts', 'sysdata-old')]
anyconfig.parser.parse_attrlist(str_, avs_sep=':', vs_sep=', ', as_sep=';')

Simple parser to parse expressions in the form of [ATTR1:VAL0,VAL1,...;ATTR2:VAL0,VAL2,..].

Parameters:
  • str – input string
  • avs_sep – char to separate attribute and values
  • vs_sep – char to separate values
  • as_sep – char to separate attributes
>>> parse_attrlist("requires:bash,zsh")
{'requires': ['bash', 'zsh']}
anyconfig.parser.parse(str_, lsep=', ', avsep=':', vssep=', ', avssep=';')

Generic parser