1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #! /usr/bin/env python
import unicodedata
import os.path
import glob
import shutil
dirToClean = "/Users/my_login/Music/for_me/toRename"
dirForStoring = "/Users/my_login/Music/for_me/namesCleaned"
extensionToKeep = ['mp3']
i_clean = len(dirToClean)
THE_DIRECTORIES_ANALYSED = []
# The following variables have been built by tools_build_variables.py
UGGLY_CHARACTERS = {'ì': 'i', 'ò': 'o', 'º': '°', 'Á': 'A', 'À': 'A', 'Ã': 'A', 'Â': 'A', 'Å': 'A', 'Ä': 'A', 'Ç': 'C', 'Æ': 'AE', 'É': 'E', 'È': 'E', 'Ë': 'E', 'Ê': 'E', 'Í': 'I', 'Ì': 'I', 'Ï': 'I', 'Î': 'I', 'Ñ': 'N', '': 'oe', '': 'OE', 'Õ': 'O', 'Ô': 'O', 'Ö': 'O', 'Ù': 'U', 'Û': 'U', 'Ú': 'U', 'Ü': 'U', 'á': 'a', 'à': 'a', 'ã': 'a', 'â': 'a', 'å': 'a', 'ä': 'a', 'ç': 'c', 'æ': 'ae', 'é': 'e', 'è': 'e', 'ë': 'e', 'ê': 'e', 'í': 'i', 'Ó': 'O', 'ï': 'i', 'î': 'i', 'ñ': 'n', 'ó': 'o', 'Ò': 'O', 'õ': 'o', 'ô': 'o', 'ö': 'o', 'ù': 'u', '': 'Y', 'û': 'u', 'ú': 'u', 'ü': 'u', 'ÿ': 'y'}
CHARACTERS_ALLOWED = " .-_,'/!°(){}[]&\\0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + os.sep
def cleanString(stringToClean):
# The following tip has been found here :
# http://www.developpez.net/forums/d907089/autres-langages/python-zope/general-python/accents-chaines-python-3-a/
stringToClean = unicodedata.normalize('NFC', stringToClean)
prettyString = ''
for oneChar in stringToClean:
if oneChar in UGGLY_CHARACTERS:
prettyString += UGGLY_CHARACTERS[oneChar]
elif oneChar in CHARACTERS_ALLOWED:
prettyString += oneChar
else:
raise ValueError('Unknown character : ' + oneChar+ \
'\nString build : ' + prettyString+ \
'\nString to clean: ' + stringToClean)
return prettyString
def giveThePathOfTheStoringDir(pathAnalysed):
# If we have
# dirToClean = "/Users/my_login/Music/for_me/toSort"
# and
# dirForStoring = "/Users/my_login/Music/for_me/namesCleaned"
# then this function changes
# "/Users/my_login/Music/for_me/toSort/path..."
# to
# "/Users/my_login/Music/for_me/namesCleaned/path..."
return dirForStoring + pathAnalysed[i_clean:]
def buildNewDir(pathOfTheDir):
# Source :
# http://python.developpez.com/faq/?page=Repertoire#mkDir
if not os.path.isdir(pathOfTheDir):
os.makedirs(pathOfTheDir)
def buildFile(pathOfTheOldFile, pathOfTheStoringDir):
baseName = cleanString(os.path.basename(pathOfTheOldFile))
pathOfTheNewFile = pathOfTheStoringDir + os.sep + baseName
shutil.copy2(pathOfTheOldFile,
pathOfTheNewFile)
def contentOfOneDirectory(pathOfTheDirTotreat):
for root, dirs, files in os.walk(pathOfTheDirTotreat):
if files:
pathOfTheStoringDir = giveThePathOfTheStoringDir(root)
pathOfTheStoringDir = cleanString(pathOfTheStoringDir)
THE_DIRECTORIES_ANALYSED.append(pathOfTheStoringDir)
print(root)
for oneFile in files:
# Link : http://docs.python.org/library/os.path.html?highlight=os.path#os.path.splitext
fileExtension = os.path.splitext(oneFile)[1][1:]
fileExtension = fileExtension.lower()
if fileExtension in extensionToKeep:
buildNewDir(pathOfTheStoringDir)
buildFile(root + os.sep + oneFile, pathOfTheStoringDir)
# We launch the main method
contentOfOneDirectory(dirToClean)
print('='*40)
# We indicate the directories with no output, maybe because of a bug of my method...
dirWithNoOutput = []
for onePath in THE_DIRECTORIES_ANALYSED:
if not os.path.isdir(onePath):
dirWithNoOutput.append(onePath)
if dirWithNoOutput:
if len(dirWithNoOutput)==1:
startOfTheMessage = "The following cleaned path has"
else:
startOfTheMessage = "The following cleaned paths have"
print("""
""" + startOfTheMessage + """ no audio files from the point of view of this script (this can be the consequence of a strange name of a directory) :""")
for i, value in enumerate(dirWithNoOutput):
print('\t' + str(value)) |
Partager