Skip to content

Commit

Permalink
Merge pull request #16 from BMorearty/14-remove-nulls-from-xml
Browse files Browse the repository at this point in the history
Issue #14: Remove null bytes before the SAX parser sees them.
  • Loading branch information
BMorearty committed Jan 30, 2012
2 parents 32b97a9 + a5cdf77 commit 81457ba
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion exportiphoto.py
Expand Up @@ -5,6 +5,7 @@

import base64
import codecs
import io
import locale
import os
import re
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 81457ba

Please sign in to comment.