def glob(cls, *parts, **kwargs): """Perform a glob with validation.""" if parts: if len(parts) == 1: p = parts[0] else: p = cls.globjoin(*parts) if not cls.absolute: p = cls.globjoin(cls.tempdir, p) else: p = cls.tempdir res = glob.glob(p, **kwargs) print("RESULTS: ", res) if res: cls.assert_equal({type(r) for r in res}, {str}) cls.assert_count_equal(glob.iglob(p, **kwargs), res) if 'root_dir' in kwargs and kwargs['root_dir'] is not None: kwargs['root_dir'] = os.fsencode(kwargs['root_dir']) bres = [os.fsencode(x) for x in res] cls.assert_count_equal(glob.glob(os.fsencode(p), **kwargs), bres) cls.assert_count_equal(glob.iglob(os.fsencode(p), **kwargs), bres) if bres: cls.assert_equal({type(r) for r in bres}, {bytes}) return res
def test_glob_integrity(self): """`globmatch` must match what glob globs.""" # Number of slashes is inconsequential # Glob really looks at what is in between. Multiple slashes are the same as one separator. # UNC mounts are special cases and it matters there. self.assertTrue( all( [ glob.globmatch( x, '**/../*.{md,py}', flags=self.flags ) for x in glob.glob('**/../*.{md,py}', flags=self.flags) ] ) ) self.assertTrue( all( [ glob.globmatch( x, './**/./../*.py', flags=self.flags ) for x in glob.glob('./**/./../*.py', flags=self.flags) ] ) ) self.assertTrue( all( [ glob.globmatch( x, './///**///./../*.py', flags=self.flags ) for x in glob.glob('./**/.//////..////*.py', flags=self.flags) ] ) )
def nglob(cls, *parts, **kwargs): """Perform a glob with validation.""" if parts: if len(parts) == 1: p = parts[0] else: p = cls.globjoin(*parts) if not cls.absolute: p = cls.globjoin(cls.tempdir, p) else: p = cls.tempdir p = '!' + p if not cls.just_negative: if not cls.absolute: p = [cls.globjoin(cls.tempdir, '**'), p] else: p = ['**', p] else: p = [p] res = glob.glob(p, **kwargs) print("RESULTS: ", res) if res: cls.assert_equal({type(r) for r in res}, {str}) cls.assert_count_equal(glob.iglob(p, **kwargs), res) bres = [os.fsencode(x) for x in res] cls.assert_count_equal(glob.glob([os.fsencode(x) for x in p], **kwargs), bres) cls.assert_count_equal(glob.iglob([os.fsencode(x) for x in p], **kwargs), bres) if bres: cls.assert_equal({type(r) for r in bres}, {bytes}) return res
def test_drive_insensitive(self): """Test drive case insensitivity.""" cwd = os.getcwd() filepath = os.path.join(cwd, 'README.md') self.assertEqual([filepath], glob.glob(filepath.replace('\\', '\\\\'))) self.assertEqual( [self.RE_DRIVE.sub(lambda m: m.group(0).upper(), filepath)], glob.glob(filepath.replace('\\', '\\\\').upper())) self.assertEqual( [self.RE_DRIVE.sub(lambda m: m.group(0).lower(), filepath)], glob.glob(filepath.replace('\\', '\\\\').lower()))
def _list_local(uri, globstr, local=None): """ List contents of local URI. Args: uri: parsed URI to list. local: local context options. Returns: On success: a list of filenames (basenames only). On failure: False. """ prefix_length = len(uri['chopped_path'])+1 #recursive = True if '**' in globstr else False try: file_list = [ item[prefix_length:] for item in glob.glob( uri['chopped_path']+'/'+globstr, flags=glob.EXTGLOB|glob.GLOBSTAR ) if item[prefix_length:] ] except OSError as err: Log.an().error( 'cannot get file list for uri: %s [%s]', uri['chopped_uri'], str(err) ) return False return file_list
def test_root(self): """Test that `glob` translates the root properly.""" # On Windows, this should translate to the current drive. # On Linux/Unix, this should translate to the root. # Basically, we should not return an empty set. self.assertTrue(len(glob.glob('/*')) > 0)
def test_negateall_bytes(self): """Negate applied to all files.""" for file in glob.glob(b'!**/', flags=glob.N | glob.NEGATEALL | glob.G, root_dir=os.fsencode(self.tempdir)): self.assert_equal(os.path.isdir(file), False)
def test_selflink(self): """Test self links.""" tempdir = TESTFN + "_dir" os.makedirs(tempdir) self.addCleanup(shutil.rmtree, tempdir) with change_cwd(tempdir): os.makedirs('dir') create_empty_file(os.path.join('dir', 'file')) os.symlink(os.curdir, os.path.join('dir', 'link')) results = glob.glob('**', flags=self.DEFAULT_FLAGS) self.assertEqual(len(results), len(set(results))) results = set(results) depth = 0 while results: path = os.path.join(*(['dir'] + ['link'] * depth)) self.assertIn(path, results) results.remove(path) if not results: break path = os.path.join(path, 'file') self.assertIn(path, results) results.remove(path) depth += 1 results = glob.glob(os.path.join('**', 'file'), flags=self.DEFAULT_FLAGS) self.assertEqual(len(results), len(set(results))) results = set(results) depth = 0 while results: path = os.path.join(*(['dir'] + ['link'] * depth + ['file'])) self.assertIn(path, results) results.remove(path) depth += 1 results = glob.glob(self.globjoin('**', ''), flags=self.DEFAULT_FLAGS) self.assertEqual(len(results), len(set(results))) results = set(results) depth = 0 while results: path = os.path.join(*(['dir'] + ['link'] * depth + [''])) self.assertIn(path, results) results.remove(path) depth += 1
def test_drive_sensitive(self): """Test drive case sensitivity (they'll be insensitive regardless of case flag).""" cwd = os.getcwd() filepath = os.path.join(cwd, 'README.md') self.assertEqual([filepath], glob.glob(filepath.replace('\\', '\\\\'), flags=glob.C)) self.assertEqual( [self.RE_DRIVE.sub(lambda m: m.group(0).upper(), filepath)], glob.glob(self.RE_DRIVE.sub(lambda m: m.group(0).upper(), filepath).replace('\\', '\\\\'), flags=glob.C)) self.assertEqual( [self.RE_DRIVE.sub(lambda m: m.group(0).lower(), filepath)], glob.glob(self.RE_DRIVE.sub(lambda m: m.group(0).lower(), filepath).replace('\\', '\\\\'), flags=glob.C))
def test_tilde_user(self): """Test tilde user cases.""" user = os.path.basename(os.path.expanduser('~')) files = os.listdir(os.path.expanduser('~{}'.format(user))) self.assertEqual( len(glob.glob('~{}/*'.format(user), flags=glob.T | glob.D)), len(files))
def test_start(self): """Test that starting directory/files are handled properly.""" self.assertEqual( sorted(['docs', 'wcmatch', 'readme.md']), sorted([ each.lower() for each in glob.glob(['BAD', 'docs', 'WCMATCH', 'readme.MD'], flags=glob.I) ]))
def set_fastq_dict(self, fastq_path): try: base_dir = fastq_path.rsplit("/", 2)[0] + "/" name_format = fastq_path.rsplit("/", 1)[-1] except: raise exceptions.IncorrectPathError(fastq_path) list_files = wcmatchglob.glob(fastq_path, flags=wcmatchglob.BRACE) if not list_files: raise exceptions.IncorrectPathError(fastq_path) list_samples = list(set([file.split("/")[-2] for file in list_files])) self.fastq_dict = {} if self.single_end: for sample in list_samples: fastq_specific = wcmatchglob.glob(base_dir + sample + "/" + name_format, flags=wcmatchglob.BRACE) if len(fastq_specific) > 1: print( f"More than 1 matching fastq file for sample {sample} in single_end mode. Sample ignored." ) else: self.fastq_dict[sample] = fastq_specific else: for sample in list_samples: fastq_specific = wcmatchglob.glob(base_dir + sample + "/" + name_format, flags=wcmatchglob.BRACE) if len(fastq_specific) > 2: print( f"More than 2 matching fastq files for sample {sample} in paired_end mode. Sample ignored." ) elif len(fastq_specific) < 2: print( f"Only than 1 matching fastq file for sample {sample} in paired_end mode. Sample ignored." ) else: self.fastq_dict[sample] = fastq_specific
def test_tilde_disabled(self): """Test when tilde is disabled.""" self.assertEqual(len(glob.glob('~/*', flags=glob.D)), 0)
def test_cwd_root_dir_pathlike_bytes(self): """Test root level glob when we switch directory via `root_dir` with a path-like object.""" self.assert_equal( glob.glob(b'EF', root_dir=pathlib.Path(self.tempdir)), [b'EF'])
def test_cwd_root_dir(self): """Test root level glob when we switch directory via `root_dir`.""" self.assert_equal(glob.glob('EF', root_dir=self.tempdir), ['EF'])
def test_cwd(self): """Test root level glob on current working directory.""" with change_cwd(self.tempdir): self.assert_equal(glob.glob('EF'), ['EF'])
for item in response: if item.title == today: print(f"Found album: {item.title} - {item.id}") today_album_exists = True album_id = item.id break # otherwise create today's album if not today_album_exists: album_config = {'title': today, 'privacy': 'hidden'} response = client.create_album(album_config) album_id = response['id'] print(response) # Glob together all files in directory that have these extension full_list = glob.glob('*.{jpg,JPG,png,PNG,gif,GIF,mov,MOV,mp4,MP4}', flags=glob.BRACE) list.sort(full_list) print(f"There are {len(full_list)} images to parse.") # while credits available, upload each file to album image_config = {'album': album_id} for image in full_list: try: now = datetime.now() file_extension = image[-4:].lower() print( f"{now} - Attempting to upload {image}, with file extension '{file_extension}'..." ) final_path = "" if file_extension == '.mp4' or file_extension == '.mov':
def test_limit_glob(self): """Test expansion limit of `glob`.""" with self.assertRaises(_wcparse.PatternLimitException): glob.glob('{1..11}', flags=glob.BRACE, limit=10)
def test_cwd(self): """Test glob cases.""" with change_cwd(self.tempdir): self.assert_equal(glob.glob('EF'), ['EF'])
def test_tilde(self): """Test tilde.""" files = os.listdir(os.path.expanduser('~')) self.assertEqual(len(glob.glob('~/*', flags=glob.T | glob.D)), len(files))