Permalink
Browse files

Issue #14: Remove null bytes before the SAX parser sees them.

  • Loading branch information...
1 parent 32b97a9 commit a5cdf778901dbe7aa3d72ed017d26e23a29933a1 @BMorearty committed Jan 30, 2012
Showing with 17 additions and 1 deletion.
  1. +17 −1 exportiphoto.py
View
@@ -5,6 +5,7 @@
import base64
import codecs
+import io
import locale
import os
import re
@@ -14,6 +15,7 @@
import time
from datetime import datetime
+from io import IOBase
from optparse import OptionParser
from xml.dom.pulldom import START_ELEMENT, END_ELEMENT, parse
from xml.dom.minidom import Node
@@ -31,6 +33,18 @@
class iPhotoLibraryError(Exception):
pass
+# Some AlbumData.xml files contain null bytes. Strip them so the SAX parser
+# doesn't fail with an Invalid Token error.
+class RemoveNullsStream(IOBase):
+ def __init__(self, filename):
+ self.file = open(filename, 'r')
+
+ def read(self, bufsize=2**20):
+ return self.file.read(bufsize).translate(None,"\0")
+
+ def close(self):
+ self.file.close()
+
class iPhotoLibrary(object):
def __init__(self, albumDir, destDir, use_album=False, use_date=False,
use_faces=False, use_metadata=False, deconflict=False, quiet=False,
@@ -65,8 +79,10 @@ def __init__(self, albumDir, destDir, use_album=False, use_date=False,
self.build_import_list()
albumDataXml = os.path.join(albumDir, "AlbumData.xml")
+ albumDataStream = RemoveNullsStream(albumDataXml)
self.status("* Parsing iPhoto Library data... ")
- self.parseAlbumData(albumDataXml)
+ self.parseAlbumData(albumDataStream)
+ albumDataStream.close()
self.status("Done.\n")
major_version = 2

0 comments on commit a5cdf77

Please sign in to comment.