<commit_before>from flask import Flask, request, make_response, render_template
from jinja2 import escape
import requests, picompute, time, os, socket, uuid, datetime, yaml, random, json, threading, subprocess
import datetime as dt
from netifaces import interfaces, ifaddresses

app = Flask(__name__)

flask_threaded_default = "1"
flask_debug_default    = "0"
flask_port_default     = "5000"

# they are all set in the first main loop
flask_threaded  = None
flask_debug     = None
flask_port      = None
loadummy_next   = None
loadummy_name   = None

MIME_HTML = 'text/html'
MIME_YAML = 'application/x-yaml'
MIME_JSON = 'application/json'


def _get_env_vars(var_start=''):
    render = unicode("")
    for key in sorted(os.environ.keys()):
        if not key.startswith(var_start): continue
        val = os.environ.get(key)
        addme = escape(("%s = %s"%(key,val)).decode('utf-8'))
        # the "str()" is necessary, otherwise this is (probably) some
        # convenience object which auto-escapes everything you add to it ...
        render += unicode(addme) + "<br>"
    return render


def format_answer(req, obj, mimetype=None):
    def format_text(obj):
        return \
            "<pre>" + \
            str(escape(yaml.safe_dump(obj, default_flow_style=False))) + \
            "</pre>"
    def format_yaml(obj):
        return yaml.dump(obj, default_flow_style=False)
    if not mimetype:
        accept = request.headers.get('Accept', "")
        mimetype = MIME_HTML
        if accept.find('yaml') != -1: mimetype = MIME_YAML
        if accept.find('json') != -1: mimetype = MIME_JSON
        app.logger.debug("Determined MIME type: {}".format(mimetype))
    else:
        app.logger.debug("Enforced MIME type: {}".format(mimetype))
    handlers = {
        MIME_YAML : format_yaml,
        MIME_HTML : format_text,
        MIME_JSON : json.dumps,
    }
    rsp_data = handlers[mimetype](obj)
    app.logger.debug("Returned data:\n{}".format(rsp_data))
    rsp = make_response(handlers[mimetype](obj))
    rsp.headers['Content-type'] = mimetype
    return rsp


@app.route('/')
def hello_world():
    rv = {}
    rv['hostname']             = socket.gethostname()
    rv['random_uuid']          = uuid_str
    rv['timestamp']            = datetime.datetime.now()
    rv['set_flask_threaded']   = flask_threaded
    rv['set_loadummy_name']    = loadummy_name<commit_msg>add text/plain output format<commit_after>from flask import Flask, request, make_response, render_template
from jinja2 import escape
import requests, picompute, time, os, socket, uuid, datetime, yaml, random, json, threading, subprocess
import datetime as dt
from netifaces import interfaces, ifaddresses

app = Flask(__name__)

flask_threaded_default = "1"
flask_debug_default    = "0"
flask_port_default     = "5000"

# they are all set in the first main loop
flask_threaded  = None
flask_debug     = None
flask_port      = None
loadummy_next   = None
loadummy_name   = None

MIME_TEXT = 'text/plain'
MIME_HTML = 'text/html'
MIME_YAML = 'application/x-yaml'
MIME_JSON = 'application/json'


def _get_env_vars(var_start=''):
    render = unicode("")
    for key in sorted(os.environ.keys()):
        if not key.startswith(var_start): continue
        val = os.environ.get(key)
        addme = escape(("%s = %s"%(key,val)).decode('utf-8'))
        # the "str()" is necessary, otherwise this is (probably) some
        # convenience object which auto-escapes everything you add to it ...
        render += unicode(addme) + "<br>"
    return render


def format_answer(req, obj, mimetype=None):
    def format_text(obj):
        return yaml.safe_dump(obj, default_flow_style=False)
    def format_html(obj):
        return \
            "<pre>" + \
            str(escape(yaml.safe_dump(obj, default_flow_style=False))) + \
            "</pre>"
    def format_yaml(obj):
        return yaml.dump(obj, default_flow_style=False)
    if not mimetype:
        accept = request.headers.get('Accept', "")
        mimetype = MIME_TEXT
        if accept == '*/*': mimetype = MIME_TEXT
        elif accept.find('yaml') != -1: mimetype = MIME_YAML
        elif accept.find('json') != -1: mimetype = MIME_JSON
        app.logger.debug("Determined MIME type: {}".format(mimetype))
    else:
        app.logger.debug("Enforced MIME type: {}".format(mimetype))
    handlers = {
        MIME_TEXT : format_text,
        MIME_YAML : format_yaml,
        MIME_HTML : format_html,
        MIME_JSON : json.dumps,
    }
    rsp_data = handlers[mimetype](obj)
    app.logger.debug("Returned data:\n{}".format(rsp_data))
    rsp = make_response(handlers[mimetype](obj))
    rsp.headers['Content-type'] = mimetype
    return rsp


@app.route('/')
def hello_world():
    rv = {}
    rv['hostname']             = socket.gethostname()
    rv['random_uuid']          = uuid_str
    rv['timestamp']            = datetime.datetime.now()
    rv['set_flask_threaded']   = flask_threaded
    rv['set_loadummy_name']    = loadummy_name
{'fraction_non_alphanumeric': 0.12587692924433755, 'fraction_numerical': 0.004008819402685909, 'mean_word_length': 3.024193548387097, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2070949', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1626, 'n_words': 373}