summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqarkai <[email protected]>2016-09-10 15:57:27 +0300
committerBradley Sepos <[email protected]>2017-03-04 09:06:30 -0500
commitb34b6735a0cf072f0b1683266ab9a067686b159a (patch)
tree1364954a0b19a459922eab200274c9717465d400
parent59dc22b8690e566270b6a543aa8856e11531578b (diff)
scripts: Modernize Python.
Replace getopt with argparse Remove useless globals Fix PEP8 warnings Replace top() with native list index Handle Expat exceptions Closes #321.
-rwxr-xr-xscripts/create_resources.py130
-rwxr-xr-xscripts/quotestring.py54
2 files changed, 56 insertions, 128 deletions
diff --git a/scripts/create_resources.py b/scripts/create_resources.py
index a27afab20..c0096e28f 100755
--- a/scripts/create_resources.py
+++ b/scripts/create_resources.py
@@ -4,31 +4,28 @@
import types
import os
import sys
-import time
-import datetime
import json
import plistlib
-import getopt
+import argparse
from xml.parsers import expat
+from xml.parsers.expat import ExpatError
resources = dict()
stack = list()
+inc_list = list()
+
stack.append(resources)
-def top(ss):
- return ss[len(ss)-1]
def end_element_handler(tag):
- global stack
-
if tag == "section":
stack.pop()
-def start_element_handler(tag, attr):
- global resources, stack
- current = top(stack)
- key = val = None
+def start_element_handler(tag, attr):
+ current = stack[-1]
+ key = None
+ val = None
if tag == "section":
key = attr["name"]
val = dict()
@@ -40,76 +37,69 @@ def start_element_handler(tag, attr):
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
- if fname != None and key != None:
+ if fname is not None and key is not None:
try:
- fp = open(fname)
+ with open(fname) as fp:
+ val = json.load(fp)
except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err) )
- val = json.load(fp)
- elif fname == None:
- print >> sys.stderr, ( "Error: No such json file %s" % fbase )
+ print >> sys.stderr, ("Error: %s" % str(err))
+ elif fname is None:
+ print >> sys.stderr, ("Error: No such json file %s" % fbase)
sys.exit(1)
elif tag == "plist":
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
- if fname != None and key != None:
+ if fname is not None and key is not None:
val = plistlib.readPlist(fname)
- elif fname == None:
- print >> sys.stderr, ( "Error: No such plist file %s" % fbase )
+ elif fname is None:
+ print >> sys.stderr, ("Error: No such plist file %s" % fbase)
sys.exit(1)
elif tag == "text":
fbase = attr["file"]
fname = find_file(fbase)
key = attr["name"]
- if fname != None and key != None:
+ if fname is not None and key is not None:
try:
- fp = open(fname)
- val = fp.read()
+ with open(fname) as fp:
+ val = fp.read()
except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err) )
+ print >> sys.stderr, ("Error: %s" % str(err))
sys.exit(1)
- elif fname == None:
- print >> sys.stderr, ( "Error: No such string file %s" % fbase )
+ elif fname is None:
+ print >> sys.stderr, ("Error: No such string file %s" % fbase)
sys.exit(1)
elif tag == "string":
key = attr["name"]
val = attr["value"]
- if val != None:
- if type(current) == types.DictType:
+ if val is not None:
+ if isinstance(current, types.DictType):
current[key] = val
- elif type(current) == types.TupleType:
+ elif isinstance(current, types.TupleType):
current.append(val)
-def cdata_handler(str):
+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
- parser.ParseFile(infile)
-
-def usage():
- print >> sys.stderr, (
- "Usage: %s [-I <inc path>] <resource list> [resource json]\n"
- "Summary:\n"
- " Creates a resource json from a resource list\n\n"
- "Options:\n"
- " I - Include path to search for files\n"
- " <resource list> Input resources file\n"
- " <resource json> Output resources json file\n"
- % sys.argv[0]
- )
-inc_list = list()
+ try:
+ parser.ParseFile(infile)
+ except ExpatError, err:
+ print >> sys.stderr, ("Error: %s" % str(err))
+ return None
+
+ return resources
-def find_file(name):
- global inc_list
+def find_file(name):
for inc_dir in inc_list:
inc = "%s/%s" % (inc_dir, name)
if os.path.isfile(inc):
@@ -120,45 +110,21 @@ def find_file(name):
return None
+
def main():
- global inc_list
+ 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()
- OPTS = "I:"
- try:
- opts, args = getopt.gnu_getopt(sys.argv[1:], OPTS)
- except getopt.GetoptError, err:
- print >> sys.stderr, str(err)
- usage()
- sys.exit(2)
-
- for o, a in opts:
- if o == "-I":
- # add to include list
- inc_list.append(a)
- else:
- assert False, "unhandled option"
-
- if len(args) > 2 or len(args) < 1:
- usage()
- sys.exit(2)
+ if args.I:
+ inc_list.append(args.I)
- try:
- infile = open(args[0])
- except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err) )
- sys.exit(1)
-
- if len(args) > 1:
- try:
- outfile = open(args[1], "w")
- except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err))
- sys.exit(1)
- else:
- outfile = sys.stdout
+ parsed_res = resource_parse_file(args.infile)
+ if parsed_res:
+ json.dump(parsed_res, args.outfile, indent=4, sort_keys=True)
- resource_parse_file(infile)
- json.dump(resources, outfile, indent=4, sort_keys=True)
main()
-
diff --git a/scripts/quotestring.py b/scripts/quotestring.py
index 1038719e7..e3712f140 100755
--- a/scripts/quotestring.py
+++ b/scripts/quotestring.py
@@ -1,62 +1,24 @@
#! /usr/bin/python
import re
-import getopt
+import argparse
import sys
-def usage():
- print >> sys.stderr, (
- "Usage: %s <input> [output]\n"
- "Summary:\n"
- " Creates a quoted string suitable for inclusion in a C char*\n\n"
- "Options:\n"
- " <input> Input file to quote\n"
- " <output> Output quoted string [stdout]\n"
- % sys.argv[0]
- )
def main():
- global inc_list
+ parser = argparse.ArgumentParser(description='Creates a quoted string suitable for inclusion in a C char*')
+ parser.add_argument('infile', metavar='<input>', type=argparse.FileType('r'), help='Input file to quote')
+ parser.add_argument('outfile', metavar='<output>', type=argparse.FileType('w'), nargs='?',
+ default=sys.stdout, help='Output quoted string [stdout]')
+ args = parser.parse_args()
- OPTS = ""
- try:
- opts, args = getopt.gnu_getopt(sys.argv[1:], OPTS)
- except getopt.GetoptError, err:
- print >> sys.stderr, str(err)
- usage()
- sys.exit(2)
-
- for o, a in opts:
- usage()
- assert False, "unhandled option"
-
- if len(args) > 2 or len(args) < 1:
- usage()
- sys.exit(2)
-
- try:
- infile = open(args[0])
- except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err) )
- sys.exit(1)
-
- if len(args) > 1:
- try:
- outfile = open(args[1], "w")
- except Exception, err:
- print >> sys.stderr, ( "Error: %s" % str(err))
- sys.exit(1)
- else:
- outfile = sys.stdout
-
- ss = infile.read()
+ ss = args.infile.read()
ss = re.sub(r'\\', r'\\\\', ss)
ss = re.sub(r'"', r'\\"', ss)
pattern = re.compile("$", re.M)
ss = re.sub(pattern, r'\\n"', ss)
pattern = re.compile("^", re.M)
ss = re.sub(pattern, "\"", ss)
- outfile.write(ss)
+ args.outfile.write(ss)
main()
-