Change some var names, read target and pattern
[ordnung.git] / ordnung.py
blob061a95b33ae25e419302704f5592d7ffdbb9ff9a
1 from path import path
2 import fnmatch
3 import os, sys
5 from optparse import OptionParser
6 from tag_wrapper import tag
8 ACCEPTEXTS = [".ogg"] # Keep it simple for now
10 def issupportedfile(name):
11 for ext in ACCEPTEXTS:
12 if os.path.splitext(name)[1] == ext:
13 return True
14 return False
16 def newpath(file, target, pattern):
17 # Create the tokens dictionary
18 tokens = {}
19 tags = tag(file)
20 # TODO: add tracknumber, disknumber and possibily compilation
21 for t in ["title", "artist", "album artist", "album", "composer", "genre", "date"]:
22 try:
23 tokens[t] = tags[t]
24 except KeyError:
25 tokens[t] = None
26 # %album artist% is %artist% if nothing can be found in the tags
27 if tokens["album artist"] == None:
28 tokens["album artist"] = tokens["artist"]
30 # That appears in no tag
31 tokens["base"] = target
33 def main():
34 # Handle arguments
35 usage = "Usage: %prog [options] directory pattern"
36 parser = OptionParser(usage=usage)
37 parser.add_option("-r", "--recursive", action="store_true",
38 dest="recursive", help="also move/copy files from sub-directories", default=False)
39 parser.add_option("-t", "--target", dest="target",
40 help="create new directory structure in directory TARGET (default=directory)")
41 (options, args) = parser.parse_args()
43 try:
44 base = args[0]
45 except IndexError:
46 print "You must specify a directory"
47 sys.exit()
49 try:
50 pattern = args[1]
51 except IndexError:
52 print "You must specify a pattern"
53 sys.exit()
55 if options.target != None:
56 target = options.target
57 else:
58 target = base
60 basepath = path(base)
61 if options.recursive:
62 files = basepath.walkfiles()
63 else:
64 files = basepath.files()
65 for file in files:
66 if issupportedfile(file):
67 print file
68 print newpath(file, target, pattern)
70 if __name__ == "__main__":
71 main()