1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#!/usr/bin/env python
#
from __future__ import print_function
import types
import os
import sys
import json
import plistlib
import argparse
from xml.parsers import expat
from xml.parsers.expat import ExpatError
resources = dict()
stack = list()
inc_list = list()
stack.append(resources)
def end_element_handler(tag):
if tag == "section":
stack.pop()
def start_element_handler(tag, attr):
current = stack[-1]
key = None
val = None
if tag == "section":
key = attr["name"]
val = dict()
stack.append(val)
elif tag == "integer":
key = attr["name"]
val = int(attr["value"])
elif tag == "json":
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
if fname is not None and key is not None:
try:
with open(fname) as fp:
val = json.load(fp)
except Exception as err:
print("Error: %s" % str(err), file=sys.stderr)
elif fname is None:
print("Error: No such json file %s" % fbase, file=sys.stderr)
sys.exit(1)
elif tag == "plist":
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
if fname is not None and key is not None:
val = plistlib.readPlist(fname)
elif fname is None:
print("Error: No such plist file %s" % fbase, file=sys.stderr)
sys.exit(1)
elif tag == "text":
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
if fname is not None and key is not None:
try:
with open(fname) as fp:
val = fp.read()
except Exception as err:
print("Error: %s" % str(err), file=sys.stderr)
sys.exit(1)
elif fname is None:
print("Error: No such string file %s" % fbase, file=sys.stderr)
sys.exit(1)
elif tag == "string":
key = attr["name"]
val = attr["value"]
if val is not None:
if isinstance(current, dict):
current[key] = val
elif isinstance(current, tuple):
current.append(val)
def cdata_handler(s):
return
def resource_parse_file(infile):
parser = expat.ParserCreate()
parser.StartElementHandler = start_element_handler
parser.EndElementHandler = end_element_handler
parser.CharacterDataHandler = cdata_handler
try:
with open(infile.name, 'rb') as file:
parser.ParseFile(file)
except ExpatError as err:
print("Error: %s" % str(err), file=sys.stderr)
return None
return resources
def find_file(name):
for inc_dir in inc_list:
inc = "%s/%s" % (inc_dir, name)
if os.path.isfile(inc):
return inc
if os.path.isfile(name):
return name
return None
def main():
parser = argparse.ArgumentParser(description='Creates a resource json from a resource list')
parser.add_argument('-I', metavar='<inc path>', help='Include path to search for files')
parser.add_argument('infile', metavar='<resource list>', type=argparse.FileType('r'), help='Input resources file')
parser.add_argument('outfile', metavar='<resource json>', type=argparse.FileType('w'), nargs='?',
default=sys.stdout, help='Output resources json file [stdout]')
args = parser.parse_args()
if args.I:
inc_list.append(args.I)
parsed_res = resource_parse_file(args.infile)
if parsed_res:
json.dump(parsed_res, args.outfile, indent=4, sort_keys=True)
main()
|