Source documentation¶
ihih - simple configuration parsers with dictionary-like interface
| Source code: | GitLab project |
|---|---|
| License: | BSD 3-Clause |
-
class
ihih.IHIH(filenames, ignore_errors=False, *args, **kwargs)¶ Bases:
dictIHIH - simple configuration parser
One key/value pair per line.
-
encoding= 'utf8'¶ define the encoding
-
_escape= '(?<!\\\\)(?:\\\\\\\\)*'¶ regexp definition of the escape sequence
-
_escaped_chars= '[\\\\\\\'\\"\\#/\\\\]'¶ regexp definition of characters to unconditionally un-escape
-
_separator= '\\='¶ regexp definition of key/value separator
Must be a fixed-width expression.
-
_extract= '^\\s*\n (?P<key>.+?)\n \\s*%(separator)s\\s*\n (?P<value>.*)'¶ extract
key = [value]on a single line
-
_quote= '["\\\']'¶ define what a quote might be
-
_quoted= '%(escape)s(?P<quote>%(quote)s)(?P<value>.*?)%(escape)s(?P=quote)'¶ how to find a quoted value
-
_bool= '^(?P<false>0|no|false|off|disabled)|(?P<true>1|yes|true|on|enabled)$'¶ regexp definition of a boolean value (used by
get_bool())
-
__init__(filenames, ignore_errors=False, *args, **kwargs)¶ attempt to parse a list of filenames
Parameters:
-
_comment= '(\\s*%(escape)s(?:\\#|//))'¶ regexp definition of an in-line comment
-
ignore_errors= False¶ do not stop on OSError when reading sources
-
reload(force=False, ignore_errors=None)¶ call
parse()on each configuration fileParameters: Returns: None
-
parse(filename, force=False, ignore_errors=None)¶ parse a configuration file
Parameters: - filename (str) – path to file to parse
- force (bool) – force (re)loading of files
- ignore_errors (bool) – ignore unreadable files,
default:
ignore_errors
Returns: bool
Note
filename should be an absolute path.
-
_unescape(value, quote=None)¶ remove escape prefix on “known escape”
See
_escaped_chars.This method attempt to utf8 encode
unicode()objects.
-
_handle_fragment(fragment, quote=None)¶ handle a fragment of a value
Provided to help on subclassing.
-
_comment_at(value)¶ return the position of the begining of a comment
-
_parse_value(value, data)¶ parse the “value” part of a “key / value”
This function handle the quoted parts and the comments.
Parameters: - value (str) – value to parse
- data – instance supporting
+=operator
Returns: parsed value
Return type: type of data
-
__contains__(key)¶ True if self contains key
Note
The key will be casted as
text_type().
-
__setitem__(key, value)¶ set item key to value
Note
The key will be casted as
text_type().
-
__getitem__(key)¶ return key value as internal type
You probably want to use one of the following:
get_text(),get_float(),get_int().Note
The key will be casted as
text_type().
-
__delitem__(key)¶ delete key from dict
Note
The key will be casted as
text_type().
-
__weakref__¶ list of weak references to the object (if defined)
-
get_text(key, default=None)¶ return key value as
text_type()or default if not foundNote
The key will be casted as
text_type().
-
get(key, default=None)¶ alias to
get_text()
-
get_float(key, default=None, errors='strict')¶ return key value as
float()or default if not foundIf errors is “ignore”, return default value instead of raising
ValueErroron failure.Note
The key will be casted as
text_type().
-
get_int(key, default=None, errors='strict', base=10)¶ return key value as
int()or default if not foundIf errors is “ignore”, return default value instead of raising
ValueErroron failure.Note
The key will be casted as
text_type().
-
-
class
ihih.IHIHI(*args, **kwargs)¶ Bases:
ihih.IHIHIHIH Interpolate -
IHIHwith variable interpolation-
_IHIHI__getkey(key, path=None)¶ return key value as internal type with interpolated variables
For more informations, see:
__getitem__().
-
_variable= '%(escape)s\\$(?P<value>\\w+|%(escape)s\\{(?P<unquoted>.+?)%(escape)s\\})'¶ regexp definition of a “variable”
-
_escaped_chars= '[\\\\\\\'\\"\\#/\\\\\\$\\{\\}]'¶ regexp definition of characters to unconditionally un-escape
-
__init__(*args, **kwargs)¶ attempt to parse a list of filenames
Parameters:
-
__setitem__(key, value)¶ set item key to value
Note
The key will be casted as
text_type().
-
_handle_fragment(fragment, quote=None)¶ search for variables in fragment
-
__getitem__(key)¶ return key value as internal type
You probably want to use one of the following:
get_text(),get_float(),get_int().Note
The key will be casted as
text_type().
-
_recursive(value)¶ recursive variable handler
Default: empty string
You can overwrite this function when subclassing and chose to return a unexpended version of the variable, raise an error or make a single, non recursive, lookup.
-