Skip to content

Commit

Permalink
Merge 3871434 into b027d16
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalobenegas committed Mar 25, 2014
2 parents b027d16 + 3871434 commit 6b79326
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions urbansim/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def configs_dir():
return mkifnotexists("configs")


def charts_dir():
return mkifnotexists("charts")


def runs_dir():
return mkifnotexists("runs")

Expand Down
64 changes: 57 additions & 7 deletions urbansimd/urbansimd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def encode_float32(obj):


def wrap_request(request, response, obj):
if (request.query.callback):
if request.query.callback:
response.content_type = "application/javascript"
s = simplejson.dumps(obj, ignore_nan=True, default=encode_float32)
print "Response: %s\n" % s
Expand All @@ -62,8 +62,14 @@ def enable_cors():
@route('/configs')
def list_configs():
def resp():
files = os.listdir(misc.configs_dir())
return files
files = [f for f in os.listdir(misc.configs_dir())
if f[-5:] == '.json']

def not_modelset(f):
c = open(os.path.join(misc.configs_dir(), f)).read()
c = json.loads(c)
return 'model' in c and c['model'] != 'modelset'
return filter(not_modelset, files)
return wrap_request(request, response, resp())


Expand Down Expand Up @@ -91,6 +97,38 @@ def resp():
return wrap_request(request, response, resp())


@route('/charts')
def list_charts():
def resp():
files = os.listdir(misc.charts_dir())
return files
return wrap_request(request, response, resp())


@route('/chart/<chartname>', method="GET")
def read_config(chartname):
def resp():
c = open(os.path.join(misc.charts_dir(), chartname)).read()
return simplejson.loads(c)
return wrap_request(request, response, resp())


@route('/chart/<chartname>', method="OPTIONS")
def ans_opt(chartname):
return {}


@route('/chart/<chartname>', method="PUT")
def write_config(chartname):
json = request.json

def resp():
s = simplejson.dumps(json, indent=4)
print s
return open(os.path.join(misc.charts_dir(), chartname), "w").write(s)
return wrap_request(request, response, resp())


@route('/datasets')
def list_datasets():
def resp():
Expand Down Expand Up @@ -190,7 +228,8 @@ def resp(estimate, simulate):
return wrap_request(request, response, resp(estimate, simulate))


def pandas_statement(table, where, sort, orderdesc, groupby, metric, limit, page):
def pandas_statement(table, where, sort, orderdesc, groupby, metric,
limit, page):
if where:
where = "[DSET.fetch('%s').apply(lambda x: bool(%s),axis=1)]" % (
table, where)
Expand Down Expand Up @@ -242,7 +281,7 @@ def resp(name):
return wrap_request(request, response, resp(name))


@route('/query', method=['OPTIONS', 'GET', 'POST'])
@route('/makechart', method=['OPTIONS', 'GET', 'POST'])
def query():
req = request.query.json
if (request.query.callback):
Expand Down Expand Up @@ -281,8 +320,19 @@ def query():

print "Executing %s\n" % s
recs = eval(s)
recs = [[int(x), float(recs.ix[x])] for x in recs.index]
s = simplejson.dumps({'records': recs}, use_decimal=True)

if 'key_dictionary' in req:
key_dictionary = req['key_dictionary']
#not sure /configs is the proper place to save dicts
dictionary_file = open("configs/" + key_dictionary).read()
dictionary = json.loads(dictionary_file)
#attention: the dictionary has keys from 0 to 15, ids come from 0 to 16
recs = [[dictionary[str(int(x))], float(recs.ix[x]) / 1000]
for x in recs.index]
else:
recs = [[x, float(recs.ix[x]) / 1000] for x in recs.index]

s = simplejson.dumps([{'key': '', 'values': recs}], use_decimal=True)
print "Response: %s\n" % s
return jsonp(request, s)

Expand Down

0 comments on commit 6b79326

Please sign in to comment.