ksconf.conf package

Submodules

ksconf.conf.delta module

class ksconf.conf.delta.DiffGlobal(type)

Bases: tuple

type

Alias for field number 0

class ksconf.conf.delta.DiffOp(tag, location, a, b)

Bases: tuple

a

Alias for field number 2

b

Alias for field number 3

location

Alias for field number 1

tag

Alias for field number 0

class ksconf.conf.delta.DiffStanza(type, stanza)

Bases: tuple

stanza

Alias for field number 1

type

Alias for field number 0

class ksconf.conf.delta.DiffStzKey(type, stanza, key)

Bases: tuple

key

Alias for field number 2

stanza

Alias for field number 1

type

Alias for field number 0

ksconf.conf.delta.compare_cfgs(a, b, allow_level0=True)

Return list of 5-tuples describing how to turn a into b.

Note

The Opcode tags borrowed from SequenceMatcher class in the difflib standard Python module.

Each tuple takes the form:

(tag, location, a, b)

tag:

Value Meaning
‘replace’ same element in both, but different values.
‘delete’ remove value b
‘insert’ insert value a
‘equal’ same values in both

location is a tuple that can take the following forms:

Tuple form Description
(0) Global file level context (e.g., both files are the same)
(1, stanza) Stanzas are the same, or completely different (no shared keys)
(2, stanza, key) Key level, indicating

Possible alternatives:

https://dictdiffer.readthedocs.io/en/latest/#dictdiffer.patch

ksconf.conf.delta.show_diff(stream, diffs, headers=None)
ksconf.conf.delta.show_text_diff(stream, a, b)
ksconf.conf.delta.summarize_cfg_diffs(delta, stream)

Summarize a delta into a human readable format. The input delta is in the format produced by the compare_cfgs() function.

ksconf.conf.merge module

ksconf.conf.merge.merge_conf_dicts(*dicts)
ksconf.conf.merge.merge_conf_files(dest, configs, dry_run=False, banner_comment=None)

ksconf.conf.parser module

Parse and write Splunk’s .conf files

According to this doc:

https://docs.splunk.com/Documentation/Splunk/7.2.3/Admin/Howtoeditaconfigurationfile

  1. Comments must start at the beginning of a line (#)
  2. Comments may not be after a stanza name or on an attribute’s value
  3. Supporting encoding is UTF-8 (and therefore ASCII too)
exception ksconf.conf.parser.ConfParserException

Bases: Exception

exception ksconf.conf.parser.DuplicateKeyException

Bases: ksconf.conf.parser.ConfParserException

exception ksconf.conf.parser.DuplicateStanzaException

Bases: ksconf.conf.parser.ConfParserException

class ksconf.conf.parser.Token

Bases: object

Immutable token object. deepcopy returns the same object

ksconf.conf.parser.cont_handler(iterable, continue_re=re.compile('^(.*)\\\\$'), breaker='\n')

Look for trailing backslashes (“\”) which indicate a value for an attribute is split across multiple lines. This function will group such lines together, and pass all other lines through as-is. Note that the continuation character must be the very last character on the line, trailing whitespace is not allowed.

Parameters:
  • iterable (iter) – lines from a configuration file
  • continue_re – regular expression to detect the continuation character
  • breaker – joining string when combining continued lines into a single string. Default ‘\n
Returns:

lines of text

Return type:

str

ksconf.conf.parser.detect_by_bom(path)
ksconf.conf.parser.inject_section_comments(section, prepend=None, append=None)
ksconf.conf.parser.parse_conf(stream, profile={'dup_key': 'overwrite', 'dup_stanza': 'exception', 'keep_comments': True, 'strict': True}, encoding=None)

Parse a .conf file. This is a wrapper around parse_conf_stream() that allows filenames or stream to be passed in.

Parameters:
  • stream (str, file) – the path to a configuration file or open file-like object to be parsed
  • profile – parsing configuration settings
  • encoding – Defaults to the system default, “uft-8”
Returns:

a mapping of the stanza and attributes. The resulting output is accessible as [stanaza][attribute] -> value

Return type:

dict

ksconf.conf.parser.parse_conf_stream(stream, keys_lower=False, handle_conts=True, keep_comments=False, dup_stanza='exception', dup_key='overwrite', strict=False)
ksconf.conf.parser.section_reader(stream, section_re=re.compile('^[\\s\\t]*\\[(.*)\\]\\s*$'))

This generator break a configuration file stream into sections. Each section contains a name and a list of text lines held within that section.

Sections that have no entries may be dropped. Any lines before the first section are send back with the section name of None.

Parameters:
  • stream (file) – configuration file input stream
  • section_re – regular expression for detecting stanza headers
Returns:

sections in the form of (section_name, lines_of_text)

Return type:

tuple

ksconf.conf.parser.smart_write_conf(filename, conf, stanza_delim='\n', sort=True, temp_suffix='.tmp')
ksconf.conf.parser.splitup_kvpairs(lines, comments_re=re.compile('^\\s*[#;]'), keep_comments=False, strict=False)

Break up ‘attribute=value’ entries in a configuration file.

Parameters:
  • lines (iter) – the body of a stanza containing associated attributes and values
  • comments_re – Regular expression used to detect comments.
  • keep_comments (bool, optional) – Should comments be preserved in the output. Defaults to False.
  • strict (bool, optional) – Should unknown content in the stanza stop processing. Defaults to False allowing “junk” to be silently ignored allowing for a best-effort parse.
Returns:

iterable of (attribute,value) tuples

ksconf.conf.parser.write_conf(stream, conf, stanza_delim='\n', sort=True)
ksconf.conf.parser.write_conf_stream(stream, conf, stanza_delim='\n', sort=True)

Module contents