跳转到主要内容

提供类似maildir的文件访问

项目描述

Filestore

filestore是一种在无需加锁的情况下处理多个进程中的文件的方法。

初始化FileStore

在临时区域创建filestore

>>> import tempfile, os
>>> temp_dir = tempfile.mkdtemp()
>>> store_dir = os.path.join(temp_dir, 'store1')
>>> os.mkdir(store_dir)
>>> from gocept.filestore import FileStore
>>> filestore = FileStore(store_dir)
>>> filestore
<gocept.filestore.filestore.FileStore object at 0x...>

到目前为止,没有发生任何事情

>>> import os
>>> os.listdir(store_dir)
[]

在使用存储之前,我们需要准备它

>>> filestore.prepare()

准备已创建tmp/new/cur目录结构

>>> sorted(os.listdir(store_dir))
['cur', 'new', 'tmp']

再次调用准备没有作用

>>> filestore.prepare()
>>> sorted(os.listdir(store_dir))
['cur', 'new', 'tmp']

如果删除了 store_dir,则会通过调用 prepare 重新创建。

>>> import shutil
>>> shutil.rmtree(store_dir)
>>> os.listdir(temp_dir)
[]
>>> filestore.prepare()
>>> os.listdir(temp_dir)
['store1']
>>> sorted(os.listdir(store_dir))
['cur', 'new', 'tmp']

使用FileStore

添加文件到存储可以使用 create 方法

>>> f = filestore.create('a-file')

文件以 'w' 模式(如果未指定)创建在 'tmp' 区域

>>> f.name
'.../tmp/a-file'
>>> f.mode
'w'

我们在 tmp 区域找到了文件。注意,filestore.list 列出了带有完整路径名称的文件,因此我们可以直接将名称传递给 file/open

>>> filestore.list('tmp')
['.../tmp/a-file']

我们得到了一个普通文件,因此可以写入它

>>> _ = f.write('Die Ente bleibt draussen!')
>>> f.close()

我们已完成文件的写入,因此可以将其移动到 new 空间以便其他应用程序拾取

>>> filestore.move('a-file', 'tmp', 'new')
>>> filestore.list('tmp')
[]
>>> filestore.list('new')
['.../new/a-file']

移动操作使用 os.move,它应该是原子的。当其他进程“看到”文件时,它可以直接使用它并将其移动到 'cur' 目录

>>> filestore.move('a-file', 'new', 'cur')
>>> filestore.list('new')
[]
>>> filestore.list('cur')
['.../cur/a-file']

文件也可以复制

>>> filestore.copy('a-file', 'cur', 'tmp')
>>> filestore.list('cur')
['.../cur/a-file']
>>> filestore.list('tmp')
['.../tmp/a-file']

最后,可以删除文件

>>> filestore.remove('a-file', 'cur')
>>> filestore.list('cur')
[]

清理

测试后删除临时目录

>>> import shutil
>>> shutil.rmtree(store_dir)

变更

1.0 (2023-07-14)

  • 取消对 Python 2.7、3.5 和 3.6 的支持。

0.5 (2023-03-16)

  • 添加对 Python 3.9、3.10 和 3.11 的支持。

  • 使用 GitHub actions 作为 CI。

0.4 (2019-11-29)

  • 将仓库迁移到 Bitbucket。

  • 将仓库迁移到 GitHub。

  • 已使 Python 3 兼容(已与 Python 2.7、3.7 和 3.8 进行测试)。

  • 用 tox 替换 bootstrap/buildout。

  • 将测试覆盖率提高到 100%。

0.3 (2009-10-08)

  • 添加了 copy() 方法。

0.2 (2007-08-30)

  • 首次公开发布。

项目详情


下载文件

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

源代码分发

gocept.filestore-1.0.tar.gz (6.3 kB 查看哈希)

上传时间 源代码

构建分发

gocept.filestore-1.0-py2.py3-none-any.whl (7.4 kB 查看哈希)

上传时间 Python 2 Python 3

由以下机构支持

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