<commit_before># -*- coding: utf-8 -*-

from flask import Flask, abort, request, render_template, Response
import dateutil.parser
from datetime import datetime
import json
import os

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)  #FIXME: only used for localhost
import settings

app = Flask(__name__)
app.config.from_object(__name__) #FIXME: use app.config for configuration

if settings.USE_MONGODB:
    #connect to mongodb
    import pymongo
    connection = pymongo.Connection(settings.MONGODB_HOST, settings.MONGODB_PORT)
    db = connection.schiffts

def read_from_file(raw=False):
    #read last saved data, if it fails set default values
    try:
        f = open(settings.SERVER_DATA_FILE, 'r')
        weather_data = f.read()
        if not raw:
            weather_data = json.loads(weather_data)

    except Exception, e:
        weather_data = {'last_rain_intensity':None,
                        'last_rain':None,
                        'last_dry':None,
                        'rain_since':None,
                        'dry_since':None,
                        'last_update_rain':False,
                        }

        if raw:
            weather_data = json.dumps(weather_data)

    return weather_data

def check_password(form):
    try:
        secret = form['secret']
        #verify password and check if data is present
        if(secret == settings.SECRET and not form['data'] == None):
            return True
    except Exception, e:
        pass

    return False

@app.route('/')
def index():
    """
    Display the webpage
    """

    last_dry = settings.DUNNO_MESSAGE
    last_rain = settings.DUNNO_MESSAGE
    rain_since = settings.DUNNO_MESSAGE
    dry_since = settings.DUNNO_MESSAGE
    rain_message = settings.SERVER_NO_RAIN_MESSAGE
    since_message = settings.SERVER_DRY_SINCE_MESSAGE
    last_update = None
    snow = False
    body_classes = ""

    dt = None

    #read old data from file
    try:
        f = open(settings.SERVER_DATA_FILE, 'r')
        weather_data = json.loads(f.read())

        if weather_data.has_key('snow') and weather_data['snow']:
            snow = weather_data['snow']

        if weather_data['last_update_rain']:
            if weather_data.has_key('last_rain') and weather_data['last_rain']:
                last_update =  dateutil.parser.parse(weather_data['last_rain'])
                dt = datetime.now() - last_update
                rain = True
                if snow:
                    rain_message = settings.SERVER_SNOW_MESSAGE
                    since_message = settings.SERVER_SNOW_SINCE_MESSAGE
                else:
                    rain_message = settings.SERVER_RAIN_MESSAGE
                    since_message = settings.SERVER_RAIN_SINCE_MESSAGE

        else: 
            if weather_data.has_key('last_dry') and weather_data['last_dry']:
                last_update =  dateutil.parser.parse(weather_data['last_dry'])
                dt = datetime.now() - last_update
                rain = False 

        #format datetime for display
        if weather_data['last_dry']:
            last_dry = dateutil.parser.parse(weather_data['last_dry']).strftime(settings.DISPLAY_DATE_FORMAT)

        if weather_data['last_rain']:
            last_rain = dateutil.parser.parse(weather_data['last_rain']).strftime(settings.DISPLAY_DATE_FORMAT)

        if weather_data.has_key('rain_since') and weather_data['rain_since']:
            rain_since = dateutil.parser.parse(weather_data['rain_since']).strftime(settings.DISPLAY_DATE_FORMAT)          

        if weather_data.has_key('dry_since') and weather_data['dry_since']:
            dry_since = dateutil.parser.parse(weather_data['dry_since']).strftime(settings.DISPLAY_DATE_FORMAT)

    except Exception, e:
        rain = False

    #get latest weather data
    try:
        latest_sample = db.weather_samples.find().sort('time', pymongo.DESCENDING)[0]

        #only display weather if the latest value is not older than 1h
        time_diff = datetime.utcnow() - latest_sample['time']
        time_diff_minutes = time_diff.days * 1440 + time_diff.seconds #py 2.6 :-/ use time_diff.total_seconds() in 2.7
        if time_diff_minutes < 60*60:
            #replace strings in weather attributes and concate them
            for attribute in latest_sample['weather']:
                attribute = attribute.replace(" ", "-")
                body_classes += "%s "%attribute

        body_classes = body_classes.strip()

    except Exception, e:
        body_classes = "no-weather-data"

    if body_classes == "":
        body_classes = "no-weather-data"

    if rain:
        body_classes += " rain"
    else:
        body_classes += " no-rain"

    if snow:
        body_classes += " snow"

    return render_template('index.html', rain_message=rain_message, since_message=since_message, last_rain=last_rain, last_dry=last_dry, dry_since=dry_since, \
                            rain_since=rain_since, last_update=last_update, rain=rain, snow=snow, body_classes=body_classes)


