本文整理汇总了Python中zope.annotation.interfaces.IAnnotations.openDataFile方法的典型用法代码示例。如果您正苦于以下问题:Python IAnnotations.openDataFile方法的具体用法?Python IAnnotations.openDataFile怎么用?Python IAnnotations.openDataFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zope.annotation.interfaces.IAnnotations
的用法示例。
在下文中一共展示了IAnnotations.openDataFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: openFileReference
# 需要导入模块: from zope.annotation.interfaces import IAnnotations [as 别名]
# 或者: from zope.annotation.interfaces.IAnnotations import openDataFile [as 别名]
def openFileReference(transmogrifier, ref):
"""
Get an open file handle in one of the following forms:
* importcontext:file.txt
* dotted.package:file.txt
* /file/system/file.txt
Where "importcontext:" means find the file in the GS import context.
Return None if there was no file to be found
"""
if ref.startswith('importcontext:'):
try:
from collective.transmogrifier.genericsetup import IMPORT_CONTEXT
from zope.annotation.interfaces import IAnnotations
context = IAnnotations(transmogrifier).get(IMPORT_CONTEXT, None)
(subdir, filename) = os.path.split(ref.replace('importcontext:', ''))
if subdir == '':
# Subdir of '' results import contexts looking for a ''
# directory I think
subdir = None
if hasattr(context, "openDataFile"):
return context.openDataFile(filename, subdir=subdir)
if hasattr(context, "readDataFile"):
import StringIO
return StringIO.StringIO(
context.readDataFile(filename, subdir=subdir))
except ImportError:
return None
# Either no import context or not there.
filename = resolvePackageReferenceOrFile(ref)
if os.path.isfile(filename):
return open(filename, 'r')
return None