コード例 #1
0
ファイル: ScanLibrary.py プロジェクト: bgporter/mp3utilities
    def Scan(self, forceScan=False):
        source = fileSource.FileSource(self.libPath)

        currentArtist = None
        artistName = ''
        currentAlbum = None
        albumName = ''
        history = None
        needsUpdating = False
        depth = 0
        now = time.time()

        for (t, f) in source:
            #print t, f
            _, itemName = os.path.split(f)
            if fileSource.kDirectory == t:
                if 0 == depth:
                    # top level of the hierarchy -- at this point we know nothing.
                    assert currentArtist is None
                    assert currentAlbum is None
                elif 1 == depth:
                    # Just entered an Artist directory.
                    assert currentArtist is None
                    assert currentAlbum is None
                    currentArtist = self.library.setdefault(itemName, {})
                    artistName = itemName
                elif 2 == depth:
                    # just entered an album directory
                    assert currentArtist is not None
                    assert currentAlbum is None
                    assert history is None
                    # see if this directory has been modified since the last time
                    # we looked at it.
                    currentAlbum = currentArtist.setdefault(itemName, {})
                    albumName = itemName
                    st = os.stat(f)
                    lastModified = st.st_mtime
                    lastUpdated = currentAlbum.setdefault(kLastModified, 0)
                    needsUpdating = forceScan or (lastModified > lastUpdated)
                    history = trackHistory.History(f)
                else:
                    # deeper? Shouldn't happen. We should handle this more elegantly
                    # than throwing an assertion error, though.
                    assert False, "Hierarchy goes too deep!"
                depth += 1
                pass
            elif fileSource.kMusic == t:
                assert currentAlbum is not None
                assert history is not None
                if needsUpdating:
                    print f
                    trackName, _ = os.path.splitext(itemName)
                    trackInfo = currentAlbum.setdefault(trackName, {})
                    if history.fileExists:
                        acq, move = history.GetTrack(itemName)
                        trackInfo['acquired'] = acq
                        trackInfo['moved'] = move
                        self.recent.AddRecent(artistName, albumName, trackName,
                                              acq)
                        mp3 = fileDestination.Mp3File(f)
                        for attr in kTrackAttributes:
                            trackInfo[attr] = getattr(mp3, attr)

            elif fileSource.kOtherFile == t:
                pass
            elif fileSource.kExitDirectory == t:
                depth -= 1
                if 2 == depth:
                    # leaving an album directory.
                    assert currentAlbum is not None
                    assert currentArtist is not None
                    assert history is not None
                    if needsUpdating:
                        st = os.stat(f)
                        currentAlbum[kLastModified] = st.st_mtime
                        needsUpdating = False
                    currentAlbum = None
                    history = None
                elif 1 == depth:
                    # leaving an artist directory.
                    assert currentAlbum is None
                    assert currentArtist is not None
                    currentArtist = None
                elif 0 == depth:
                    # leaving the top level directory, which should only happen as
                    # the very last thing we do here.
                    assert currentAlbum is None
                    assert currentArtist is None
                else:
                    assert False, "Hierarchy error!"
コード例 #2
0
ファイル: shuffler.py プロジェクト: bgporter/mp3utilities
        default="",
        help=
        "Input file containing directores to force onto the drive (1 per line, relative to `src')"
    )

    args = parser.parse_args()

    if args.test:
        import doctest
        print "running module tests..."
        doctest.testmod()
        print "done."
        sys.exit(0)

    # get an inventory of all the files that are already on the destination
    destSource = fileSource.FileSource(args.dest)
    print "Getting list of files at destination {0}".format(args.dest)
    destInventory = GetMusicFiles(destSource)

    # get a list of the files that we want to have pinned on the dest
    if args.pinned:
        pinnedSrc = fileSource.FileSource(args.src, args.pinned)
        print "Getting list of files to pin onto destination"
        pinnedFiles = GetMusicFiles(pinnedSrc)
    else:
        pinnedFiles = []
    # get a list of all of the source files that aren't pinned.
    src = fileSource.FileSource(args.src)
    print "Getting list of all source files on {0}".format(args.src)
    srcFiles = GetMusicFiles(src)
