6.7. 小结

第 5 章 介绍的 fileinfo.py 程序现在应该完全理解了。

"""Framework for getting filetype-specific metadata.

Instantiate appropriate class with filename.  Returned object acts like a
dictionary, with key-value pairs for each piece of metadata.
    import fileinfo
    info = fileinfo.MP3FileInfo("/music/ap/mahadeva.mp3")
    print "\\n".join(["%s=%s" % (k, v) for k, v in info.items()])

Or use listDirectory function to get info on all files in a directory.
    for info in fileinfo.listDirectory("/music/ap/", [".mp3"]):
        ...

Framework can be extended by adding classes for particular file types, e.g.
HTMLFileInfo, MPGFileInfo, DOCFileInfo.  Each class is completely responsible for
parsing its files appropriately; see MP3FileInfo for example.
"""
import os
import sys
from UserDict import UserDict

def stripnulls(data):
    "strip whitespace and nulls"
    return data.replace("\00", "").strip()

class FileInfo(UserDict):
    "store file metadata"
    def __init__(self, filename=None):
        UserDict.__init__(self)
        self["name"] = filename

class MP3FileInfo(FileInfo):
    "store ID3v1.0 MP3 tags"
    tagDataMap = {"title"   : (  3,  33, stripnulls),
                  "artist"  : ( 33,  63, stripnulls),
                  "album"   : ( 63,  93, stripnulls),
                  "year"    : ( 93,  97, stripnulls),
                  "comment" : ( 97, 126, stripnulls),
                  "genre"   : (127, 128, ord)}

    def __parse(self, filename):
        "parse ID3v1.0 tags from MP3 file"
        self.clear()
        try:                               
            fsock = open(filename, "rb", 0)
            try:                           
                fsock.seek(-128, 2)        
                tagdata = fsock.read(128)  
            finally:                       
                fsock.close()              
            if tagdata[:3] == "TAG":
                for tag, (start, end, parseFunc) in self.tagDataMap.items():
                    self[tag] = parseFunc(tagdata[start:end])               
        except IOError:                    
            pass                           

    def __setitem__(self, key, item):
        if key == "name" and item:
            self.__parse(item)
        FileInfo.__setitem__(self, key, item)

def listDirectory(directory, fileExtList):                                        
    "get list of file info objects for files of particular extensions"
    fileList = [os.path.normcase(f)
                for f in os.listdir(directory)]           
    fileList = [os.path.join(directory, f) 
               for f in fileList
                if os.path.splitext(f)[1] in fileExtList] 
    def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):      
        "get file info class from filename extension"                             
        subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]       
        return hasattr(module, subclass) and getattr(module, subclass) or FileInfo
    return [getFileInfoClass(f)(f) for f in fileList]                             

if __name__ == "__main__":
    for info in listDirectory("/music/_singles/", [".mp3"]):
        print "\n".join(["%s=%s" % (k, v) for k, v in info.items()])
        print

在研究下一章之前,时时彩计划软件公式:确保你可以无困难地完成下面的事情:

韩国时时彩官方开奖 重庆时时彩后三不定胆 江西时时彩走势图gjhfwiqo79 高赔率时时彩平台群 时时彩第三方支付平台
湖北省时时彩走势图 代理时时彩有多少反水 新强时时彩五星走势图 刷时时彩平台赚返点 时时彩网站搭建教程
大龙虾时时彩软件用法 助赢时时彩软件好用吗 江西时时彩开奖结果查 时时彩平台送彩金 什么时时彩计划软件好
领航时时彩软件主页 彩八仙时时彩手机版 吉林高中物理老师姓名 时时彩012路算法 时时彩可以提现吗
云南时时彩走势图 励志的故事 北京赛车10pk 吉林快3号码,吉林快3开奖结果-爱彩乐 安徽快3攻略
云南时时彩上市 1396cp北京赛车pk10 第一彩彩票 内蒙古十一选五技巧 福建11选5遗漏
东方6+1开奖结果中奖规则 重庆时时彩组三稳出 幸运28评测网 江西11选5 河南十一选五手机板
qq彩票网 名城国际娱乐城 广西快3开奖号码 下载青海11选5 重庆幸运农场玩法