KeyFile
- class KeyFile(**kwargs)
- Constructors:
new() -> GLib.KeyFile
Constructors
- class KeyFile
- classmethod new() KeyFile
Creates a new empty
KeyFile
object. Useload_from_file()
,load_from_data()
,load_from_dirs()
orload_from_data_dirs()
to read an existing key file.Added in version 2.6.
Methods
- class KeyFile
-
- get_boolean(group_name: str, key: str) bool
Returns the value associated with
key
undergroup_name
as a boolean.If
key
cannot be found thenFalse
is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as a boolean thenFalse
is returned anderror
is set toINVALID_VALUE
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_boolean_list(group_name: str, key: str) list[bool]
Returns the values associated with
key
undergroup_name
as booleans.If
key
cannot be found thenNone
is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as booleans thenNone
is returned anderror
is set toINVALID_VALUE
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_comment(group_name: str | None = None, key: str | None = None) str
Retrieves a comment above
key
fromgroup_name
. Ifkey
isNone
thencomment
will be read from abovegroup_name
. If bothkey
andgroup_name
areNone
, thencomment
will be read from above the first group in the file.Note that the returned string does not include the ‘#’ comment markers, but does include any whitespace after them (on each line). It includes the line breaks between lines, but does not include the final line break.
Added in version 2.6.
- Parameters:
group_name – a group name, or
None
key – a key
- get_double(group_name: str, key: str) float
Returns the value associated with
key
undergroup_name
as a double. Ifgroup_name
isNone
, the start_group is used.If
key
cannot be found then 0.0 is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as a double then 0.0 is returned anderror
is set toINVALID_VALUE
.Added in version 2.12.
- Parameters:
group_name – a group name
key – a key
- get_double_list(group_name: str, key: str) list[float]
Returns the values associated with
key
undergroup_name
as doubles.If
key
cannot be found thenNone
is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as doubles thenNone
is returned anderror
is set toINVALID_VALUE
.Added in version 2.12.
- Parameters:
group_name – a group name
key – a key
- get_groups() tuple[list[str], int]
Returns all groups in the key file loaded with
key_file
. The array of returned groups will beNone
-terminated, solength
may optionally beNone
.Added in version 2.6.
- get_int64(group_name: str, key: str) int
Returns the value associated with
key
undergroup_name
as a signed 64-bit integer. This is similar toget_integer()
but can return 64-bit results without truncation.Added in version 2.26.
- Parameters:
group_name – a non-
None
group namekey – a non-
None
key
- get_integer(group_name: str, key: str) int
Returns the value associated with
key
undergroup_name
as an integer.If
key
cannot be found then 0 is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as an integer, or is out of range for aint
, then 0 is returned anderror
is set toINVALID_VALUE
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_integer_list(group_name: str, key: str) list[int]
Returns the values associated with
key
undergroup_name
as integers.If
key
cannot be found thenNone
is returned anderror
is set toKEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as integers, or are out of range forint
, thenNone
is returned anderror
is set toINVALID_VALUE
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_keys(group_name: str) tuple[list[str], int]
Returns all keys for the group name
group_name
. The array of returned keys will beNone
-terminated, solength
may optionally beNone
. In the event that thegroup_name
cannot be found,None
is returned anderror
is set toGROUP_NOT_FOUND
.Added in version 2.6.
- Parameters:
group_name – a group name
- get_locale_for_key(group_name: str, key: str, locale: str | None = None) str | None
Returns the actual locale which the result of
get_locale_string()
orget_locale_string_list()
came from.If calling
get_locale_string()
orget_locale_string_list()
with exactly the samekey_file
,group_name
,key
andlocale
, the result of those functions will have originally been tagged with the locale that is the result of this function.Added in version 2.56.
- Parameters:
group_name – a group name
key – a key
locale – a locale identifier or
None
- get_locale_string(group_name: str, key: str, locale: str | None = None) str
Returns the value associated with
key
undergroup_name
translated in the givenlocale
if available. Iflocale
isNone
then the current locale is assumed.If
locale
is to be non-None
, or if the current locale will change over the lifetime of theKeyFile
, it must be loaded withKEEP_TRANSLATIONS
in order to load strings for all locales.If
key
cannot be found thenNone
is returned anderror
is set toKEY_NOT_FOUND
. If the value associated withkey
cannot be interpreted or no suitable translation can be found then the untranslated value is returned.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
locale – a locale identifier or
None
- get_locale_string_list(group_name: str, key: str, locale: str | None = None) list[str]
Returns the values associated with
key
undergroup_name
translated in the givenlocale
if available. Iflocale
isNone
then the current locale is assumed.If
locale
is to be non-None
, or if the current locale will change over the lifetime of theKeyFile
, it must be loaded withKEEP_TRANSLATIONS
in order to load strings for all locales.If
key
cannot be found thenNone
is returned anderror
is set toKEY_NOT_FOUND
. If the values associated withkey
cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array isNone
-terminated, solength
may optionally beNone
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
locale – a locale identifier or
None
- get_start_group() str | None
Returns the name of the start group of the file.
Added in version 2.6.
- get_string(group_name: str, key: str) str
Returns the string value associated with
key
undergroup_name
. Unlikeget_value()
, this function handles escape sequences like s.In the event the key cannot be found,
None
is returned anderror
is set toKEY_NOT_FOUND
. In the event that thegroup_name
cannot be found,None
is returned anderror
is set toGROUP_NOT_FOUND
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_string_list(group_name: str, key: str) list[str]
Returns the values associated with
key
undergroup_name
.In the event the key cannot be found,
None
is returned anderror
is set toKEY_NOT_FOUND
. In the event that thegroup_name
cannot be found,None
is returned anderror
is set toGROUP_NOT_FOUND
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- get_uint64(group_name: str, key: str) int
Returns the value associated with
key
undergroup_name
as an unsigned 64-bit integer. This is similar toget_integer()
but can return large positive results without truncation.Added in version 2.26.
- Parameters:
group_name – a non-
None
group namekey – a non-
None
key
- get_value(group_name: str, key: str) str
Returns the raw value associated with
key
undergroup_name
. Useget_string()
to retrieve an unescaped UTF-8 string.In the event the key cannot be found,
None
is returned anderror
is set toKEY_NOT_FOUND
. In the event that thegroup_name
cannot be found,None
is returned anderror
is set toGROUP_NOT_FOUND
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
- has_group(group_name: str) bool
Looks whether the key file has the group
group_name
.Added in version 2.6.
- Parameters:
group_name – a group name
- load_from_bytes(bytes: Bytes, flags: KeyFileFlags) bool
Loads a key file from the data in
bytes
into an emptyKeyFile
structure. If the object cannot be created then%error
is set to aGKeyFileError
.Added in version 2.50.
- Parameters:
bytes – a
Bytes
flags – flags from
KeyFileFlags
- load_from_data(data: str, length: int, flags: KeyFileFlags) bool
Loads a key file from memory into an empty
KeyFile
structure. If the object cannot be created then%error
is set to aGKeyFileError
.Added in version 2.6.
- Parameters:
data – key file loaded in memory
length – the length of
data
in bytes (or (gsize)-1 if data is nul-terminated)flags – flags from
KeyFileFlags
- load_from_data_dirs(file: str, flags: KeyFileFlags) tuple[bool, str]
This function looks for a key file named
file
in the paths returned fromget_user_data_dir()
andget_system_data_dirs()
, loads the file intokey_file
and returns the file’s full path infull_path
. If the file could not be loaded then an%error
is set to either aFileError
orGKeyFileError
.Added in version 2.6.
- Parameters:
file – a relative path to a filename to open and parse
flags – flags from
KeyFileFlags
- load_from_dirs(file: str, search_dirs: Sequence[str], flags: KeyFileFlags) tuple[bool, str]
This function looks for a key file named
file
in the paths specified insearch_dirs
, loads the file intokey_file
and returns the file’s full path infull_path
.If the file could not be found in any of the
search_dirs
,NOT_FOUND
is returned. If the file is found but the OS returns an error when opening or reading the file, a%G_FILE_ERROR
is returned. If there is a problem parsing the file, a%G_KEY_FILE_ERROR
is returned.Added in version 2.14.
- Parameters:
file – a relative path to a filename to open and parse
search_dirs –
None
-terminated array of directories to searchflags – flags from
KeyFileFlags
- load_from_file(file: str, flags: KeyFileFlags) bool
Loads a key file into an empty
KeyFile
structure.If the OS returns an error when opening or reading the file, a
%G_FILE_ERROR
is returned. If there is a problem parsing the file, a%G_KEY_FILE_ERROR
is returned.This function will never return a
NOT_FOUND
error. If thefile
is not found,NOENT
is returned.Added in version 2.6.
- Parameters:
file – the path of a filename to load, in the GLib filename encoding
flags – flags from
KeyFileFlags
- remove_comment(group_name: str | None = None, key: str | None = None) bool
Removes a comment above
key
fromgroup_name
. Ifkey
isNone
thencomment
will be removed abovegroup_name
. If bothkey
andgroup_name
areNone
, thencomment
will be removed above the first group in the file.Added in version 2.6.
- Parameters:
group_name – a group name, or
None
key – a key
- remove_group(group_name: str) bool
Removes the specified group,
group_name
, from the key file.Added in version 2.6.
- Parameters:
group_name – a group name
- remove_key(group_name: str, key: str) bool
Removes
key
ingroup_name
from the key file.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key name to remove
- save_to_file(filename: str) bool
Writes the contents of
key_file
tofilename
usingfile_set_contents()
. If you need stricter guarantees about durability of the written file than are provided byfile_set_contents()
, usefile_set_contents_full()
with the return value ofto_data()
.This function can fail for any of the reasons that
file_set_contents()
may fail.Added in version 2.40.
- Parameters:
filename – the name of the file to write to
- set_boolean(group_name: str, key: str, value: bool) None
Associates a new boolean value with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
value –
True
orFalse
- set_boolean_list(group_name: str, key: str, list: Sequence[bool]) None
Associates a list of boolean values with
key
undergroup_name
. Ifkey
cannot be found then it is created. Ifgroup_name
isNone
, the start_group is used.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
list – an array of boolean values
- set_comment(group_name: str | None, key: str | None, comment: str) bool
Places a comment above
key
fromgroup_name
.If
key
isNone
thencomment
will be written abovegroup_name
. If bothkey
andgroup_name
areNone
, thencomment
will be written above the first group in the file.Note that this function prepends a ‘#’ comment marker to each line of
comment
.Added in version 2.6.
- Parameters:
group_name – a group name, or
None
key – a key
comment – a comment
- set_double(group_name: str, key: str, value: float) None
Associates a new double value with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.12.
- Parameters:
group_name – a group name
key – a key
value – a double value
- set_double_list(group_name: str, key: str, list: Sequence[float]) None
Associates a list of double values with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.12.
- Parameters:
group_name – a group name
key – a key
list – an array of double values
- set_int64(group_name: str, key: str, value: int) None
Associates a new integer value with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.26.
- Parameters:
group_name – a group name
key – a key
value – an integer value
- set_integer(group_name: str, key: str, value: int) None
Associates a new integer value with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
value – an integer value
- set_integer_list(group_name: str, key: str, list: Sequence[int]) None
Associates a list of integer values with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
list – an array of integer values
- set_list_separator(separator: int) None
Sets the character which is used to separate values in lists. Typically ‘;’ or ‘,’ are used as separators. The default list separator is ‘;’.
Added in version 2.6.
- Parameters:
separator – the separator
- set_locale_string(group_name: str, key: str, locale: str, string: str) None
Associates a string value for
key
andlocale
undergroup_name
. If the translation forkey
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
locale – a locale identifier
string – a string
- set_locale_string_list(group_name: str, key: str, locale: str, list: Sequence[str]) None
Associates a list of string values for
key
andlocale
undergroup_name
. If the translation forkey
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
locale – a locale identifier
list – a
None
-terminated array of locale string values
- set_string(group_name: str, key: str, string: str) None
Associates a new string value with
key
undergroup_name
. Ifkey
cannot be found then it is created. Ifgroup_name
cannot be found then it is created. Unlikeset_value()
, this function handles characters that need escaping, such as newlines.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
string – a string
- set_string_list(group_name: str, key: str, list: Sequence[str]) None
Associates a list of string values for
key
undergroup_name
. Ifkey
cannot be found then it is created. Ifgroup_name
cannot be found then it is created.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
list – an array of string values
- set_uint64(group_name: str, key: str, value: int) None
Associates a new integer value with
key
undergroup_name
. Ifkey
cannot be found then it is created.Added in version 2.26.
- Parameters:
group_name – a group name
key – a key
value – an integer value
- set_value(group_name: str, key: str, value: str) None
Associates a new value with
key
undergroup_name
.If
key
cannot be found then it is created. Ifgroup_name
cannot be found then it is created. To set an UTF-8 string which may contain characters that need escaping (such as newlines or spaces), useset_string()
.Added in version 2.6.
- Parameters:
group_name – a group name
key – a key
value – a string