Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. """Framework for getting filetype-specific metadata.
  2.  
  3. This is the same as fileinfo.py with one difference:
  4. instead of inheriting from UserDict, we inherit directly from dict.
  5. This capability was introduced in Python 2.2, and makes UserDict
  6. unnecessary (along with its cousins, UserList and UserString).
  7.  
  8. This program is part of "Dive Into Python", a free Python book for
  9. experienced programmers. Visit http://diveintopython.org/ for the
  10. latest version.
  11. """
  12.  
  13. __author__ = "Mark Pilgrim (mark@diveintopython.org)"
  14. __version__ = "$Revision: 1.2 $"
  15. __date__ = "$Date: 2004/05/05 21:57:19 $"
  16. __copyright__ = "Copyright (c) 2001 Mark Pilgrim"
  17. __license__ = "Python"
  18.  
  19. import os
  20. import sys
  21.  
  22. def stripnulls(data):
  23. "strip whitespace and nulls"
  24. return data.replace("\00", " ").strip()
  25.  
  26. class FileInfo(dict):
  27. "store file metadata"
  28. def __init__(self, filename=None):
  29. self["name"] = filename
  30.  
  31. class MP3FileInfo(FileInfo):
  32. "store ID3v1.0 MP3 tags"
  33. tagDataMap = {"title" : ( 3, 33, stripnulls),
  34. "artist" : ( 33, 63, stripnulls),
  35. "album" : ( 63, 93, stripnulls),
  36. "year" : ( 93, 97, stripnulls),
  37. "comment" : ( 97, 126, stripnulls),
  38. "genre" : (127, 128, ord)}
  39.  
  40. def __parse(self, filename):
  41. "parse ID3v1.0 tags from MP3 file"
  42. self.clear()
  43. try:
  44. fsock = open(filename, "rb", 0)
  45. try:
  46. fsock.seek(-128, 2)
  47. tagdata = fsock.read(128)
  48. finally:
  49. fsock.close()
  50. if tagdata[:3] == 'TAG':
  51. for tag, (start, end, parseFunc) in self.tagDataMap.items():
  52. self[tag] = parseFunc(tagdata[start:end])
  53. except IOError:
  54. pass
  55.  
  56. def __setitem__(self, key, item):
  57. if key == "name" and item:
  58. self.__parse(item)
  59. FileInfo.__setitem__(self, key, item)
  60.  
  61. def listDirectory(directory, fileExtList):
  62. "get list of file info objects for files of particular extensions"
  63. fileList = [os.path.normcase(f) for f in os.listdir(directory)]
  64. fileList = [os.path.join(directory, f) for f in fileList \
  65. if os.path.splitext(f)[1] in fileExtList]
  66. def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):
  67. "get file info class from filename extension"
  68. subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]
  69. return hasattr(module, subclass) and getattr(module, subclass) or FileInfo
  70. return [getFileInfoClass(f)(f) for f in fileList]
  71.  
  72. if __name__ == "__main__":
  73. for info in listDirectory("C:\\Users\\ser\\Desktop\\PythonPrograms", ["InFlames.mp3"]):
  74. print "\n".join(["%s=%s" % (k, v) for k, v in info.items()])
  75. print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement