[TxMt] Re: Parsing plist with escape char

Per Persson persquare at mac.com
Thu Sep 29 14:50:04 UTC 2016


On Sep 29, 2016, at 01:14 PM, Allan Odgaard <mailinglist at textmate.org> wrote:

On 29 Sep 2016, at 12:11, Per Persson wrote:

It works mostly well, but trying to parse a command file that contains a command mapped to the escape key using plistlib/biplist fails with an exception:
[…]
Any clues how to handle this?
The issue is that XML does not allow ASCII control characters (and python checks this). XML 1.1 should allow them when entity-encoded, but both python 2.7.12 and 3.5.2 still throw an exception (for &#x1B;) in my test.
The biplist is using plistlib for XML property lists, so it may actually work for binary property lists.
So when encountering this exception, you could try convert the property list to its binary form (using plutil) and see if that works.
You could also look into using PyObjC and import NSDictionary, this should definitely read the file without problems.

Thanks Allan,
plutil did the trick. In fact it could actually convert the plist file to JSON with the escape properly escaped (pun intended;) 
For the records, this is what I ended up with (no need for plistlib/biplist):

def json_from_plist(path):
   p = Popen(['plutil', '-convert', 'json', path, '-o', '-'], stdout=PIPE, stderr=PIPE)
   res, err = p.communicate()
   return json.loads(res) if not err else {}

/Per

_______________________________________________
textmate mailing list
textmate at lists.macromates.com
http://lists.macromates.com/listinfo/textmate
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macromates.com/textmate/attachments/20160929/3dd53cd3/attachment.html>


More information about the textmate mailing list