示例#1
0
def glob0(dirname, basename):
    from qrc_pathlib import QrcPath

    pathname = str(QrcPath(':').joinpath(dirname, basename))

    if not basename:
        # `os.path.split()` returns an empty basename for paths ending with a
        # directory separator.  'q*x/' should match only directories.
        if QtCore.QFileInfo(pathname).isDir():
            yield pathname
    else:
        if QtCore.QFileInfo(pathname).exists():
            yield pathname
示例#2
0
def iglob(pathname, *, recursive=False):
    from qrc_pathlib import QrcPath

    if not pathname:
        return

    if pathname.startswith(':'):
        pathname = pathname[1:]

    dirname, basename = os.path.split(pathname)

    if not has_magic(pathname):
        if not dirname:
            if QtCore.QFileInfo(':' + pathname).exists():
                yield str(QrcPath(':').joinpath(pathname))
        else:
            if QtCore.QFileInfo(':' + pathname).isDir():
                yield str(QrcPath(':').joinpath(pathname))

        return

    if not dirname:
        if recursive and _isrecursive(basename):
            yield from glob2(dirname, basename)
        else:
            yield from glob1(dirname, basename)

        return

    dirs = iglob(dirname, recursive=recursive)

    if has_magic(basename):
        if recursive and _isrecursive(basename):
            glob_in_dir = glob2
        else:
            glob_in_dir = glob1
    else:
        glob_in_dir = glob0

    for dirname in dirs:
        for name in glob_in_dir(dirname, basename):
            yield name
示例#3
0
def glob1(dirname, pattern):
    """
    Enumerate given dir_path by filtering it using pattern.

    @type dir_path: QrcPath
    """
    from qrc_pathlib import QrcPath

    if not dirname:
        dirname = ':'

    pathname = str(QrcPath(':').joinpath(dirname))

    if not QtCore.QFileInfo(pathname).isDir():
        return

    filters = QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs | QtCore.QDir.Files
    if _ishidden(pattern):
        filters |= QtCore.QDir.Hidden

    d = QtCore.QDirIterator(pathname, [pattern], filters)
    while d.hasNext():
        yield d.next()
示例#4
0
def _rlistdir(dirname):
    from qrc_pathlib import QrcPath

    if not dirname:
        dirname = ':'

    pathname = str(QrcPath(':').joinpath(dirname))

    i = QtCore.QDirIterator(
        pathname, [], QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs
        | QtCore.QDir.Files | QtCore.QDir.Hidden,
        QtCore.QDirIterator.Subdirectories)
    while i.hasNext():
        p = i.next()
        yield p
示例#5
0
    def copy_to(self, target):
        """
        Copy file to a given path.

        @type other_path: Path
        """
        f = QtCore.QFile(str(self))

        if not f.open(QtCore.QIODevice.ReadOnly):
            raise FileNotFoundError("No such file or directory: '{}'".format(str(self)))
        else:
            try:
                if not f.copy(target.as_posix()):
                    raise IOError("unable top copy '{}' to '{}': {}".format(str(self), str(target), f.error()))
            finally:
                f.close()
示例#6
0
    def open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None):
        if self.is_dir():
            raise IsADirectoryError("Is a directory: '{}'".format(str(self)))

        if buffering not in (-1, 0):
            raise ValueError("buffering must be either -1 or 0")

        if newline not in (None, ''):
            raise ValueError("newline must be either None or ''")

        if 'b' in mode and 't' in mode:
            raise ValueError("can't have text and binary mode at once")

        allowed_mode = ['r', 'b', 't']

        for c in mode:
            if c not in allowed_mode:
                raise ValueError("invalid mode: '{}'".format(mode))
            else:
                allowed_mode.remove(c)

        opening_mode = QtCore.QIODevice.ReadOnly

        if buffering == 0:
            opening_mode |= QtCore.QIODevice.Unbuffered

        if newline is None:
            opening_mode |= QtCore.QIODevice.Text

        f = QtCore.QFile(str(self))
        if f.open(opening_mode):
            try:
                data = f.readAll().data()  # QRC resource are compressed, there is no point of trying to reuse RAM

                if 'b' in mode:
                    return BytesIO(data)
                else:
                    return StringIO(data.decode(encoding=encoding or 'utf-8', errors=errors or 'strict'))
            finally:
                f.close()
        else:
            raise FileNotFoundError("No such file or directory: '{}'".format(str(self)))
示例#7
0
    def resolve(self, path):
        if not QtCore.QFile(str(path)).exists():
            raise FileNotFoundError("No such file or directory: '{}'".format(
                str(path)))

        return QtCore.QFileInfo(str(path)).canonicalFilePath()
示例#8
0
 def iterdir(self):
     iter = QtCore.QDirIterator(str(self), QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs | QtCore.QDir.Files)
     while iter.hasNext():
         yield QrcPath(iter.next())
示例#9
0
 def _init(self, template=None):
     self._stat = QtCore.QFileInfo(str(self))
     self._closed = False