我正在 github 上写一个软件。它基本上是一个带有一些额外功能的托盘图标。我想提供一段工作代码,而实际上不必让用户安装本质上是可选功能的依赖项,而且我实际上不想导入我不会使用的东西,所以我认为这样的代码将是“好的解决方案”:
---- IN LOADING FUNCTION ----
features = []
for path in sys.path:
if os.path.exists(os.path.join(path, 'pynotify')):
features.append('pynotify')
if os.path.exists(os.path.join(path, 'gnomekeyring.so')):
features.append('gnome-keyring')
#user dialog to ask for stuff
#notifications available, do you want them enabled?
dlg = ConfigDialog(features)
if not dlg.get_notifications():
features.remove('pynotify')
service_start(features ...)
---- SOMEWHERE ELSE ------
def service_start(features, other_config):
if 'pynotify' in features:
import pynotify
#use pynotify...
不过也有一些问题。如果用户格式化他的机器并安装他的操作系统的最新版本并重新部署这个应用程序,功能会突然消失而没有警告。解决方案是在配置窗口中显示:
if 'pynotify' in features:
#gtk checkbox
else:
#gtk label reading "Get pynotify and enjoy notification pop ups!"
但是,如果这是一个 mac,我怎么知道我没有让用户去寻找他们永远无法填补的依赖项?
第二个问题是:
if os.path.exists(os.path.join(path, 'gnomekeyring.so')):
问题。我可以确定该文件在所有 Linux 发行版中始终称为 gnomekeyring.so 吗?
其他人如何测试这些功能?基本问题
try:
import pynotify
except:
pynotify = disabled
是代码是全球性的,这些可能会乱七八糟,即使用户不想要 pynotify....无论如何它都已加载。
那么人们认为解决这个问题的最好方法是什么?