<commit_before>#!/usr/bin/env python
""" Raspberry Camera Web Server main file.
"""
#------------------------------------------------------------------------------
import web
import config
import json
import multiprocessing
import time

#------------------------------------------------------------------------------
URLS = (
    '/', 'Main',
    '/lastimage.jpg', 'LastImage',
    '/ajax/camera', 'AjaxCamera',
)

WEB_ENV = {'version': config.VERSION, 'camera_name': config.CAMERA_NAME}
TMPLS = web.template.render('templates', globals=WEB_ENV)

web.config.debug = config.WEB_DEBUG


#------------------------------------------------------------------------------
def getInt(string_value, default_value=0):
    try:
        int_value = int(string_value)
        return int_value
    except ValueError:
        return default_value


#------------------------------------------------------------------------------
class Main(object):
    """Class to Handle root urls."""

    def GET(self):
        """http GET response method."""

        return TMPLS.main()


#------------------------------------------------------------------------------
class LastImage(object):
    """Class to handle image queries."""

    def GET(self):
        """http GET response method."""

        web.header("Content-Type", "images/jpeg")
        return open("lastimage.jpg", "rb").read()


#------------------------------------------------------------------------------
class AjaxCamera(object):
    """Class to handle camera ajax queries."""

    def PUT(self):
        """http PUT response method."""

        params = web.input()
        cam_config = web.camera_config

        if 'brightness' in params:
            old_val = cam_config['brightness']
            cam_config['brightness'] = getInt(params['brightness'], old_val)

        if 'contrast' in params:
            old_val = cam_config['contrast']
            cam_config['contrast'] = getInt(params['contrast'], old_val)

        if 'hflip' in params and params.hflip:
            cam_config['hflip'] = not cam_config.get('hflip', False)

        if 'vflip' in params and params.vflip:
            cam_config['vflip'] = not cam_config.get('vflip', False)

        return json.dumps({'ok': True})


#------------------------------------------------------------------------------
def camera_loop(camera_config):
    """Camera update loop."""

    print "start loop"
    try:
        while 1 == 1:
            time.sleep(5)
            print "camera ", camera_config

    except KeyboardInterrupt:
        print "Ending loop"


#------------------------------------------------------------------------------
def main():
    """Main function."""

    manager = multiprocessing.Manager()

    camera_config = manager.dict()
    camera_config['brightness'] = 50
    camera_config['contrast'] = 0
    camera_config['hflip'] = False
    camera_config['vflip'] = False

    app = web.application(URLS, globals())

    web.camera_config = camera_config

    loop = multiprocessing.Process(target=camera_loop, args=(camera_config,))<commit_msg>check picamera import<commit_after>#!/usr/bin/env python
""" Raspberry Camera Web Server main file.
"""
#------------------------------------------------------------------------------
import web
import config
import json
import multiprocessing
import time
import imp

try:
    imp.find_module('eggs')
    found_picamera = True
    import pycamera
except ImportError:
    found_picamera = False

#------------------------------------------------------------------------------
URLS = (
    '/', 'Main',
    '/lastimage.jpg', 'LastImage',
    '/ajax/camera', 'AjaxCamera',
)

WEB_ENV = {'version': config.VERSION, 'camera_name': config.CAMERA_NAME}
TMPLS = web.template.render('templates', globals=WEB_ENV)

web.config.debug = config.WEB_DEBUG


#------------------------------------------------------------------------------
def getInt(string_value, default_value=0):
    try:
        int_value = int(string_value)
        return int_value
    except ValueError:
        return default_value


#------------------------------------------------------------------------------
class Main(object):
    """Class to Handle root urls."""

    def GET(self):
        """http GET response method."""

        return TMPLS.main()


#------------------------------------------------------------------------------
class LastImage(object):
    """Class to handle image queries."""

    def GET(self):
        """http GET response method."""

        web.header("Content-Type", "images/jpeg")
        return open("lastimage.jpg", "rb").read()


#------------------------------------------------------------------------------
class AjaxCamera(object):
    """Class to handle camera ajax queries."""

    def PUT(self):
        """http PUT response method."""

        params = web.input()
        cam_config = web.camera_config

        if 'brightness' in params:
            old_val = cam_config['brightness']
            cam_config['brightness'] = getInt(params['brightness'], old_val)

        if 'contrast' in params:
            old_val = cam_config['contrast']
            cam_config['contrast'] = getInt(params['contrast'], old_val)

        if 'hflip' in params and params.hflip:
            cam_config['hflip'] = not cam_config.get('hflip', False)

        if 'vflip' in params and params.vflip:
            cam_config['vflip'] = not cam_config.get('vflip', False)

        return json.dumps({'ok': True})


#------------------------------------------------------------------------------
def camera_loop(camera_config):
    """Camera update loop."""

    print "start loop"
    try:
        while 1 == 1:
            time.sleep(5)
            print "camera ", camera_config

    except KeyboardInterrupt:
        print "Ending loop"


#------------------------------------------------------------------------------
def main():
    """Main function."""

    if found_picamera:
        print "picamera founded"
    else:
        print "picamera not founded"

    manager = multiprocessing.Manager()

    camera_config = manager.dict()
    camera_config['brightness'] = 50
    camera_config['contrast'] = 0
    camera_config['hflip'] = False
    camera_config['vflip'] = False

    app = web.application(URLS, globals())

    web.camera_config = camera_config

    loop = multiprocessing.Process(target=camera_loop, args=(camera_config,))
{'fraction_non_alphanumeric': 0.30153029356652095, 'fraction_numerical': 0.0021861336664584633, 'mean_word_length': 1.4748840803709429, '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': '290891', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1570, 'n_words': 389}