本文整理汇总了Python中fs.memoryfs.MemoryFS.isdir方法的典型用法代码示例。如果您正苦于以下问题:Python MemoryFS.isdir方法的具体用法?Python MemoryFS.isdir怎么用?Python MemoryFS.isdir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fs.memoryfs.MemoryFS
的用法示例。
在下文中一共展示了MemoryFS.isdir方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BigFS
# 需要导入模块: from fs.memoryfs import MemoryFS [as 别名]
# 或者: from fs.memoryfs.MemoryFS import isdir [as 别名]
#.........这里部分代码省略.........
return True, unpack(">I", g.read(4))[0]
else:
# use only 3 bytes
return True, unpack(">I", "\0" + g.read(3))[0]
return False, size
def _add_resource(self, path):
if path.endswith('/'):
path = path[:-1]
if path:
self._path_fs.makedir(path, recursive=True, allow_recreate=True)
else:
dirpath, filename = pathsplit(path)
if dirpath:
self._path_fs.makedir(dirpath, recursive=True, allow_recreate=True)
f = self._path_fs.open(path, 'w')
f.close()
def close(self):
"""Finalizes the zip file so that it can be read.
No further operations will work after this method is called."""
if hasattr(self, 'bf') and self.bf:
self.bf.close()
self.bf = _ExceptionProxy()
@synchronize
def open(self, path, mode="r", **kwargs):
path = normpath(relpath(path))
if 'r' in mode:
if self.file_mode not in 'ra':
raise OperationFailedError("open file", path=path, msg="Big file must be opened for reading ('r') or appending ('a')")
try:
return self.entries[path].getfile(self.bf)
except KeyError:
raise ResourceNotFoundError(path)
if 'w' in mode:
raise OperationFailedError("open file", path=path, msg="Big file cannot be edited ATM")
raise ValueError("Mode must contain be 'r' or 'w'")
@synchronize
def getcontents(self, path):
if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path)
try:
contents = self.entries[path].getcontents(self.bf)
except KeyError:
raise ResourceNotFoundError(path)
except RuntimeError:
raise OperationFailedError("read file", path=path, msg="Big file must be oppened with 'r' or 'a' to read")
return contents
def desc(self, path):
if self.isdir(path):
return "Dir in big file: %s" % self.big_path
else:
return "File in big file: %s" % self.big_path
def isdir(self, path):
return self._path_fs.isdir(path)
def isfile(self, path):
return self._path_fs.isfile(path)
def exists(self, path):
return self._path_fs.exists(path)
@synchronize
def makedir(self, dirname, recursive=False, allow_recreate=False):
dirname = normpath(dirname)
if self.file_mode not in "wa":
raise OperationFailedError("create directory", path=dirname, msg="Big file must be opened for writing ('w') or appending ('a')")
if not dirname.endswith('/'):
dirname += '/'
self._add_resource(dirname)
def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
return self._path_fs.listdir(path, wildcard, full, absolute, dirs_only, files_only)
@synchronize
def getinfo(self, path):
if not self.exists(path):
raise ResourceNotFoundError(path)
path = normpath(path).lstrip('/')
info = {'size': 0}
if path in self.entries:
be = self.entries[path]
info['size'] = be.realSize
info['file_size'] = be.realSize
info['stored_size'] = be.storedSize
info['is_compressed'] = be.isCompressed
info['offset'] = be.offset
info['internal_filename'] = be.filename
info['filename'] = path
return info
示例2: SlfFS
# 需要导入模块: from fs.memoryfs import MemoryFS [as 别名]
# 或者: from fs.memoryfs.MemoryFS import isdir [as 别名]
class SlfFS(FS):
"""
Implements a read-only file system on top of a SLF-file
"""
_meta = {
'thread_safe': False,
'virtual': False,
'read_only': True,
'unicode_paths': False,
'case_insensitive_paths': False,
'network': False,
'atomic.setcontents': False
}
def __init__(self, slf_filename):
super(SlfFS, self).__init__()
if isinstance(slf_filename, str):
slf_filename = os.path.expanduser(os.path.expandvars(slf_filename))
slf_filename = os.path.normpath(os.path.abspath(slf_filename))
try:
self.file_name = slf_filename
self.file = open(slf_filename, 'rb')
except FileNotFoundError as e:
raise CreateFailedError(
'Slf file not found ({0})'.format(slf_filename),
details=e
)
else:
self.file_name = 'file-like'
self.file = slf_filename
self.header = SlfHeader.from_bytes(self.file.read(SlfHeader.get_size()))
self.entries = list(map(self._read_entry, range(self.header['number_of_entries'])))
self.library_name = self.header['library_name']
self.library_path = self.header['library_path']
self.sort = self.header['sort']
self.version = self.header['version']
self._path_fs = MemoryFS()
for e in self.entries:
path = _get_normalized_filename(e['file_name']).split('/')
directory = '/'.join(path[:-1]) if len(path) > 2 else '/'
if self._path_fs.isfile(directory):
# Sometimes there exists a file that has the same name as a directory
# Solution: Rename it with a _DIRECTORY_CONFLICT suffix
self._path_fs.move(directory, directory + DIRECTORY_CONFLICT_SUFFIX)
if self._path_fs.isdir('/'.join(path)):
self._path_fs.createfile('/'.join(path) + DIRECTORY_CONFLICT_SUFFIX)
else:
self._path_fs.makedir(directory, recursive=True, allow_recreate=True)
self._path_fs.createfile('/'.join(path))
def _read_entry(self, index):
entry_size = SlfEntry.get_size()
self.file.seek(-entry_size * (self.header['number_of_entries'] - index), os.SEEK_END)
return SlfEntry.from_bytes(self.file.read(entry_size))
def __str__(self):
return '<SlfFS: {0}>'.format(self['library_name'])
def isfile(self, path):
return self._path_fs.isfile(path)
def isdir(self, path):
return self._path_fs.isdir(path)
def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
return self._path_fs.listdir(path, wildcard, full, absolute, dirs_only, files_only)
def open(self, path, mode='r', buffering=-1, encoding='ascii', errors=None, newline=None, line_buffering=False, **kwargs):
if mode != 'r' and mode != 'rb':
raise UnsupportedError(WRITING_NOT_SUPPORTED_ERROR.format('open'))
if not self.exists(path):
raise ResourceNotFoundError(path)
if self.isdir(path):
raise ResourceInvalidError(path)
slf_entry = self._get_slf_entry_for_path(path)
self.file.seek(slf_entry['offset'], os.SEEK_SET)
if mode == 'rb':
return io.BytesIO(self.file.read(slf_entry['length']))
return io.StringIO(self.file.read(slf_entry['length']).decode(encoding))
def getinfo(self, path):
if not self.exists(path):
raise ResourceNotFoundError(path)
if self.isdir(path):
return {
'size': 0
}
slf_entry = self._get_slf_entry_for_path(path)
return {
'size': slf_entry['length'],
'modified_time': slf_entry['time']
}
#.........这里部分代码省略.........