def get_files(): try: root, dirs, files = next(walk) if ignore_hidden: dirs[:] = [ d for d in dirs if not is_hidden(os.path.join(root, d)) ] except StopIteration: return None else: number_of_files = len(files) if number_of_files: mparms = { 'count': number_of_files, 'directory': root, } log.debug("Adding %(count)d files from '%(directory)r'" % mparms) self.window.set_statusbar_message(ngettext( "Adding %(count)d file from '%(directory)s' ...", "Adding %(count)d files from '%(directory)s' ...", number_of_files), mparms, translate=None, echo=None) return (os.path.join(root, f) for f in files)
def add_files(self, filenames, target=None): """Add files to the tagger.""" ignoreregex = None pattern = config.setting['ignore_regex'] if pattern: ignoreregex = re.compile(pattern) ignore_hidden = config.setting["ignore_hidden_files"] new_files = [] for filename in filenames: filename = os.path.normpath(os.path.realpath(filename)) if ignore_hidden and is_hidden(filename): log.debug("File ignored (hidden): %r" % (filename)) continue if ignoreregex is not None and ignoreregex.search(filename): log.info("File ignored (matching %r): %r" % (pattern, filename)) continue if filename not in self.files: file = open_file(filename) if file: self.files[filename] = file new_files.append(file) if new_files: log.debug("Adding files %r", new_files) new_files.sort(key=lambda x: x.filename) if target is None or target is self.unmatched_files: self.unmatched_files.add_files(new_files) target = None for file in new_files: file.load(partial(self._file_loaded, target=target))
def add_files(self, filenames, target=None): """Add files to the tagger.""" ignoreregex = None pattern = config.setting['ignore_regex'] if pattern: ignoreregex = re.compile(pattern) ignore_hidden = config.setting["ignore_hidden_files"] new_files = [] for filename in filenames: filename = os.path.normpath(os.path.realpath(filename)) if ignore_hidden and is_hidden(filename): log.debug("File ignored (hidden): %r" % (filename)) continue # Ignore .smbdelete* files which Applie iOS SMB creates by renaming a file when it cannot delete it if os.path.basename(filename).startswith(".smbdelete"): log.debug("File ignored (.smbdelete): %r", filename) continue if ignoreregex is not None and ignoreregex.search(filename): log.info("File ignored (matching %r): %r" % (pattern, filename)) continue if filename not in self.files: file = open_file(filename) if file: self.files[filename] = file new_files.append(file) if new_files: log.debug("Adding files %r", new_files) new_files.sort(key=lambda x: x.filename) if target is None or target is self.unclustered_files: self.unclustered_files.add_files(new_files) target = None for file in new_files: file.load(partial(self._file_loaded, target=target))
def get_files(): try: root, dirs, files = next(walk) if ignore_hidden: dirs[:] = [d for d in dirs if not is_hidden(os.path.join(root, d))] except StopIteration: return None else: number_of_files = len(files) if number_of_files: mparms = { 'count': number_of_files, 'directory': root, } log.debug("Adding %(count)d files from '%(directory)r'" % mparms) self.window.set_statusbar_message( ngettext( "Adding %(count)d file from '%(directory)s' ...", "Adding %(count)d files from '%(directory)s' ...", number_of_files), mparms, translate=None, echo=None ) return (os.path.join(root, f) for f in files)
def add_files(self, filenames, target=None): """Add files to the tagger.""" ignoreregex = None config = get_config() pattern = config.setting['ignore_regex'] if pattern: try: ignoreregex = re.compile(pattern) except re.error as e: log.error( "Failed evaluating regular expression for ignore_regex: %s", e) ignore_hidden = config.setting["ignore_hidden_files"] new_files = [] for filename in filenames: filename = normpath(filename) if ignore_hidden and is_hidden(filename): log.debug("File ignored (hidden): %r" % (filename)) continue # Ignore .smbdelete* files which Applie iOS SMB creates by renaming a file when it cannot delete it if os.path.basename(filename).startswith(".smbdelete"): log.debug("File ignored (.smbdelete): %r", filename) continue if ignoreregex is not None and ignoreregex.search(filename): log.info("File ignored (matching %r): %r" % (pattern, filename)) continue if filename not in self.files: file = open_file(filename) if file: self.files[filename] = file new_files.append(file) QtCore.QCoreApplication.processEvents() if new_files: log.debug("Adding files %r", new_files) new_files.sort(key=lambda x: x.filename) self.window.set_sorting(False) self._pending_files_count += len(new_files) unmatched_files = [] for i, file in enumerate(new_files): file.load( partial(self._file_loaded, target=target, unmatched_files=unmatched_files)) # Calling processEvents helps processing the _file_loaded # callbacks in between, which keeps the UI more responsive. # Avoid calling it to often to not slow down the loading to much # Using an uneven number to have the unclustered file counter # not look stuck in certain digits. if i % 17 == 0: QtCore.QCoreApplication.processEvents()
def _scan_paths_recursive(paths, recursive, ignore_hidden): local_paths = list(paths) while local_paths: current_path = local_paths.pop(0) try: if os.path.isdir(current_path): for entry in os.scandir(current_path): if ignore_hidden and is_hidden(entry.path): continue if recursive and entry.is_dir(): local_paths.append(entry.path) else: yield entry.path else: yield current_path except OSError as err: log.warning(err)
def add_files(self, filenames, target=None, result=None): """Add files to the tagger.""" if result: filenames = result # Handles add_directory task results coming from a worker thread ignoreregex = None pattern = config.setting['ignore_regex'] if pattern: try: ignoreregex = re.compile(pattern) except re.error as e: log.error( "Failed evaluating regular expression for ignore_regex: %s", e) ignore_hidden = config.setting["ignore_hidden_files"] new_files = [] for filename in filenames: filename = os.path.normpath(os.path.realpath(filename)) if ignore_hidden and is_hidden(filename): log.debug("File ignored (hidden): %r" % (filename)) continue # Ignore .smbdelete* files which Applie iOS SMB creates by renaming a file when it cannot delete it if os.path.basename(filename).startswith(".smbdelete"): log.debug("File ignored (.smbdelete): %r", filename) continue if ignoreregex is not None and ignoreregex.search(filename): log.info("File ignored (matching %r): %r" % (pattern, filename)) continue if filename not in self.files: file = open_file(filename) if file: self.files[filename] = file new_files.append(file) QtCore.QCoreApplication.processEvents() if new_files: log.debug("Adding files %r", new_files) new_files.sort(key=lambda x: x.filename) self.window.set_sorting(False) self._pending_files_count += len(new_files) for file in new_files: file.load(partial(self._file_loaded, target=target)) QtCore.QCoreApplication.processEvents()
def _scan_dir(self, folders, recursive, ignore_hidden): files = [] local_folders = list(folders) while local_folders: current_folder = local_folders.pop(0) current_folder_files = [] try: for entry in os.scandir(current_folder): if ignore_hidden and is_hidden(entry.path): continue if recursive and entry.is_dir(): local_folders.append(entry.path) else: current_folder_files.append(entry.path) except FileNotFoundError: pass number_of_files = len(current_folder_files) if number_of_files: mparms = { 'count': number_of_files, 'directory': current_folder, } log.debug("Adding %(count)d files from '%(directory)r'" % mparms) self.window.set_statusbar_message(ngettext( "Adding %(count)d file from '%(directory)s' ...", "Adding %(count)d files from '%(directory)s' ...", number_of_files), mparms, translate=None, echo=None) files.extend(current_folder_files) QtCore.QCoreApplication.processEvents() return files
def test(self): self.assertTrue(util.is_hidden('/a/b/.c.mp3')) self.assertTrue(util.is_hidden('/a/.b/.c.mp3')) self.assertFalse(util.is_hidden('/a/.b/c.mp3'))
def test_macos(self): with NamedTemporaryFile() as f: self.assertFalse(util.is_hidden(f.name), "%s expected not to be hidden" % f.name) subprocess.run(('SetFile', '-a', 'V', f.name)) # nosec: B603 self.assertTrue(util.is_hidden(f.name), "%s expected to be hidden" % f.name)
def test_windows(self): from ctypes import windll with NamedTemporaryFile() as f: self.assertFalse(util.is_hidden(f.name), "%s expected not to be hidden" % f.name) windll.kernel32.SetFileAttributesW(f.name, 2) self.assertTrue(util.is_hidden(f.name), "%s expected to be hidden" % f.name)