<commit_before>#!/usr/bin/env python
import BaseHTTPServer
import SimpleHTTPServer
import SocketServer
from cStringIO import StringIO
import PIL.Image
import threading
import evdev

from uinput_map import js_map

SERVER_PORT = 8080

send_key_lock = threading.Lock()
def send_key(js_keycode, state):
	if js_keycode not in js_map:
		raise KeyError("Unkown javascript keycode: {}".format(js_keycode))
	send_key_lock.acquire()
	with evdev.UInput() as uinput:
		uinput.write(evdev.ecodes.EV_KEY, js_map[js_keycode], 1 if state else 0)
		uinput.syn()
	send_key_lock.release()

class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
	"""Handle requests in a separate thread."""
	daemon_threads = True

# https://github.com/ev3dev/ev3dev/wiki/Using-the-LCD
fbmap = {chr(i): ''.join(chr(int(x)) for x in '{:08b}'.format(i)[::-1]) for i in range(256)}
def jpg_from_fb():
	with open('/dev/fb0', 'r') as f:
		x = ''.join(fbmap[char] for char in f.read())
	sio = StringIO()
	im = PIL.Image.fromstring('P', (192,128), x)
	im.putpalette((124,145,111, 0,0,0))
	im.convert('RGB').crop((0,0,178,128)).save(sio, 'JPEG')
	jpg = sio.getvalue()
	sio.close()
	return jpg

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
	def do_GET(self):
		"""Serve a GET request."""
		if self.path.endswith('mjpg'):
			self.send_response(200)
			self.send_header('Content-Type', 'multipart/x-mixed-replace;boundary=boundarydonotcross')
			self.end_headers()
			i = 0<commit_msg>Made jpg_from_fb leverage PIL's built in least-sig-bit mode<commit_after>#!/usr/bin/env python
import BaseHTTPServer
import SimpleHTTPServer
import SocketServer
from cStringIO import StringIO
import PIL.Image
import threading
import evdev

from uinput_map import js_map

SERVER_PORT = 8080

send_key_lock = threading.Lock()
def send_key(js_keycode, state):
	if js_keycode not in js_map:
		raise KeyError("Unkown javascript keycode: {}".format(js_keycode))
	send_key_lock.acquire()
	with evdev.UInput() as uinput:
		uinput.write(evdev.ecodes.EV_KEY, js_map[js_keycode], 1 if state else 0)
		uinput.syn()
	send_key_lock.release()

class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
	"""Handle requests in a separate thread."""
	daemon_threads = True

# https://github.com/ev3dev/ev3dev/wiki/Using-the-LCD
fb_palette = (124,145,111)+(0,0,0)*255
def jpg_from_fb():
	sio = StringIO()
	with open('/dev/fb0', 'r') as f:
		x = f.read()
	im = PIL.Image.fromstring('1', (192,128), x, 'raw', '1;R')
	im = im.convert('P')
	im.putpalette(fb_palette)
	im.convert('RGB').crop((0,0,178,128)).save(sio, 'JPEG')
	jpg = sio.getvalue()
	sio.close()
	return jpg

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
	def do_GET(self):
		"""Serve a GET request."""
		if self.path.endswith('mjpg'):
			self.send_response(200)
			self.send_header('Content-Type', 'multipart/x-mixed-replace;boundary=boundarydonotcross')
			self.end_headers()
			i = 0
{'fraction_non_alphanumeric': 0.145769622833843, 'fraction_numerical': 0.030241250424736663, 'mean_word_length': 5.556792873051225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5657115', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1120, 'n_words': 236}