在用户首选应用程序(xdg open replacement)中打开url或文件。

pyxdg-open的Python项目详细描述


python xdg open(从xdg-utils)克隆。基本上决定了如何打开和 然后使用系统桌面文件打开给定的URL。它具有可定制的搜索功能 顺序、自定义mime到应用程序调用或到桌面文件映射以及 打开多个文件。它还支持列表文件(如defaults.list),因此 它应该可以在默认的config下正常工作。

这样做的动机是提供一个文件打开器,它在没有流行的 桌面环境,仍然支持Desktop Entry Specification。而且是 很好地提供了一些没有过多转义或其他输入的东西 相关错误,例如xdg-open: be more paranoid in escaping。这太多了 在python中比在bash中更容易实现。

示例config文件:

# 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

“搜索顺序”定义桌面文件搜索的顺序。默认情况下 首先从位于 “桌面文件路径”(必须定义“列表文件”)。第二个桌面文件 通常从定义的“桌面文件路径”中搜索。

pyxdg open还支持类似于mimi的自定义配置,这些配置可以添加到 “搜索订单”列表。

如果在“桌面文件路径”中搜索桌面文件,则第一个桌面 已选择列出匹配mime类型的文件。如果有很多 首选与特定mime类型和一个桌面文件匹配的桌面文件, 然后选择将mime类型、桌面文件对添加到mimeapps.list或 defaults.list文件。另一种方法是将自己的自定义映射添加到配置 从示例中看到的文件,并在 “搜索订单”列表。

示例

打印所有找到的桌面文件,这些文件在不真正打开的情况下打开pdf文件 什么都行。有助于查找所有可能的桌面文件,在本例中为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]

假设我的配置文件中有以下内容:

...search_order=my_own_mappings,
               list_files,
               desktop_file_paths[my_own_mappings]application/pdf=zathura.desktopaudio/=vlc.desktop...

现在运行following可以用两个参数正确运行vlc,这样两个音频 曲目最终出现在VLC播放列表中。这是因为默认的“vlc.desktop”文件 在Exec key值中有'u'。例如,如果这是“u”或“f”, 将同时启动两个VLC实例来播放“track01.mp3”和 track02.mp3:

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

由于“zathura.desktop”在exec字符串中包含“%f”,因此只有一个文件是 accepted和pyxdg open启动两个实例:

$ 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

这也适用于以下情况,如图所示:

$ 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

易于安装

简单用户安装示例:

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

可执行控制台脚本名可以用‘–exec_name=’控制 争论。默认值是“pyxdg open”。本例中的可执行脚本 放置在‘$home/.local/bin’中,如果 需要。这也会安装所需的依赖项。

archlinux pkgbuild

pyxdg open的pkgbuild文件及其依赖项可以从 abs-repopyxdg-open-gitpython-desktop-file-parser-git

默认情况下,pyxdg open git在使用时与xdg utils冲突 –exec_name='xdg打开'abs repo还包含一个用于xdg utils的pkgbuild 没有“xdg打开”的: xdg-utils-without-open

可选依赖项

目前,pyxdg open在使用“python magic”时比在默认情况下更好地工作 python mimetypes库,仅在找不到“python magic”时使用。

待办事项

  • 记录类似程序的差异
  • 读取桌面缓存文件
  • 添加缺少的xdg open功能

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在一个问题被连续正确回答三次/并添加差异后,我如何将程序循环回开始   Java中未实例化的匿名类   java如何在Android中录制视频,只允许横向模式和最长时间录制时间   java从另一个活动发送实时消息   多线程java线程和互斥   java禁用Spring安全日志   JAVA伊奥。StreamCorruptedException:在与子级和父级ProcessBuilder通信时写入子级中的标准输出时,流头无效   使用Java(HttpURLConnection)对Restheart进行身份验证(对于Mongodb)   java如何解决Jenkins中的SAXParseException?   java为什么我需要mockito来测试Spring应用程序?   计算sin-cos和tan时缺乏精度(java)   java Hibernate。不同项目中相同一对一映射的不同行为   java图像滑块:如何使用JavaFX将图像放在另一个图像上   java Mockito在使用when时抛出NotAMockException   http Java servlet发送回响应