Utilities/binplistlib.py

Sat, 05 Jul 2014 11:41:14 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 05 Jul 2014 11:41:14 +0200
changeset 3669
ac84ac3c0f05
parent 3591
2f2a4a76dd22
child 4021
195a471c327b
permissions
-rw-r--r--

Continued renaming eric5 for PyQt5 to eric6.

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

eric ide

mercurial