Skip to content

Instantly share code, notes, and snippets.

@anabarasan
Created November 22, 2013 10:48
Show Gist options
  • Save anabarasan/7598016 to your computer and use it in GitHub Desktop.
Save anabarasan/7598016 to your computer and use it in GitHub Desktop.
Generate orangescape model json which can be used to create models in orangescape studio, from Excel File containing the field information
# GenerateJson.py
# generates the JSON to be used to create model using the Import Json Functionality
# requires xlrd library, which can be obtained from https://pypi.python.org/pypi/xlrd/0.9.2
# xlrd documentation => http://www.lexicon.net/sjmachin/xlrd.html
# Anbarasan <nasarabna@gmail.com>
# usage GenerateJson.py <path to Model params Excel file>
# In Excel :
# SheetName should be Model Name
# first column should be fieldname
# second column should be field type
import json, sys, xlrd
class GenerateOrangeScapeModelJson:
def __init__(self, excel):
self.workbook = xlrd.open_workbook(excel)
def getModels(self):
return self.workbook.sheets() # get the sheets from the xl workbook
def getModelParams(self, model):
modelparams = {}
for row in xrange(0, int(model.nrows)):
paramattributes = {}
paramname, paramtype = model.cell(row,0).value, model.cell(row,1).value
if (len(paramname.strip()) > 0):
if paramtype == 'Number':
paramattributes["Category"] = "Number"
paramattributes["Format"] = "###.##"
elif paramtype == 'Date':
paramattributes["Category"] = "Date"
paramattributes["Format"] = "dd/mm/yyyy"
else:
paramattributes["Category"] = "Text"
paramattributes["Format"] = "Text"
modelparams[paramname] = paramattributes
return modelparams
def createJsonFile(self, modelname, modeljson):
modeldefinition = file('out/' + modelname + '.json', 'w')
modeldefinition.write(json.dumps(modeljson))
modeldefinition.close()
def convert(self):
modelList = self.getModels()
for model in modelList:
modelname = model.name
print "generating json for %s" % modelname
modeljson = {}
params = {}
modeljson[modelname] = {"Layout":5}
modeljson[modelname]["Fields"] = self.getModelParams(model)
self.createJsonFile(modelname, modeljson)
models = GenerateOrangeScapeModelJson(sys.argv[1])
models.convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment