Example #1
0
 def test_iterdir(self):
     self.assertEqual(
         set(QrcPath(':dir').iterdir()),
         {
             QrcPath(':dir/1.txt'),
             QrcPath(':dir/2.txt'),
             QrcPath(':dir/image.jpeg')
         })
Example #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
Example #3
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
Example #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
Example #5
0
    def test_glob(self):
        self.assertEqual(
            set(QrcPath(':').glob('*.txt')),
            {
                QrcPath(':42.txt'),
                QrcPath(':привет.txt')
            }
        )

        self.assertEqual(
            set(QrcPath(':').glob('**/*.txt')),
            {
                QrcPath(':42.txt'),
                QrcPath(':привет.txt'),
                QrcPath(':dir/1.txt'),
                QrcPath(':dir/2.txt')
            }
        )
Example #6
0
    def test_rglob(self):
        self.assertEqual(
            set(QrcPath(':').rglob('*.txt')),
            {
                QrcPath(':42.txt'),
                QrcPath(':привет.txt'),
                QrcPath(':dir/1.txt'),
                QrcPath(':dir/2.txt')
            }
        )

        self.assertEqual(
            set(QrcPath(':').rglob('image.*')),
            {
                QrcPath(':image.png'),
                QrcPath(':dir/image.jpeg')
            }
        )
Example #7
0
    def test_open(self):
        with self.assertRaises(IsADirectoryError):
            QrcPath(':').open()

        with self.assertRaises(FileNotFoundError):
            QrcPath(':doesnotexist.txt').open()

        with self.assertRaises(ValueError):
            QrcPath(':42.txt').open('rw')

        with self.assertRaises(ValueError):
            QrcPath(':42.txt').open(buffering=100)

        with self.assertRaises(ValueError):
            QrcPath(':42.txt').open(newline='\r')

        with QrcPath(':42.txt').open('rb') as f:
            self.assertEqual(f.read(), b'42\n')

        with QrcPath(':42.txt').open('r') as f:
            self.assertEqual(f.read(), '42\n')
Example #8
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()
Example #9
0
 def test_is_socket(self):
     self.assertFalse(QrcPath(':').is_socket())
Example #10
0
 def test_is_symlink(self):
     self.assertFalse(QrcPath(':').is_symlink())
Example #11
0
 def test_is_file(self):
     self.assertTrue(QrcPath(':42.txt').is_file())
     self.assertTrue(QrcPath(':привет.txt').is_file())
     self.assertTrue(QrcPath(':dir/1.txt').is_file())
Example #12
0
 def test_replace(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').replace('new_name')
Example #13
0
 def test_mkdir(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').mkdir()
Example #14
0
 def test_cwd(self):
     with self.assertRaises(NotImplementedError):
         QrcPath.cwd()
Example #15
0
 def text_exists(self):
     self.assertTrue(QrcPath(':'))
Example #16
0
 def test_unlink(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').unlink()
Example #17
0
 def test_write_text(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').write_text('data')
Example #18
0
 def test_touch(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').touch()
Example #19
0
 def test_symlink_to(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').symlink_to('path')
Example #20
0
    def test_samefile(self):
        self.assertTrue(QrcPath(':привет.txt').samefile(QrcPath(':привет.txt')))
        self.assertTrue(QrcPath(':/привет.txt').samefile(QrcPath(':привет.txt')))

        self.assertTrue(QrcPath(':dir').samefile(QrcPath(':dir')))
        self.assertTrue(QrcPath(':dir').samefile(QrcPath(':dir/')))
        self.assertTrue(QrcPath(':/dir').samefile(QrcPath(':dir')))

        self.assertFalse(QrcPath(':42.txt').samefile(QrcPath(':привет.txt')))
Example #21
0
 def test_is_fifo(self):
     self.assertFalse(QrcPath(':').is_fifo())
Example #22
0
 def test_home(self):
     with self.assertRaises(NotImplementedError):
         QrcPath.home()
Example #23
0
 def test_is_char_device(self):
     self.assertFalse(QrcPath(':').is_char_device())
Example #24
0
 def test_read_text(self):
     self.assertEqual(QrcPath(':привет.txt').read_text(), 'привет\n')
Example #25
0
 def test_lchmod(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').lchmod(0o777)
Example #26
0
 def test_group(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').group()
Example #27
0
 def test_lstat(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').lstat()
Example #28
0
 def test_is_dir(self):
     self.assertTrue(QrcPath(':').is_dir())
Example #29
0
 def test_expand_user(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').expanduser()
Example #30
0
    def test_resolve(self):
        self.assertEqual(QrcPath(':привет.txt').resolve(), QrcPath(':привет.txt'))

        with self.assertRaises(FileNotFoundError):
            QrcPath(':doesnotexist.txt').resolve()