Esempio n. 1
0
 def test_iterdir(self):
     self.assertEqual(
         set(QrcPath(':dir').iterdir()),
         {
             QrcPath(':dir/1.txt'),
             QrcPath(':dir/2.txt'),
             QrcPath(':dir/image.jpeg')
         })
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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')
            }
        )
Esempio n. 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')
            }
        )
Esempio n. 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')
Esempio n. 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()
Esempio n. 9
0
 def test_is_socket(self):
     self.assertFalse(QrcPath(':').is_socket())
Esempio n. 10
0
 def test_is_symlink(self):
     self.assertFalse(QrcPath(':').is_symlink())
Esempio n. 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())
Esempio n. 12
0
 def test_replace(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').replace('new_name')
Esempio n. 13
0
 def test_mkdir(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').mkdir()
Esempio n. 14
0
 def test_cwd(self):
     with self.assertRaises(NotImplementedError):
         QrcPath.cwd()
Esempio n. 15
0
 def text_exists(self):
     self.assertTrue(QrcPath(':'))
Esempio n. 16
0
 def test_unlink(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').unlink()
Esempio n. 17
0
 def test_write_text(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').write_text('data')
Esempio n. 18
0
 def test_touch(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').touch()
Esempio n. 19
0
 def test_symlink_to(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').symlink_to('path')
Esempio n. 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')))
Esempio n. 21
0
 def test_is_fifo(self):
     self.assertFalse(QrcPath(':').is_fifo())
Esempio n. 22
0
 def test_home(self):
     with self.assertRaises(NotImplementedError):
         QrcPath.home()
Esempio n. 23
0
 def test_is_char_device(self):
     self.assertFalse(QrcPath(':').is_char_device())
Esempio n. 24
0
 def test_read_text(self):
     self.assertEqual(QrcPath(':привет.txt').read_text(), 'привет\n')
Esempio n. 25
0
 def test_lchmod(self):
     with self.assertRaises(PermissionError):
         QrcPath(':').lchmod(0o777)
Esempio n. 26
0
 def test_group(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').group()
Esempio n. 27
0
 def test_lstat(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').lstat()
Esempio n. 28
0
 def test_is_dir(self):
     self.assertTrue(QrcPath(':').is_dir())
Esempio n. 29
0
 def test_expand_user(self):
     with self.assertRaises(NotImplementedError):
         QrcPath(':').expanduser()
Esempio n. 30
0
    def test_resolve(self):
        self.assertEqual(QrcPath(':привет.txt').resolve(), QrcPath(':привет.txt'))

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