<commit_before>import os
import time
import flask
import logging
import dropbox
import time_machine
import functools
import dropbox_flask_session
import forms
import tasks
from main import app
import pytz

timezone = pytz.timezone(app.config.get('TIMEZONE', 'UTC'))

if app.debug:
    try:
        import flask_debugtoolbar
        toolbar = flask_debugtoolbar.DebugToolbarExtension(app)
    except ImportError:
        pass

    handler = logging.StreamHandler()
    handler.setLevel(logging.DEBUG)

    logger = logging.getLogger('')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)

def _to_unicode(line):
    if isinstance(line, str):
        line = unicode(line, 'utf-8', 'replace')
    else:
        line = unicode(line)

    return line

def view_decorator(f):
    @functools.wraps(f)
    def _view_decorator(*args, **kwargs):
        context = dict(dropbox_session=dropbox_flask_session.DropboxSession())
        ret = f(context, *args, **kwargs)

        if not ret or isinstance(ret, dict):
            return flask.render_template('%s.html' % f.__name__, **context)
        else:
            return ret

    return _view_decorator


@app.route('/')
@view_decorator
def index(context):
    dropbox_session = context['dropbox_session']
    if dropbox_session.link() and dropbox_session.is_linked():
        return flask.redirect(flask.url_for('list_dropbox'))
    else:
        return flask.redirect(flask.url_for('authenticate'))


@app.route('/restore/', methods=['GET', 'POST'])
@view_decorator
def restore(context):
    if flask.session.get('request_token'):
        lines = tasks.get_redis_log(dict(flask.session.items()))
        context['log'] = [_to_unicode(l) for l in lines]
    else:
        return flask.redirect(flask.url_for('authenticate'))


@app.route('/dropbox/', methods=['GET', 'POST'])
@app.route('/dropbox/<path:path>', methods=['GET', 'POST'])
@view_decorator
def list_dropbox(context, path=''):
    form = context['form'] = forms.RestoreForm(flask.request.form, path=path)

    try:
        tm = time_machine.TimeMachine(context['dropbox_session'])
        context['account'] = tm.account_info()
    except dropbox.rest.ErrorResponse, exception:
        if exception.status == 401:<commit_msg>dropbox apparently has more error codes, catching 403 as well<commit_after>import os
import time
import flask
import logging
import dropbox
import time_machine
import functools
import dropbox_flask_session
import forms
import tasks
from main import app
import pytz

timezone = pytz.timezone(app.config.get('TIMEZONE', 'UTC'))

if app.debug:
    try:
        import flask_debugtoolbar
        toolbar = flask_debugtoolbar.DebugToolbarExtension(app)
    except ImportError:
        pass

    handler = logging.StreamHandler()
    handler.setLevel(logging.DEBUG)

    logger = logging.getLogger('')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)

def _to_unicode(line):
    if isinstance(line, str):
        line = unicode(line, 'utf-8', 'replace')
    else:
        line = unicode(line)

    return line

def view_decorator(f):
    @functools.wraps(f)
    def _view_decorator(*args, **kwargs):
        context = dict(dropbox_session=dropbox_flask_session.DropboxSession())
        ret = f(context, *args, **kwargs)

        if not ret or isinstance(ret, dict):
            return flask.render_template('%s.html' % f.__name__, **context)
        else:
            return ret

    return _view_decorator


@app.route('/')
@view_decorator
def index(context):
    dropbox_session = context['dropbox_session']
    if dropbox_session.link() and dropbox_session.is_linked():
        return flask.redirect(flask.url_for('list_dropbox'))
    else:
        return flask.redirect(flask.url_for('authenticate'))


@app.route('/restore/', methods=['GET', 'POST'])
@view_decorator
def restore(context):
    if flask.session.get('request_token'):
        lines = tasks.get_redis_log(dict(flask.session.items()))
        context['log'] = [_to_unicode(l) for l in lines]
    else:
        return flask.redirect(flask.url_for('authenticate'))


@app.route('/dropbox/', methods=['GET', 'POST'])
@app.route('/dropbox/<path:path>', methods=['GET', 'POST'])
@view_decorator
def list_dropbox(context, path=''):
    form = context['form'] = forms.RestoreForm(flask.request.form, path=path)

    try:
        tm = time_machine.TimeMachine(context['dropbox_session'])
        context['account'] = tm.account_info()
    except dropbox.rest.ErrorResponse, exception:
        if exception.status in (401, 403):
{'fraction_non_alphanumeric': 0.12860603391323497, 'fraction_numerical': 0.0030830213609337152, 'mean_word_length': 3.756020942408377, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2182918', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1402, 'n_words': 302}