source: misc/music-files-sanitize.py@ 374

Last change on this file since 374 was 349, checked in by Rick van der Zwet, 13 years ago

Make it actually do something..

  • Property svn:executable set to *
File size: 2.1 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4"""
5Simple trick to rename all files to be compliant with FAT32 filesystems
6Usefull if you like to copy your files from a UFS/EXT filesystem to a FAT
7filesystem.
8
9By default it will dryrun to avoid (major) disasters.
10
11Version: $Id$
12License: BSDLike http://rickvanderzwet.nl/LICENSE
13Rick van der Zwet <info@rickvanderzwet.nl>
14"""
15
16import re
17import os
18import sys
19import string
20import argparse
21
22
23parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawTextHelpFormatter)
24parser.add_argument('-r', '--rename', dest="dryrun", action='store_false',help="Rename the files",default=True)
25parser.add_argument('directories', nargs='+', metavar='DIR', type=str)
26args = parser.parse_args()
27
28print "# INFO: Going to parse directories %s" % args.directories
29
30trans_table = {
31 u'é' : 'e',
32 u'&' : 'and',
33 u'Ø' : 'O',
34 u'Ì' : 'u',
35 u'’' : "'",
36 u'ö' : 'o',
37 u'á' : 'a',
38}
39
40word_table = {
41 "'m" : ' am',
42 "'d" : ' would',
43 "n't" : ' not',
44 "n's" : 'n his',
45 "'n'" : 'and',
46 "n'" : 'ng',
47 "'s" : ' is',
48 "'ll" : ' will',
49 "A'k" : "Als ik",
50 "'T" : "Het",
51 "'t" : "het",
52}
53
54replace_re = re.compile('[^a-zA-Z0-9\-\.\ ]+')
55
56def make_ascii(source):
57 source = source.decode('utf-8')
58 dest = ""
59 for i in source:
60 if trans_table.has_key(i):
61 dest += trans_table[i]
62 else:
63 dest += i
64 for key,value in word_table.iteritems():
65 dest = dest.replace(key,value)
66
67 # Debug decoding
68 # print "---"
69 # print "Original : ", source,
70 # for c in source: print ord(c),
71 # print "\nConverted: ", dest,
72 # for c in dest: print ord(c),
73 # print "\n"
74
75 dest = replace_re.sub('', dest)
76 return str(dest)
77
78for dir_root in args.directories:
79 for root, dirs, files in os.walk(dir_root,topdown=False):
80 for file in files + dirs:
81 new_file = make_ascii(file)
82 if new_file != file:
83 source = os.path.join(root,file)
84 target = os.path.join(root,new_file)
85 prefix = '# (dryrun) ' if args.dryrun else ''
86 print prefix, "Source:", source
87 print prefix, "Target:", target
88 if not args.dryrun:
89 os.rename(source,target)
Note: See TracBrowser for help on using the repository browser.