BookmarkFile#
Added in version 2.12.
- class BookmarkFile(**kwargs)#
GBookmarkFile lets you parse, edit or create files containing bookmarks.
Bookmarks refer to a URI, along with some meta-data about the resource pointed by the URI like its MIME type, the application that is registering the bookmark and the icon that should be used to represent the bookmark. The data is stored using the Desktop Bookmark Specification.
The syntax of the bookmark files is described in detail inside the
Desktop Bookmark Specification, here is a quick summary: bookmark
files use a sub-class of the XML Bookmark Exchange Language
specification, consisting of valid UTF-8 encoded XML, under the
<xbel> root element; each bookmark is stored inside a
<bookmark> element, using its URI: no relative paths can
be used inside a bookmark file. The bookmark may have a user defined
title and description, to be used instead of the URI. Under the
<metadata> element, with its owner attribute set to
http://freedesktop.org, is stored the meta-data about a resource
pointed by its URI. The meta-data consists of the resource’s MIME
type; the applications that have registered a bookmark; the groups
to which a bookmark belongs to; a visibility flag, used to set the
bookmark as “private” to the applications and groups that has it
registered; the URI and MIME type of an icon, to be used when
displaying the bookmark inside a GUI.
Here is an example of a bookmark file: bookmarks.xbel
A bookmark file might contain more than one bookmark; each bookmark is accessed through its URI.
The important caveat of bookmark files is that when you add a new
bookmark you must also add the application that is registering it, using
add_application or set_application_info.
If a bookmark has no applications then it won’t be dumped when creating
the on disk representation, using to_data or
to_file.
Constructors#
- class BookmarkFile
- classmethod new() BookmarkFile#
Creates a new empty
BookmarkFileobject.Use
load_from_file(),load_from_data()orload_from_data_dirs()to read an existing bookmark file.Added in version 2.12.
Methods#
- class BookmarkFile
- add_application(uri: str, name: str | None = None, exec: str | None = None) None#
Adds the application with
nameandexecto the list of applications that have registered a bookmark foruriintobookmark.Every bookmark inside a
BookmarkFilemust have at least an application registered. Each application must provide a name, a command line useful for launching the bookmark, the number of times the bookmark has been registered by the application and the last time the application registered this bookmark.If
nameisNone, the name of the application will be the same returned byget_application_name(); ifexecisNone, the command line will be a composition of the program name as returned byget_prgname()and the “%u” modifier, which will be expanded to the bookmark’s URI.This function will automatically take care of updating the registrations count and timestamping in case an application with the same
namehad already registered a bookmark foruriinsidebookmark.If no bookmark for
uriis found, one is created.Added in version 2.12.
- Parameters:
uri – a valid URI
name – the name of the application registering the bookmark or
Noneexec – command line to be used to launch the bookmark or
None
- add_group(uri: str, group: str) None#
Adds
groupto the list of groups to which the bookmark foruribelongs to.If no bookmark for
uriis found then it is created.Added in version 2.12.
- Parameters:
uri – a valid URI
group – the group name to be added
- free() None#
Frees a
BookmarkFile.Added in version 2.12.
- get_added(uri: str) int#
Gets the time the bookmark for
uriwas added tobookmarkIn the event the URI cannot be found, -1 is returned and
erroris set toURI_NOT_FOUND.Added in version 2.12.
Deprecated since version 2.66:
- Use
get_added_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
- Use
- get_added_date_time(uri: str) DateTime#
Gets the time the bookmark for
uriwas added tobookmarkIn the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.66.
- Parameters:
uri – a valid URI
- get_app_info(uri: str, name: str) tuple[bool, str, int, int]#
Gets the registration information of
app_namefor the bookmark foruri. Seeset_application_info()for more information about the returned data.The string returned in
app_execmust be freed.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND. In the event that no application with nameapp_namehas registered a bookmark foruri,Falseis returned and error is set toAPP_NOT_REGISTERED. In the event that unquoting the command line fails, an error of the %G_SHELL_ERROR domain is set andFalseis returned.Added in version 2.12.
Deprecated since version 2.66:
- Use
get_application_info()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
name – an application’s name
- Use
- get_application_info(uri: str, name: str) tuple[bool, str, int, DateTime]#
Gets the registration information of
app_namefor the bookmark foruri. Seeset_application_info()for more information about the returned data.The string returned in
app_execmust be freed.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND. In the event that no application with nameapp_namehas registered a bookmark foruri,Falseis returned and error is set toAPP_NOT_REGISTERED. In the event that unquoting the command line fails, an error of the %G_SHELL_ERROR domain is set andFalseis returned.Added in version 2.66.
- Parameters:
uri – a valid URI
name – an application’s name
- get_applications(uri: str) list[str]#
Retrieves the names of the applications that have registered the bookmark for
uri.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_description(uri: str) str#
Retrieves the description of the bookmark for
uri.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_groups(uri: str) list[str]#
Retrieves the list of group names of the bookmark for
uri.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.The returned array is
Noneterminated, solengthmay optionally beNone.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_icon(uri: str) tuple[bool, str, str]#
Gets the icon of the bookmark for
uri.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_is_private(uri: str) bool#
Gets whether the private flag of the bookmark for
uriis set.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND. In the event that the private flag cannot be found,Falseis returned anderroris set toINVALID_VALUE.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_mime_type(uri: str) str#
Retrieves the MIME type of the resource pointed by
uri.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND. In the event that the MIME type cannot be found,Noneis returned anderroris set toINVALID_VALUE.Added in version 2.12.
- Parameters:
uri – a valid URI
- get_modified(uri: str) int#
Gets the time when the bookmark for
uriwas last modified.In the event the URI cannot be found, -1 is returned and
erroris set toURI_NOT_FOUND.Added in version 2.12.
Deprecated since version 2.66:
- Use
get_modified_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
- Use
- get_modified_date_time(uri: str) DateTime#
Gets the time when the bookmark for
uriwas last modified.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.66.
- Parameters:
uri – a valid URI
- get_title(uri: str | None = None) str#
Returns the title of the bookmark for
uri.If
uriisNone, the title ofbookmarkis returned.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI or
None
- get_uris() list[str]#
Returns all URIs of the bookmarks in the bookmark file
bookmark. The array of returned URIs will beNone-terminated, solengthmay optionally beNone.Added in version 2.12.
- get_visited(uri: str) int#
Gets the time the bookmark for
uriwas last visited.In the event the URI cannot be found, -1 is returned and
erroris set toURI_NOT_FOUND.Added in version 2.12.
Deprecated since version 2.66:
- Use
get_visited_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
- Use
- get_visited_date_time(uri: str) DateTime#
Gets the time the bookmark for
uriwas last visited.In the event the URI cannot be found,
Noneis returned anderroris set toURI_NOT_FOUND.Added in version 2.66.
- Parameters:
uri – a valid URI
- has_application(uri: str, name: str) bool#
Checks whether the bookmark for
uriinsidebookmarkhas been registered by applicationname.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI
name – the name of the application
- has_group(uri: str, group: str) bool#
Checks whether
groupappears in the list of groups to which the bookmark foruribelongs to.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
uri – a valid URI
group – the group name to be searched
- has_item(uri: str) bool#
Looks whether the desktop bookmark has an item with its URI set to
uri.Added in version 2.12.
- Parameters:
uri – a valid URI
- load_from_data(data: list[int]) bool#
Loads a bookmark file from memory into an empty
BookmarkFilestructure. If the object cannot be created thenerroris set to aGBookmarkFileError.Added in version 2.12.
- Parameters:
data – desktop bookmarks loaded in memory
- load_from_data_dirs(file: str) tuple[bool, str]#
This function looks for a desktop bookmark file named
filein the paths returned fromget_user_data_dir()andget_system_data_dirs(), loads the file intobookmarkand returns the file’s full path infull_path. If the file could not be loaded thenerroris set to either aFileErrororGBookmarkFileError.Added in version 2.12.
- Parameters:
file – a relative path to a filename to open and parse
- load_from_file(filename: str) bool#
Loads a desktop bookmark file into an empty
BookmarkFilestructure. If the file could not be loaded thenerroris set to either aFileErrororGBookmarkFileError.Added in version 2.12.
- Parameters:
filename – the path of a filename to load, in the GLib file name encoding
- move_item(old_uri: str, new_uri: str | None = None) bool#
Changes the URI of a bookmark item from
old_uritonew_uri. Any existing bookmark fornew_uriwill be overwritten. Ifnew_uriisNone, then the bookmark is removed.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND.Added in version 2.12.
- Parameters:
old_uri – a valid URI
new_uri – a valid URI, or
None
- remove_application(uri: str, name: str) bool#
Removes application registered with
namefrom the list of applications that have registered a bookmark foruriinsidebookmark.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND. In the event that no application with nameapp_namehas registered a bookmark foruri,Falseis returned and error is set toAPP_NOT_REGISTERED.Added in version 2.12.
- Parameters:
uri – a valid URI
name – the name of the application
- remove_group(uri: str, group: str) bool#
Removes
groupfrom the list of groups to which the bookmark foruribelongs to.In the event the URI cannot be found,
Falseis returned anderroris set toURI_NOT_FOUND. In the event no group was defined,Falseis returned anderroris set toINVALID_VALUE.Added in version 2.12.
- Parameters:
uri – a valid URI
group – the group name to be removed
- remove_item(uri: str) bool#
Removes the bookmark for
urifrom the bookmark filebookmark.Added in version 2.12.
- Parameters:
uri – a valid URI
- set_added(uri: str, added: int) None#
Sets the time the bookmark for
uriwas added intobookmark.If no bookmark for
uriis found then it is created.Added in version 2.12.
Deprecated since version 2.66:
- Use
set_added_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
added – a timestamp or -1 to use the current time
- Use
- set_added_date_time(uri: str, added: DateTime) None#
Sets the time the bookmark for
uriwas added intobookmark.If no bookmark for
uriis found then it is created.Added in version 2.66.
- Parameters:
uri – a valid URI
added – a
DateTime
- set_app_info(uri: str, name: str, exec: str, count: int, stamp: int) bool#
Sets the meta-data of application
nameinside the list of applications that have registered a bookmark foruriinsidebookmark.You should rarely use this function; use
add_application()andremove_application()instead.namecan be any UTF-8 encoded string used to identify an application.execcan have one of these two modifiers: “%f”, which will be expanded as the local file name retrieved from the bookmark’s URI; “%u”, which will be expanded as the bookmark’s URI. The expansion is done automatically when retrieving the stored command line using theget_application_info()function.countis the number of times the application has registered the bookmark; if is < 0, the current registration count will be increased by one, if is 0, the application withnamewill be removed from the list of registered applications.stampis the Unix time of the last registration; if it is -1, the current time will be used.If you try to remove an application by setting its registration count to zero, and no bookmark for
uriis found,Falseis returned anderroris set toURI_NOT_FOUND; similarly, in the event that no applicationnamehas registered a bookmark foruri,Falseis returned and error is set toAPP_NOT_REGISTERED. Otherwise, if no bookmark foruriis found, one is created.Added in version 2.12.
Deprecated since version 2.66:
- Use
set_application_info()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
name – an application’s name
exec – an application’s command line
count – the number of registrations done for this application
stamp – the time of the last registration for this application
- Use
- set_application_info(uri: str, name: str, exec: str, count: int, stamp: DateTime | None = None) bool#
Sets the meta-data of application
nameinside the list of applications that have registered a bookmark foruriinsidebookmark.You should rarely use this function; use
add_application()andremove_application()instead.namecan be any UTF-8 encoded string used to identify an application.execcan have one of these two modifiers: “%f”, which will be expanded as the local file name retrieved from the bookmark’s URI; “%u”, which will be expanded as the bookmark’s URI. The expansion is done automatically when retrieving the stored command line using theget_application_info()function.countis the number of times the application has registered the bookmark; if is < 0, the current registration count will be increased by one, if is 0, the application withnamewill be removed from the list of registered applications.stampis the Unix time of the last registration.If you try to remove an application by setting its registration count to zero, and no bookmark for
uriis found,Falseis returned anderroris set toURI_NOT_FOUND; similarly, in the event that no applicationnamehas registered a bookmark foruri,Falseis returned and error is set toAPP_NOT_REGISTERED. Otherwise, if no bookmark foruriis found, one is created.Added in version 2.66.
- Parameters:
uri – a valid URI
name – an application’s name
exec – an application’s command line
count – the number of registrations done for this application
stamp – the time of the last registration for this application, which may be
Noneifcountis 0
- set_description(uri: str | None, description: str) None#
Sets
descriptionas the description of the bookmark foruri.If
uriisNone, the description ofbookmarkis set.If a bookmark for
uricannot be found then it is created.Added in version 2.12.
- Parameters:
uri – a valid URI or
Nonedescription – a string
- set_groups(uri: str, groups: list[str] | None = None) None#
Sets a list of group names for the item with URI
uri. Each previously set group name list is removed.If
uricannot be found then an item for it is created.Added in version 2.12.
- Parameters:
uri – an item’s URI
groups – an array of group names, or
Noneto remove all groups
- set_icon(uri: str, href: str | None, mime_type: str) None#
Sets the icon for the bookmark for
uri. IfhrefisNone, unsets the currently set icon.hrefcan either be a full URL for the icon file or the icon name following the Icon Naming specification.If no bookmark for
uriis found one is created.Added in version 2.12.
- Parameters:
uri – a valid URI
href – the URI of the icon for the bookmark, or
Nonemime_type – the MIME type of the icon for the bookmark
- set_is_private(uri: str, is_private: bool) None#
Sets the private flag of the bookmark for
uri.If a bookmark for
uricannot be found then it is created.Added in version 2.12.
- Parameters:
uri – a valid URI
is_private –
Trueif the bookmark should be marked as private
- set_mime_type(uri: str, mime_type: str) None#
Sets
mime_typeas the MIME type of the bookmark foruri.If a bookmark for
uricannot be found then it is created.Added in version 2.12.
- Parameters:
uri – a valid URI
mime_type – a MIME type
- set_modified(uri: str, modified: int) None#
Sets the last time the bookmark for
uriwas last modified.If no bookmark for
uriis found then it is created.The “modified” time should only be set when the bookmark’s meta-data was actually changed. Every function of
BookmarkFilethat modifies a bookmark also changes the modification time, except forset_visited_date_time().Added in version 2.12.
Deprecated since version 2.66:
- Use
set_modified_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
modified – a timestamp or -1 to use the current time
- Use
- set_modified_date_time(uri: str, modified: DateTime) None#
Sets the last time the bookmark for
uriwas last modified.If no bookmark for
uriis found then it is created.The “modified” time should only be set when the bookmark’s meta-data was actually changed. Every function of
BookmarkFilethat modifies a bookmark also changes the modification time, except forset_visited_date_time().Added in version 2.66.
- Parameters:
uri – a valid URI
modified – a
DateTime
- set_title(uri: str | None, title: str) None#
Sets
titleas the title of the bookmark foruriinside the bookmark filebookmark.If
uriisNone, the title ofbookmarkis set.If a bookmark for
uricannot be found then it is created.Added in version 2.12.
- Parameters:
uri – a valid URI or
Nonetitle – a UTF-8 encoded string
- set_visited(uri: str, visited: int) None#
Sets the time the bookmark for
uriwas last visited.If no bookmark for
uriis found then it is created.The “visited” time should only be set if the bookmark was launched, either using the command line retrieved by
get_application_info()or by the default application for the bookmark’s MIME type, retrieved usingget_mime_type(). Changing the “visited” time does not affect the “modified” time.Added in version 2.12.
Deprecated since version 2.66:
- Use
set_visited_date_time()instead, as time_tis deprecated due to the year 2038 problem.
- Parameters:
uri – a valid URI
visited – a timestamp or -1 to use the current time
- Use
- set_visited_date_time(uri: str, visited: DateTime) None#
Sets the time the bookmark for
uriwas last visited.If no bookmark for
uriis found then it is created.The “visited” time should only be set if the bookmark was launched, either using the command line retrieved by
get_application_info()or by the default application for the bookmark’s MIME type, retrieved usingget_mime_type(). Changing the “visited” time does not affect the “modified” time.Added in version 2.66.
- Parameters:
uri – a valid URI
visited – a
DateTime
- to_file(filename: str) bool#
This function outputs
bookmarkinto a file. The write process is guaranteed to be atomic by usingfile_set_contents()internally.Added in version 2.12.
- Parameters:
filename – path of the output file