<commit_before># -*- coding: utf-8 -*-
"""SIP Master Controller (REST)."""
import os

import redis
from flask import request
from flask_api import FlaskAPI, status


APP = FlaskAPI(__name__)
DB = redis.Redis(host=os.getenv('DATABASE_HOST'))


@APP.route('/')
def root():
    """."""
    return {
        "message": "Welcome to the SIP Master Controller",
        "_links": {
            "items": [
                {"href": "{}state".format(request.url)}
            ]
        }
    }


@APP.route('/state', methods=['GET', 'PUT'])
def state():

    """Return the SDP State."""
    states = ['OFF', 'INIT', 'STANDBY', 'ON', 'DISABLE', 'FAULT', 'ALARM',
              'UNKNOWN']

    if request.method == 'PUT':
        requested_state = request.data.get('state', '').upper()
        if requested_state not in states:
            return ({'error': 'Invalid state: {}'.format(requested_state),
                     'allowed_states': states},
                    status.HTTP_400_BAD_REQUEST)
        response = {'message': 'Accepted state: {}'.format(requested_state)}
        try:
            DB.set('state', requested_state)
        except redis.exceptions.ConnectionError:
            response['error'] = 'Unable to connect to database.'
        return response

    try:
        current_state = DB.get('state')
        if current_state is None:
            DB.set('state', 'INIT')
            current_state = 'INIT'
        else:
            current_state = current_state.decode('utf-8')
        return {'state': '{}'.format(current_state)}
    except redis.exceptions.ConnectionError:
        return {'state:': 'UNKNOWN',
                'error:': 'Unable to connect to database.'}
<commit_msg>Fixed a bug in the toy master controller example causing the unit test to fail when the test database was not available.<commit_after># -*- coding: utf-8 -*-
"""SIP Master Controller (REST)."""
import os

import redis
from flask import request
from flask_api import FlaskAPI, status


APP = FlaskAPI(__name__)
DB = redis.Redis(host=os.getenv('DATABASE_HOST'))


@APP.route('/')
def root():
    """."""
    return {
        "message": "Welcome to the SIP Master Controller",
        "_links": {
            "items": [
                {"href": "{}state".format(request.url)}
            ]
        }
    }


@APP.route('/state', methods=['GET', 'PUT'])
def state():

    """Return the SDP State."""
    states = ['OFF', 'INIT', 'STANDBY', 'ON', 'DISABLE', 'FAULT', 'ALARM',
              'UNKNOWN']

    if request.method == 'PUT':
        requested_state = request.data.get('state', '').upper()
        if requested_state not in states:
            return ({'error': 'Invalid state: {}'.format(requested_state),
                     'allowed_states': states},
                    status.HTTP_400_BAD_REQUEST)
        response = {'message': 'Accepted state: {}'.format(requested_state)}
        try:
            DB.set('state', requested_state)
        except redis.exceptions.ConnectionError:
            response['error'] = 'Unable to connect to database.'
        return response

    try:
        current_state = DB.get('state')
        if current_state is None:
            DB.set('state', 'INIT')
            current_state = 'INIT'
        else:
            current_state = current_state.decode('utf-8')
        return {'state': '{}'.format(current_state)}
    except redis.exceptions.ConnectionError:
        return {'state': 'UNKNOWN',
                'error': 'Unable to connect to database.'}

{'fraction_non_alphanumeric': 0.151931330472103, 'fraction_numerical': 0.002861230329041488, 'mean_word_length': 2.332697807435653, 'pattern_counts': {'":': 8, '<': 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': '369649', 'n_tokens_mistral': 1010, 'n_tokens_neox': 981, 'n_words': 265}