Skip to content

Instantly share code, notes, and snippets.

@pwc3
Created August 6, 2013 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwc3/6161201 to your computer and use it in GitHub Desktop.
Save pwc3/6161201 to your computer and use it in GitHub Desktop.
Opens a property list file in a text editor, converting from binary if necessary.
#!/usr/bin/env python
import argparse
import subprocess
import sys
EDITOR = 'vim'
BINARY = 'Apple binary property list'
XML = 'XML document text'
def convert_to_binary(filename):
print 'Converting "%s" to Binary Plist' % filename
subprocess.call('plutil -convert binary1 "%s"' % filename, shell=True)
def convert_to_xml(filename):
print 'Converting "%s" to XML Plist' % filename
subprocess.call('plutil -convert xml1 "%s"' % filename, shell=True)
def get_file_type(filename):
output = subprocess.check_output('file "%s"' % filename, shell=True)
output = output.strip()
if output.endswith(BINARY):
return BINARY
elif output.endswith(XML):
return XML
else:
return output
def open_in_vim(filename):
print 'Opening "%s" in %s' % (filename, EDITOR)
subprocess.call('"%s" "%s"' % (EDITOR, filename), shell=True)
def parse_args(argv):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser()
parser.add_argument('filename',
metavar='FILE',
help="input filename")
return parser.parse_args(argv)
def main(argv=None):
options = parse_args(argv)
filetype = get_file_type(options.filename)
if filetype == BINARY:
convert_to_xml(options.filename)
open_in_vim(options.filename)
convert_to_binary(options.filename)
elif filetype == XML:
open_in_vim(options.filename)
else:
print >>sys.stderr, 'Unknown filetype: %s' % filetype
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment