Utilities/binplistlib.py

Tue, 02 Oct 2012 19:53:31 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 02 Oct 2012 19:53:31 +0200
changeset 2087
795992a5c561
parent 1965
96f5a76e1845
child 2302
f29e9405c851
permissions
-rw-r--r--

Made the sixth set of Qt5 compatibility changes.

1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a library for reading and writing binary property list files.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 Binary Property List (plist) files provide a faster and smaller serialization
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 format for property lists on OS X. This is a library for generating binary
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 plists which can be read by OS X, iOS, or other clients.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 The API models the plistlib API, and will call through to plistlib when
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 XML serialization or deserialization is required.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 To generate plists with UID values, wrap the values with the Uid object. The
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 value must be an int.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 To generate plists with NSData/CFData values, wrap the values with the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Data object. The value must be a bytes object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Date values can only be datetime.datetime objects.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
24 The exceptions InvalidPlistException and NotBinaryPlistException may be
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 thrown to indicate that the data cannot be serialized or deserialized as
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 a binary plist.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Plist generation example:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 <pre>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 from binplistlib import *
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 from datetime import datetime
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 plist = {'aKey':'aValue',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 '0':1.322,
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 'now':datetime.now(),
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 'list':[1,2,3],
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 'tuple':('a','b','c')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 }
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 writePlist(plist, "example.plist")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 except (InvalidPlistException, NotBinaryPlistException) as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 print("Something bad happened:", e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 </pre>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 Plist parsing example:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 <pre>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 from binplistlib import *
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 plist = readPlist("example.plist")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 print(plist)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 except (InvalidPlistException, NotBinaryPlistException) as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 print("Not a plist:", e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 </pre>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # Ported from the Python 2 biplist.py script.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 # Original License:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 # Copyright (c) 2010, Andrew Wooster
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 # All rights reserved.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 # Redistribution and use in source and binary forms, with or without
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 # modification, are permitted provided that the following conditions are met:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 # * Redistributions of source code must retain the above copyright notice,
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 # this list of conditions and the following disclaimer.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 # * Redistributions in binary form must reproduce the above copyright
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 # notice, this list of conditions and the following disclaimer in the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 # documentation and/or other materials provided with the distribution.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 # * Neither the name of biplist nor the names of its contributors may be
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
71 # used to endorse or promote products derived from this software without
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 # specific prior written permission.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 #
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 from collections import namedtuple
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 from io import BytesIO
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 import calendar
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 import datetime
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 import math
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 import plistlib
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 from struct import pack, unpack
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 __all__ = [
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 'Uid', 'Data', 'readPlist', 'writePlist', 'readPlistFromBytes',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 'writePlistToBytes', 'InvalidPlistException', 'NotBinaryPlistException'
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 ]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 apple_reference_date_offset = 978307200
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 class Uid(int):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 Class implementing a wrapper around integers for representing UID values.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 This is used in keyed archiving.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 def __repr__(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 return "Uid(%d)" % self
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 class Data(bytes):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 Class implementing a wrapper around bytes types for representing Data values.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 pass
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 class InvalidPlistException(Exception):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 Exception raised when the plist is incorrectly formatted.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 pass
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 class NotBinaryPlistException(Exception):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 Exception raised when a binary plist was expected but not encountered.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 pass
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 def readPlist(pathOrFile):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 Module function to read a plist file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 @param pathOrFile name of the plist file (string) or an open file (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @return reference to the read object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @exception InvalidPlistException raised to signal an invalid plist file
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 didOpen = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 if isinstance(pathOrFile, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 pathOrFile = open(pathOrFile, 'rb')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 didOpen = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 reader = PlistReader(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 result = reader.parse()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 except NotBinaryPlistException as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 pathOrFile.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 result = plistlib.readPlist(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 except Exception as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 raise InvalidPlistException(e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if didOpen:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 pathOrFile.close()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
159
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 def writePlist(rootObject, pathOrFile, binary=True):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 Module function to write a plist file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 @param rootObject reference to the object to be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 @param pathOrFile name of the plist file (string) or an open file (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 @param binary flag indicating the generation of a binary plist file (boolean)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 if not binary:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 plistlib.writePlist(rootObject, pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 didOpen = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 if isinstance(pathOrFile, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 pathOrFile = open(pathOrFile, 'wb')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 didOpen = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 writer = PlistWriter(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 writer.writeRoot(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 if didOpen:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 pathOrFile.close()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
182
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 def readPlistFromBytes(data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 Module function to read from a plist bytes object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 @param data plist data (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 @return reference to the read object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @exception InvalidPlistException raised to signal an invalid plist file
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 return readPlist(BytesIO(data))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
193
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 def writePlistToBytes(rootObject, binary=True):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 Module function to write a plist bytes object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 @param rootObject reference to the object to be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 @param binary flag indicating the generation of a binary plist bytes object (boolean)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 if not binary:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 return plistlib.writePlistToBytes(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 io = BytesIO()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 writer = PlistWriter(io)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 writer.writeRoot(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 return io.getvalue()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
209
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 def is_stream_binary_plist(stream):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 Module function to check, if the stream is a binary plist.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 @param stream plist stream (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 @return flag indicating a binary plist (boolean)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 stream.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 header = stream.read(7)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 if header == b'bplist0':
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 return True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 return False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 PlistTrailer = namedtuple('PlistTrailer',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 'offsetSize, objectRefSize, offsetCount, topLevelObjectNumber, offsetTableOffset')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 PlistByteCounts = namedtuple('PlistByteCounts',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 'nullBytes, boolBytes, intBytes, realBytes, dateBytes, dataBytes, stringBytes, '
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 'uidBytes, arrayBytes, setBytes, dictBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
230
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 class PlistReader(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 Class implementing the plist reader.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 file = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 contents = b''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 offsets = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 currentOffset = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 def __init__(self, fileOrStream):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 Constructor
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 @param fileOrStream open file containing the plist data (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 self.file = fileOrStream
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 def parse(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 Public method to parse the plist data.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 @return unpickled object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 return self.readRoot()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 def reset(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 Private method to reset the instance object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 self.trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.contents = b''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 self.offsets = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 self.currentOffset = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 def readRoot(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 Private method to read the root object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 @return unpickled object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 # Get the header, make sure it's a valid file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 if not is_stream_binary_plist(self.file):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 raise NotBinaryPlistException()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 self.file.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 self.contents = self.file.read()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 if len(self.contents) < 32:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 raise InvalidPlistException("File is too short.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 trailerContents = self.contents[-32:]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 self.trailer = PlistTrailer._make(unpack("!xxxxxxBBQQQ", trailerContents))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 offset_size = self.trailer.offsetSize * self.trailer.offsetCount
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 offset = self.trailer.offsetTableOffset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
287 offset_contents = self.contents[offset:offset + offset_size]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 offset_i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 while offset_i < self.trailer.offsetCount:
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
290 begin = self.trailer.offsetSize * offset_i
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
291 tmp_contents = offset_contents[begin:begin + self.trailer.offsetSize]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 tmp_sized = self.getSizedInteger(tmp_contents, self.trailer.offsetSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 self.offsets.append(tmp_sized)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 offset_i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 self.setCurrentOffsetToObjectNumber(self.trailer.topLevelObjectNumber)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 result = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 except TypeError as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 raise InvalidPlistException(e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 def setCurrentOffsetToObjectNumber(self, objectNumber):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 Private method to set the current offset.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 @param objectNumber number of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 self.currentOffset = self.offsets[objectNumber]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 def readObject(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 Private method to read the object data.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 @return unpickled object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 result = None
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
316 tmp_byte = self.contents[self.currentOffset:self.currentOffset + 1]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 marker_byte = unpack("!B", tmp_byte)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 format = (marker_byte >> 4) & 0x0f
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 extra = marker_byte & 0x0f
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 self.currentOffset += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 def proc_extra(extra):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 if extra == 0b1111:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 #self.currentOffset += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 extra = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 return extra
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 # bool, null, or fill byte
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 if format == 0b0000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 if extra == 0b0000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 elif extra == 0b1000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 result = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 elif extra == 0b1001:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 result = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 elif extra == 0b1111:
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
337 pass # fill byte
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 "Invalid object found at offset: {0}".format(self.currentOffset - 1))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 # int
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 elif format == 0b0001:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 result = self.readInteger(pow(2, extra))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 # real
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 elif format == 0b0010:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 result = self.readReal(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 # date
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 elif format == 0b0011 and extra == 0b0011:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 result = self.readDate()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 # data
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 elif format == 0b0100:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 result = self.readData(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 # ascii string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 elif format == 0b0101:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 result = self.readAsciiString(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 # Unicode string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 elif format == 0b0110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 result = self.readUnicode(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 # uid
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 elif format == 0b1000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 result = self.readUid(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 # array
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 elif format == 0b1010:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 result = self.readArray(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 # set
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 elif format == 0b1100:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 result = set(self.readArray(extra))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 # dict
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 elif format == 0b1101:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 result = self.readDict(extra)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
379 else:
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 "Invalid object found: {{format: {0}, extra: {1}}}".format(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 bin(format), bin(extra)))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 def readInteger(self, bytes):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 Private method to read an Integer object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 @param bytes length of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 @return integer object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 result = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 original_offset = self.currentOffset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
394 data = self.contents[self.currentOffset:self.currentOffset + bytes]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 result = self.getSizedInteger(data, bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 self.currentOffset = original_offset + bytes
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 def readReal(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 Private method to read a Real object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 @param length length of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 @return float object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 result = 0.0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 to_read = pow(2, length)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
408 data = self.contents[self.currentOffset:self.currentOffset + to_read]
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
409 if length == 2: # 4 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 result = unpack('>f', data)[0]
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
411 elif length == 3: # 8 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 result = unpack('>d', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 "Unknown real of length {0} bytes".format(to_read))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
418 def readRefs(self, count):
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 Private method to read References.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
421
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 @param count amount of the references (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
423 @return list of references (list of integers)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 refs = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 while i < count:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 fragment = self.contents[
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
429 self.currentOffset:self.currentOffset + self.trailer.objectRefSize]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 ref = self.getSizedInteger(fragment, len(fragment))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 refs.append(ref)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 self.currentOffset += self.trailer.objectRefSize
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 return refs
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 def readArray(self, count):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 Private method to read an Array object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 @param count number of array elements (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 @return list of unpickled objects
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 result = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 values = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 while i < len(values):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447 self.setCurrentOffsetToObjectNumber(values[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 value = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 result.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 def readDict(self, count):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 Private method to read a Dictionary object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 @param count number of dictionary elements (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 @return dictionary of unpickled objects
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 result = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 keys = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 values = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 while i < len(keys):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 self.setCurrentOffsetToObjectNumber(keys[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 key = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 self.setCurrentOffsetToObjectNumber(values[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 value = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 result[key] = value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 def readAsciiString(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 Private method to read an ASCII encoded string.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 @param length length of the string (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 @return ASCII encoded string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 result = str(unpack("!{0}s".format(length),
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
481 self.contents[self.currentOffset:self.currentOffset + length])[0],
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 encoding="ascii")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 self.currentOffset += length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 def readUnicode(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
488 Private method to read an Unicode encoded string.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 @param length length of the string (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 @return unicode encoded string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
493 actual_length = length * 2
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
494 data = self.contents[self.currentOffset:self.currentOffset + actual_length]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 # unpack not needed?!! data = unpack(">%ds" % (actual_length), data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 self.currentOffset += actual_length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 return data.decode('utf_16_be')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 def readDate(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 Private method to read a date.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 @return date object (datetime.datetime)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 global apple_reference_date_offset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
506 result = unpack(">d", self.contents[self.currentOffset:self.currentOffset + 8])[0]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 result = datetime.datetime.utcfromtimestamp(result + apple_reference_date_offset)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 self.currentOffset += 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 def readData(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 Private method to read some bytes.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
514
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
515 @param length number of bytes to read (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 @return Data object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
518 result = self.contents[self.currentOffset:self.currentOffset + length]
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 self.currentOffset += length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 return Data(result)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 def readUid(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 Private method to read a UID.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 @param length length of the UID (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 @return Uid object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
529 return Uid(self.readInteger(length + 1))
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 def getSizedInteger(self, data, bytes):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533 Private method to read an integer of a specific size.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 @param data data to extract the integer from (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 @param bytes length of the integer (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 result = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 # 1, 2, and 4 byte integers are unsigned
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 if bytes == 1:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 result = unpack('>B', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 elif bytes == 2:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 result = unpack('>H', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 elif bytes == 4:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 result = unpack('>L', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 elif bytes == 8:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 result = unpack('>q', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 raise InvalidPlistException("Encountered integer longer than 8 bytes.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
552
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 class HashableWrapper(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555 Class wrapping a hashable value.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 def __init__(self, value):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 self.value = value
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
559
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 def __repr__(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 return "<HashableWrapper: %s>" % [self.value]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
563
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 class BoolWrapper(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 Class wrapping a boolean value.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 def __init__(self, value):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 self.value = value
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
570
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 def __repr__(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 return "<BoolWrapper: %s>" % self.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
573
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
574
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
575 class PlistWriter(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
576 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577 Class implementing the plist writer.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 header = b'bplist00bybiplist1.0'
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 file = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 byteCounts = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582 trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 computedUniques = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 writtenReferences = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 referencePositions = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 wrappedTrue = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 wrappedFalse = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 def __init__(self, file):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 Constructor
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 @param file file to write the plist data to (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 self.file = file
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597 self.wrappedTrue = BoolWrapper(True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 self.wrappedFalse = BoolWrapper(False)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
600 def reset(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602 Private method to reset the instance object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
603 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 self.byteCounts = PlistByteCounts(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
605 self.trailer = PlistTrailer(0, 0, 0, 0, 0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
606
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 # A set of all the uniques which have been computed.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608 self.computedUniques = set()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
609 # A list of all the uniques which have been written.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 self.writtenReferences = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 # A dict of the positions of the written uniques.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 self.referencePositions = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
613
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
614 def positionOfObjectReference(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 Private method to get the position of an object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 If the given object has been written already, return its
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 position in the offset table. Otherwise, return None.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 @return position of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 return self.writtenReferences.get(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 def writeRoot(self, root):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627 Public method to write an object to a plist file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 Strategy is:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 <li>write header</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 <li>wrap root object so everything is hashable</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 <li>compute size of objects which will be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 <li>need to do this in order to know how large the object refs
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 will be in the list/dict/set reference lists</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 </ul></li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 <li>write objects
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640 <li>keep objects in writtenReferences</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 <li>keep positions of object references in referencePositions</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 <li>write object references with the length computed previously</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 </ul></li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 <li>computer object reference length</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 <li>write object reference positions</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 <li>write trailer</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
647 </ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 @param root reference to the object to be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
651 output = self.header
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
652 wrapped_root = self.wrapRoot(root)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
653 should_reference_root = True # not isinstance(wrapped_root, HashableWrapper)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
654 self.computeOffsets(wrapped_root, asReference=should_reference_root, isRoot=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
655 self.trailer = self.trailer._replace(
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
656 **{'objectRefSize': self.intSize(len(self.computedUniques))})
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 (_, output) = self.writeObjectReference(wrapped_root, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 output = self.writeObject(wrapped_root, output, setReferencePosition=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 # output size at this point is an upper bound on how big the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 # object reference offsets need to be.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 self.trailer = self.trailer._replace(**{
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
663 'offsetSize': self.intSize(len(output)),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
664 'offsetCount': len(self.computedUniques),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
665 'offsetTableOffset': len(output),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
666 'topLevelObjectNumber': 0
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 })
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 output = self.writeOffsetTable(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 output += pack('!xxxxxxBBQQQ', *self.trailer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 self.file.write(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 def wrapRoot(self, root):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 Private method to generate object wrappers.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 @param root object to be wrapped
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678 @return wrapped object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 if isinstance(root, bool):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 if root is True:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 return self.wrappedTrue
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684 return self.wrappedFalse
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 elif isinstance(root, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 n = set()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 for value in root:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 n.add(self.wrapRoot(value))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 elif isinstance(root, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 n = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 for key, value in root.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 n[self.wrapRoot(key)] = self.wrapRoot(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
695 elif isinstance(root, list):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 n = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
697 for value in root:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 n.append(self.wrapRoot(value))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 elif isinstance(root, tuple):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 n = tuple([self.wrapRoot(value) for value in root])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 return root
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 def incrementByteCount(self, field, incr=1):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 self.byteCounts = self.byteCounts._replace(
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
708 **{field: self.byteCounts.__getattribute__(field) + incr})
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 def computeOffsets(self, obj, asReference=False, isRoot=False):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 def check_key(key):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 if key is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 raise InvalidPlistException('Dictionary keys cannot be null in plists.')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 elif isinstance(key, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 raise InvalidPlistException('Data cannot be dictionary keys in plists.')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 elif not isinstance(key, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 raise InvalidPlistException('Keys must be strings.')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 def proc_size(size):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 if size > 0b1110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 size += self.intSize(size)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 return size
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 # If this should be a reference, then we keep a record of it in the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 # uniques table.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 if asReference:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 if obj in self.computedUniques:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 self.computedUniques.add(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 if obj is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 self.incrementByteCount('nullBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 elif isinstance(obj, BoolWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 self.incrementByteCount('boolBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 elif isinstance(obj, Uid):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 size = self.intSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
737 self.incrementByteCount('uidBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
738 elif isinstance(obj, int):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 size = self.intSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
740 self.incrementByteCount('intBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 elif isinstance(obj, (float)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 size = self.realSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
743 self.incrementByteCount('realBytes', incr=1 + size)
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
744 elif isinstance(obj, datetime.datetime):
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 self.incrementByteCount('dateBytes', incr=2)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 elif isinstance(obj, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
748 self.incrementByteCount('dataBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 elif isinstance(obj, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
751 self.incrementByteCount('stringBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 elif isinstance(obj, HashableWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 obj = obj.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 if isinstance(obj, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
756 self.incrementByteCount('setBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 for value in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
759 elif isinstance(obj, (list, tuple)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
761 self.incrementByteCount('arrayBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 for value in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 elif isinstance(obj, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
765 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
766 self.incrementByteCount('dictBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 for key, value in obj.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 check_key(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 self.computeOffsets(key, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 raise InvalidPlistException("Unknown object type.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 def writeObjectReference(self, obj, output):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
776 Private method to write an object reference.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 Tries to write an object reference, adding it to the references
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 table. Does not write the actual object bytes or set the reference
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 position. Returns a tuple of whether the object was a new reference
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 (True if it was, False if it already was in the reference table)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 and the new output.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 @param obj object to be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 @param output output stream to append the object to
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 @return flag indicating a new reference and the new output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 position = self.positionOfObjectReference(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 if position is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 self.writtenReferences[obj] = len(self.writtenReferences)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 output += self.binaryInt(len(self.writtenReferences) - 1,
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 bytes=self.trailer.objectRefSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 return (True, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 output += self.binaryInt(position, bytes=self.trailer.objectRefSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 return (False, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 def writeObject(self, obj, output, setReferencePosition=False):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 Private method to serialize the given object to the output.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 @param obj object to be serialized
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 @param output output to be serialized to (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 @param setReferencePosition flag indicating, that the reference
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 position the object was written to shall be recorded (boolean)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 @return new output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
808 def proc_variable_length(format, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 result = ''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 if length > 0b1110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 result += pack('!B', (format << 4) | 0b1111)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 result = self.writeObject(length, result)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814 result += pack('!B', (format << 4) | length)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 if setReferencePosition:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 self.referencePositions[obj] = len(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 if obj is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 output += pack('!B', 0b00000000)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 elif isinstance(obj, BoolWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 if obj.value is False:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 output += pack('!B', 0b00001000)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 output += pack('!B', 0b00001001)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827 elif isinstance(obj, Uid):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828 size = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829 output += pack('!B', (0b1000 << 4) | size - 1)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 output += self.binaryInt(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831 elif isinstance(obj, int):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 bytes = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 root = math.log(bytes, 2)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 output += pack('!B', (0b0001 << 4) | int(root))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835 output += self.binaryInt(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 elif isinstance(obj, float):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 # just use doubles
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
838 output += pack('!B', (0b0010 << 4) | 3)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
839 output += self.binaryReal(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840 elif isinstance(obj, datetime.datetime):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 timestamp = calendar.timegm(obj.utctimetuple())
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 timestamp -= apple_reference_date_offset
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 output += pack('!B', 0b00110011)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844 output += pack('!d', float(timestamp))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 elif isinstance(obj, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 output += proc_variable_length(0b0100, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847 output += obj
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848 elif isinstance(obj, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849 # Python 3 uses unicode strings only
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 bytes = obj.encode('utf_16_be')
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
851 output += proc_variable_length(0b0110, len(bytes) / 2)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 output += bytes
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 elif isinstance(obj, HashableWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 obj = obj.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 if isinstance(obj, (set, list, tuple)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
856 if isinstance(obj, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 output += proc_variable_length(0b1100, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
858 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 output += proc_variable_length(0b1010, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 objectsToWrite = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 for objRef in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 (isNew, output) = self.writeObjectReference(objRef, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 objectsToWrite.append(objRef)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 for objRef in objectsToWrite:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867 output = self.writeObject(objRef, output, setReferencePosition=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868 elif isinstance(obj, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
869 output += proc_variable_length(0b1101, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 keys = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871 values = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 objectsToWrite = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 for key, value in obj.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
874 keys.append(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
875 values.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876 for key in keys:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
877 (isNew, output) = self.writeObjectReference(key, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
878 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
879 objectsToWrite.append(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
880 for value in values:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
881 (isNew, output) = self.writeObjectReference(value, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
882 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
883 objectsToWrite.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
884 for objRef in objectsToWrite:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
885 output = self.writeObject(objRef, output, setReferencePosition=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
886 return output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
887
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
888 def writeOffsetTable(self, output):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
889 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
890 Private method to write all of the object reference offsets.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
892 @param output current output (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 @return new output (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 all_positions = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
896 writtenReferences = list(self.writtenReferences.items())
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 writtenReferences.sort(key=lambda x: x[1])
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
898 for obj, order in writtenReferences:
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
899 position = self.referencePositions.get(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
900 if position is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
901 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
902 "Error while writing offsets table. Object not found. {0}"
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
903 .format(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
904 output += self.binaryInt(position, self.trailer.offsetSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
905 all_positions.append(position)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
906 return output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
908 def binaryReal(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
909 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
910 Private method to pack a real object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
911
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
912 @param obj real to be packed
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 @return serialized object (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 # just use doubles
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916 result = pack('>d', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 def binaryInt(self, obj, bytes=None):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
921 Private method to pack an integer object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 @param obj integer to be packed
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 @param bytes length the integer should be packed into (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 @return serialized object (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 result = ''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 if bytes is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 bytes = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 if bytes == 1:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 result += pack('>B', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 elif bytes == 2:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 result += pack('>H', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 elif bytes == 4:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 result += pack('>L', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 elif bytes == 8:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 result += pack('>q', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 "Core Foundation can't handle integers with size greater than 8 bytes.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
943 def intSize(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 Private method to determine the number of bytes necessary to store the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
946 given integer.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
947
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948 @param obj integer object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 @return number of bytes required (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 # SIGNED
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
952 if obj < 0: # Signed integer, always 8 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953 return 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 # UNSIGNED
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
955 elif obj <= 0xFF: # 1 byte
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 return 1
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
957 elif obj <= 0xFFFF: # 2 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 return 2
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
959 elif obj <= 0xFFFFFFFF: # 4 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960 return 4
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961 # SIGNED
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
962 # 0x7FFFFFFFFFFFFFFF is the max.
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
963 elif obj <= 0x7FFFFFFFFFFFFFFF: # 8 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
964 return 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
965 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
966 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
967 "Core Foundation can't handle integers with size greater than 8 bytes.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
968
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
969 def realSize(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
970 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
971 Private method to determine the number of bytes necessary to store the
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
972 given real.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
973
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
974 @param obj real object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
975 @return number of bytes required (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
976 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
977 return 8

eric ide

mercurial