<commit_before>from kivy.app import App
from kivy.base import EventLoop
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.storage.jsonstore import JsonStore

import requests

class Model(object):
    def __init__(self, name):
        self.name = name
        self.presenters = []

    # return data for id here
    def get(self, id):
        raise Exception("not implemented")

    # set data for id here
    def _set(self, id, data):
        raise Exception("not implemented")

    def set(self, id, data):
        self._set(id, data)
        for p in self.presenters:
            p.modelEvent(self, id)

# Transient Dict Model.
class DictModel(Model):
    def __init__(self, name):
        super(DictModel, self).__init__(name)
        self.data = {}

    def get(self, id):
        if id in self.data.keys():
            return self.data[id]
        else:
            return None

    def _set(self, id, data):
        self.data[id] = data

# Persistent JSON Model.
class JsonModel(Model):
    def __init__(self, name, path_prefix='storage/'):
        super(JsonModel, self).__init__(name)
        self.store = JsonStore(path_prefix + name + '.json')

    def get(self, id):
        if self.store.exists(id):
            return self.store[id]
        return None

    def _set(self, id, data):
        if not data:
            self.store.delete(id)
        else:
            self.store[id] = data

# Transient Rest HTTP Model.
class RestModel(DictModel):
    # request can be an OAuthRequest specified to your needs or a plain HTTP request.
    def __init__(self, name, request):
        super(RestModel, self).__init__(name)
        self.r = request

    # We cache data locally.
    def get(self, id, url):
        data = super(RestModel, self).get(id)
        if data is None:
            data = self._get(id, url)
            self.set(id, data)
        return data

    def _get(self, id, url):
        resp = self.r.get(url + str(id))
        if resp.status_code != requests.codes.ok:
            return None
        return resp.json()

    def post(self, url, data):
        resp = self.r.post(url, data=data)
        if resp.status_code != requests.codes.ok:
            return {}
        return resp.json()

    def put(self, id, url, data):
        if data:
            self.r.put(url + str(id), data=data)

    def delete(self, id, url):
        self.r.delete(url + str(id))


class View(Screen):
    def __init__(self, presenter, **kwargs):
        super(View, self).__init__(**kwargs)
        self.presenter = presenter

    # update view based on new data here
    def _update(self, data):
        pass

    def update(self, data):
        self._update(data)
        self.canvas.ask_update()

    def event(self, e):
        self.presenter.userEvent(e)

# A View is just a small wrapper around kivy screens; no need for lots of functionality here.


class Runnable(object):
    # hook for kivy's on_pause
    def onPause(self):
        return True

    # hook for kivy's on_resume
    def onResume(self):
        pass
<commit_msg>restmodel changed to work with oauth using kivy.network.urlrequest instead of requests<commit_after>from kivy.app import App
from kivy.base import EventLoop
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.storage.jsonstore import JsonStore


class Model(object):
    def __init__(self, name):
        self.name = name
        self.presenters = []

    # return data for id here
    def get(self, id):
        raise Exception("not implemented")

    # set data for id here
    def _set(self, id, data):
        raise Exception("not implemented")

    def set(self, id, data):
        self._set(id, data)
        for p in self.presenters:
            p.modelEvent(self, ("set", data))

# Transient Dict Model.
class DictModel(Model):
    def __init__(self, name):
        super(DictModel, self).__init__(name)
        self.data = {}

    def get(self, id):
        if id in self.data.keys():
            return self.data[id]
        else:
            return None

    def _set(self, id, data):
        self.data[id] = data

# Persistent JSON Model.
class JsonModel(Model):
    def __init__(self, name, path_prefix='storage/'):
        super(JsonModel, self).__init__(name)
        self.store = JsonStore(path_prefix + name + '.json')

    def get(self, id):
        if self.store.exists(id):
            return self.store[id]
        return None

    def _set(self, id, data):
        if not data:
            self.store.delete(id)
        else:
            self.store[id] = data

# Transient Rest HTTP Model.
class RestModel(DictModel):
    # request can be an OAuthRequest specified to your needs or a plain HTTP request.
    def __init__(self, name, request, id_key="id"):
        super(RestModel, self).__init__(name)
        self.r = request
        self.id_key = id_key
        
    def get(self, id, url, on_success=None, **kwargs):
        on_success = on_success or self._get
        data = super(RestModel, self).get(id)
        if data:
            for p in self.presenters:
                p.modelEvent(self, ("get", data))
        else:
            self.r.request(url + str(id), method="GET", on_success=on_success, **kwargs)
    
    def _get(self, req, data):
        self.set(self.id_key, data)
        for p in self.presenters:
            p.modelEvent(self, ("get", data))
 
    def post(self, url, data, **kwargs):
        self.r.request(url, req_body=data, method="POST", **kwargs)

    def put(self, id, url, data, **kwargs):
        if data:
            self.r.request(url + str(id), req_body=data, method="PUT", **kwargs)

    def delete(self, id, url, **kwargs):
        self.r.request(url + str(id), method="DELETE", **kwargs)


class View(Screen):
    def __init__(self, presenter, **kwargs):
        super(View, self).__init__(**kwargs)
        self.presenter = presenter

    # update view based on new data here
    def _update(self, data):
        pass

    def update(self, data):
        self._update(data)
        self.canvas.ask_update()

    def event(self, e):
        self.presenter.userEvent(e)

# A View is just a small wrapper around kivy screens; no need for lots of functionality here.


class Runnable(object):
    # hook for kivy's on_pause
    def onPause(self):
        return True

    # hook for kivy's on_resume
    def onResume(self):
        pass

{'fraction_non_alphanumeric': 0.11226089683286297, 'fraction_numerical': 0.0, 'mean_word_length': 2.3068947641264903, 'pattern_counts': {'":': 0, '<': 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': '4467790', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1955, 'n_words': 585}