Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jan 30, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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