storage.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Blockly Demo: Storage
  2. Copyright 2012 Google Inc.
  3. https://developers.google.com/blockly/
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. """Store and retrieve XML with App Engine.
  15. """
  16. __author__ = "q.neutron@gmail.com (Quynh Neutron)"
  17. import cgi
  18. from random import randint
  19. from google.appengine.ext import db
  20. from google.appengine.api import memcache
  21. import logging
  22. print "Content-Type: text/plain\n"
  23. def keyGen():
  24. # Generate a random string of length KEY_LEN.
  25. KEY_LEN = 6
  26. CHARS = "abcdefghijkmnopqrstuvwxyz23456789" # Exclude l, 0, 1.
  27. max_index = len(CHARS) - 1
  28. return "".join([CHARS[randint(0, max_index)] for x in range(KEY_LEN)])
  29. class Xml(db.Model):
  30. # A row in the database.
  31. xml_hash = db.IntegerProperty()
  32. xml_content = db.TextProperty()
  33. forms = cgi.FieldStorage()
  34. if "xml" in forms:
  35. # Store XML and return a generated key.
  36. xml_content = forms["xml"].value
  37. xml_hash = hash(xml_content)
  38. lookup_query = db.Query(Xml)
  39. lookup_query.filter("xml_hash =", xml_hash)
  40. lookup_result = lookup_query.get()
  41. if lookup_result:
  42. xml_key = lookup_result.key().name()
  43. else:
  44. trials = 0
  45. result = True
  46. while result:
  47. trials += 1
  48. if trials == 100:
  49. raise Exception("Sorry, the generator failed to get a key for you.")
  50. xml_key = keyGen()
  51. result = db.get(db.Key.from_path("Xml", xml_key))
  52. xml = db.Text(xml_content, encoding="utf_8")
  53. row = Xml(key_name = xml_key, xml_hash = xml_hash, xml_content = xml)
  54. row.put()
  55. print xml_key
  56. if "key" in forms:
  57. # Retrieve stored XML based on the provided key.
  58. key_provided = forms["key"].value
  59. # Normalize the string.
  60. key_provided = key_provided.lower().strip()
  61. # Check memcache for a quick match.
  62. xml = memcache.get("XML_" + key_provided)
  63. if xml is None:
  64. # Check datastore for a definitive match.
  65. result = db.get(db.Key.from_path("Xml", key_provided))
  66. if not result:
  67. xml = ""
  68. else:
  69. xml = result.xml_content
  70. # Save to memcache for next hit.
  71. if not memcache.add("XML_" + key_provided, xml, 3600):
  72. logging.error("Memcache set failed.")
  73. print xml.encode("utf-8")