使用标准文件函数访问zip档案中的文件
zipaccess的Python项目详细描述
修补程序函数[模块]:文件+打开,和OS.PATH:存在+文件 启用使用标准文件函数访问ZIP存档文件中的文件。
- 套餐:
- http://pypi.python.org/pypi/zipaccess
- 项目:
- https://github.com/iki/zipaccess
- 问题:
- https://github.com/iki/zipaccess/issues
- 更新:
- https://github.com/iki/zipaccess/commits/master.atom
- 通过pip安装:
- pip install zipaccess
- 通过easy_install安装:
- easy_install zipaccess
- 源通过git:
- git clone https://github.com/iki/zipaccess
- 源通过hg-git:
- hg clone git://github.com/iki/zipaccess
用法
用作类。不要实例化它:
class AnyZipAccess(zipaccess.ZipAccess): any_zip = True # custom modifications here AnyZipAccess.enable()
或用作模块:
zipaccess.enable() zipaccess.addzip('data.zip')
或者,将zipaccess.zipaccess注册为os.zipaccess:
zipaccess.enable(register=True)
因此,其他模块可以轻松使用:
try: os.zipaccess.enable(locals()) except AttributeError: pass
关于Google App Engine(gae)
的说明在gae上,即使在本地名称空间中,内置模块的更改也不会反映出来。
要在任何GAE模块中使用ZIP访问,您需要为该模块本地变量启用它:
try: import zipaccess zipaccess.enable(locals()) except ImportError: pass
或者,仅在本地启用ZIP访问(如果已注册):
try: os.zipaccess.enable(locals()) except AttributeError: pass
或在外部启用目标模块:
zipaccess.enable([ 'babel.core', 'babel.localedata', 'tipfy.template', 'werkzeug.debug.tbtools', ]) # It imports all the modules though, which may be not what you want. # If you know, how to hook on module import, let me kindly know.
注意,OS.PATH ISO文件和OS.PATH存在全局补丁。即使在盖伊, 它们适用于给定实例的所有模块。如果某些模块支持 zip文件,它们通常首先检查是否存在常规文件,如果没有,则 拆分路径并检查是否存在zip文件。 启用ZIP访问后,常规文件检查将成功,这是正常的 只要随后使用的file()或open()函数也被修补。
示例:
The tipfy.debugger.get_loader() function checks if templates are located in a regular directory or in a zip file (^{tt5}$ usually). Then it returns tipfy.template.Loader or tipfy.template.ZipLoader accordingly. With zipaccess enabled, the standard tipfy.template.Loader will be used, and therefore ^{tt6}$ is needed to make it work.
See https://github.com/moraes/tipfy/blob/master/tipfy/debugger/__init__.py#L26.