Skip to content

Instantly share code, notes, and snippets.

@dahlia
Created March 11, 2012 16:03
Show Gist options
  • Save dahlia/2016929 to your computer and use it in GitHub Desktop.
Save dahlia/2016929 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import os
import os.path
import unicodedata
def copy(src, dst):
yield src.decode('utf-8'), dst.decode('utf-8')
if os.path.isdir(src):
if not os.path.isdir(dst):
os.mkdir(dst)
for filename_nfc in os.listdir(src):
if isinstance(filename_nfc, unicode):
filename_nfc = filename_nfc.encode('utf-8')
filename_nfc_unicode = filename_nfc.decode('utf-8')
filename_nfd = unicodedata.normalize('NFD', filename_nfc_unicode)
filename_nfd = filename_nfd.encode('utf-8')
iterate = copy(os.path.join(src, filename_nfd),
os.path.join(dst, filename_nfc))
for pair in iterate:
yield pair
else: # file
with open(src, 'rb') as src_file:
with open(dst, 'wb') as dst_file:
while True:
buf = src_file.read(1048576)
if buf:
dst_file.write(buf)
else:
break
if len(sys.argv) < 3:
print>>sys.stderr, 'usage:', sys.argv[0], 'src', 'dst'
for src, dst in copy(*sys.argv[1:]):
print src.encode('utf-8'), '->', dst.encode('utf-8')
#!/usr/bin/env python
import sys
import plistlib
import urllib
import unicodedata
def renorm(s):
unquoted = urllib.unquote(s)
unistr = unquoted.decode('utf-8')
renormed = unicodedata.normalize('NFC', unistr)
u8str = renormed.encode('utf-8')
quoted = urllib.quote(u8str)
return quoted
if len(sys.argv) < 2:
print>>sys.stderr, 'usage:', sys.argv[0], 'iTunes-Music-Library.xml'
raise SystemExit
library = plistlib.readPlist(sys.argv[1])
for track in library['Tracks'].itervalues():
try:
loc = track['Location']
except KeyError:
continue
parts = loc.split('/')
loc = parts[0] + '/' + '/'.join(renorm(part) for part in parts[1:])
track['Location'] = renorm(loc)
plistlib.writePlist(library, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment