GLib.KeyFile¶
Fields¶
None
Methods¶
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Details¶
- class GLib.KeyFile¶
GKeyFile
parses .ini-like config files.GKeyFile
lets you parse, edit or create files containing groups of key-value pairs, which we call ‘key files’ for lack of a better name. Several freedesktop.org specifications use key files. For example, the Desktop Entry Specification and the Icon Theme Specification.The syntax of key files is described in detail in the Desktop Entry Specification, here is a quick summary: Key files consists of groups of key-value pairs, interspersed with comments.
```txt
- this is just an example
- there can be comments before the first group
[First Group]
Name=Key File Example\tthis value shows\nescaping
- localized strings are stored in multiple key-value pairs
Welcome=Hello Welcome[de]=Hallo Welcome[fr_FR]=Bonjour Welcome[it]=Ciao
[Another Group]
Numbers=2;20;-200;0
Booleans=true;false;true;true ```
Lines beginning with a
#
and blank lines are considered comments.Groups are started by a header line containing the group name enclosed in
[
and]
, and ended implicitly by the start of the next group or the end of the file. Each key-value pair must be contained in a group.Key-value pairs generally have the form
key=value
, with the exception of localized strings, which have the formkey[locale]=value
, with a locale identifier of the formlang_COUNTRY@MODIFIER
whereCOUNTRY
andMODIFIER
are optional. As a special case, the localeC
is associated with the untranslated pairkey=value
(since GLib 2.84). Space before and after the=
character is ignored. Newline, tab, carriage return and backslash characters in value are escaped as\n
,\t
,\r
, and\\\\
, respectively. To preserve leading spaces in values, these can also be escaped as\s
.Key files can store strings (possibly with localized variants), integers, booleans and lists of these. Lists are separated by a separator character, typically
;
or,
. To use the list separator character in a value in a list, it has to be escaped by prefixing it with a backslash.This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some important differences:
.ini files use the
;
character to begin comments, key files use the#
character.Key files do not allow for ungrouped keys meaning only comments can precede the first group.
Key files are always encoded in UTF-8.
Key and Group names are case-sensitive. For example, a group called
[GROUP]
is a different from[group]
..ini files don’t have a strongly typed boolean entry type, they only have
GetProfileInt()
. In key files, onlytrue
andfalse
(in lower case) are allowed.
Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same key multiple times; the last entry wins. Key files may also contain multiple groups with the same name; they are merged together. Another difference is that keys and group names in key files are not restricted to ASCII characters.
Here is an example of loading a key file and reading a value:
```c g_autoptr(
GLib.Error
) error =None
; g_autoptr(GLib.KeyFile
) key_file =GLib.KeyFile.new
();if (!:obj:GLib.KeyFile.load_from_file (key_file, “key-file.ini”, flags, &error)) { if (!:obj:GLib.Error.matches (error, G_FILE_ERROR,
GLib.FileError.NOENT
)) g_warning (“Error loading key file: %s”, error->message); return; }g_autofree
str
*val =GLib.KeyFile.get_string
(key_file, “Group Name”, “SomeKey”, &error); if (val ==None
&& !:obj:GLib.Error.matches (error, G_KEY_FILE_ERROR,GLib.KeyFileError.KEY_NOT_FOUND
)) { g_warning (“Error finding key in key file: %s”, error->message); return; } else if (val ==None
) { // Fall back to a default value. val =GLib.strdup
(“default-value”); } ```Here is an example of creating and saving a key file:
```c g_autoptr(
GLib.KeyFile
) key_file =GLib.KeyFile.new
(); conststr
*val = …; g_autoptr(GLib.Error
) error =None
;GLib.KeyFile.set_string
(key_file, “Group Name”, “SomeKey”, val);// Save as a file. if (!:obj:GLib.KeyFile.save_to_file (key_file, “key-file.ini”, &error)) { g_warning (“Error saving key file: %s”, error->message); return; }
// Or store to a
GLib.Bytes
for use elsewhere. gsize data_len; g_autofree guint8 *data = (guint8 *)GLib.KeyFile.to_data
(key_file, &data_len, &error); if (data ==None
) { g_warning (“Error saving key file: %s”, error->message); return; } g_autoptr(GLib.Bytes
) bytes =GLib.Bytes.new_take
(g_steal_pointer (&data), data_len); ```- classmethod new()[source]¶
- Returns:
an empty [struct`GLib`.KeyFile].
- Return type:
Creates a new empty [struct`GLib`.KeyFile] object.
Use [method`GLib`.KeyFile.load_from_file], [method`GLib`.KeyFile.load_from_data], [method`GLib`.KeyFile.load_from_dirs] or [method`GLib`.KeyFile.load_from_data_dirs] to read an existing key file.
New in version 2.6.
- get_boolean(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the value associated with the key as a boolean, or false if the key was not found or could not be parsed.
- Return type:
Returns the value associated with key under group_name as a boolean.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with key cannot be interpreted as a boolean then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.
New in version 2.6.
- get_boolean_list(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the values associated with the key as a list of booleans, or
NULL
if the key was not found or could not be parsed. The returned list of booleans should be freed with [func`GLib`.free] when no longer needed.- Return type:
[
bool
]
Returns the values associated with key under group_name as booleans.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with key cannot be interpreted as booleans then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.
New in version 2.6.
- get_comment(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
a comment that should be freed with [func`GLib`.free]
- Return type:
Retrieves a comment above key from group_name.
If key is
NULL
then comment will be read from above group_name. If both key and group_name areNULL
, then comment 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.New in version 2.6.
- get_double(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the value associated with the key as a double, or
0.0
if the key was not found or could not be parsed.- Return type:
Returns the value associated with key under group_name as a double.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with key cannot be interpreted as a double then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.
New in version 2.12.
- get_double_list(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the values associated with the key as a list of doubles, or
NULL
if the key was not found or could not be parsed. The returned list of doubles should be freed with [func`GLib`.free] when no longer needed.- Return type:
[
float
]
Returns the values associated with key under group_name as doubles.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with key cannot be interpreted as doubles then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.
New in version 2.12.
- get_groups()[source]¶
- Returns:
a newly-allocated
NULL
-terminated array of strings. Use [func`GLib`.strfreev] to free it.- length:
return location for the number of returned groups, or
NULL
to ignore
- Return type:
Returns all groups in the key file loaded with self.
The array of returned groups will be
NULL
-terminated, so length may optionally beNULL
.New in version 2.6.
- get_int64(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the value associated with the key as a signed 64-bit integer, or
0
if the key was not found or could not be parsed.- Return type:
Returns the value associated with key under group_name as a signed 64-bit integer.
This is similar to [method`GLib`.KeyFile.get_integer] but can return 64-bit results without truncation.
New in version 2.26.
- get_integer(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the value associated with the key as an integer, or
0
if the key was not found or could not be parsed.- Return type:
Returns the value associated with key under group_name as an integer.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with key cannot be interpreted as an integer, or is out of range for a
gint
, then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.New in version 2.6.
- get_integer_list(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the values associated with the key as a list of integers, or
NULL
if the key was not found or could not be parsed. The returned list of integers should be freed with [func`GLib`.free] when no longer needed.- Return type:
[
int
]
Returns the values associated with key under group_name as integers.
If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with key cannot be interpreted as integers, or are out of range for
gint
, then [error`GLib`.KeyFileError.INVALID_VALUE] is returned.New in version 2.6.
- get_keys(group_name)[source]¶
- Parameters:
group_name (
str
) – a group name- Raises:
- Returns:
a newly-allocated
NULL
-terminated array of strings. Use [func`GLib`.strfreev] to free it.- length:
return location for the number of keys returned, or
NULL
to ignore
- Return type:
Returns all keys for the group name group_name.
The array of returned keys will be
NULL
-terminated, so length may optionally beNULL
. If the group_name cannot be found, [error`GLib`.KeyFileError.GROUP_NOT_FOUND] is returned.New in version 2.6.
- get_locale_for_key(group_name, key, locale)[source]¶
- Parameters:
- Returns:
the locale from the file, or
NULL
if the key was not found or the entry in the file was was untranslated- Return type:
Returns the actual locale which the result of [method`GLib`.KeyFile.get_locale_string] or [method`GLib`.KeyFile.get_locale_string_list] came from.
If calling [method`GLib`.KeyFile.get_locale_string] or [method`GLib`.KeyFile.get_locale_string_list] with exactly the same self, group_name, key and locale, the result of those functions will have originally been tagged with the locale that is the result of this function.
New in version 2.56.
- get_locale_string(group_name, key, locale)[source]¶
- Parameters:
- Raises:
- Returns:
a newly allocated string or
NULL
if the specified key cannot be found.- Return type:
Returns the value associated with key under group_name translated in the given locale if available.
If locale is
C
then the untranslated value is returned (since GLib 2.84).If locale is
NULL
then the current locale is assumed.If locale is to be non-
NULL
, or if the current locale will change over the lifetime of the [struct`GLib`.KeyFile], it must be loaded with [flags`GLib`.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. If the value associated with key cannot be interpreted or no suitable translation can be found then the untranslated value is returned.
New in version 2.6.
- get_locale_string_list(group_name, key, locale)[source]¶
- Parameters:
- Raises:
- Returns:
a newly allocated
NULL
-terminated string array orNULL
if the key isn’t found. The string array should be freed with [func`GLib`.strfreev].- Return type:
[
str
]
Returns the values associated with key under group_name translated in the given locale if available.
If locale is
C
then the untranslated value is returned (since GLib 2.84).If locale is
NULL
then the current locale is assumed.If locale is to be non-
NULL
, or if the current locale will change over the lifetime of the [struct`GLib`.KeyFile], it must be loaded with [flags`GLib`.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.If key cannot be found then [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. If the values associated with key cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array is
NULL
-terminated, so length may optionally beNULL
.New in version 2.6.
- get_string(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
a newly allocated string or
NULL
if the specified key cannot be found.- Return type:
Returns the string value associated with key under group_name.
Unlike [method`GLib`.KeyFile.get_value], this function handles escape sequences like
\s
.If the key cannot be found, [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. If the group_name cannot be found, [error`GLib`.KeyFileError.GROUP_NOT_FOUND] is returned.
New in version 2.6.
- get_string_list(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
a
NULL
-terminated string array orNULL
if the specified key cannot be found. The array should be freed with [func`GLib`.strfreev].- Return type:
[
str
]
Returns the values associated with key under group_name.
If the key cannot be found, [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. If the group_name cannot be found, [error`GLib`.KeyFileError.GROUP_NOT_FOUND] is returned.
New in version 2.6.
- get_uint64(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
the value associated with the key as an unsigned 64-bit integer, or
0
if the key was not found or could not be parsed.- Return type:
Returns the value associated with key under group_name as an unsigned 64-bit integer.
This is similar to [method`GLib`.KeyFile.get_integer] but can return large positive results without truncation.
New in version 2.26.
- get_value(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
a newly allocated string or
NULL
if the specified key cannot be found.- Return type:
Returns the raw value associated with key under group_name.
Use [method`GLib`.KeyFile.get_string] to retrieve an unescaped UTF-8 string.
If the key cannot be found, [error`GLib`.KeyFileError.KEY_NOT_FOUND] is returned. If the group_name cannot be found, [error`GLib`.KeyFileError.GROUP_NOT_FOUND] is returned.
New in version 2.6.
- has_group(group_name)[source]¶
- Parameters:
group_name (
str
) – a group name- Returns:
true if group_name is a part of self, false otherwise.
- Return type:
Looks whether the key file has the group group_name.
New in version 2.6.
- load_from_bytes(bytes, flags)[source]¶
- Parameters:
bytes (
GLib.Bytes
) – a [struct`GLib`.Bytes]flags (
GLib.KeyFileFlags
) – flags from [flags`GLib`.KeyFileFlags]
- Raises:
- Returns:
true if a key file could be loaded, false otherwise
- Return type:
Loads a key file from the data in bytes into an empty [struct`GLib`.KeyFile] structure.
If the object cannot be created then a [error`GLib`.KeyFileError] is returned.
New in version 2.50.
- load_from_data(data, length, flags)[source]¶
- Parameters:
data (
str
) – key file loaded in memorylength (
int
) – the length of data in bytes (or(gsize)-1
if data is nul-terminated)flags (
GLib.KeyFileFlags
) – flags from [flags`GLib`.KeyFileFlags]
- Raises:
- Returns:
true if a key file could be loaded, false otherwise
- Return type:
Loads a key file from memory into an empty [struct`GLib`.KeyFile] structure.
If the object cannot be created then a [error`GLib`.KeyFileError is returned.
New in version 2.6.
- load_from_data_dirs(file, flags)[source]¶
- Parameters:
file (
str
) – a relative path to a filename to open and parseflags (
GLib.KeyFileFlags
) – flags from [flags`GLib`.KeyFileFlags]
- Raises:
- Returns:
true if a key file could be loaded, false otherwise
- full_path:
return location for a string containing the full path of the file, or
NULL
to ignore
- Return type:
Looks for a key file named file in the paths returned from [func`GLib`.get_user_data_dir] and [func`GLib`.get_system_data_dirs], loads the file into self and returns the file’s full path in full_path.
If the file could not be loaded then either a [error`GLib`.FileError] or [error`GLib`.KeyFileError] is returned.
New in version 2.6.
- load_from_dirs(file, search_dirs, flags)[source]¶
- Parameters:
file (
str
) – a relative path to a filename to open and parsesearch_dirs ([
str
]) –NULL
-terminated array of directories to searchflags (
GLib.KeyFileFlags
) – flags from [flags`GLib`.KeyFileFlags]
- Raises:
- Returns:
true if a key file could be loaded, false otherwise
- full_path:
return location for a string containing the full path of the file, or
NULL
to ignore
- Return type:
Looks for a key file named file in the paths specified in search_dirs, loads the file into self and returns the file’s full path in full_path.
If the file could not be found in any of the search_dirs, [error`GLib`.KeyFileError.NOT_FOUND] is returned. If the file is found but the OS returns an error when opening or reading the file, a [error`GLib`.FileError] is returned. If there is a problem parsing the file, a [error`GLib`.KeyFileError] is returned.
New in version 2.14.
- load_from_file(file, flags)[source]¶
- Parameters:
file (
str
) – the path of a filename to load, in the GLib filename encodingflags (
GLib.KeyFileFlags
) – flags from [flags`GLib`.KeyFileFlags]
- Raises:
- Returns:
true if a key file could be loaded, false otherwise
- Return type:
Loads a key file into an empty [struct`GLib`.KeyFile] structure.
If the OS returns an error when opening or reading the file, a [error`GLib`.FileError] is returned. If there is a problem parsing the file, a [error`GLib`.KeyFileError] is returned.
This function will never return a [error`GLib`.KeyFileError.NOT_FOUND] error. If the file is not found, [error`GLib`.FileError.NOENT] is returned.
New in version 2.6.
- remove_comment(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
true if the comment was removed, false otherwise
- Return type:
Removes a comment above key from group_name.
If key is
NULL
then comment will be removed above group_name. If both key and group_name areNULL
, then comment will be removed above the first group in the file.New in version 2.6.
- remove_group(group_name)[source]¶
- Parameters:
group_name (
str
) – a group name- Raises:
- Returns:
true if the group was removed, false otherwise
- Return type:
Removes the specified group, group_name, from the key file.
New in version 2.6.
- remove_key(group_name, key)[source]¶
- Parameters:
- Raises:
- Returns:
true if the key was removed, false otherwise
- Return type:
Removes key in group_name from the key file.
New in version 2.6.
- save_to_file(filename)[source]¶
- Parameters:
filename (
str
) – the name of the file to write to- Raises:
- Returns:
true if successful, false otherwise
- Return type:
Writes the contents of self to filename using [func`GLib`.file_set_contents].
If you need stricter guarantees about durability of the written file than are provided by [func`GLib`.file_set_contents], use [func`GLib`.file_set_contents_full] with the return value of [method`GLib`.KeyFile.to_data].
This function can fail for any of the reasons that [func`GLib`.file_set_contents] may fail.
New in version 2.40.
- set_boolean(group_name, key, value)[source]¶
-
Associates a new boolean value with key under group_name.
If key cannot be found then it is created.
New in version 2.6.
- set_boolean_list(group_name, key, list)[source]¶
- Parameters:
Associates a list of boolean values with key under group_name.
If key cannot be found then it is created.
New in version 2.6.
- set_comment(group_name, key, comment)[source]¶
- Parameters:
- Raises:
- Returns:
true if the comment was written, false otherwise
- Return type:
Places a comment above key from group_name.
If key is
NULL
then comment will be written above group_name. If both key and group_name areNULL
, then comment will be written above the first group in the file.Note that this function prepends a
#
comment marker to each line of comment.New in version 2.6.
- set_double(group_name, key, value)[source]¶
-
Associates a new double value with key under group_name.
If key cannot be found then it is created.
New in version 2.12.
- set_double_list(group_name, key, list)[source]¶
- Parameters:
Associates a list of double values with key under group_name.
If key cannot be found then it is created.
New in version 2.12.
- set_int64(group_name, key, value)[source]¶
-
Associates a new integer value with key under group_name.
If key cannot be found then it is created.
New in version 2.26.
- set_integer(group_name, key, value)[source]¶
-
Associates a new integer value with key under group_name.
If key cannot be found then it is created.
New in version 2.6.
- set_integer_list(group_name, key, list)[source]¶
- Parameters:
Associates a list of integer values with key under group_name.
If key cannot be found then it is created.
New in version 2.6.
- set_list_separator(separator)[source]¶
- Parameters:
separator (
int
) – the separator
Sets the character which is used to separate values in lists.
Typically
;
or,
are used as separators. The default list separator is;
.New in version 2.6.
- set_locale_string(group_name, key, locale, string)[source]¶
- Parameters:
Associates a string value for key and locale under group_name.
If the translation for key cannot be found then it is created.
If locale is
C
then the untranslated value is set (since GLib 2.84).New in version 2.6.
- set_locale_string_list(group_name, key, locale, list)[source]¶
- Parameters:
Associates a list of string values for key and locale under group_name.
If locale is
C
then the untranslated value is set (since GLib 2.84).If the translation for key cannot be found then it is created.
New in version 2.6.
- set_string(group_name, key, string)[source]¶
-
Associates a new string value with key under group_name.
If key cannot be found then it is created. If group_name cannot be found then it is created. Unlike [method`GLib`.KeyFile.set_value], this function handles characters that need escaping, such as newlines.
New in version 2.6.
- set_string_list(group_name, key, list)[source]¶
- Parameters:
Associates a list of string values for key under group_name.
If key cannot be found then it is created. If group_name cannot be found then it is created.
New in version 2.6.
- set_uint64(group_name, key, value)[source]¶
-
Associates a new integer value with key under group_name.
If key cannot be found then it is created.
New in version 2.26.
- set_value(group_name, key, value)[source]¶
-
Associates a new value with key under group_name.
If key cannot be found then it is created. If group_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), use [method`GLib`.KeyFile.set_string].
New in version 2.6.