Skip to main content

pyxdg-open

Python xdg-open (from xdg-utils) clone. Basically determines how to open and then opens given URL using system desktop files. It features customizable search order, custom mime to application call or to desktop file mappings and proper opening of multiple files. It also supports list files (like defaults.list), so it should work as expected out of the box with the default config.

Motivation for this is to provide file opener which works well without a popular desktop environment and still supports Desktop Entry Specification. Also it’s good to provide something which doesn’t have plethora of escaping or other input related bugs, e.g. xdg-open: be more paranoid in escaping. This is much easier and cleaner to achieve in Python than in Bash.

Example config file:

# Example config file, by default it should be located at:
# ~/.config/pyxdg-open/pyxdg-open.conf
#
# Paths where to search for desktop files in order of preference. It's a comma
# separated list.
desktop_file_paths = ~/.local/share/applications/,
                     /usr/share/applications/,
                     /usr/local/share/applications/

# List files under desktop_file_paths which are first searched for desktop
# files. If this is empty no list files are used.
list_files = mimeapps.list,
             defaults.list

# If default terminal emulator is not specified then first desktop file with
# TerminalEmulator as Category will be used.
#default_terminal_emulator = urxvtc

# Default search order. This means, first use list_files to find the appropriate
# desktop file and if not found, proceed to searching desktop files from desktop
# file paths.
search_order = list_files,
               desktop_file_paths

# An example of a custom search which can be added to the 'search_order' list,
# in this case, with name 'my_own_mappings'.
#
# Matching can be done with either mime type or file name ending. Mime type can
# be either full "type/subtype", or partial "type/" or "/subtype".
#
# Target can be either a command to which file name is appended before
# executing, or relative, or absolute path to a desktop file. Relative paths are
# interpreted to be relative to given desktop_file_paths.
# There's a special command '!bashwrap' which wraps following executable with
# bash, for more information see:
# http://wor.github.io/bash/2013/07/26/start-bash-and-terminal-program.html
# A command target supports also Desktop file specifiactions Exec value field
# keys "%f", "%F", "%u" and "%U". If no field key is given "%F" is appended to
# the command by default.
#[my_own_mappings]
#application/pdf = zathura
#video/          = vlc %U
#audio/          = vlc %U
#rar             = file-roller
#text/plain      = !bashwrap vim
#tar.gz          = /tmp/some_app.desktop
#inode/directory = urxvtc.desktop

´search_order´ defines the order of desktop file searches. By default the desktop files are first searched from the list files located in the ´desktop_file_paths´ (´list_files´ must be defined). Second the desktop files are searched normally from the defined ´desktop_file_paths´.

pyxdg-open also support mimi like custom configs which can be added to the ´search_order´ list.

If ´desktop_file_paths´ are searched for the desktop file then the first desktop file which has the matching mime type listed is selected. If there are many desktop files which match certain mime type and one desktop file is preferred, then the option is to add the mime type, desktop file pair to mimeapps.list or defaults.list file. Another way is to just add own custom mappings to the config file as seen from the example and add this sections name as first in the ´search_order´ list.

Examples

Print all found desktop files, which open PDF files without really opening anything. Useful for finding all possible desktop files, in this case for PDF:

$ pyxdg-open --dryrun --print-found some.pdf
Found desktop files:
/usr/share/applications/zathura.desktop [list_files]
/usr/share/applications/zathura.desktop [desktop_file_paths]
/home/wor/.local/share/applications/wine-extension-pdf.desktop [desktop_file_paths]
/usr/share/applications/xpdf.desktop [desktop_file_paths]
/usr/share/applications/zathura-pdf-poppler.desktop [desktop_file_paths]
/usr/share/applications/gimp.desktop [desktop_file_paths]

Let’s say that I have following in my config file:

...
search_order = my_own_mappings,
               list_files,
               desktop_file_paths

[my_own_mappings]
application/pdf = zathura.desktop
audio/          = vlc.desktop
...

Now running following runs correctly vlc with two parameters, so that both audio tracks end up in the vlc playlist. This is because default ´vlc.desktop´ file has ´%U´ in the Exec key value. If this had been, for example, ´%u´ or ´%f´, two instances of vlc would be launched simultaneously playing ´track01.mp3´ and ´track02.mp3´:

$ pyxdg-open -v1 --dryrun track01.mp3 track02.mp3
...
INFO:run_exec:613: Calling exec string: /usr/bin/vlc track01.mp3 track02.mp3

As ´zathura.desktop´ contains ´%f´ in the Exec string, only one file is accepted and pyxdg-open launches two instances:

$ pyxdg-open -v1 --dryrun test0.pdf test1.pdf
...
INFO:run_exec:613: Calling exec string: zathura /tmp/test0.pdf
INFO:run_exec:613: Calling exec string: zathura /tmp/test1.pdf

This also works correctly with following, as can be seen:

$ pyxdg-open -v1 --dryrun test0.pdf test1.pdf audio.mp3 audio.flac
...
INFO:run_exec:613: Calling exec string: /usr/bin/vlc audio.mp3 audio.flac
...
INFO:run_exec:613: Calling exec string: zathura /tmp/test0.pdf
INFO:run_exec:613: Calling exec string: zathura /tmp/test1.pdf

Easy install

Example of a simple user install:

python3 setup.py install --exec_name="xdg-open" --user

The executable console script name can be controlled with the ´–exec_name=´ argument. The default is ´pyxdg-open´. The executable script in this example is placed in ´$HOME/.local/bin´ which can be added to the PATH variable if needed. This also installs needed dependencies.

Archlinux PKGBUILD

PKGBUILD files for pyxdg-open and it’s dependencies can be found from my abs-repo: pyxdg-open-git and python-desktop-file-parser-git

By default the pyxdg-open-git conflicts with xdg-utils as it uses –exec_name=´xdg-open´. The abs repo also contains a PKGBUILD for xdg-utils which does not have ´xdg-open´: xdg-utils-without-open

Dependencies

Optional Dependencies

At the moment pyxdg-open works better with ´python-magic´ than, with the default python mimetypes library which is used only if ´python-magic´ is not found.

Similar Programs

List of xdg-open replacements on Archlinux wiki

TODO

  • Document differences to similar programs

  • Read desktop cache files

  • Add missing xdg-open functionality

Metadata

Release files for pyxdg-open 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyxdg-open 0.2.1
File Size Uploaded Provenance Actions
pyxdg-open-0.2.1.tar.gz 29.9 kB Without provenance Details for pyxdg-open-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyxdg-open 0.2.1
File Interpreter ABI Platform Actions
pyxdg_open-0.2.1-py3-none-any.whl Python 3 none any Details for pyxdg_open-0.2.1-py3-none-any.whl

Total release size: 50.9 kB

Release files / pyxdg-open-0.2.1.tar.gz

Download URL pyxdg-open-0.2.1.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b3681d28647368497cfe1cbc49638274d6b470b062dde303eb3ad5b75b5ef307
BLAKE2b-256 checksum
How to use checksums
9cc91c96273e4d9665c12ca5893d9dd41177ad4932bebee18850c978de0ea4ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pyxdg_open-0.2.1-py3-none-any.whl

Download URL pyxdg_open-0.2.1-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b531f0ebebe5bdb77c49135f15f96babe9d7ef2856d8e3fe2644782b4de82f8
BLAKE2b-256 checksum
How to use checksums
048b9493561a8b42b3ba56d2ccf41d5e2f12ab4650fa14a71eb6c0085c7a93e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pyxdg-open 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page