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.DiffHeader(name, mtime=None)

Bases: object

detect_mtime()
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, preserve_empty=False)

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.compare_stanzas(a, b, stanza_name, preserve_empty=False)
ksconf.conf.delta.is_equal(delta)

Is the delta output show that the compared objects are identical

ksconf.conf.delta.reduce_stanza(stanza, keep_attrs)

Pre-process a stanzas so that only a common set of keys will be compared.

Parameters:
  • stanza (dict) – Stanzas containing attributes and values
  • keep_attrs ((list, set, tuple, dict)) – Listing of attributes to preserve
Returns:

a reduced copy of stanza.

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_app_local(app_folder, cleanup=True)

Find everything in local, if it has a corresponding file in default, merge. This function assumes standard Splunk app path names.

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

Dest is treated as both the output, and the highest priority source.

ksconf.conf.meta module

Incomplete documentation available here:

https://docs.splunk.com/Documentation/Splunk/latest/Admin/Defaultmetaconf

Specifically, attribute-level ACls aren’t discussed nor is the magic “import” directive.

LEVELS:

0 - global (or 1 stanza=”default”) 1 - conf 2 - stanzas 3 - attribute
class ksconf.conf.meta.MetaData

Bases: object

static expand_layers(layers)
Parameters:layers (list(dict)) – layer of stanzas, starting with the global ending with conf/stanza/attr
Returns:Expanded layer
Return type:dict
feed_conf(conf)
feed_file(stream)
get(*names)
get_layer(*names)
iter_raw()

RAW

classmethod parse_meta(stanza)

Split out the values of ‘access’ (maybe more someday) :param stanza: content of a meta stanza :return: extended meta data :rtype: dict

regex_access = '(?:^|\\s*,\\s*)(?P<action>read|write)\\s*:\\s*\\[\\s*(?P<roles>[^\\]]+?)\\s*\\]'
walk()
write_stream(stream, sort=True)
class ksconf.conf.meta.MetaLayer(name)

Bases: object

data
items(prefix=None)

Helpful when rebuilding the input file.

resolve(name)
update(*args, **kwargs)
walk(_prefix=())

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.conf_attr_boolean(value)
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 must be preserved. 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 for a best-effort parse.
Returns:

iterable of (attribute,value) tuples

class ksconf.conf.parser.update_conf(conf_path, profile={'dup_key': 'overwrite', 'dup_stanza': 'exception', 'keep_comments': True, 'strict': True}, encoding=None, make_missing=False)

Bases: object

Context manager that allows for simple in-place updates to conf files. This provides a simple dict-like interface for easy updates.

Usage example:

with update_conf("app.conf") as conf:
    conf["launcher"]["version"] = "1.0.2"
    conf["install"]["build"] = 33
Parameters:
  • conf_path (str) – Path to .conf file to be edited.
  • profile (dict) – Parsing settings and strictness profile.
  • encoding (str) – encoding to use for file operations.
  • make_missing (bool) – When true, a new blank configuration file will be created with the updates rather than raising an exception.
keys()
update(*args, **kwargs)
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