<commit_before>from gevent import http, pywsgi
from geventwebsocket.handler import WebSocketHandler
from pb import PhotoBooth
import ConfigParser
import flickr
import json
import mimetypes
import os
import qrcode
import re
import sys
import time

def ensure_path(path):
    if not os.path.exists(path):
        os.makedirs(path)

config = ConfigParser.ConfigParser()
config.read('photobooth.conf')

pwd = os.path.abspath(os.path.dirname(__file__))

strips_path = os.path.join(pwd, 'static', 'photos', 'strips')
ensure_path(strips_path)

photos_path = os.path.join(pwd, 'static', 'photos', 'raw')
ensure_path(photos_path)

templates_path = os.path.join(pwd, 'templates')
templates = {}

# create photo booth

photo_booth = PhotoBooth()
photo_booth.lomo_darkness = config.getfloat('photobooth', 'lomo_darkness')
photo_booth.lomo_saturation = config.getfloat('photobooth', 'lomo_saturation')

# template stuff

def autoload_templates():
    for filename in os.listdir(templates_path):
        load_template(path, cache=True)

def load_template(filename, cache=False):
    path = os.path.join(templates_path, filename)
    template = templates.get(path, None)
    if not template:
        f = open(path)
        template = f.read()
        f.close()
        if cache:
            templates[filename] = template
    return template

# websocket utils

def send(ws, params):
    ws.send(json.dumps(params))

# apps

def websocket_app(environ, start_response):
    
    path = environ["PATH_INFO"].rstrip('/')
    
    if not path:
        
        start_response("200 OK", [('Content-Type', 'text/html')])
        return load_template('base.html')
    
    elif path == '/init':
        
        data = {'photos': []}
        
        for filename in os.listdir(strips_path):
            data['photos'].append(filename)
        
        start_response("200 OK", [('Content-Type', 'application/json')])
        return json.dumps(data)
    
    elif path == '/pb':
        
        ws = environ["wsgi.websocket"]
        #message = ws.wait()
        
        # create new photo set
        photoset = photo_booth.new_set()
        
        send(ws, {'action': 'preset'})
        
        # take each photo
        count = config.getint('photobooth', 'count')
        for i in xrange(count):
            
            # pre-photo message
            send(ws, {
                'action': 'prephoto',
                'index': i,
                'count': count,
            })
            
            # take photo
            photo_booth.take_photo(photoset)
            photo_path = photoset['photos'][-1]
            
            # post-photo message
            send(ws, {
                'action': 'postphoto',
                'index': i,
                'count': count,
                'filename': photo_path.split('/')[-1],
                'localPath': photo_path,
            })
            
        send(ws, {'action': 'postset'})
        
        # combine into photo strip and delete originals
        strip_path = photo_booth.printout(photoset)
        photo_booth.cleanup(photoset)
        
        # upload to Flickr if enabled
        if config.get('photobooth', 'upload') == '1':
            send(ws, {'action': 'processing'})
            flickr_url = flickr.upload(strip_path)
            time.sleep(2)
        else:
            flickr_url = None
        
        # return final response
        ws.send(json.dumps({
            'action': 'strip',
            'photoId': photoset['id'],
            'localPath': strip_path,
            'flickrUrl': flickr_url or '',
            'qrCodeUrl': qrcode.image_url(flickr_url) if flickr_url else '',
        }))
        
        # close websocket connection
        ws.send(json.dumps({'action': 'close'}))
    
    start_response("404 NOT FOUND", [('Content-Type', 'text/plain')])
    return []
    
def static_app(request):

    path = os.path.join(pwd, 'static', request.uri.strip('/'))
<commit_msg>make qrcode work even when uploading is disabled. this needs a better fix<commit_after>from gevent import http, pywsgi
from geventwebsocket.handler import WebSocketHandler
from pb import PhotoBooth
import ConfigParser
import flickr
import json
import mimetypes
import os
import qrcode
import re
import sys
import time

def ensure_path(path):
    if not os.path.exists(path):
        os.makedirs(path)

config = ConfigParser.ConfigParser()
config.read('photobooth.conf')

pwd = os.path.abspath(os.path.dirname(__file__))

strips_path = os.path.join(pwd, 'static', 'photos', 'strips')
ensure_path(strips_path)

photos_path = os.path.join(pwd, 'static', 'photos', 'raw')
ensure_path(photos_path)

templates_path = os.path.join(pwd, 'templates')
templates = {}

# create photo booth

photo_booth = PhotoBooth()
photo_booth.lomo_darkness = config.getfloat('photobooth', 'lomo_darkness')
photo_booth.lomo_saturation = config.getfloat('photobooth', 'lomo_saturation')

# template stuff

def autoload_templates():
    for filename in os.listdir(templates_path):
        load_template(path, cache=True)

def load_template(filename, cache=False):
    path = os.path.join(templates_path, filename)
    template = templates.get(path, None)
    if not template:
        f = open(path)
        template = f.read()
        f.close()
        if cache:
            templates[filename] = template
    return template

# websocket utils

def send(ws, params):
    ws.send(json.dumps(params))

# apps

def websocket_app(environ, start_response):
    
    path = environ["PATH_INFO"].rstrip('/')
    
    if not path:
        
        start_response("200 OK", [('Content-Type', 'text/html')])
        return load_template('base.html')
    
    elif path == '/init':
        
        data = {'photos': []}
        
        for filename in os.listdir(strips_path):
            data['photos'].append(filename)
        
        start_response("200 OK", [('Content-Type', 'application/json')])
        return json.dumps(data)
    
    elif path == '/pb':
        
        ws = environ["wsgi.websocket"]
        #message = ws.wait()
        
        # create new photo set
        photoset = photo_booth.new_set()
        
        send(ws, {'action': 'preset'})
        
        # take each photo
        count = config.getint('photobooth', 'count')
        for i in xrange(count):
            
            # pre-photo message
            send(ws, {
                'action': 'prephoto',
                'index': i,
                'count': count,
            })
            
            # take photo
            photo_booth.take_photo(photoset)
            photo_path = photoset['photos'][-1]
            
            # post-photo message
            send(ws, {
                'action': 'postphoto',
                'index': i,
                'count': count,
                'filename': photo_path.split('/')[-1],
                'localPath': photo_path,
            })
            
        send(ws, {'action': 'postset'})
        
        # combine into photo strip and delete originals
        strip_path = photo_booth.printout(photoset)
        photo_booth.cleanup(photoset)
        
        # upload to Flickr if enabled
        if config.get('photobooth', 'upload') == '1':
            send(ws, {'action': 'processing'})
            flickr_url = flickr.upload(strip_path)
            time.sleep(2)
        else:
            flickr_url = None
        
        # return final response
        ws.send(json.dumps({
            'action': 'strip',
            'photoId': photoset['id'],
            'localPath': strip_path,
            'flickrUrl': flickr_url or '',
            'qrCodeUrl': qrcode.image_url(flickr_url or 'http://sunlightlabs.com/photobooth/'),
        }))
        
        # close websocket connection
        ws.send(json.dumps({'action': 'close'}))
    
    start_response("404 NOT FOUND", [('Content-Type', 'text/plain')])
    return []
    
def static_app(request):

    path = os.path.join(pwd, 'static', request.uri.strip('/'))

{'fraction_non_alphanumeric': 0.12570497556084723, 'fraction_numerical': 0.003258553703471613, 'mean_word_length': 2.135559921414538, 'pattern_counts': {'":': 0, '<': 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': '1616689', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2403, 'n_words': 527}