跳转到主要内容

位置感知应用程序启动器

项目描述

dwim 程序是一个位置感知应用程序启动器。要使用它,您需要在 ~/.dwimrc 中创建一个配置文件。此配置文件是一个简单的 Python 脚本,它定义了您想要自动启动的应用程序、应用程序的启动顺序以及某些应用程序是否仅在您的计算机位于特定的物理位置时启动。位置感知是通过匹配您的计算机连接到的网络(您的当前 网关MAC地址)的唯一属性来实现的。

每次您运行 dwim 程序时,您的 ~/.dwimrc 配置文件都会被评估,您的应用程序将自动启动。如果您再次运行 dwim,它不会启动应用程序的重复实例,但如果您退出应用程序然后再次运行 dwim,该应用程序将再次启动。

安装

dwim 软件包可在 PyPI 上找到,这意味着安装应该非常简单

$ pip install dwim

实际上安装Python包的方法有很多种(例如,用户的site-packages目录虚拟环境或者全局安装),在这里我没有打算展开讨论这个问题,所以如果你感到有些害怕,请在返回这些说明之前先了解一下你的选项;-)。

用法

使用dwim包有两种方式:作为命令行程序dwim和作为Python API。关于Python API的详细信息,请参阅Read the Docs上的API文档。下面将描述命令行界面。

请注意,在使用程序之前,您需要创建一个配置文件(见下文)。

命令行界面

用法: dwim [选项]

dwim程序是一个位置感知的应用程序启动器。要使用它,您需要创建一个位于~/.dwimrc的配置文件。这个配置文件是一个简单的Python脚本,它定义了您想要自动启动哪些应用程序,以及应用程序的启动顺序,以及是否应该根据特定的物理位置启动某些应用程序。

位置感知是通过检查您的网关(将您连接到外部世界的网络设备,通常是路由器)的MAC地址与您在~/.dwimrc中定义的已知MAC地址集合来实现的。

每次运行dwim程序时,您的~/.dwimrc配置文件都会被评估,并自动启动您的应用程序。如果您再次运行dwim,它不会启动应用程序的重复实例,但当你退出一个应用程序然后再次运行dwim时,应用程序将会再次启动。

支持选项

选项

描述

-c, --config=FILE

覆盖默认配置文件的存储位置。

-v, --verbose

增加日志详细程度(可重复)。

-q, --quiet

减少日志详细程度(可重复)。

-h, --help

显示此信息并退出。

创建配置文件

要使用dwim,您需要在~/.dwimrc处创建一个配置文件。配置文件是一个简单的Python脚本,定义了您想要自动启动哪些应用程序,应用程序应该按照什么顺序启动,以及是否应在特定物理位置启动某些应用程序。配置脚本可以访问由dwim Python包提供的函数。请参阅文档以了解可用函数。下面的示例显示了最有用的函数。

启动第一个程序

如果您想从简单的示例开始,可以尝试以下操作

launch_program('pidgin')

当您创建了上面的配置脚本并运行dwim程序时,它将在第一次运行时启动Pidgin聊天客户端。在下次运行时,将不会发生任何操作,因为Pidgin已经在运行了。

修改“正在运行”检查

默认的“正在运行”检查如下所示

# Replace `pidgin' with any program name.
pidof $(which pidgin)

这种逻辑可能不适用于所有程序。例如,在我的配置文件中,我使用包装脚本启动Dropbox客户端。一旦Dropbox客户端启动,包装脚本就会终止,因此pidof检查失败。解决方案是自定义“正在运行”检查

launch_program('dropbox start', is_running='pgrep -f "$HOME/.dropbox-dist/*/dropbox"')

上面的例子是为Dropbox客户端设计的,但相同的原理也可以应用于所有其他程序。唯一的技巧是找到一个可以正确判断程序是否正在运行的shell命令。不幸的是,这部分不能以完全通用的方式自动化。下面的高级配置示例包含更多定义自定义 pidof 检查和 pgrep -f 检查的例子。

启用位置感知

启用位置感知的第一步是将以下行添加到您的配置文件中

determine_network_location()

即使您没有向此函数传递任何信息,它也会报告您当前网关的MAC地址。这让我不必记录执行相同操作所需的shell命令 :-). 运行 dwim 命令,并注意类似以下的一行

We're not connected to a known network (unknown gateway MAC address 84:9c:a6:76:23:8e).

现在编辑您的配置文件,并更改您刚才添加的行

location = determine_network_location(home=['84:9c:a6:76:23:8e'])

现在重新运行 dwim,它将说

We're connected to the home network.

我们刚才做了什么?我们记下了当前网关的MAC地址,并将该MAC地址与名为“home”的位置关联起来。现在,在我们的配置文件中,我们可以根据我们是否连接到家庭网络来启动程序

if location == 'home':
   # Client for Music Player Daemon.
   launch_program('ario --minimized')
else:
   # Standalone music player.
   launch_program('rhythmbox')

下面的高级配置示例(我的配置文件)包含了一个结合多个网络和具有多个网关的网络的高级示例。

