ksconf.commands package

Module contents

class ksconf.commands.KsconfCmd(name)

Bases: object

Ksconf command specification base class.

add_parser(subparser)
description = None
format = 'default'
help = None
launch(args)

Handle flow control between pre_run() / run() / post_run()

maturity = 'alpha'
post_run(args, exec_info=None)

Any custom clean up work that needs done. Always called if run() was. Presence of exc_info indicates failure.

pre_run(args)

Pre-run hook. Any exceptions here prevent run() from being called.

register_args(parser)

This function in passed the

run(args)

Actual works happens here. Return code should be an EXIT_CODE_* from consts.

class ksconf.commands.ConfDirProxy(name, mode, parse_profile=None)

Bases: object

get_file(relpath)
class ksconf.commands.ConfFileProxy(name, mode, stream=None, parse_profile=None, is_file=None)

Bases: object

close()
data
dump(data)
is_file()
load(profile=None)
reset()
set_parser_option(**kwargs)

Setting a key to None will remove that setting.

stream
class ksconf.commands.ConfFileType(mode='r', action='open', parse_profile=None, accept_dir=False)

Bases: object

Factory for creating conf file object types; returns a lazy-loader ConfFile proxy class

Started from argparse.FileType() and then changed everything. With our use case, it’s often necessary to delay writing, or read before writing to a conf file (depending on weather or not –dry-run mode is enabled, for example.)

Instances of FileType are typically passed as type= arguments to the ArgumentParser add_argument() method.

Parameters:
  • mode (str) – How the file is to be opened. Accepts “r”, “w”, and “r+”.
  • action (str) – Determine how much work should be handled during argument parsing vs handed off to the caller. Supports ‘none’, ‘open’, ‘load’. Full descriptions below.
  • parse_profile – parsing configuration settings passed along to the parser
  • accept_dir (bool) – Should the CLI accept a directory of config files instead of an individual file. Defaults to False.

Values for action

Action Description
none No preparation or testing is done on the filename.
open Ensure the file exists an can be opened.
load Ensure the file can be opened and parsed successfully.

Once invoked, instances of this class will return a ConfFileProxy object, or a ConfDirProxy object if a directory is passed in via the CLI.

ksconf.commands.dedent(text)

Remove any common leading whitespace from every line in text.

This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.

Note that tabs and spaces are both treated as whitespace, but they are not equal: the lines ” hello” and “thello” are considered to have no common leading whitespace. (This behaviour is new in Python 2.5; older versions of this module incorrectly expanded tabs before searching for common leading whitespace.)