Exemple #1
0
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
    if _check_py3():
        plistlib.dump(value, fp, fmt=fmt, sort_keys=sort_keys, skipkeys=skipkeys)
    else:
        if fmt == FMT_XML:
            # We need to monkey patch a bunch here too in order to avoid auto-sorting
            # of keys
            writer = plistlib.PlistWriter(fp)
            def writeDict(d):
                if d:
                    writer.beginElement("dict")
                    items = sorted(d.items()) if sort_keys else d.items()
                    for key, value in items:
                        if not isinstance(key, basestring):
                            if skipkeys:
                                continue
                            raise TypeError("keys must be strings")
                        writer.simpleElement("key", key)
                        writer.writeValue(value)
                    writer.endElement("dict")
                else:
                    writer.simpleElement("dict")
            writer.writeDict = writeDict
            writer.writeln("<plist version=\"1.0\">")
            writer.writeValue(value)
            writer.writeln("</plist>")
        elif fmt == FMT_BINARY:
            # Assume binary at this point
            writer = _BinaryPlistWriter(fp, sort_keys=sort_keys, skipkeys=skipkeys)
            writer.write(value)
        else:
            # Not a proper format
            raise ValueError("Unsupported format: {}".format(fmt))
Exemple #2
0
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
    if _check_py3():
        plistlib.dump(value, fp, fmt=fmt, sort_keys=sort_keys, skipkeys=skipkeys)
    else:
        # We need to monkey patch a bunch here too in order to avoid auto-sorting
        # of keys
        writer = plistlib.PlistWriter(fp)
        def writeDict(d):
            if d:
                writer.beginElement("dict")
                items = sorted(d.items()) if sort_keys else d.items()
                for key, value in items:
                    if not isinstance(key, (str,unicode)):
                        if skipkeys:
                            continue
                        raise TypeError("keys must be strings")
                    writer.simpleElement("key", key)
                    writer.writeValue(value)
                writer.endElement("dict")
            else:
                writer.simpleElement("dict")
        writer.writeDict = writeDict
        writer.writeln("<plist version=\"1.0\">")
        writer.writeValue(value)
        writer.writeln("</plist>")
    replace = parsehex(args.replace)
else:
    find = parsestr(args.find)
    replace = parsestr(args.replace)

d = dict(Find=plistlib.Data(find),
         Replace=plistlib.Data(replace))  # , Disabled=False)
if args.comment:
    d["Comment"] = args.comment

if args.file:
    d["Name"] = args.file

if args.clover:
    if args.file:
        d = dict(KernelAndKextPatches=dict(KextsToPatch=[d]))
    else:
        d = dict(ACPI=dict(DSDT=dict(Patches=[d])))

if args.whole or args.clover:
    plistlib.writePlist(d, sys.stdout)
else:
    f = BytesIO()
    # We don't want the stuff at the top of a full plist
    plw = plistlib.PlistWriter(f, writeHeader=False, indentLevel=4)
    plw.writeValue(d)
    sys.stdout.write(f.getvalue())

if len(find) != len(replace):
    print("Warning: find and replace lengths do not match")