# Path: wapiti.py
# def get_json(*args, **kwargs):
#     resp = get_url(*args, **kwargs)
#     return json.loads(resp.text)


from base import Input
from wapiti import get_json
from stats import dist_stats
from datetime import datetime, timedelta, date
from math import ceil
from collections import OrderedDict, defaultdict

def partition_reverts(revs):
    reverted = OrderedDict()
    clean = []
    for i, rev in enumerate(revs):
        if rev['rev_id'] in reverted:
            continue
        window = REVERT_WINDOW  # if comment contains 'revert' do something
        if 'revert' in rev['rev_comment'].lower():
            window += SLEUTHING_FACTOR
        wrevs = revs[max(i - window, 0):i]
        for wi, wrev in enumerate(wrevs):
            if wrev['rev_sha1'] == rev['rev_sha1']:
                # found a revert thang
                wreverted = dict([(r['rev_id'], r) for r in wrevs[wi + 1:]])
                reverted.update(wreverted)
                reverted[rev['rev_id']] = rev
                break
        if rev['rev_len'] == 0:
            reverted[rev['rev_id']] = rev

    clean = [r for r in revs if r['rev_id'] not in reverted]
    return reverted.values(), clean


class Revisions(Input):
    prefix = 'rv'

    def fetch(self):
        ret = {}

# Path: wapiti.py
# def get_json(*args, **kwargs):
#     resp = get_url(*args, **kwargs)
#     return json.loads(resp.text)


# Path: inputs/revisions.py
from base import Input
from wapiti import get_json
from stats import dist_stats
from datetime import datetime, timedelta, date
from math import ceil
from collections import OrderedDict, defaultdict


def partition_reverts(revs):
    reverted = OrderedDict()
    clean = []
    for i, rev in enumerate(revs):
        if rev['rev_id'] in reverted:
            continue
        window = REVERT_WINDOW  # if comment contains 'revert' do something
        if 'revert' in rev['rev_comment'].lower():
            window += SLEUTHING_FACTOR
        wrevs = revs[max(i - window, 0):i]
        for wi, wrev in enumerate(wrevs):
            if wrev['rev_sha1'] == rev['rev_sha1']:
                # found a revert thang
                wreverted = dict([(r['rev_id'], r) for r in wrevs[wi + 1:]])
                reverted.update(wreverted)
                reverted[rev['rev_id']] = rev
                break
        if rev['rev_len'] == 0:
            reverted[rev['rev_id']] = rev

    clean = [r for r in revs if r['rev_id'] not in reverted]
    return reverted.values(), clean


class Revisions(Input):
    prefix = 'rv'

    def fetch(self):
        ret = {}

        revs = get_json('http://toolserver.org/~slaporte/rs/all/?title=' + self.page_title.replace(' ', '_'))