Use --action instead of --move, --copy, --preview
[ordnung.git] / ordnung.py
blobbc0247400e1195a4825e085b3237e801a487543b
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", ".mp3"] # 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 # Now replace all tokens by their values
34 for i in tokens:
35 repl = "%" + i + "%"
36 if tokens[i] != None:
37 pattern = pattern.replace(repl, tokens[i][0])
39 # Add the extension and return the new path
40 return pattern + os.path.splitext(file)[1]
42 def safeprint(string):
43 """
44 Print string first trying to normally encode it, sending it to the
45 console in "raw" UTF-8 if it fails.
47 This is a workaround for Windows's broken console
48 """
49 try:
50 print string
51 except UnicodeEncodeError:
52 print string.encode("utf-8")
54 def files_to_move(base_dir, pattern, target_dir=None, recursive=False):
55 """
56 Figure out which files to move where
57 """
58 if target_dir == None:
59 target_dir = base_dir
61 basepath = path(base_dir)
62 if recursive:
63 files = basepath.walkfiles()
64 else:
65 files = basepath.files()
67 # This list will later contain all the (origin, destination) tuples
68 files_return = []
70 for file in files:
71 if issupportedfile(file):
72 t = [ file, newpath(file, target_dir, pattern) ]
73 files_return.append(t)
75 return files_return
77 def main():
78 # Handle arguments
79 usage = "Usage: %prog [options] directory pattern"
80 parser = OptionParser(usage=usage)
81 parser.add_option("-a", "--action", dest="action",
82 help="action to execute. Possible values: preview (default), move, copy", default="preview")
83 parser.add_option("-r", "--recursive", action="store_true",
84 dest="recursive", help="also move/copy files from sub-directories", default=False)
85 parser.add_option("-t", "--target", dest="target",
86 help="create new directory structure in directory TARGET (default=directory)")
87 (options, args) = parser.parse_args()
89 try:
90 base = args[0]
91 except IndexError:
92 print "You must specify a directory"
93 sys.exit()
95 try:
96 pattern = args[1]
97 except IndexError:
98 print "You must specify a pattern"
99 sys.exit()
101 for i in files_to_move(target_dir=options.target, base_dir=base,
102 pattern=pattern, recursive=options.recursive):
103 if options.action == "preview":
104 safeprint (i[0] + " --> " + i[1])
105 elif options.action == "move":
106 print "todo" #TODO
107 elif options.action == "copy":
108 print "todo" #TODO
109 else:
110 print "Invalid value for \"action\""
111 return
113 if __name__ == "__main__":
114 main()