@app.route(settings.RAIN_UPDATE_PATH, methods=['POST'])
def update_rain():
    """<commit_msg>add test parameter for weather animation<commit_after># -*- coding: utf-8 -*-

from flask import Flask, abort, request, render_template, Response
import dateutil.parser
from datetime import datetime
import json
import os

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)  #FIXME: only used for localhost
import settings

app = Flask(__name__)
app.config.from_object(__name__) #FIXME: use app.config for configuration

if settings.USE_MONGODB:
    #connect to mongodb
    import pymongo
    connection = pymongo.Connection(settings.MONGODB_HOST, settings.MONGODB_PORT)
    db = connection.schiffts

def read_from_file(raw=False):
    #read last saved data, if it fails set default values
    try:
        f = open(settings.SERVER_DATA_FILE, 'r')
        weather_data = f.read()
        if not raw:
            weather_data = json.loads(weather_data)

    except Exception, e:
        weather_data = {'last_rain_intensity':None,
                        'last_rain':None,
                        'last_dry':None,
                        'rain_since':None,
                        'dry_since':None,
                        'last_update_rain':False,
                        }

        if raw:
            weather_data = json.dumps(weather_data)

    return weather_data

def check_password(form):
    try:
        secret = form['secret']
        #verify password and check if data is present
        if(secret == settings.SECRET and not form['data'] == None):
            return True
    except Exception, e:
        pass

    return False

@app.route('/', methods=['GET'])
def index():
    """
    Display the webpage
    """

    last_dry = settings.DUNNO_MESSAGE
    last_rain = settings.DUNNO_MESSAGE
    rain_since = settings.DUNNO_MESSAGE
    dry_since = settings.DUNNO_MESSAGE
    rain_message = settings.SERVER_NO_RAIN_MESSAGE
    since_message = settings.SERVER_DRY_SINCE_MESSAGE
    last_update = None
    snow = False
    body_classes = ""

    dt = None

    #read old data from file
    try:
        f = open(settings.SERVER_DATA_FILE, 'r')
        weather_data = json.loads(f.read())

        if weather_data.has_key('snow') and weather_data['snow']:
            snow = weather_data['snow']

        if weather_data['last_update_rain']:
            if weather_data.has_key('last_rain') and weather_data['last_rain']:
                last_update =  dateutil.parser.parse(weather_data['last_rain'])
                dt = datetime.now() - last_update
                rain = True
                if snow:
                    rain_message = settings.SERVER_SNOW_MESSAGE
                    since_message = settings.SERVER_SNOW_SINCE_MESSAGE
                else:
                    rain_message = settings.SERVER_RAIN_MESSAGE
                    since_message = settings.SERVER_RAIN_SINCE_MESSAGE

        else: 
            if weather_data.has_key('last_dry') and weather_data['last_dry']:
                last_update =  dateutil.parser.parse(weather_data['last_dry'])
                dt = datetime.now() - last_update
                rain = False 

        #format datetime for display
        if weather_data['last_dry']:
            last_dry = dateutil.parser.parse(weather_data['last_dry']).strftime(settings.DISPLAY_DATE_FORMAT)

        if weather_data['last_rain']:
            last_rain = dateutil.parser.parse(weather_data['last_rain']).strftime(settings.DISPLAY_DATE_FORMAT)

        if weather_data.has_key('rain_since') and weather_data['rain_since']:
            rain_since = dateutil.parser.parse(weather_data['rain_since']).strftime(settings.DISPLAY_DATE_FORMAT)          

        if weather_data.has_key('dry_since') and weather_data['dry_since']:
            dry_since = dateutil.parser.parse(weather_data['dry_since']).strftime(settings.DISPLAY_DATE_FORMAT)

    except Exception, e:
        rain = False

    #if GET parameter is present, overwrite weather variables
    weather = request.args.get('weather', '')

    if weather == 'rain':
        rain = True
        snow = False
    elif weather == 'snow':
        rain = True
        snow = True

    #get latest weather data
    try:
        latest_sample = db.weather_samples.find().sort('time', pymongo.DESCENDING)[0]

        #only display weather if the latest value is not older than 1h
        time_diff = datetime.utcnow() - latest_sample['time']
        time_diff_minutes = time_diff.days * 1440 + time_diff.seconds #py 2.6 :-/ use time_diff.total_seconds() in 2.7
        if time_diff_minutes < 60*60:
            #replace strings in weather attributes and concate them
            for attribute in latest_sample['weather']:
                attribute = attribute.replace(" ", "-")
                body_classes += "%s "%attribute

        body_classes = body_classes.strip()

    except Exception, e:
        body_classes = "no-weather-data"

    #add additional classes to the body
    if body_classes == "":
        body_classes = "no-weather-data"

    if rain:
        body_classes += " rain"
    else:
        body_classes += " no-rain"

    if snow:
        body_classes += " snow"

    return render_template('index.html', rain_message=rain_message, since_message=since_message, last_rain=last_rain, last_dry=last_dry, dry_since=dry_since, \
                            rain_since=rain_since, last_update=last_update, rain=rain, snow=snow, body_classes=body_classes)


@app.route(settings.RAIN_UPDATE_PATH, methods=['POST'])
def update_rain():
    """
{'fraction_non_alphanumeric': 0.09736669478024552, 'fraction_numerical': 0.0029987817449161278, 'mean_word_length': 2.488721804511278, 'pattern_counts': {'":': 2, '<': 5, '<?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': '3661040', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3071, 'n_words': 666}