class documentation

class DefaultConfigFileParser(ConfigFileParser): (source)

View In Hierarchy

Based on a simplified subset of INI and YAML formats. Here is the supported syntax

# this is a comment
; this is also a comment (.ini style)
---            # lines that start with --- are ignored (yaml style)
-------------------
[section]      # .ini-style section names are treated as comments

# how to specify a key-value pair (all of these are equivalent):
name value     # key is case sensitive: "Name" isn't "name"
name = value   # (.ini style)  (white space is ignored, so name = value same as name=value)
name: value    # (yaml style)
--name value   # (argparse style)

# how to set a flag arg (eg. arg which has action="store_true")
--name
name
name = True    # "True" and "true" are the same

# how to specify a list arg (eg. arg which has action="append")
fruit = [apple, orange, lemon]
indexes = [1, 12, 35 , 40]
Method get_syntax_description Returns a string describing the config file syntax.
Method parse Parses the keys and values from a config file.
Method serialize Does the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents.
Method _quote_value_if_needed_to_read_back_unchanged Write a value the way parse() reads back as that same value.
def get_syntax_description(self): (source)

Returns a string describing the config file syntax.

def parse(self, stream): (source)

Parses the keys and values from a config file.

NOTE: For keys that were specified to configargparse as action="store_true" or "store_false", the config file value must be one of: "yes", "no", "on", "off", "true", "false". Otherwise an error will be raised.

Parameters
stream:io.IOBaseA config file input stream (such as an open file object).
Returns
OrderedDictItems where the keys are strings and the values are either strings or lists (eg. to support config file formats like YAML which allow lists).
def serialize(self, items): (source)

Does the inverse of config parsing by taking parsed values and converting them back to a string representing config file contents.

Parameters
itemsan OrderedDict of items to be converted to the config file format. Keys are strings, and values are a str, int, float or bool, or a list whose elements are any of these or lists of them.
Returns
Contents of config file as a string
def _quote_value_if_needed_to_read_back_unchanged(self, key, value): (source)

Write a value the way parse() reads back as that same value.

parse() strips the whitespace around a value and one pair of quotes around it, and reads whitespace followed by '#' or ';' as the start of a comment, unless the value is quoted. So a value is written as is when that reads back unchanged, and otherwise in double or single quotes.

Parameters
keythe config file key the value is written under.
valuethe value as a string.
Returns
strthe value as it should be written after "key = "
Raises
ValueErrorif no form of the value reads back unchanged, eg. one that looks like a [list] or that holds both kinds of quotes.