<commit_before>"""$Id$"""
import os
import base64
import sha
import re

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import images

class LogoProgram(db.Model):
    code = db.TextProperty()
    img  = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    hash = db.StringProperty()

hash_re = re.compile(r"/([A-Za-z0-9_-]*)(.*)")

class Papert(webapp.RequestHandler):
  def get(self):
    hash, extra = hash_re.match(self.request.path).groups()
    
    values = {'code':""}

    program = None
    
    if hash:
        program = LogoProgram.all().filter('hash = ', hash).get()

    if program and extra == ".png":
        self.response.headers['Content-Type'] = 'image/png'
        self.response.out.write(program.img)
    else:
        if program:
            values['code'] = program.code
            values['hash'] = hash
    
        page = os.path.join(os.path.dirname(__file__), 'index.html.tmpl')
        self.response.out.write(template.render(page, values))

  def post(self):
    code = self.request.get('code',None)
    img = self.request.get('img',"")
    if code.strip():
        hash = base64.b64encode(sha.sha(code.strip()).digest()[:6], "-_")
        if not LogoProgram.all().filter('hash =', hash).get():
            program = LogoProgram()
            program.code = code
            program.hash = hash
            if img:
                img = base64.b64decode(img)
                img = images.Image(img)
                img.resize(width = 125, height = 125)
                program.img = img.execute_transforms(output_encoding=images.PNG)
            program.put()
    else:
        hash = ""
    
    self.redirect("/%s" % hash)
    
application = webapp.WSGIApplication([('/.*', Papert)],debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
<commit_msg>making images cacheable, using memcache on the backend<commit_after>"""$Id$"""
import os
import base64
import sha
import re

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.api import memcache

class LogoProgram(db.Model):
    code = db.TextProperty()
    img  = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    hash = db.StringProperty()

hash_re = re.compile(r"/([A-Za-z0-9_-]*)(.*)")

class Papert(webapp.RequestHandler):
  def get(self):
    hash, extra = hash_re.match(self.request.path).groups()
    
    values = {'code':""}

    program = None
    
    if hash:
        program = memcache.get("program: %s" % hash)
        if program is None:
            program = LogoProgram.all().filter('hash = ', hash).get()
            memcache.set("program: %s" % hash, program, 3600)

    if program and extra == ".png":
        self.response.headers['Content-Type'] = 'image/png'
        self.response.headers['Cache-Control'] = 'public'
        self.response.out.write(program.img)
    else:
        if program:
            values['code'] = program.code
            values['hash'] = hash
    
        page = os.path.join(os.path.dirname(__file__), 'index.html.tmpl')
        self.response.out.write(template.render(page, values))

  def post(self):
    code = self.request.get('code',None)
    img = self.request.get('img',"")
    if code.strip():
        hash = base64.b64encode(sha.sha(code.strip()).digest()[:6], "-_")
        if not LogoProgram.all().filter('hash =', hash).get():
            program = LogoProgram()
            program.code = code
            program.hash = hash
            if img:
                img = base64.b64decode(img)
                img = images.Image(img)
                img.resize(width = 125, height = 125)
                program.img = img.execute_transforms(output_encoding=images.PNG)
            program.put()
    else:
        hash = ""
    
    self.redirect("/%s" % hash)
    
application = webapp.WSGIApplication([('/.*', Papert)],debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

{'fraction_non_alphanumeric': 0.14589169000933708, 'fraction_numerical': 0.00980392156862745, 'mean_word_length': 2.5708333333333333, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '429611', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1341, 'n_words': 274}