<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing, subprocess, sys, threading, traceback
import time, os, re, signal, socket, sqlite3
import dbus, dbus.service
import cherrypy
from select import select
from settings import Settings
import event
import resource

# used to get a pretty name from signal numbers
_signal_lut = dict((k, v) for v, k in signal.__dict__.iteritems() if v.startswith('SIG') and not v.startswith('SIG_'))

_states = {
	1<<0: 'STOPPED',
	1<<1: 'STOPPING',
	1<<2: 'STARTING',
	1<<3: 'RUNNING',
	1<<4: 'CRASHED'
}

# the reverse of k,v is deliberate
for v,k in _states.items():
	sys.modules[__name__].__dict__[k] = v

html_escape_table = {
	"&": "&amp;",
	'"': "&quot;",
	"'": "&apos;",
	">": "&gt;",
	"<": "&lt;",
	" ": "&nbsp;",
}

def html_escape(text):
	return "".join(html_escape_table.get(c,c) for c in text)

def statename(state):
	return _states[state]

class StateError(Exception):
	pass

class StartError(Exception):
	def __init__(self, message, stdout):
		Exception.__init__(self, message)
		self.stdout = stdout
		
	def __str__(self):
		stdout = ''.join(self.stdout)
		return self.message + '\n\n' + stdout

def settings(resolution=None, fullscreen=True):
	settings = Settings()
	
	cmd = settings['Files.BinaryPath']
	args = [
		'--uds-log', 'slideshow.sock', 
		'--browser', 'sqlite://%s' % (os.path.abspath('site.db')),
		'--collection-id', str(settings['Runtime.queue']),
	]
	
	args.append('--resolution')
	if resolution:
		print 'using custom resolution'
		args.append(str(resolution))
	else:
		args.append(str(settings.resolution()))
	
	if isinstance(fullscreen, basestring):
		fullscreen = fullscreen in ['1', 'True', 'true']
	
	if fullscreen:
		args.append('--fullscreen')
	
	env = dict(
		DISPLAY=settings['Appearance.Display'],
		SLIDESHOW_NO_ABORT='',
		SDL_VIDEO_X11_XRANDR='0',
		HOME=os.environ['HOME']
	)
	for k,v in settings['Env'].items():
		env['SLIDESHOW_' + k] = v
	
	cwd = settings['Path.BasePath']
	
	return (cmd, args, env, cwd)

