Utilities/binplistlib.py

Sun, 29 Sep 2013 18:22:08 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 29 Sep 2013 18:22:08 +0200
changeset 2965
d133c7edd88a
parent 2302
f29e9405c851
child 2969
0e1af1313b8b
permissions
-rw-r--r--

Continued correcting doc strings by using the new doc string checker.

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
2302
f29e9405c851 Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1965
diff changeset
3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
1720
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):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
109 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
110 Public method to return an object representation.
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
111
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
112 @return object representation (string)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
113 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 return "Uid(%d)" % self
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
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 class Data(bytes):
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 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
120 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 pass
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
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 class InvalidPlistException(Exception):
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 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
127 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 pass
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
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 class NotBinaryPlistException(Exception):
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 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
134 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 pass
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
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 def readPlist(pathOrFile):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 Module function to read a plist file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 @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
143 @return reference to the read object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @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
145 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 didOpen = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 if isinstance(pathOrFile, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 pathOrFile = open(pathOrFile, 'rb')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 didOpen = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 reader = PlistReader(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 result = reader.parse()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 except NotBinaryPlistException as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 pathOrFile.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 result = plistlib.readPlist(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 except Exception as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 raise InvalidPlistException(e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 if didOpen:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 pathOrFile.close()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
164
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 def writePlist(rootObject, pathOrFile, binary=True):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 Module function to write a plist file.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 @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
170 @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
171 @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
172 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 if not binary:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 plistlib.writePlist(rootObject, pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 didOpen = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 if isinstance(pathOrFile, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 pathOrFile = open(pathOrFile, 'wb')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 didOpen = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 writer = PlistWriter(pathOrFile)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 writer.writeRoot(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 if didOpen:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 pathOrFile.close()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
187
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 def readPlistFromBytes(data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 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
191
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 @param data plist data (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 @return reference to the read object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 return readPlist(BytesIO(data))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
197
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 def writePlistToBytes(rootObject, binary=True):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 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
201
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 @param rootObject reference to the object to be written
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
203 @param binary flag indicating the generation of a binary plist bytes
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
204 object (boolean)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
205 @return bytes object containing the plist data
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 if not binary:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 return plistlib.writePlistToBytes(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 io = BytesIO()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 writer = PlistWriter(io)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 writer.writeRoot(rootObject)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 return io.getvalue()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
215
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 def is_stream_binary_plist(stream):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 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
219
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 @param stream plist stream (file object)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 @return flag indicating a binary plist (boolean)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 stream.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 header = stream.read(7)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 if header == b'bplist0':
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 return True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 return False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 PlistTrailer = namedtuple('PlistTrailer',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 'offsetSize, objectRefSize, offsetCount, topLevelObjectNumber, offsetTableOffset')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 PlistByteCounts = namedtuple('PlistByteCounts',
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 'nullBytes, boolBytes, intBytes, realBytes, dateBytes, dataBytes, stringBytes, '
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 'uidBytes, arrayBytes, setBytes, dictBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
236
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 class PlistReader(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 Class implementing the plist reader.
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 file = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 contents = b''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 offsets = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 currentOffset = 0
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 def __init__(self, fileOrStream):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 Constructor
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 @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
252 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 self.file = fileOrStream
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 def parse(self):
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 Public method to parse the plist data.
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 @return unpickled 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 return self.readRoot()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 def reset(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 Private method to reset the instance object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 self.trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 self.contents = b''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 self.offsets = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 self.currentOffset = 0
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 def readRoot(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 Private method to read the root object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 @return unpickled object
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
278 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
279 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 # 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
284 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
285 raise NotBinaryPlistException()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 self.file.seek(0)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 self.contents = self.file.read()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 if len(self.contents) < 32:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 raise InvalidPlistException("File is too short.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 trailerContents = self.contents[-32:]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 try:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 self.trailer = PlistTrailer._make(unpack("!xxxxxxBBQQQ", trailerContents))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 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
294 offset = self.trailer.offsetTableOffset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
295 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
296 offset_i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 while offset_i < self.trailer.offsetCount:
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
298 begin = self.trailer.offsetSize * offset_i
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
299 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
300 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
301 self.offsets.append(tmp_sized)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 offset_i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 self.setCurrentOffsetToObjectNumber(self.trailer.topLevelObjectNumber)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 result = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 except TypeError as e:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 raise InvalidPlistException(e)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 return result
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 setCurrentOffsetToObjectNumber(self, objectNumber):
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 set the current offset.
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 @param objectNumber number of the object (integer)
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 self.currentOffset = self.offsets[objectNumber]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 def readObject(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 Private method to read the object data.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 @return unpickled object
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
322 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
323 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 result = None
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
326 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
327 marker_byte = unpack("!B", tmp_byte)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 format = (marker_byte >> 4) & 0x0f
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 extra = marker_byte & 0x0f
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 self.currentOffset += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 def proc_extra(extra):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 if extra == 0b1111:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334 #self.currentOffset += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 extra = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 return extra
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 # bool, null, or fill byte
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 if format == 0b0000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 if extra == 0b0000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 result = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 elif extra == 0b1000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 result = False
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 elif extra == 0b1001:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 result = True
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 elif extra == 0b1111:
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
347 pass # fill byte
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 "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
351 # int
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 elif format == 0b0001:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 result = self.readInteger(pow(2, extra))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 # real
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 elif format == 0b0010:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 result = self.readReal(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 # date
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 elif format == 0b0011 and extra == 0b0011:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 result = self.readDate()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 # data
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 elif format == 0b0100:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 result = self.readData(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 # ascii string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 elif format == 0b0101:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 result = self.readAsciiString(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 # Unicode string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 elif format == 0b0110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 result = self.readUnicode(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 # uid
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 elif format == 0b1000:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 result = self.readUid(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 # array
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 elif format == 0b1010:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 result = self.readArray(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381 # set
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 elif format == 0b1100:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 result = set(self.readArray(extra))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 # dict
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 elif format == 0b1101:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 extra = proc_extra(extra)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 result = self.readDict(extra)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
389 else:
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 "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
392 bin(format), bin(extra)))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 def readInteger(self, bytes):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 Private method to read an Integer object.
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 @param bytes length of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 @return integer object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 result = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 original_offset = self.currentOffset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
404 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
405 result = self.getSizedInteger(data, bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 self.currentOffset = original_offset + bytes
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 def readReal(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 Private method to read a Real object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 @param length length of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 @return float object
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
415 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
416 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 result = 0.0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 to_read = pow(2, length)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
420 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
421 if length == 2: # 4 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 result = unpack('>f', data)[0]
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
423 elif length == 3: # 8 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
424 result = unpack('>d', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
426 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
427 "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
428 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
430 def readRefs(self, count):
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 Private method to read References.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
434 @param count amount of the references (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 @return list of references (list of integers)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 refs = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 while i < count:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 fragment = self.contents[
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
441 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
442 ref = self.getSizedInteger(fragment, len(fragment))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 refs.append(ref)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444 self.currentOffset += self.trailer.objectRefSize
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
446 return refs
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
447
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
448 def readArray(self, count):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 Private method to read an Array object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 @param count number of array elements (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453 @return list of unpickled objects
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 result = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 values = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 while i < len(values):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 self.setCurrentOffsetToObjectNumber(values[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 value = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 result.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 def readDict(self, count):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 Private method to read a Dictionary object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 @param count number of dictionary elements (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 @return dictionary of unpickled objects
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 result = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 keys = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 values = self.readRefs(count)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 i = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 while i < len(keys):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 self.setCurrentOffsetToObjectNumber(keys[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478 key = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 self.setCurrentOffsetToObjectNumber(values[i])
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 value = self.readObject()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 result[key] = value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 i += 1
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 def readAsciiString(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 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
488
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 @param length length of the string (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 @return ASCII encoded string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 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
493 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
494 encoding="ascii")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 self.currentOffset += length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
496 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 def readUnicode(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 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
501
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502 @param length length of the string (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 @return unicode encoded string
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
505 actual_length = length * 2
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
506 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
507 # 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
508 self.currentOffset += actual_length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 return data.decode('utf_16_be')
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 readDate(self):
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 a date.
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 @return date object (datetime.datetime)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 global apple_reference_date_offset
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
518 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
519 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
520 self.currentOffset += 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 def readData(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525 Private method to read some bytes.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 @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
528 @return Data object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
530 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
531 self.currentOffset += length
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 return Data(result)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534 def readUid(self, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
535 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 Private method to read a UID.
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 @param length length of the UID (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 @return Uid object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 """
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
541 return Uid(self.readInteger(length + 1))
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 def getSizedInteger(self, data, bytes):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 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
546
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 @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
548 @param bytes length of the integer (integer)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
549 @return read integer (integer)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
550 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
551 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 result = 0
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 # 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
555 if bytes == 1:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 result = unpack('>B', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 elif bytes == 2:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 result = unpack('>H', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 elif bytes == 4:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 result = unpack('>L', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561 elif bytes == 8:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 result = unpack('>q', data)[0]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564 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
565 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
567
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 class HashableWrapper(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 Class wrapping a hashable value.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 def __init__(self, value):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
573 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
574 Constructor
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
575
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
576 @param value object value
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
577 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 self.value = value
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
579
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 def __repr__(self):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
581 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
582 Public method to generate a representation of the object.
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
583
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
584 @return object representation (string)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
585 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586 return "<HashableWrapper: %s>" % [self.value]
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
588
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 class BoolWrapper(object):
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 Class wrapping a boolean value.
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 def __init__(self, value):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
594 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
595 Constructor
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
596
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
597 @param value object value (boolean)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
598 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 self.value = value
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
600
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 def __repr__(self):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
602 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
603 Public method to generate a representation of the object.
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
604
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
605 @return object representation (string)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
606 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 return "<BoolWrapper: %s>" % self.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
608
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
609
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
610 class PlistWriter(object):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
611 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
612 Class implementing the plist writer.
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 header = b'bplist00bybiplist1.0'
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
615 file = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
616 byteCounts = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
617 trailer = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
618 computedUniques = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
619 writtenReferences = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
620 referencePositions = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 wrappedTrue = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
622 wrappedFalse = None
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 def __init__(self, file):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 Constructor
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 @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
629 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 self.reset()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 self.file = file
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 self.wrappedTrue = BoolWrapper(True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 self.wrappedFalse = BoolWrapper(False)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635 def reset(self):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 Private method to reset the instance object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 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
640 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
641
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 # 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
643 self.computedUniques = set()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 # 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
645 self.writtenReferences = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 # 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
647 self.referencePositions = {}
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 def positionOfObjectReference(self, obj):
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 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
652
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
653 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
654 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
655
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
656 @param obj object
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
657 @return position of the object (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
658 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
659 return self.writtenReferences.get(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 def writeRoot(self, root):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
663 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
664
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 Strategy is:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 <li>write header</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 <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
669 <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
670 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 <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
672 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
673 </ul></li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 <li>write objects
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 <ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 <li>keep objects in writtenReferences</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 <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
678 <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
679 </ul></li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680 <li>computer object reference length</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 <li>write object reference positions</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 <li>write trailer</li>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 </ul>
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 @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
686 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 output = self.header
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 wrapped_root = self.wrapRoot(root)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
689 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
690 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
691 self.trailer = self.trailer._replace(
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
692 **{'objectRefSize': self.intSize(len(self.computedUniques))})
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 (_, output) = self.writeObjectReference(wrapped_root, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 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
695
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
696 # 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
697 # object reference offsets need to be.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 self.trailer = self.trailer._replace(**{
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
699 'offsetSize': self.intSize(len(output)),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
700 'offsetCount': len(self.computedUniques),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
701 'offsetTableOffset': len(output),
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
702 'topLevelObjectNumber': 0
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 })
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 output = self.writeOffsetTable(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 output += pack('!xxxxxxBBQQQ', *self.trailer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 self.file.write(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 def wrapRoot(self, root):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711 Private method to generate object wrappers.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 @param root object to be wrapped
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 @return wrapped object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716 if isinstance(root, bool):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 if root is True:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 return self.wrappedTrue
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 return self.wrappedFalse
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 elif isinstance(root, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 n = set()
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 for value in root:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 n.add(self.wrapRoot(value))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 elif isinstance(root, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 n = {}
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 for key, value in root.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 n[self.wrapRoot(key)] = self.wrapRoot(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 elif isinstance(root, list):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 n = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
733 for value in root:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 n.append(self.wrapRoot(value))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 elif isinstance(root, tuple):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 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
738 return HashableWrapper(n)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 return root
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 def incrementByteCount(self, field, incr=1):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
743 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
744 Public method to increment the byte count.
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
745
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
746 @param field field to evaluate
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
747 @param incr byte count increment (integer)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
748 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749 self.byteCounts = self.byteCounts._replace(
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
750 **{field: self.byteCounts.__getattribute__(field) + incr})
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 def computeOffsets(self, obj, asReference=False, isRoot=False):
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
753 """
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
754 Public method to compute offsets of an object.
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
755
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
756 @param obj plist object
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
757 @param asReference flag indicating offsets as references (boolean)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
758 @param isRoot flag indicating a root object (boolean)
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
759 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
760 plist file
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
761 """
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
762 def check_key(key):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
763 if key is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 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
765 elif isinstance(key, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
766 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
767 elif not isinstance(key, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 raise InvalidPlistException('Keys must be strings.')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 def proc_size(size):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
771 if size > 0b1110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 size += self.intSize(size)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
773 return size
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
774
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 # 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
776 # uniques table.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
777 if asReference:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778 if obj in self.computedUniques:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 return
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 self.computedUniques.add(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 if obj is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 self.incrementByteCount('nullBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 elif isinstance(obj, BoolWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 self.incrementByteCount('boolBytes')
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
787 elif isinstance(obj, Uid):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
788 size = self.intSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
789 self.incrementByteCount('uidBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 elif isinstance(obj, int):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 size = self.intSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
792 self.incrementByteCount('intBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 elif isinstance(obj, (float)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
794 size = self.realSize(obj)
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
795 self.incrementByteCount('realBytes', incr=1 + size)
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
796 elif isinstance(obj, datetime.datetime):
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 self.incrementByteCount('dateBytes', incr=2)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 elif isinstance(obj, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
800 self.incrementByteCount('dataBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 elif isinstance(obj, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
803 self.incrementByteCount('stringBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
804 elif isinstance(obj, HashableWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
805 obj = obj.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
806 if isinstance(obj, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
807 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
808 self.incrementByteCount('setBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
809 for value in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
810 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
811 elif isinstance(obj, (list, tuple)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
812 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
813 self.incrementByteCount('arrayBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
814 for value in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
815 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
816 elif isinstance(obj, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 size = proc_size(len(obj))
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
818 self.incrementByteCount('dictBytes', incr=1 + size)
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 for key, value in obj.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 check_key(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 self.computeOffsets(key, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
822 self.computeOffsets(value, asReference=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
823 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
824 raise InvalidPlistException("Unknown object type.")
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
825
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826 def writeObjectReference(self, obj, output):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
827 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828 Private method to write an object reference.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
829
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
830 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
831 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
832 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
833 (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
834 and the new output.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
835
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
836 @param obj object to be written
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
837 @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
838 @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
839 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
840 position = self.positionOfObjectReference(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
841 if position is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
842 self.writtenReferences[obj] = len(self.writtenReferences)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
843 output += self.binaryInt(len(self.writtenReferences) - 1,
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
844 bytes=self.trailer.objectRefSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
845 return (True, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
847 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
848 return (False, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
849
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850 def writeObject(self, obj, output, setReferencePosition=False):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 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
853
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 @param obj object to be serialized
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 @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
856 @param setReferencePosition flag indicating, that the reference
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
857 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
858 @return new output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 def proc_variable_length(format, length):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 result = ''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
862 if length > 0b1110:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 result += pack('!B', (format << 4) | 0b1111)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
864 result = self.writeObject(length, result)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
865 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
866 result += pack('!B', (format << 4) | length)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
867 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
868
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
869 if setReferencePosition:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
870 self.referencePositions[obj] = len(output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
871
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
872 if obj is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
873 output += pack('!B', 0b00000000)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
874 elif isinstance(obj, BoolWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
875 if obj.value is False:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
876 output += pack('!B', 0b00001000)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
877 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
878 output += pack('!B', 0b00001001)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
879 elif isinstance(obj, Uid):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
880 size = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
881 output += pack('!B', (0b1000 << 4) | size - 1)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
882 output += self.binaryInt(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
883 elif isinstance(obj, int):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
884 bytes = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
885 root = math.log(bytes, 2)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
886 output += pack('!B', (0b0001 << 4) | int(root))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
887 output += self.binaryInt(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
888 elif isinstance(obj, float):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
889 # just use doubles
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
890 output += pack('!B', (0b0010 << 4) | 3)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
891 output += self.binaryReal(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
892 elif isinstance(obj, datetime.datetime):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
893 timestamp = calendar.timegm(obj.utctimetuple())
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
894 timestamp -= apple_reference_date_offset
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
895 output += pack('!B', 0b00110011)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
896 output += pack('!d', float(timestamp))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
897 elif isinstance(obj, Data):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
898 output += proc_variable_length(0b0100, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
899 output += obj
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
900 elif isinstance(obj, str):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
901 # Python 3 uses unicode strings only
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
902 bytes = obj.encode('utf_16_be')
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
903 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
904 output += bytes
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
905 elif isinstance(obj, HashableWrapper):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
906 obj = obj.value
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
907 if isinstance(obj, (set, list, tuple)):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
908 if isinstance(obj, set):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
909 output += proc_variable_length(0b1100, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
910 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
911 output += proc_variable_length(0b1010, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
912
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
913 objectsToWrite = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
914 for objRef in obj:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
915 (isNew, output) = self.writeObjectReference(objRef, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
916 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
917 objectsToWrite.append(objRef)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
918 for objRef in objectsToWrite:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
919 output = self.writeObject(objRef, output, setReferencePosition=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
920 elif isinstance(obj, dict):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
921 output += proc_variable_length(0b1101, len(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
922 keys = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
923 values = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
924 objectsToWrite = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
925 for key, value in obj.items():
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
926 keys.append(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
927 values.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
928 for key in keys:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
929 (isNew, output) = self.writeObjectReference(key, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
930 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
931 objectsToWrite.append(key)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
932 for value in values:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
933 (isNew, output) = self.writeObjectReference(value, output)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
934 if isNew:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
935 objectsToWrite.append(value)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
936 for objRef in objectsToWrite:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
937 output = self.writeObject(objRef, output, setReferencePosition=True)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
938 return output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
939
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
940 def writeOffsetTable(self, output):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
941 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
942 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
943
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
944 @param output current output (bytes)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
945 @return new output (bytes)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
946 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
947 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
948 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
949 all_positions = []
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
950 writtenReferences = list(self.writtenReferences.items())
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
951 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
952 for obj, order in writtenReferences:
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
953 position = self.referencePositions.get(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
954 if position is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
955 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
956 "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
957 .format(obj))
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
958 output += self.binaryInt(position, self.trailer.offsetSize)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
959 all_positions.append(position)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
960 return output
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
961
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
962 def binaryReal(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
963 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
964 Private method to pack a real object.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
965
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
966 @param obj real to be packed
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
967 @return serialized object (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 # just use doubles
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
970 result = pack('>d', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
971 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
972
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
973 def binaryInt(self, obj, bytes=None):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
974 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
975 Private method to pack an integer object.
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 @param obj integer to be packed
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
978 @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
979 @return serialized object (bytes)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
980 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
981 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
982 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
983 result = ''
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
984 if bytes is None:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
985 bytes = self.intSize(obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
986 if bytes == 1:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
987 result += pack('>B', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
988 elif bytes == 2:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
989 result += pack('>H', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
990 elif bytes == 4:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
991 result += pack('>L', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
992 elif bytes == 8:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
993 result += pack('>q', obj)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
994 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
995 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
996 "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
997 return result
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
998
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
999 def intSize(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1000 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1001 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
1002 given integer.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1003
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1004 @param obj integer object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1005 @return number of bytes required (integer)
2965
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1006 @exception InvalidPlistException raised to indicate an invalid
d133c7edd88a Continued correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1007 plist file
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1008 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1009 # SIGNED
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
1010 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
1011 return 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1012 # UNSIGNED
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
1013 elif obj <= 0xFF: # 1 byte
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1014 return 1
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
1015 elif obj <= 0xFFFF: # 2 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1016 return 2
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
1017 elif obj <= 0xFFFFFFFF: # 4 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1018 return 4
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1019 # SIGNED
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1020 # 0x7FFFFFFFFFFFFFFF is the max.
1965
96f5a76e1845 Fixed some PEP-8 related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1720
diff changeset
1021 elif obj <= 0x7FFFFFFFFFFFFFFF: # 8 bytes
1720
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1022 return 8
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1023 else:
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1024 raise InvalidPlistException(
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1025 "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
1026
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1027 def realSize(self, obj):
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1028 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1029 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
1030 given real.
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1031
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1032 @param obj real object
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1033 @return number of bytes required (integer)
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1034 """
201622cf8a01 Added a bookmarks importer for Apple Safari.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1035 return 8

eric ide

mercurial