1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | """
|
---|
5 | Simple trick to rename all files to be compliant with FAT32 filesystems
|
---|
6 | Usefull if you like to copy your files from a UFS/EXT filesystem to a FAT
|
---|
7 | filesystem.
|
---|
8 |
|
---|
9 | By default it will dryrun to avoid (major) disasters.
|
---|
10 |
|
---|
11 | Version: $Id$
|
---|
12 | License: BSDLike http://rickvanderzwet.nl/LICENSE
|
---|
13 | Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
14 | """
|
---|
15 |
|
---|
16 | import re
|
---|
17 | import os
|
---|
18 | import sys
|
---|
19 | import string
|
---|
20 | import argparse
|
---|
21 |
|
---|
22 |
|
---|
23 | parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawTextHelpFormatter)
|
---|
24 | parser.add_argument('-r', '--rename', dest="dryrun", action='store_false',help="Rename the files",default=True)
|
---|
25 | parser.add_argument('directories', nargs='+', metavar='DIR', type=str)
|
---|
26 | args = parser.parse_args()
|
---|
27 |
|
---|
28 | trans_table = {
|
---|
29 | u'é' : 'e',
|
---|
30 | u'&' : 'and',
|
---|
31 | u'Ã' : 'O',
|
---|
32 | u'Ì' : 'u',
|
---|
33 | u'â' : "'",
|
---|
34 | u'ö' : 'o',
|
---|
35 | u'á' : 'a',
|
---|
36 | }
|
---|
37 |
|
---|
38 | word_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 |
|
---|
52 | replace_re = re.compile('[^a-zA-Z0-9\-\.\ ]+')
|
---|
53 |
|
---|
54 | def 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 |
|
---|
76 | for 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)
|
---|