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

Last change on this file since 327 was 297, checked in by Rick van der Zwet, 14 years ago

Damm FAT32 directories with all the limits...

  • Property svn:executable set to *
File size: 2.0 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
28trans_table = {
29 u'é' : 'e',
30 u'&' : 'and',
31 u'Ø' : 'O',
32 u'Ì' : 'u',
33 u'’' : "'",
34 u'ö' : 'o',
35 u'á' : 'a',
36}
37
38word_table = {
39 "'m" : ' am',
40 "'d" : ' would',
41 "n't" : ' not',
42 "n's" : 'n his',
43 "'n'" : 'and',
44 "n'" : 'ng',
45 "'s" : ' is',
46 "'ll" : ' will',
47 "A'k" : "Als ik",
48 "'T" : "Het",
49 "'t" : "het",
50}
51
52replace_re = re.compile('[^a-zA-Z0-9\-\.\ ]+')
53
54def make_ascii(source):
55 source = source.decode('utf-8')
56 dest = ""
57 for i in source:
58 if trans_table.has_key(i):
59 dest += trans_table[i]
60 else:
61 dest += i
62 for key,value in word_table.iteritems():
63 dest = dest.replace(key,value)
64
65 # Debug decoding
66 # print "---"
67 # print "Original : ", source,
68 # for c in source: print ord(c),
69 # print "\nConverted: ", dest,
70 # for c in dest: print ord(c),
71 # print "\n"
72
73 dest = replace_re.sub('', dest)
74 return str(dest)
75
76for root, dirs, files in os.walk(sys.argv[1],topdown=False):
77 for file in files + dirs:
78 new_file = make_ascii(file)
79 if new_file != file:
80 source = os.path.join(root,file)
81 target = os.path.join(root,new_file)
82 prefix = '# (dryrun) ' if args.dryrun else ''
83 print prefix, "Source:", source
84 print prefix, "Target:", target
85 if not args.dryrun:
86 os.rename(source,target)
Note: See TracBrowser for help on using the repository browser.