#!/usr/bin/env python # vim: set expandtab: # vim: set softtabstop=2: # vim: set shiftwidth=2: # vim: set tabstop=2: ### ### This is a web service for use with App ### Inventor for Android () ### This particular service stores and retrieves tag-value pairs ### using the protocol necessary to communicate with the TinyWebDB ### component of an App Inventor app. ### THIS IS A MODIFIED VERSION FOR CSV FILES ### ### Author: ShivalWolf (shivalwolf@gmail.com ### Based off customwebdb code by Author: David Wolber (wolber@usfca.edu), using sample of Hal Abelson import logging from cgi import escape from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from django.utils import simplejson as json import urllib import csv import os IntroMessage = '''Go Away''' class MainPage(webapp.RequestHandler): def get(self): write_page_header(self); self.response.out.write(IntroMessage) self.response.out.write('') def post(self): write_page_header(self); self.response.out.write(IntroMessage) self.response.out.write('') ######################################## ### Implementing the operations ### Each operation is design to respond to the JSON request ### or to the Web form, depending on whether the fmt input to the post ### is json or html. ### Each operation is a class. The class includes the method that ### actually, manipualtes the DB, followed by the methods that respond ### to post and to get. class GetValue(webapp.RequestHandler): def get_value(self, tag): outvalue="" data = tag.split("||") filetoload=data[0] DELIM=data[1].strip() if DELIM == "comma": DELIM ="," elif DELIM == "tab": DELIM = "\t" else: DELIM = "" outvalue="BAD DELIMITER" logging.error("CSV FILE: '"+filetoload+"'"); logging.error("delimiter: '"+DELIM+"'"); if filetoload.endswith(".csv") or filetoload.endswith(".txt"): if "http" in tag: try: csvfile=urllib.urlopen(filetoload) except: outvalue="\"Could not load CSV from URL\"" else: if os.path.isfile(filetoload): csvfile=open(filetoload,"r") else: outvalue="\"File does not exist\"" else: outvalue="\"Invalid File\"" if outvalue == "": reader = csv.reader(csvfile, delimiter=str(DELIM)) header = reader.next() reader = csv.DictReader(csvfile, header, delimiter=str(DELIM)) outvalue=json.dumps( [ row for row in reader ] ) WritePhoneOrWeb(self, lambda : json.dump(["VALUE", tag, outvalue], self.response.out)) def post(self): tag = self.request.get('tag') self.get_value(tag) def get(self): self.response.out.write('''Go Away\n''') ######################################## #### Utilty procedures for generating the output def WritePhoneOrWeb(handler, writer): handler.response.headers['Content-Type'] = 'application/jsonrequest' writer() def write_page_header(self): self.response.headers['Content-Type'] = 'text/html' self.response.out.write(''' Tiny CSV DB ''') self.response.out.write('

App Inventor for Android: Custom Tiny CSV DB Service

') application = webapp.WSGIApplication([('/', MainPage), ('/getvalue', GetValue)], debug=True) def main(): run_wsgi_app(application) if __name__ == '__main__': main() ### Copyright 2009 Google Inc. ### ### Licensed under the Apache License, Version 2.0 (the "License"); ### you may not use this file except in compliance with the License. ### You may obtain a copy of the License at ### ### http://www.apache.org/licenses/LICENSE-2.0 ### ### Unless required by applicable law or agreed to in writing, software ### distributed under the License is distributed on an "AS IS" BASIS, ### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ### See the License for the specific language governing permissions and ### limitations under the License. ###