示例配置文件

我已经使用了 dwim(之前以Bash脚本的形式存在)多年的变体,所以我的配置文件已经增长了很多。因此,它可能提供了您可以做的有趣事物的例子

# vim: fileencoding=utf-8

# ~/.dwimrc: Profile for dwim, my location aware application launcher.
# For more information please see https://github.com/xolox/python-dwim/.

# Standard library modules.
import os
import time

# Packages provided by dwim and its dependencies.
from executor import execute
from dwim import (determine_network_location, launch_program, LaunchStatus
                  set_random_background, wait_for_internet_connection)

# This is required for graphical Vim and gnome-terminal to have nicely
# anti-aliased fonts. See http://awesome.naquadah.org/wiki/Autostart.
if launch_program('gnome-settings-daemon') == LaunchStatus.started:

    # When my window manager is initially started I need to wait for a moment
    # before launching user programs because otherwise strange things can
    # happen, for example programs that place an icon in the notification area
    # might be started in the background without adding the icon, so there's
    # no way to access the program but `dwim' will never restart the program
    # because it's already running! ಠ_ಠ
    logger.debug("Sleeping for 10 seconds to give Awesome a moment to initialize ..")
    time.sleep(10)

# Determine the physical location of this computer by matching the MAC address
# of the gateway against a set of known MAC addresses. In my own copy I've
# documented which MAC addresses belong to which devices, but that doesn't seem
# very relevant for the outside world :-)
location = determine_network_location(home=['84:9C:A6:76:23:8E'],
                                      office=['00:15:C5:5F:92:79',
                                              'B6:25:B2:19:28:61',
                                              '00:18:8B:F8:AF:33'])

# Correctly configure my multi-monitor setup based on physical location.
if location == 'home':
    # At home I use a 24" ASUS monitor as my primary screen.
    # My MacBook Air sits to the left as the secondary screen.
    execute('xrandr --output eDP1 --auto --noprimary')
    execute('xrandr --output HDMI1 --auto --primary')
    execute('xrandr --output HDMI1 --right-of eDP1')
if location == 'work':
    # At work I use a 24" LG monitor as my primary screen.
    # My Asus Zenbook sits to the right as the secondary screen.
    execute('xrandr --output eDP1 --auto')
    execute('xrandr --output HDMI1 --auto')
    execute('xrandr --output HDMI1 --left-of eDP1')

# Set a random desktop background from my collection of wallpapers. I use the
# program `feh' for this because it supports my desktop environment / window
# manager (Awesome). You can install `feh' using `sudo apt-get install feh'.
set_random_background(command='feh --bg-scale {image}',
                      directory=os.path.expanduser('~/Pictures/Backgrounds'))

# Start my favorite programs.
launch_program('gvim')
launch_program('nm-applet')
launch_program('keepassx $HOME/Documents/Passwords/Personal.kdb -min -lock',
               is_running='pgrep -f "keepassx $HOME/Documents/Passwords/Personal.kdb"')
# I actually use three encrypted key passes, two of them for work. I omitted
# those here, but their existence explains the complex is_running command.
launch_program('fluxgui', is_running='pgrep -f $(which fluxgui)')

# The remaining programs require an active internet connection.
wait_for_internet_connection()

launch_program('chromium-browser', is_running='pidof /usr/lib/chromium-browser/chromium-browser')
launch_program('pidgin')
if location == 'home':
    # Mozilla Thunderbird is only useful at home (at work IMAPS port 993 is blocked).
    launch_program('thunderbird', is_running='pidof /usr/lib/thunderbird/thunderbird')
launch_program('dropbox start', is_running='pgrep -f "$HOME/.dropbox-dist/*/dropbox"')
launch_program('spotify')

位置感知

位置感知是通过匹配您当前网络网关的MAC地址(您的路由器)来工作的。我之前还使用过公共IPv4地址,但考虑到大多数消费者都会有动态IP地址,我认为网关MAC地址是匹配的最稳定的唯一属性。

关于名称

在编程文化中,DWIM的缩写代表 Do What I Mean。链接到的维基百科文章提到 Interlisp,但我实际上是从 Perl 世界得知这个术语的。我选择这个名称来命名我的应用程序启动器是因为我喜欢让计算机系统预知我的需求。插入网络线,启动我的笔记本电脑,并在我启动时立即提供所有常用程序(取决于我的物理位置)是Do What I Mean的一个很好的例子,如果您问我的话 :-)

联系方式

dwim的最新版本可在 PyPIGitHub 上找到。文档托管在 Read the Docs 上。有关错误报告,请 GitHub 上创建问题。如果您有疑问、建议等,请随时通过 peter@peterodding.com 发送电子邮件。

许可证

本软件采用 MIT 许可证 许可。

© 2017 Peter Odding。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分发

dwim-0.3.1.tar.gz (17.3 kB 查看哈希值)

上传时间: 源代码

构建发行版

dwim-0.3.1-py2.py3-none-any.whl (18.1 kB 查看哈希值)

上传时间: Python 2 Python 3

由...

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面