configfile module

class configfile.ConfigFile[source]

Bases: object

Store options in a plain text file in form of: key=value

append(filename, maxsplit=1)[source]

Load options from file and append them to current options.

Parameters:
  • filename (str) – full path

  • maxsplit (int) – split lines only n times on ‘=’

askQuestion(message)[source]

Call previously registered function to ask a question.

Parameters:

message (str) – question that should be shown

boolValue(key, default=False)[source]

Return a ‘bool’ instance of key’s value.

Parameters:
  • key (str) – string used as key

  • default (bool) – return this if key is not set

Returns:

value of ‘key’ or ‘default’

if ‘key’ is not set.

Return type:

bool

clearHandlers()[source]

Reset error and question handlers.

hasKey(key)[source]

True if key is set.

Parameters:

key (str) – string used as key

Returns:

True if the key is set

Return type:

bool

intValue(key, default=0)[source]

Return a ‘int’ instance of key’s value.

Parameters:
  • key (str) – string used as key

  • default (int) – return this if key is not set

Returns:

value of key or default

if key is not set.

Return type:

int

keys()[source]
listValue(key, type_key='str:value', default=[])[source]

Return a list of values

Size of the list must be stored in key.size

Parameters:
  • key (str) – used base-key

  • type_key (str) – pattern of ‘value-type:value-name’. See examples below.

  • default (list) – default value

Returns:

value of key or default

if key is not set.

Return type:

list

type_key pattern examples:

'str:value'               => return str values from key.value
'int:type'                => return int values from key.type
'bool:enabled'            => return bool values from key.enabled
('str:value', 'int:type') => return tuple of values
load(filename, **kwargs)[source]

Reset current options and load new options from file.

Parameters:

filename (str) – full path

notifyError(message)[source]

Call previously registered function to show an error.

Parameters:

message (str) – error message that should be shown

remapKey(old_key, new_key)[source]

Remap keys to a new key name.

Parameters:
  • old_key (str) – old key name

  • new_key (str) – new key name

remapKeyRegex(pattern, replace)[source]

Remap keys to a new key name using re.sub().

Parameters:
  • pattern (str) – part of key name that should be replaced

  • replace (str, method) – string or a callable function which will be used to replace all matches of pattern.

removeKey(key)[source]

Remove key from options.

Parameters:

key (str) – string used as key

removeKeysStartsWith(prefix)[source]

Remove key from options which start with given prefix.

Parameters:

prefix (str) – prefix for keys (key starts with this string) that should be removed

save(filename)[source]

Save all options to file.

Parameters:

filename (str) – full path

Returns:

True if successful

Return type:

bool

setBoolValue(key, value)[source]

Set a bool value for key.

Parameters:
  • key (str) – string used as key

  • value (bool) – store this option

setErrorHandler(handler)[source]

Register a function that should be called for notifying errors.

handler (method): callable function

setIntValue(key, value)[source]

Set an integer value for key.

Parameters:
  • key (str) – string used as key

  • value (int) – store this option

setListValue(key, type_key, value)[source]

Set a list of values.

Size of the list will be stored in key.size

Parameters:
  • key (str) – used base-key

  • type_key (str) – pattern of ‘value-type:value-name’. See examples below.

  • value (list) – that should be stored

type_key pattern examples:

'str:value'               => return str values from key.value
'int:type'                => return int values from key.type
'bool:enabled'            => return bool values from key.enabled
('str:value', 'int:type') => return tuple of values
setQuestionHandler(handler)[source]

Register a function that should be called for asking questions.

handler (method): callable function

setStrValue(key, value)[source]

Set a string value for key.

Parameters:
  • key (str) – string used as key

  • value (str) – store this value

strValue(key, default='')[source]

Return a ‘str’ instance of key’s value.

Parameters:
  • key (str) – Key identifying the value in the config file.

  • default (str) – Default value if key is not present.

Returns:

Value of key or default.

Return type:

str

