一个简单的包装。没有花哨的铃铛和口哨,只有一个带有cTypes的文字包装。只有127行代码!

inotify_simple的Python项目详细描述


inotify_simple是一个简单的python包装 inotify。不 花哨的铃铛和口哨,只是一个带有cTypes的文字包装。只有122 代码行!

inotify_init()被包装为一个类,它只不过是保持 生成的inotify文件描述符。提供了read()方法 它从文件描述符中读取可用数据并返回事件 用^{tt5}解包后作为namedtuple对象$ 模块。inotify_add_watch()inotify_rm_watch()被包装 不做任何更改,获取并返回监视描述符整数 调用代码应该能够跟踪自身,就像一个 使用c中的inotify。与python 2.7或python一起使用>;=3.2。

View on PyPIFork me on github| Read the docs

安装

要安装inotify_simple,请运行:

$ pip3 install inotify_simple

或从源安装:

$ python3 setup.py install

注意:如果在python<;3.4上,则需要后端口的enum34 module

inotify_simple是少量代码,不太可能更改太多 在将来,直到inotify本身或python改变,所以您还可以 只需复制并粘贴到您的项目中,以避免额外的依赖性 风险很低。

简介

外面有很多不起眼的Python包装。I found them all unsatisfactory。大多数都太高级了 对我来说,他们提供的便利实际上是有限的 一种是用作者想象之外的其他方式使用inotify。 其他是c扩展,需要为不同的平台编译 以及python版本,而不是使用cTypes的纯python模块。这个 一个是相当低级的,实际上只是做了inotify本身所做的事情 再也没有了。所以如果我写得对的话,希望它会一直存在 无需更改、重新编译或 注意我。

示例用法

importosfrominotify_simpleimportINotify,flagsos.mkdir('/tmp/inotify_test')inotify=INotify()watch_flags=flags.CREATE|flags.DELETE|flags.MODIFY|flags.DELETE_SELFwd=inotify.add_watch('/tmp/inotify_test',watch_flags)# Now create, delete and modify some files in the directory being monitored:os.chdir('/tmp/inotify_test')# CREATE event for a directory:os.system('mkdir foo')# CREATE event for a file:os.system('echo hello > test.txt')# MODIFY event for the file:os.system('echo world >> test.txt')# DELETE event for the fileos.system('rm test.txt')# DELETE event for the directoryos.system('rmdir foo')os.chdir('/tmp')# DELETE_SELF on the original directory. # Also generates an IGNORED event# indicating the watch was removed.os.system('rmdir inotify_test')# And see the corresponding events:foreventininotify.read():print(event)forflaginflags.from_mask(event.mask):print('    '+str(flag))

输出如下:

Event(wd=1, mask=1073742080, cookie=0, name=u'foo')
    flags.CREATE
    flags.ISDIR
Event(wd=1, mask=256, cookie=0, name=u'test.txt')
    flags.CREATE
Event(wd=1, mask=2, cookie=0, name=u'test.txt')
    flags.MODIFY
Event(wd=1, mask=512, cookie=0, name=u'test.txt')
    flags.DELETE
Event(wd=1, mask=1073742336, cookie=0, name=u'foo')
    flags.DELETE
    flags.ISDIR
Event(wd=1, mask=1024, cookie=0, name=u'')
    flags.DELETE_SELF
Event(wd=1, mask=32768, cookie=0, name=u'')
    flags.IGNORED

注意,这些标志,因为它们是用enum.IntEnum定义的, 打印为它们的名称,而不是它们的整数值。然而 它们仍然是整数,因此可以按位和或等 对于掩码等,flags.from_mask()方法按位和掩码 使用所有可能的标志并返回匹配项列表。这是给 方便且有助于调试正在发生的事件, 但性能关键代码通常应该按位屏蔽 为了不做不必要的检查,利益标志本身。

See here了解更多信息。

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

推荐PyPI第三方库


热门话题
java将spring j_spring_安全检查迁移到登录   log4j2中自定义appender中AppConfig的java问题   在将java转换为Json时是否可以忽略内部类名和变量   用java将PDF文件转换为十六进制格式   java将值从AsyncTask返回到主类   java如何导入带有部署变量类名的静态函数?   java Spring Boot@ConfigurationProperties未从环境检索属性   java为什么API调用需要80毫秒的延迟才能到达控制器(在Google应用程序引擎中)?   XML配置中MarshallingMessageConverter中的java设置MarshallTo获取无效属性“MarshallTo”   java从群中获取facebook帖子   @ComponentScan的java excludeFilters不起作用   java将单选按钮值从一个类传递到另一个类   java使JTextArea在Swing中可滚动   java Android增强现实应用程序:将球坐标旋转到设备坐标系