コード例 #3
0
import sys
import os

import fileSource

if __name__ =="__main__":
   import argparse
   parser = argparse.ArgumentParser("List subdirectories with MP3 files.")
   parser.add_argument("-s", "--src", action="store", nargs="?",
      default=os.getcwd(), help="Source directory containing mp3 files")  

   args = parser.parse_args()

   srcPath = os.path.normpath(args.src)


   paths = []

   fs = fileSource.FileSource(srcPath)
   for (fileType, filePath) in fs:
      if fileSource.kDirectory == fileType:
         relative = os.path.relpath(filePath, srcPath)
         pathComponents = relative.split(os.sep)
         if 2 == len(pathComponents):
            paths.append(relative)

   for p in sorted(paths, key=unicode.lower):
      print p.encode("utf-8")


コード例 #4
0
ファイル: editMetadata.py プロジェクト: bgporter/mp3utilities
        '--force',
        action="store_true",
        help="Don't edit, just update all fields as passed on command line")

    args = parser.parse_args()

    args = vars(args)

    path = args.pop('src')
    force = args.pop('force')

    # get rid of any items in the dict with a value of None.
    for (k, v) in args.items():
        if v is None:
            args.pop(k)
        fs = fileSource.FileSource(path)

    editor = None

    for (fileType, filePath) in fs:
        if fileType == fileSource.kDirectory:
            # create a new editor for each directory, resetting all the
            # remembered values.
            editor = MetadataEditor(args)
            print "Entering {0}".format(filePath)
        elif fileType == fileSource.kMusic:
            retval = editor.EditFile(filePath, force)
            if not retval:
                break

    print "Done."
コード例 #5
0
import os

import fileDestination
import fileHistory
import fileSource
import trackHistory

fs = fileSource.FileSource(u"/Volumes/zappa_files/music/")

newHistory = None

for (t, p) in fs:
    if t == fileSource.kDirectory:
        try:
            oldHistory = fileHistory.History(
                fileDestination.NormalizeFilename(p))
            if oldHistory.fileExists:
                lastTime = int(float(oldHistory.Oldest()))
                print u"{0}: {1}".format(lastTime, p).encode("utf-8")
                if newHistory:
                    newHistory.Save()
                # create an empty new-style history file in the same location.
                newHistory = trackHistory.History(p)
        except UnicodeDecodeError, e:
            print p.encode("utf-8")
            print str(e)

    elif (t == fileSource.kMusic):
        track = os.path.split(p)[1]
        print "   " + track.encode('utf-8')
        # add a track to the new history file using the same (old) timestamp
コード例 #6
0
ファイル: mover.py プロジェクト: bgporter/mp3utilities
    # Real operation -- first, get the file source object ready to go.
    others = None
    # if there's an input file name given, it should contain one directory
    # path per line (relative to src). Create a cleaned-up list of those
    # directories.
    if args.input:
        print "INPUT: %s" % args.input
        others = []
        try:
            with open(args.input, "r") as f:
                for dirName in f:
                    others.append(dirName.strip())
        except IOError:
            print "Error opening input file '%s'" % args.input

    source = fileSource.FileSource(unicode(args.src), others)

    # ...then prepare the destination handler object.
    mode = args.mode
    debug = False
    if 'debug' == mode:
        debug = True
        mode = 'copy'

    dest = fileDestination.FileDestination(unicode(args.dest), mode, args.dupe,
                                           args.rate, args.musiconly,
                                           args.cleanup, debug)

    toHandle = list(source)
    fileCount = sum(1 for item in toHandle if item[0] == fileSource.kMusic)