[297] | 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 |
|
---|
[349] | 28 | print "# INFO: Going to parse directories %s" % args.directories
|
---|
| 29 |
|
---|
[297] | 30 | trans_table = {
|
---|
| 31 | u'é' : 'e',
|
---|
| 32 | u'&' : 'and',
|
---|
| 33 | u'Ã' : 'O',
|
---|
| 34 | u'Ì' : 'u',
|
---|
| 35 | u'â' : "'",
|
---|
| 36 | u'ö' : 'o',
|
---|
| 37 | u'á' : 'a',
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | word_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 |
|
---|
| 54 | replace_re = re.compile('[^a-zA-Z0-9\-\.\ ]+')
|
---|
| 55 |
|
---|
| 56 | def 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 |
|
---|
[349] | 78 | for 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)
|
---|