本文整理匯總了Python中zim.fs.File.readlines方法的典型用法代碼示例。如果您正苦於以下問題:Python File.readlines方法的具體用法?Python File.readlines怎麽用?Python File.readlines使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zim.fs.File
的用法示例。
在下文中一共展示了File.readlines方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_template
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import readlines [as 別名]
def get_template(format, template):
'''Returns a Template object for a template name, file path, or File object'''
# NOTE: here the "category" needs to be a format at the same time !
if isinstance(template, File):
file = template
else:
if not is_path_re.match(template):
file = None
path = list(data_dirs(('templates', format)))
path.reverse()
for dir in path:
for basename in dir.list():
name = basename.rsplit('.')[0] # robust if no '.' in basename
if name == template:
file = dir.file(basename)
if file.exists(): # is a file
break
if not file:
file = File(template)
else:
file = File(template)
logger.info('Loading template from: %s', file)
if not file.exists():
raise AssertionError, 'No such file: %s' % file
basename, ext = file.basename.rsplit('.', 1)
resources = file.dir.subdir(basename)
return Template(file.readlines(), format, name=file.path, resources_dir=resources)
示例2: get_template
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import readlines [as 別名]
def get_template(format, name):
'''Returns a Template object for a tempalte name or a file path'''
if is_path_re.match(name):
file = File(name)
else:
templates = list_templates(format)
#~ if not name in templates: FIXME exception type
#~ raise
file = File(templates[name])
logger.info('Loading template from: %s', file)
return Template(file.readlines(), format, name=file)
示例3: ConfigFile
# 需要導入模塊: from zim.fs import File [as 別名]
# 或者: from zim.fs.File import readlines [as 別名]
class ConfigFile(object):
'''Container object for a config file
Maps to a "base" file in the home folder, used to write new values,
and one or more default files, e.g. in C{/usr/share/zim}, which
are the fallback to get default values
@ivar file: the underlying file object for the base config file
in the home folder
@note: this class implement similar API to the L{File} class but
is explicitly not a sub-class of L{File} because config files should
typically not be moved, renamed, etc. It just implements the reading
and writing methods.
'''
def __init__(self, path, file=None):
'''Constructor
@param path: either basename as string or tuple with relative path,
is resolved relative to the default config dir for zim.
@param file: optional argument for some special case to
override the base file in the home folder.
'''
if isinstance(path, basestring):
path = (path,)
self._path = tuple(path)
if file:
self.file = file
else:
self.file = File((XDG_CONFIG_HOME, 'zim') + self._path)
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.file.path)
def __eq__(self, other):
return isinstance(other, ConfigFile) \
and other._path == self._path \
and other.file == self.file
@property
def basename(self):
return self.file.basename
def default_files(self):
'''Generator that yields default config files (read-only) to
use instead of the standard file when it is still empty.
Typically only the first one is used.
'''
for dir in config_dirs():
default = dir.file(self._path)
if default.exists():
yield default
def touch(self):
'''Ensure the custom file in the home folder exists. Either by
copying a default config file, or touching an empty file.
Intended to be called before trying to edit the file with an
external editor.
'''
if not self.file.exists():
for file in self.default_files():
file.copyto(self.file)
break
else:
self.file.touch() # create empty file
def read(self, fail=False):
'''Read the base file or first default file
@param fail: if C{True} a L{FileNotFoundError} error is raised
when neither the base file or a default file are found. If
C{False} it will return C{''} for a non-existing file.
@returns: file content as a string
'''
try:
return self.file.read()
except FileNotFoundError:
for file in self.default_files():
return file.read()
else:
if fail:
raise
else:
return ''
def readlines(self, fail=False):
'''Read the base file or first default file
@param fail: if C{True} a L{FileNotFoundError} error is raised
when neither the base file or a default file are found. If
C{False} it will return C{[]} for a non-existing file.
@returns: file content as a list of lines
'''
try:
return self.file.readlines()
except FileNotFoundError:
for file in self.default_files():
return file.readlines()
else:
if fail:
raise
else:
#.........這裏部分代碼省略.........