Created
May 15, 2012 12:26
-
-
Save gka/2701371 to your computer and use it in GitHub Desktop.
Kartograph web.py API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import web | |
import json | |
from kartograph.kartograph import Kartograph | |
urls = ( | |
'/1.0/svg/', 'GenerateSVG', | |
) | |
app = web.application(urls, globals()) | |
kartographInstance = Kartograph() | |
class JsonApi(object): | |
""" base class for api classes """ | |
def jsonify(self, result): | |
data = web.input() | |
if 'callback' in data: | |
return data.callback + '(' + json.dumps(result) + ');' | |
else: | |
return json.dumps(result) | |
def error(self, message): | |
return self.jsonify(dict(result='error', message=message)) | |
class GenerateSVG(JsonApi): | |
def GET(self): | |
data = web.input() | |
if 'format' in data: | |
format = data['format'] | |
else: | |
format = 'json' | |
if 'config' in data: | |
if format == 'json': | |
try: | |
cfg = json.loads(data['config']) | |
except: | |
return self.error('error while parsing json map configuration') | |
else: | |
return self.error('unknown map configuration format: ' + format) | |
global kartographInstance | |
k = kartographInstance | |
web.header('Content-Type', 'image/svg+xml') | |
return k.generate(cfg, preview=False) | |
else: | |
return self.error('no map configuration given') | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment