def test_iterdir(self): self.assertEqual( set(QrcPath(':dir').iterdir()), { QrcPath(':dir/1.txt'), QrcPath(':dir/2.txt'), QrcPath(':dir/image.jpeg') })
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
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
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
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') } )
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') } )
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')
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()
def test_is_socket(self): self.assertFalse(QrcPath(':').is_socket())
def test_is_symlink(self): self.assertFalse(QrcPath(':').is_symlink())
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())
def test_replace(self): with self.assertRaises(PermissionError): QrcPath(':').replace('new_name')
def test_mkdir(self): with self.assertRaises(PermissionError): QrcPath(':').mkdir()
def test_cwd(self): with self.assertRaises(NotImplementedError): QrcPath.cwd()
def text_exists(self): self.assertTrue(QrcPath(':'))
def test_unlink(self): with self.assertRaises(PermissionError): QrcPath(':').unlink()
def test_write_text(self): with self.assertRaises(PermissionError): QrcPath(':').write_text('data')
def test_touch(self): with self.assertRaises(PermissionError): QrcPath(':').touch()
def test_symlink_to(self): with self.assertRaises(PermissionError): QrcPath(':').symlink_to('path')
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')))
def test_is_fifo(self): self.assertFalse(QrcPath(':').is_fifo())
def test_home(self): with self.assertRaises(NotImplementedError): QrcPath.home()
def test_is_char_device(self): self.assertFalse(QrcPath(':').is_char_device())
def test_read_text(self): self.assertEqual(QrcPath(':привет.txt').read_text(), 'привет\n')
def test_lchmod(self): with self.assertRaises(PermissionError): QrcPath(':').lchmod(0o777)
def test_group(self): with self.assertRaises(NotImplementedError): QrcPath(':').group()
def test_lstat(self): with self.assertRaises(NotImplementedError): QrcPath(':').lstat()
def test_is_dir(self): self.assertTrue(QrcPath(':').is_dir())
def test_expand_user(self): with self.assertRaises(NotImplementedError): QrcPath(':').expanduser()
def test_resolve(self): self.assertEqual(QrcPath(':привет.txt').resolve(), QrcPath(':привет.txt')) with self.assertRaises(FileNotFoundError): QrcPath(':doesnotexist.txt').resolve()