"""Blockly Demo: Storage

Copyright 2012 Google Inc.
http://blockly.googlecode.com/

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.
"""

"""Store and retrieve XML with App Engine.
"""

__author__ = "q.neutron@gmail.com (Quynh Neutron)"

import cgi
from random import randint
from google.appengine.ext import db
from google.appengine.api import memcache
import logging

print "Content-Type: text/plain\n"

def keyGen():
  # Generate a random string of len KEY_LEN.
  KEY_LEN = 6
  CHARS = "abcdefghijkmnopqrstuvwxyz23456789" # Exclude l, 0, 1.
  max_index = len(CHARS) - 1
  return "".join([CHARS[randint(0, max_index)] for x in range(KEY_LEN)])

class Xml(db.Model):
  # A row in the database.
  xml_key = db.StringProperty()
  xml_hash = db.IntegerProperty()
  xml_content = db.TextProperty()

forms = cgi.FieldStorage()
if "xml" in forms:
  # Store XML and return a generated key.
  xml_content = forms["xml"].value
  xml_hash = hash(xml_content)
  hash_lookup = db.GqlQuery("SELECT * FROM Xml WHERE xml_hash = %d" % xml_hash)
  xml_keys = [row.xml_key for row in hash_lookup]
  if xml_keys: xml_key = xml_keys[0]
  else:
    xml_key = ""
    prev_keys = set([row.xml_key for row in db.GqlQuery(
        "SELECT xml_key FROM Xml")])
    trials = 0
    while xml_key == "" or xml_key in prev_keys:
      trials += 1
      if trials == 100:
        raise Exception("Sorry, the generator failed to get a key for you.")
      xml_key = keyGen()
    xml = db.Text(xml_content, encoding="utf_8")
    row = Xml(xml_key = xml_key, xml_hash = xml_hash, xml_content = xml)
    row.put()
  print xml_key

if "key" in forms:
  # Retrieve stored XML based on the provided key.
  key_provided = forms["key"].value
  xml = memcache.get(key_provided)
  if not xml:
    query_result = db.GqlQuery("SELECT * FROM Xml WHERE xml_key = '%s'" %
                               key_provided)
    xml_content = [row.xml_content for row in query_result]
    if not xml_content:
      xml = ""
    else:
      xml = xml_content[0]
      if not memcache.add(key_provided, xml, 3600):
        logging.error("Memcache set failed.")
  print xml.encode("utf-8") 
  stats = memcache.get_stats()
  logging.info("Memcache Statistics: " + str(stats))
  
@@ -716,16 +716,26 @@
 ort cgi%0A
+import re%0A
 from ran
@@ -1429,27 +1429,28 @@
 tent)%0A  
-hash_
 lookup
+_query
  = db.Gq
@@ -1514,274 +1514,152 @@
 )%0A  
-xml_keys = %5Brow.xml_key for row in hash_lookup%5D%0A  if xml_keys: xml_key = xml_keys%5B0%5D%0A  else:%0A    xml_key = %22%22%0A    prev_keys = set(%5Brow.xml_key for row in db.GqlQuery(%0A        %22SELECT xml_key FROM Xml%22)%5D)%0A    trials = 0%0A    while xml_key == %22%22 or xml_key in prev_keys
+lookup_result = lookup_query.get()%0A  if lookup_result: xml_key = lookup_result.xml_key%0A  else:%0A    trials = 0%0A    result = True%0A    while result
 :%0A  
@@ -1800,16 +1800,121 @@
 eyGen()%0A
+      query = db.GqlQuery(%22SELECT * FROM Xml WHERE xml_key = '%25s'%22 %25 xml_key)%0A      result = query.get()%0A
     xml 
@@ -2217,28 +2217,92 @@
 ml:%0A    
-query_result
+if not re.match('%5E%5Cw+$', key_provided):%0A      xml = %22%22%0A    else:%0A      query
  = db.Gq
@@ -2382,16 +2382,18 @@
         
+  
 key_prov
@@ -2406,64 +2406,33 @@
     
-xml_content = %5Brow.xml_content for row in query_result%5D%0A
+  result = query.get()%0A  
     
@@ -2438,26 +2438,21 @@
  if not 
-xml_conten
+resul
 t:%0A     
@@ -2444,32 +2444,34 @@
 t result:%0A      
+ 
+ 
 xml = %22%22%0A    els
@@ -2463,30 +2463,34 @@
 ml = %22%22%0A    
+  
 else:%0A
+  
       xml = 
@@ -2489,16 +2489,23 @@
   xml = 
+result.
 xml_cont
@@ -2511,12 +2511,11 @@
 tent
-%5B0%5D%0A
+%0A  
     
@@ -2562,16 +2562,18 @@
  3600):%0A
+  
         
@@ -2637,17 +2637,16 @@
 %22utf-8%22)
- 
 %0A  stats

Clean up queries in storage.py and heed http://xkcd.com/327/.