Skip to content

Instantly share code, notes, and snippets.

@FrantisekGazo
Created June 25, 2016 11:20
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 FrantisekGazo/f32939012eeab128da51e39c76a534b9 to your computer and use it in GitHub Desktop.
Save FrantisekGazo/f32939012eeab128da51e39c76a534b9 to your computer and use it in GitHub Desktop.
Python script for generating java enum/StringDef or swift enum based on keys in json file
#!/usr/bin/python
#######################################################################
#
# Script for generating java or swift class based on keys in json file.
#
# @author FrantisekGazo
# @version 2016-06-23
#
#######################################################################
import argparse
import sys, os
import json
def main(args):
# prepare class and package name
packageName = ''
className = args.class_name
lastDotIndex = args.class_name.rfind('.')
if lastDotIndex > 0:
packageName = args.class_name[:lastDotIndex]
print packageName
className = args.class_name[lastDotIndex+1:]
print className
# open output file
outputFilePath = os.path.join(args.output_dir, packageName, className)
if args.platform == 'android':
outputFilePath += '.java'
elif args.platform == 'ios':
outputFilePath += '.swift'
if not os.path.exists(os.path.dirname(outputFilePath)):
os.makedirs(os.path.dirname(outputFilePath))
outputFile = open(outputFilePath, 'w+')
# parse json
with open(args.input_json) as data_file:
data = json.load(data_file)
# get keys
keys = []
for o in data:
keys.append(o["key"])
print "############################################################"
print "#"
print "# args: ", args
print "#"
if args.platform == 'android':
if args.type == 'StringDef':
if packageName:
print >> outputFile, "package " + packageName + ";"
print >> outputFile, ""
print >> outputFile, "import android.support.annotation.StringDef;"
print >> outputFile, "import java.lang.annotation.Retention;"
print >> outputFile, "import java.lang.annotation.RetentionPolicy;"
print >> outputFile, ""
print >> outputFile, "/** GENERATED */"
print >> outputFile, "@Retention(RetentionPolicy.SOURCE)"
print >> outputFile, "@StringDef(value = {"
for key in keys:
print >> outputFile, "\t" + className + "." + key + ","
print >> outputFile, "})"
print >> outputFile, "public @interface " + className + " {"
for key in keys:
print >> outputFile, "\tString " + key + " = \"" + key + "\";"
print >> outputFile, "}"
elif args.type == 'enum':
if packageName:
print >> outputFile, "package " + packageName + ";"
print >> outputFile, ""
print >> outputFile, "/** GENERATED */"
print >> outputFile, "public enum " + className + " {"
c = len(keys)
for key in keys:
c -= 1
print >> outputFile, "\t" + key + ("," if c > 0 else "")
print >> outputFile, "}"
else:
print 'Unsupported type'
sys.exit(2)
elif args.platform == 'ios':
print >> outputFile, "/** GENERATED */"
print >> outputFile, "public enum " + className + " {"
for key in keys:
print >> outputFile, "\t case " + key
print >> outputFile, "}"
print "############################################################"
if __name__ == '__main__':
# doc for argparse: https://docs.python.org/3/library/argparse.html
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input-json', required=True)
parser.add_argument('-o', '--output-dir', required=True)
parser.add_argument('-c', '--class-name', required=True)
parser.add_argument('-p', '--platform', required=True)
parser.add_argument('-t', '--type', required=False, default='enum')
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment