def test_ignore_file(self): res = list(util.sorted_walk(self.base, ('x',))) self.assertEqual(len(res), 2) self.assertEqual(res[0], (self.base, ['d'], ['y'])) self.assertEqual(res[1], (os.path.join(self.base, 'd'), [], ['z']))
def test_sorted_files(self): res = list(util.sorted_walk(self.base)) self.assertEqual(len(res), 2) self.assertEqual(res[0], (self.base, [b'd'], [b'x', b'y'])) self.assertEqual(res[1], (os.path.join(self.base, b'd'), [], [b'z']))
def produce(): def create_mf(path): try: mf = MediaFile(path) if( len(mf.lyrics) == 0 or self.force): #print_(" -%s:" % (mf.title), ui.colorize('yellow', 'Queued')) return mf except FileTypeError: return None for path in basePath: if os.path.isdir(path): # Find all files in the directory. filepaths = [] for root, dirs, files in sorted_walk(path): for filename in files: yield create_mf(os.path.join(root, filename)) else: # Just add the file. yield create_mf(path)
def albums_in_dir(path): """Recursively searches the given directory and returns an iterable of (path, items) where path is a containing directory and items is a list of Items that is probably an album. Specifically, any folder containing any media files is an album. """ for root, dirs, files in sorted_walk(path): # Get a list of items in the directory. items = [] for filename in files: try: i = library.Item.from_path(os.path.join(root, filename)) except mediafile.FileTypeError: pass except mediafile.UnreadableFileError: log.warn('unreadable file: ' + filename) else: items.append(i) # If it's nonempty, yield it. if items: yield root, items
def test_ignore_everything(self): res = list(util.sorted_walk(self.base, ('*',))) self.assertEqual(len(res), 1) self.assertEqual(res[0], (self.base, [], []))
def test_ignore_directory(self): res = list(util.sorted_walk(self.base, ('d',))) self.assertEqual(len(res), 1) self.assertEqual(res[0], (self.base, [], ['x', 'y']))
def albums_in_dir(path): """Recursively searches the given directory and returns an iterable of (paths, items) where paths is a list of directories and items is a list of Items that is probably an album. Specifically, any folder containing any media files is an album. """ collapse_pat = collapse_paths = collapse_items = None ignore = config['ignore'].as_str_seq() for root, dirs, files in sorted_walk(path, ignore=ignore, logger=log): items = [os.path.join(root, f) for f in files] # If we're currently collapsing the constituent directories in a # multi-disc album, check whether we should continue collapsing # and add the current directory. If so, just add the directory # and move on to the next directory. If not, stop collapsing. if collapse_paths: if (not collapse_pat and collapse_paths[0] in ancestry(root)) or \ (collapse_pat and collapse_pat.match(os.path.basename(root))): # Still collapsing. collapse_paths.append(root) collapse_items += items continue else: # Collapse finished. Yield the collapsed directory and # proceed to process the current one. if collapse_items: yield collapse_paths, collapse_items collapse_pat = collapse_paths = collapse_items = None # Check whether this directory looks like the *first* directory # in a multi-disc sequence. There are two indicators: the file # is named like part of a multi-disc sequence (e.g., "Title Disc # 1") or it contains no items but only directories that are # named in this way. start_collapsing = False for marker in MULTIDISC_MARKERS: marker_pat = re.compile(MULTIDISC_PAT_FMT % marker, re.I) match = marker_pat.match(os.path.basename(root)) # Is this directory the root of a nested multi-disc album? if dirs and not items: # Check whether all subdirectories have the same prefix. start_collapsing = True subdir_pat = None for subdir in dirs: # The first directory dictates the pattern for # the remaining directories. if not subdir_pat: match = marker_pat.match(subdir) if match: subdir_pat = re.compile( r'^%s\d' % re.escape(match.group(1)), re.I ) else: start_collapsing = False break # Subsequent directories must match the pattern. elif not subdir_pat.match(subdir): start_collapsing = False break # If all subdirectories match, don't check other # markers. if start_collapsing: break # Is this directory the first in a flattened multi-disc album? elif match: start_collapsing = True # Set the current pattern to match directories with the same # prefix as this one, followed by a digit. collapse_pat = re.compile( r'^%s\d' % re.escape(match.group(1)), re.I ) break # If either of the above heuristics indicated that this is the # beginning of a multi-disc album, initialize the collapsed # directory and item lists and check the next directory. if start_collapsing: # Start collapsing; continue to the next iteration. collapse_paths = [root] collapse_items = items continue # If it's nonempty, yield it. if items: yield [root], items # Clear out any unfinished collapse. if collapse_paths and collapse_items: yield collapse_paths, collapse_items
def albums_in_dir(path, ignore=()): """Recursively searches the given directory and returns an iterable of (path, items) where path is a containing directory and items is a list of Items that is probably an album. Specifically, any folder containing any media files is an album. Directories and file names that match the glob patterns in ``ignore`` are skipped. """ collapse_root = None collapse_items = None for root, dirs, files in sorted_walk(path, ignore): # Get a list of items in the directory. items = [] for filename in files: try: i = library.Item.from_path(os.path.join(root, filename)) except mediafile.FileTypeError: pass except mediafile.UnreadableFileError: log.warn('unreadable file: ' + filename) else: items.append(i) # If we're collapsing, test to see whether we should continue to # collapse. If so, just add to the collapsed item set; # otherwise, end the collapse and continue as normal. if collapse_root is not None: if collapse_root in ancestry(root): # Still collapsing. collapse_items += items continue else: # Collapse finished. Yield the collapsed directory and # proceed to process the current one. if collapse_items: yield collapse_root, collapse_items collapse_root = collapse_items = None # Does the current directory look like a multi-disc album? If # so, begin collapsing here. if dirs and not items: # Must be only directories. multidisc = False for marker in MULTIDISC_MARKERS: pat = MULTIDISC_PAT_FMT % marker if all(re.search(pat, dirname, re.I) for dirname in dirs): multidisc = True break # This becomes True only when all directories match a # pattern for a single marker. if multidisc: # Start collapsing; continue to the next iteration. collapse_root = root collapse_items = [] continue # If it's nonempty, yield it. if items: yield root, items # Clear out any unfinished collapse. if collapse_root is not None and collapse_items: yield collapse_root, collapse_items
def test_ignore_file(self): res = list(util.sorted_walk(self.base, ('x', ))) self.assertEqual(len(res), 2) self.assertEqual(res[0], (self.base, ['d'], ['y'])) self.assertEqual(res[1], (os.path.join(self.base, 'd'), [], ['z']))
def albums_in_dir(path, ignore=()): """Recursively searches the given directory and returns an iterable of (path, items) where path is a containing directory and items is a list of Items that is probably an album. Specifically, any folder containing any media files is an album. Directories and file names that match the glob patterns in ``ignore`` are skipped. """ collapse_root = None collapse_items = None for root, dirs, files in sorted_walk(path, ignore): # Get a list of items in the directory. items = [] for filename in files: try: i = library.Item.from_path(os.path.join(root, filename)) except mediafile.FileTypeError: pass except mediafile.UnreadableFileError: log.warn(u'unreadable file: {0}'.format( displayable_path(filename))) else: items.append(i) # If we're collapsing, test to see whether we should continue to # collapse. If so, just add to the collapsed item set; # otherwise, end the collapse and continue as normal. if collapse_root is not None: if collapse_root in ancestry(root): # Still collapsing. collapse_items += items continue else: # Collapse finished. Yield the collapsed directory and # proceed to process the current one. if collapse_items: yield collapse_root, collapse_items collapse_root = collapse_items = None # Does the current directory look like a multi-disc album? If # so, begin collapsing here. if dirs and not items: # Must be only directories. multidisc = False for marker in MULTIDISC_MARKERS: pat = MULTIDISC_PAT_FMT % marker if all(re.search(pat, dirname, re.I) for dirname in dirs): multidisc = True break # This becomes True only when all directories match a # pattern for a single marker. if multidisc: # Start collapsing; continue to the next iteration. collapse_root = root collapse_items = [] continue # If it's nonempty, yield it. if items: yield root, items # Clear out any unfinished collapse. if collapse_root is not None and collapse_items: yield collapse_root, collapse_items
def albums_in_dir(path): """Recursively searches the given directory and returns an iterable of (paths, items) where paths is a list of directories and items is a list of Items that is probably an album. Specifically, any folder containing any media files is an album. """ collapse_pat = collapse_paths = collapse_items = None for root, dirs, files in sorted_walk(path, ignore=config['ignore'].as_str_seq(), logger=log): # Get a list of items in the directory. items = [] for filename in files: try: i = library.Item.from_path(os.path.join(root, filename)) except mediafile.FileTypeError: pass except mediafile.UnreadableFileError: log.warn(u'unreadable file: {0}'.format( displayable_path(filename))) else: items.append(i) # If we're currently collapsing the constituent directories in a # multi-disc album, check whether we should continue collapsing # and add the current directory. If so, just add the directory # and move on to the next directory. If not, stop collapsing. if collapse_paths: if (not collapse_pat and collapse_paths[0] in ancestry(root)) or \ (collapse_pat and collapse_pat.match(os.path.basename(root))): # Still collapsing. collapse_paths.append(root) collapse_items += items continue else: # Collapse finished. Yield the collapsed directory and # proceed to process the current one. if collapse_items: yield collapse_paths, collapse_items collapse_pat = collapse_paths = collapse_items = None # Check whether this directory looks like the *first* directory # in a multi-disc sequence. There are two indicators: the file # is named like part of a multi-disc sequence (e.g., "Title Disc # 1") or it contains no items but only directories that are # named in this way. start_collapsing = False for marker in MULTIDISC_MARKERS: marker_pat = re.compile(MULTIDISC_PAT_FMT % marker, re.I) match = marker_pat.match(os.path.basename(root)) # Is this directory the root of a nested multi-disc album? if dirs and not items: # Check whether all subdirectories have the same prefix. start_collapsing = True subdir_pat = None for subdir in dirs: # The first directory dictates the pattern for # the remaining directories. if not subdir_pat: match = marker_pat.match(subdir) if match: subdir_pat = re.compile( r'^%s\d' % re.escape(match.group(1)), re.I) else: start_collapsing = False break # Subsequent directories must match the pattern. elif not subdir_pat.match(subdir): start_collapsing = False break # If all subdirectories match, don't check other # markers. if start_collapsing: break # Is this directory the first in a flattened multi-disc album? elif match: start_collapsing = True # Set the current pattern to match directories with the same # prefix as this one, followed by a digit. collapse_pat = re.compile(r'^%s\d' % re.escape(match.group(1)), re.I) break # If either of the above heuristics indicated that this is the # beginning of a multi-disc album, initialize the collapsed # directory and item lists and check the next directory. if start_collapsing: # Start collapsing; continue to the next iteration. collapse_paths = [root] collapse_items = items continue # If it's nonempty, yield it. if items: yield [root], items # Clear out any unfinished collapse. if collapse_paths and collapse_items: yield collapse_paths, collapse_items