class configfile.ConfigFileWithProfiles(default_profile_name='')[source]

Bases: ConfigFile

Store options in profiles as ‘profileX.key=value’

Parameters:

default_profile_name (str) – default name of the first profile.

addProfile(name)[source]

Add a new profile if the name is not already in use.

Parameters:

name (str) – new profile name

Returns:

new profile ID

Return type:

str

append(filename)[source]

Load options from file and append them to current options.

Parameters:

filename (str) – full path

currentProfile()[source]

Currently selected profile ID. Profile IDs are strings!

Returns:

profile ID

Return type:

str

hasProfileKey(key, profile_id=None)[source]

True if key is set in profile.

Parameters:
  • key (str) – string used as key

  • profile_id (str, int) – valid profile ID

Returns:

True if key is set.

Return type:

bool

load(filename)[source]

Reset current options and load new options from file.

Parameters:

filename (str) – full path

profileBoolValue(key, default=False, profile_id=None)[source]
profileExists(profile_id)[source]

True if the profile exists.

Parameters:

profile_id (str, int) – profile ID

Returns:

True if profile_id exists.

Return type:

bool

profileExistsByName(name)[source]

True if the profile exists.

Parameters:

name (str) – profile name

Returns:

True if name exists.

Return type:

bool

profileIntValue(key, default=0, profile_id=None)[source]
profileKey(key, profile_id=None)[source]

Prefix for keys with profile. e.g. ‘profile1.key’

Parameters:
  • key (str) – Key identifier.

  • profile_id (str, int) – Valid profile ID.

Returns:

Key with prefix ‘profile1.key’

Return type:

str

profileListValue(key, type_key='str:value', default=[], profile_id=None)[source]
profileName(profile_id=None)[source]

Name of the profile.

Parameters:

profile_id (str, int) – valid profile ID

Returns:

name of profile

Return type:

str

profileStrValue(key, default='', profile_id=None)[source]

Return the value of key related to profile_id.

Returns:

The value.

Return type:

str

profiles()[source]

List of all available profile IDs. Profile IDs are strings!

Returns:

all available profile IDs as strings

Return type:

list

profilesSortedByName()[source]

List of available profile IDs alphabetically sorted by their names. Profile IDs are strings!

Returns:

all available profile IDs as strings

Return type:

list

remapProfileKey(oldKey, newKey, profileId=None)[source]

Remap profile keys to a new key name.

Parameters:
  • oldKey (str) – old key name

  • newKey (str) – new key name

  • profileId (str, int) – valid profile ID

removeProfile(profile_id=None)[source]

Remove profile and all its keys and values.

Parameters:

profile_id (str, int) – valid profile ID

Returns:

True if successful

Return type:

bool

removeProfileKey(key, profile_id=None)[source]

Remove the key from profile.

Parameters:
  • key (str) – key name

  • profile_id (str, int) – valid profile ID

removeProfileKeysStartsWith(prefix, profile_id=None)[source]

Remove the keys starting with prefix from profile.

Parameters:
  • prefix (str) – prefix for keys (key starts with this string) that should be removed.

  • profile_id (str, int) – valid profile ID

setCurrentProfile(profile_id)[source]

Change the current profile.

Parameters:

profile_id (str, int) – valid profile ID

Returns:

True if successful

Return type:

bool

setCurrentProfileByName(name)[source]

Change the current profile by a givin name.

Parameters:

name (str) – valid profile name

Returns:

True if successful

Return type:

bool

setProfileBoolValue(key, value, profile_id=None)[source]
setProfileIntValue(key, value, profile_id=None)[source]
setProfileListValue(key, type_key, value, profile_id=None)[source]
setProfileName(name, profile_id=None)[source]

Change the name of the profile.

Parameters:
  • name (str) – new profile name

  • profile_id (str, int) – valid profile ID

Returns:

True if successful.

Return type:

bool

setProfileStrValue(key, value, profile_id=None)[source]