class _Log:
	severity_lut = {
		'!!': 0,
		'WW': 1,
		'  ': 2,
		'--': 3,
		'DD': 4
	}
	severity_revlut = dict([(v,k) for k,v in severity_lut.items()])
	
	def __init__(self, size=5):
		self.size = size
		
	def push(self, line):
		try:
			c = cherrypy.thread_data.db.cursor()
			match = re.match('\((..)\) \[(.*)\] (.*)', line)
			
			severity = 2
			stamp = time.mktime(time.localtime())
			message = line
			
			if match:
				severity, stampstr, message = match.groups()
				stamp = time.mktime(time.strptime(stampstr, '%Y-%m-%d %H:%M:%S'))
			
			c.execute("""
				INSERT INTO log (
					type,
					severity,
					stamp,
					message
				) VALUES (
					0,
					:severity,
					:stamp,
					:message
				)
			""", dict(severity=self.severity_lut.get(severity, 2), stamp=stamp, message=html_escape(message)))
			cherrypy.thread_data.db.commit()
		except:
			traceback.print_exc()
	
	def __iter__(self):
		c = cherrypy.thread_data.db.cursor()
		lines = c.execute("""
			SELECT
				log.severity as severity,
				user.name as user,
				log.stamp as stamp,
				log.message as message
			FROM
				log,
				user
			WHERE
				log.user_id = user.id
			ORDER BY
				log.stamp DESC,
				log.id DESC
			LIMIT :limit;
		""", dict(limit=self.size)).fetchall()
		lines.reverse()
		
		def f(severity, user, stamp, message):
			severity_str = self.severity_revlut[severity].replace(' ', '&nbsp;')
			formated_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(stamp))
			return '({severity}) {stamp} {user} {message}'.format(severity=severity_str, user=user, stamp=formated_time, message=message.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"))
		
		lines = [f(**x) for x in lines]
		
		return lines.__iter__()

class _Daemon(threading.Thread):
	def __init__(self):<commit_msg>should escape with everything else<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing, subprocess, sys, threading, traceback
import time, os, re, signal, socket, sqlite3
import dbus, dbus.service
import cherrypy
from select import select
from settings import Settings
import event
import resource

# used to get a pretty name from signal numbers
_signal_lut = dict((k, v) for v, k in signal.__dict__.iteritems() if v.startswith('SIG') and not v.startswith('SIG_'))

_states = {
	1<<0: 'STOPPED',
	1<<1: 'STOPPING',
	1<<2: 'STARTING',
	1<<3: 'RUNNING',
	1<<4: 'CRASHED'
}

# the reverse of k,v is deliberate
for v,k in _states.items():
	sys.modules[__name__].__dict__[k] = v

html_escape_table = {
	"&": "&amp;",
	'"': "&quot;",
	"'": "&apos;",
	">": "&gt;",
	"<": "&lt;",
	" ": "&nbsp;",
	"\t": "&nbsp;&nbsp;&nbsp;&nbsp;",
}

def html_escape(text):
	return "".join(html_escape_table.get(c,c) for c in text)

def statename(state):
	return _states[state]

class StateError(Exception):
	pass

class StartError(Exception):
	def __init__(self, message, stdout):
		Exception.__init__(self, message)
		self.stdout = stdout
		
	def __str__(self):
		stdout = ''.join(self.stdout)
		return self.message + '\n\n' + stdout

def settings(resolution=None, fullscreen=True):
	settings = Settings()
	
	cmd = settings['Files.BinaryPath']
	args = [
		'--uds-log', 'slideshow.sock', 
		'--browser', 'sqlite://%s' % (os.path.abspath('site.db')),
		'--collection-id', str(settings['Runtime.queue']),
	]
	
	args.append('--resolution')
	if resolution:
		print 'using custom resolution'
		args.append(str(resolution))
	else:
		args.append(str(settings.resolution()))
	
	if isinstance(fullscreen, basestring):
		fullscreen = fullscreen in ['1', 'True', 'true']
	
	if fullscreen:
		args.append('--fullscreen')
	
	env = dict(
		DISPLAY=settings['Appearance.Display'],
		SLIDESHOW_NO_ABORT='',
		SDL_VIDEO_X11_XRANDR='0',
		HOME=os.environ['HOME']
	)
	for k,v in settings['Env'].items():
		env['SLIDESHOW_' + k] = v
	
	cwd = settings['Path.BasePath']
	
	return (cmd, args, env, cwd)

class _Log:
	severity_lut = {
		'!!': 0,
		'WW': 1,
		'  ': 2,
		'--': 3,
		'DD': 4
	}
	severity_revlut = dict([(v,k) for k,v in severity_lut.items()])
	
	def __init__(self, size=5):
		self.size = size
		
	def push(self, line):
		try:
			c = cherrypy.thread_data.db.cursor()
			match = re.match('\((..)\) \[(.*)\] (.*)', line)
			
			severity = 2
			stamp = time.mktime(time.localtime())
			message = line
			
			if match:
				severity, stampstr, message = match.groups()
				stamp = time.mktime(time.strptime(stampstr, '%Y-%m-%d %H:%M:%S'))
			
			c.execute("""
				INSERT INTO log (
					type,
					severity,
					stamp,
					message
				) VALUES (
					0,
					:severity,
					:stamp,
					:message
				)
			""", dict(severity=self.severity_lut.get(severity, 2), stamp=stamp, message=html_escape(message)))
			cherrypy.thread_data.db.commit()
		except:
			traceback.print_exc()
	
	def __iter__(self):
		c = cherrypy.thread_data.db.cursor()
		lines = c.execute("""
			SELECT
				log.severity as severity,
				user.name as user,
				log.stamp as stamp,
				log.message as message
			FROM
				log,
				user
			WHERE
				log.user_id = user.id
			ORDER BY
				log.stamp DESC,
				log.id DESC
			LIMIT :limit;
		""", dict(limit=self.size)).fetchall()
		lines.reverse()
		
		def f(severity, user, stamp, message):
			severity_str = self.severity_revlut[severity].replace(' ', '&nbsp;')
			formated_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(stamp))
			return '({severity}) {stamp} {user} {message}'.format(severity=severity_str, user=user, stamp=formated_time, message=message)
		
		lines = [f(**x) for x in lines]
		
		return lines.__iter__()

class _Daemon(threading.Thread):
	def __init__(self):
{'fraction_non_alphanumeric': 0.17709158089205596, 'fraction_numerical': 0.006598046978094484, 'mean_word_length': 3.736875, 'pattern_counts': {'":': 11, '<': 25, '<?xml version=': 0, '>': 5, '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': '3263629', 'n_tokens_mistral': 3300, 'n_tokens_neox': 2835, 'n_words': 624}