|
| 1 | +"""A :class:`graphtage.Filetype` for parsing, diffing, and rendering Apple plist files.""" |
| 2 | +import os |
| 3 | +from xml.parsers.expat import ExpatError |
| 4 | +from typing import Optional, Tuple, Union |
| 5 | + |
| 6 | +from plistlib import dumps, load |
| 7 | + |
| 8 | +from . import json |
| 9 | +from .edits import Edit, EditCollection, Match |
| 10 | +from .graphtage import BoolNode, BuildOptions, Filetype, FloatNode, KeyValuePairNode, IntegerNode, LeafNode, StringNode |
| 11 | +from .printer import Printer |
| 12 | +from .sequences import SequenceFormatter, SequenceNode |
| 13 | +from .tree import ContainerNode, GraphtageFormatter, TreeNode |
| 14 | + |
| 15 | + |
| 16 | +class PLISTNode(ContainerNode): |
| 17 | + def __init__(self, root: TreeNode): |
| 18 | + self.root: TreeNode = root |
| 19 | + |
| 20 | + def to_obj(self): |
| 21 | + return self.root.to_obj() |
| 22 | + |
| 23 | + def edits(self, node: 'TreeNode') -> Edit: |
| 24 | + if isinstance(node, PLISTNode): |
| 25 | + return EditCollection( |
| 26 | + from_node=self, |
| 27 | + to_node=node, |
| 28 | + edits=iter(( |
| 29 | + Match(self, node, 0), |
| 30 | + self.root.edits(node.root) |
| 31 | + )), |
| 32 | + collection=list, |
| 33 | + add_to_collection=list.append, |
| 34 | + explode_edits=False |
| 35 | + ) |
| 36 | + return self.root.edits(node) |
| 37 | + |
| 38 | + def calculate_total_size(self) -> int: |
| 39 | + return self.root.calculate_total_size() |
| 40 | + |
| 41 | + def print(self, printer: Printer): |
| 42 | + printer.write(PLIST_HEADER) |
| 43 | + self.root.print(printer) |
| 44 | + printer.write(PLIST_FOOTER) |
| 45 | + |
| 46 | + def __iter__(self): |
| 47 | + yield self.root |
| 48 | + |
| 49 | + def __len__(self) -> int: |
| 50 | + return 1 |
| 51 | + |
| 52 | + |
| 53 | +def build_tree(path: str, options: Optional[BuildOptions] = None, *args, **kwargs) -> PLISTNode: |
| 54 | + """Constructs a PLIST tree from an PLIST file.""" |
| 55 | + with open(path, "rb") as stream: |
| 56 | + data = load(stream) |
| 57 | + return PLISTNode(json.build_tree(data, options=options, *args, **kwargs)) |
| 58 | + |
| 59 | + |
| 60 | +class PLISTSequenceFormatter(SequenceFormatter): |
| 61 | + is_partial = True |
| 62 | + |
| 63 | + def __init__(self): |
| 64 | + super().__init__('', '', '') |
| 65 | + |
| 66 | + def print_SequenceNode(self, printer: Printer, node: SequenceNode): |
| 67 | + self.parent.print(printer, node) |
| 68 | + |
| 69 | + def print_ListNode(self, printer: Printer, *args, **kwargs): |
| 70 | + printer.write("<array>") |
| 71 | + super().print_SequenceNode(printer, *args, **kwargs) |
| 72 | + printer.write("</array>") |
| 73 | + |
| 74 | + def print_MultiSetNode(self, printer: Printer, *args, **kwargs): |
| 75 | + printer.write("<dict>") |
| 76 | + super().print_SequenceNode(printer, *args, **kwargs) |
| 77 | + printer.write("</dict>") |
| 78 | + |
| 79 | + def print_KeyValuePairNode(self, printer: Printer, node: KeyValuePairNode): |
| 80 | + printer.write("<key>") |
| 81 | + if isinstance(node.key, StringNode): |
| 82 | + printer.write(node.key.object) |
| 83 | + else: |
| 84 | + self.print(printer, node.key) |
| 85 | + printer.write("</key>") |
| 86 | + printer.newline() |
| 87 | + self.print(printer, node.value) |
| 88 | + |
| 89 | + print_MappingNode = print_MultiSetNode |
| 90 | + |
| 91 | + |
| 92 | +def _plist_header_footer() -> Tuple[str, str]: |
| 93 | + string = "1234567890" |
| 94 | + encoded = dumps(string).decode("utf-8") |
| 95 | + expected = f"<string>{string}</string>" |
| 96 | + body_offset = encoded.find(expected) |
| 97 | + if body_offset <= 0: |
| 98 | + raise ValueError("Unexpected plist encoding!") |
| 99 | + return encoded[:body_offset], encoded[body_offset+len(expected):] |
| 100 | + |
| 101 | + |
| 102 | +PLIST_HEADER: str |
| 103 | +PLIST_FOOTER: str |
| 104 | +PLIST_HEADER, PLIST_FOOTER = _plist_header_footer() |
| 105 | + |
| 106 | + |
| 107 | +class PLISTFormatter(GraphtageFormatter): |
| 108 | + sub_format_types = [PLISTSequenceFormatter] |
| 109 | + |
| 110 | + def print(self, printer: Printer, *args, **kwargs): |
| 111 | + # PLIST uses an eight-space indent |
| 112 | + printer.indent_str = " " * 8 |
| 113 | + super().print(printer, *args, **kwargs) |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def write_obj(printer: Printer, obj): |
| 117 | + encoded = dumps(obj).decode("utf-8") |
| 118 | + printer.write(encoded[len(PLIST_HEADER):-len(PLIST_FOOTER)]) |
| 119 | + |
| 120 | + def print_StringNode(self, printer: Printer, node: StringNode): |
| 121 | + printer.write(f"<string>{node.object}</string>") |
| 122 | + |
| 123 | + def print_IntegerNode(self, printer: Printer, node: IntegerNode): |
| 124 | + printer.write(f"<integer>{node.object}</integer>") |
| 125 | + |
| 126 | + def print_FloatNode(self, printer: Printer, node: FloatNode): |
| 127 | + printer.write(f"<real>{node.object}</real>") |
| 128 | + |
| 129 | + def print_BoolNode(self, printer, node: BoolNode): |
| 130 | + if node.object: |
| 131 | + printer.write("<true />") |
| 132 | + else: |
| 133 | + printer.write("<false />") |
| 134 | + |
| 135 | + def print_LeafNode(self, printer: Printer, node: LeafNode): |
| 136 | + self.write_obj(printer, node.object) |
| 137 | + |
| 138 | + def print_PLISTNode(self, printer: Printer, node: PLISTNode): |
| 139 | + printer.write(PLIST_HEADER) |
| 140 | + self.print(printer, node.root) |
| 141 | + printer.write(PLIST_FOOTER) |
| 142 | + |
| 143 | + |
| 144 | +class PLIST(Filetype): |
| 145 | + """The Apple PLIST filetype.""" |
| 146 | + def __init__(self): |
| 147 | + """Initializes the PLIST file type. |
| 148 | +
|
| 149 | + By default, PLIST associates itself with the "plist" and "application/x-plist" MIME types. |
| 150 | +
|
| 151 | + """ |
| 152 | + super().__init__( |
| 153 | + 'plist', |
| 154 | + 'application/x-plist' |
| 155 | + ) |
| 156 | + |
| 157 | + def build_tree(self, path: str, options: Optional[BuildOptions] = None) -> TreeNode: |
| 158 | + tree = build_tree(path=path, options=options) |
| 159 | + for node in tree.dfs(): |
| 160 | + if isinstance(node, StringNode): |
| 161 | + node.quoted = False |
| 162 | + return tree |
| 163 | + |
| 164 | + def build_tree_handling_errors(self, path: str, options: Optional[BuildOptions] = None) -> Union[str, TreeNode]: |
| 165 | + try: |
| 166 | + return self.build_tree(path=path, options=options) |
| 167 | + except ExpatError as ee: |
| 168 | + return f'Error parsing {os.path.basename(path)}: {ee})' |
| 169 | + |
| 170 | + def get_default_formatter(self) -> PLISTFormatter: |
| 171 | + return PLISTFormatter.DEFAULT_INSTANCE |
0 commit comments