Left: | ||
Right: |
OLD | NEW |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The plist file object.""" | 2 """The plist file object.""" |
3 | 3 |
4 from __future__ import unicode_literals | 4 from __future__ import unicode_literals |
5 | 5 |
6 import binascii | |
7 import os | |
8 import plistlib | 6 import plistlib |
9 | 7 |
10 from binplist import binplist | 8 import biplist |
11 | 9 |
12 | 10 |
13 class PlistFile(object): | 11 class PlistFile(object): |
14 """Class that defines a plist file. | 12 """Class that defines a plist file. |
15 | 13 |
16 Attributes: | 14 Attributes: |
17 root_key: the plist root key (instance of plistlib._InternalDict). | 15 root_key: the plist root key (instance of plistlib._InternalDict). |
18 """ | 16 """ |
19 | 17 |
20 def __init__(self): | 18 def __init__(self): |
21 """Initializes the plist file object.""" | 19 """Initializes the plist file object.""" |
22 super(PlistFile, self).__init__() | 20 super(PlistFile, self).__init__() |
23 self.root_key = None | 21 self.root_key = None |
24 | 22 |
25 def GetValueByPath(self, path_segments): | 23 def GetValueByPath(self, path_segments): |
26 """Retrieves a plist value by path. | 24 """Retrieves a plist value by path. |
27 | 25 |
28 Args: | 26 Args: |
29 path_segments: a list of path segments strings relative from the root | 27 path_segments: a list of path segments strings relative from the root |
30 of the plist. | 28 of the plist. |
31 | 29 |
32 Returns: | 30 Returns: |
33 The value of the key specified by the path or None. | 31 The value of the key specified by the path or None. |
34 """ | 32 """ |
35 key = self.root_key | 33 key = self.root_key |
36 for path_segment in path_segments: | 34 for path_segment in path_segments: |
37 # pylint: disable=protected-access | 35 # pylint: disable=protected-access |
38 if isinstance(key, (dict, plistlib._InternalDict)): | 36 if isinstance(key, (dict, plistlib._InternalDict)): |
Joachim Metz
2018/05/18 05:09:52
Does biplist generate objects of type plistlib._In
onager
2018/05/18 12:29:56
Done.
| |
39 key = key.get(path_segment) | 37 key = key.get(path_segment) |
40 | 38 |
41 elif isinstance(key, list): | 39 elif isinstance(key, list): |
42 try: | 40 try: |
43 list_index = int(path_segment, 10) | 41 list_index = int(path_segment, 10) |
44 except ValueError: | 42 except ValueError: |
45 return None | 43 return None |
46 | 44 |
47 key = key[list_index] | 45 key = key[list_index] |
48 | 46 |
49 else: | 47 else: |
50 return None | 48 return None |
51 | 49 |
52 if not key: | 50 if not key: |
53 return None | 51 return None |
54 | 52 |
55 return key | 53 return key |
56 | 54 |
57 def Read(self, file_object): | 55 def Read(self, file_object): |
58 """Reads a plist from a file-like object. | 56 """Reads a plist from a file-like object. |
59 | 57 |
60 Args: | 58 Args: |
61 file_object: the file-like object. | 59 file_object: the file-like object. |
Joachim Metz
2018/05/18 05:09:52
This appears to be an old docstring style, could y
onager
2018/05/18 12:29:57
Done.
| |
62 | 60 |
63 Raises: | 61 Raises: |
64 IOError: if the plist file-like object cannot be read. | 62 IOError: if the plist file-like object cannot be read. |
65 """ | 63 """ |
66 try: | 64 try: |
67 file_object.seek(0, os.SEEK_SET) | 65 self.root_key = biplist.readPlist(file_object) |
68 # Note that binplist.readPlist does not seek to offset 0. | |
69 self.root_key = binplist.readPlist(file_object) | |
70 | 66 |
71 except ( | 67 except ( |
72 AttributeError, binascii.Error, binplist.FormatError, LookupError, | 68 biplist.NotBinaryPlistException, |
73 OverflowError, ValueError) as exception: | 69 biplist.InvalidPlistException) as exception: |
74 raise IOError(exception) | 70 raise IOError(exception) |
OLD | NEW |