Skip to content

Commit

Permalink
Add compatibility for Python <3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolson committed Jun 8, 2022
1 parent d734763 commit 2cb3afc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/plist-file-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class PlutilParser implements Parser {
class PythonParser implements Parser {
toXml(uri: string): string {
const python = `
import plistlib
import sys, codecs, plistlib
sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)
fp = open("""${uri.replace(/\\/g,'\\\\')}""", 'rb')
pl = plistlib.load(fp)
print(plistlib.dumps(pl).decode('utf-8'))
`;
const output = spawnSync('python', ['-X', 'utf8', '-c', python], { encoding: 'utf8' });
const output = spawnSync('python', ['-c', python], { encoding: 'utf8' });
if (String(output.stderr).length) {
throw Error(String(output.stderr));
}
Expand All @@ -42,7 +44,9 @@ print(plistlib.dumps(pl).decode('utf-8'))

async toBinary(uri: string, xmlString: string): Promise<void> {
const python = `
import sys, os, tempfile, shutil, plistlib
import sys, os, codecs, tempfile, shutil, plistlib
sys.stdin = codecs.getreader('utf8')(sys.stdin.buffer)
fp = tempfile.NamedTemporaryFile(mode='wb', delete=False)
pl = plistlib.loads(sys.stdin.read().encode('utf-8'), fmt=plistlib.FMT_XML)
Expand All @@ -52,7 +56,7 @@ fp.close()
shutil.copy(path, """${uri.replace(/\\/g,'\\\\')}""")
os.remove(path)
`;
const output = spawnSync('python', ['-X', 'utf8', '-c', python], { input: xmlString, encoding: 'utf8' });
const output = spawnSync('python', ['-c', python], { input: xmlString, encoding: 'utf8' });
if (String(output.stderr).length) {
throw Error(String(output.stderr));
}
Expand Down

0 comments on commit 2cb3afc

Please sign in to comment.