<commit_before>import os
from datetime import timedelta
from time import sleep
import io
from threading import Thread
import sys
import logging
from logging.handlers import RotatingFileHandler

from flask import Flask
from flask.ext.libsass import Sass
from flask.ext.bower import Bower
from flask.ext.script import Manager
import pkg_resources

from garden_lighting.web.auth import Auth, fucked_auth
from garden_lighting.web.devices import DeviceGroup, DefaultDevice

app = Flask(__name__)

manager = Manager(app)

Bower(app)

Sass(
    {'layout': 'static/sass/layout.scss',
     'lights': 'static/sass/lights.scss',
     'control': 'static/sass/control.scss'}, app,
    url_path='/static/sass/',
    include_paths=[
        pkg_resources.resource_filename('garden_lighting.web', 'static/sass'),
    ]
)

auth = None
rules_path = None
log = None

running = True
control = None
scheduler = None
devices = None

def setup_logging(logger, level):
    logger.setLevel(level)

    console = logging.StreamHandler(sys.stderr)
    console.setLevel(level)
    logger.addHandler(console)

    file_handler = RotatingFileHandler("lighting.log")
    file_handler.setLevel(level)
    logger.addHandler(file_handler)

    global log
    log = io.StringIO()
    handler = logging.StreamHandler(log)
    handler.setLevel(level)
    logger.addHandler(handler)


@app.before_request
def before_request():
    if not auth.auth():
        return fucked_auth()


def shutdown(thread):
    app.logger.info('Shutting down!')
    global running
    running = False
    thread.join()
    sys.exit(0)

def run():
    while running:
        try:
            scheduler.run()
            control.run()
        except Exception as e:
            app.logger.exception(e)
        sleep(2)

@manager.option('-p', '--port', help='The port', default=80)
@manager.option('-c', '--config', help='The config', default="config.py")
@manager.option('-t', '--token', help='The token', default="")
@manager.option('-s', '--secret', help='The secret key', default="")
@manager.option('-r', '--rules', help='The rules save file', default="rules.bin")
def runserver(port, config, token, secret, rules):
    setup_logging(app.logger, logging.INFO)

    app.logger.info("Initialising hardware interface")
    global control
    try:
        from garden_lighting.light_control import LightControl
        control = LightControl(timedelta(seconds=3), 1, app.logger)
        control.init()
    except:
        from garden_lighting.light_control_dummy import LightControl
        control = LightControl(timedelta(seconds=3), 1, app.logger)
        control.init()

    global devices
    devices = new_group("root", "root")

    app.logger.warn("Initialising scheduling thread")
    global scheduler
    from garden_lighting.web.scheduler import DeviceScheduler
    scheduler = DeviceScheduler(0.5, devices, control, app.logger)

    app.logger.info("Configuration %s" % {'port': port,
                                          'config': config,
                                          'token': token,
                                          'secret': secret,
                                          'rules': rules})

    # Loading config
    if os.path.isfile(config):
        with open(config) as f:
            code = compile(f.read(), config, 'exec')

            lcl = {}

            exec(code, globals(), lcl)

            port = lcl['port']
            token = lcl['token']
            secret = lcl['secret']

    global auth
    auth = Auth(token, app.logger)

    global rules_path
    rules_path = rules

    scheduler.read(rules, devices)

    app.secret_key = secret

    from garden_lighting.web.api import api

    app.register_blueprint(api)

    from garden_lighting.web.control import control as controls

    app.register_blueprint(controls)

    from garden_lighting.web.lights import lights

    app.register_blueprint(lights)

    app.logger.warn("Starting scheduling thread")
    thread = Thread(target=run)
    thread.start()

    app.run(host='0.0.0.0', port=int(port), debug=False, use_reloader=False)
    shutdown(thread)
<commit_msg>Format log<commit_after>import os
from datetime import timedelta
from time import sleep
import io
from threading import Thread
import sys
import logging
from logging.handlers import RotatingFileHandler

from flask import Flask
from flask.ext.libsass import Sass
from flask.ext.bower import Bower
from flask.ext.script import Manager
import pkg_resources

from garden_lighting.web.auth import Auth, fucked_auth
from garden_lighting.web.devices import DeviceGroup, DefaultDevice

app = Flask(__name__)

manager = Manager(app)

Bower(app)

Sass(
    {'layout': 'static/sass/layout.scss',
     'lights': 'static/sass/lights.scss',
     'control': 'static/sass/control.scss'}, app,
    url_path='/static/sass/',
    include_paths=[
        pkg_resources.resource_filename('garden_lighting.web', 'static/sass'),
    ]
)

auth = None
rules_path = None
log = None

running = True
control = None
scheduler = None
devices = None


def setup_logging(logger, level):
    logger.setLevel(level)
    formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s ' \
                                  '[in %(pathname)s:%(lineno)d]')

    console = logging.StreamHandler(sys.stderr)
    console.setLevel(level)
    console.setFormatter(formatter)
    logger.addHandler(console)

    file_handler = RotatingFileHandler("lighting.log")
    file_handler.setLevel(level)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)


@app.before_request
def before_request():
    if not auth.auth():
        return fucked_auth()


def shutdown(thread):
    app.logger.info('Shutting down!')
    global running
    running = False
    thread.join()
    sys.exit(0)


def run():
    while running:
        try:
            scheduler.run()
            control.run()
        except Exception as e:
            app.logger.exception(e)
        sleep(2)


@manager.option('-p', '--port', help='The port', default=80)
@manager.option('-c', '--config', help='The config', default="config.py")
@manager.option('-t', '--token', help='The token', default="")
@manager.option('-s', '--secret', help='The secret key', default="")
@manager.option('-r', '--rules', help='The rules save file', default="rules.bin")
def runserver(port, config, token, secret, rules):
    setup_logging(app.logger, logging.INFO)

    app.logger.info("Initialising hardware interface")
    global control
    try:
        from garden_lighting.light_control import LightControl

        control = LightControl(timedelta(seconds=3), 1, app.logger)
        control.init()
    except:
        from garden_lighting.light_control_dummy import LightControl

        control = LightControl(timedelta(seconds=3), 1, app.logger)
        control.init()

    global devices
    devices = new_group("root", "root")

    app.logger.info("Initialising scheduling thread")
    global scheduler
    from garden_lighting.web.scheduler import DeviceScheduler

    scheduler = DeviceScheduler(0.5, devices, control, app.logger)

    app.logger.info("Configuration %s" % {'port': port,
                                          'config': config,
                                          'token': token,
                                          'secret': secret,
                                          'rules': rules})

    # Loading config
    if os.path.isfile(config):
        with open(config) as f:
            code = compile(f.read(), config, 'exec')

            lcl = {}

            exec(code, globals(), lcl)

            port = lcl['port']
            token = lcl['token']
            secret = lcl['secret']

    global auth
    auth = Auth(token, app.logger)

    global rules_path
    rules_path = rules

    scheduler.read(rules, devices)

    app.secret_key = secret

    from garden_lighting.web.api import api

    app.register_blueprint(api)

    from garden_lighting.web.control import control as controls

    app.register_blueprint(controls)

    from garden_lighting.web.lights import lights

    app.register_blueprint(lights)

    app.logger.info("Starting scheduling thread")
    thread = Thread(target=run)
    thread.start()

    app.run(host='0.0.0.0', port=int(port), debug=False, use_reloader=False)
    shutdown(thread)

{'fraction_non_alphanumeric': 0.11661457081029093, 'fraction_numerical': 0.003366193796585718, 'mean_word_length': 2.8371771217712176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4120332', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2393, 'n_words': 579}