From 297459a0671843a64ed4a61d20a72e1a1555c367 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 20 Oct 2019 19:17:01 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ Kfet/__init__.py | 0 Kfet/exceptions.py | 10 +++++ Kfet/sync.py | 100 +++++++++++++++++++++++++++++++++++++++++++++ setup.py | 21 ++++++++++ 5 files changed, 134 insertions(+) create mode 100644 Kfet/__init__.py create mode 100644 Kfet/exceptions.py create mode 100644 Kfet/sync.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 894a44c..e1165fd 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,9 @@ wheels/ *.egg MANIFEST +# Kfet db +db.json + # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. diff --git a/Kfet/__init__.py b/Kfet/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Kfet/exceptions.py b/Kfet/exceptions.py new file mode 100644 index 0000000..a48aa19 --- /dev/null +++ b/Kfet/exceptions.py @@ -0,0 +1,10 @@ +class InvalidUrl(Exception): + def __init__(self, expression, message): + self.expression = expression + self.message = message + + +class InvalidLogin(Exception): + def __init__(self, expression, message): + self.expression = expression + self.message = message diff --git a/Kfet/sync.py b/Kfet/sync.py new file mode 100644 index 0000000..c27209d --- /dev/null +++ b/Kfet/sync.py @@ -0,0 +1,100 @@ +import json +import requests +from Kfet.exceptions import InvalidUrl, InvalidLogin +from os.path import isfile +from datetime import datetime +from dateutil.parser import parse + + +class Sync: + def __init__(self, url, login): + self.url = url + self.login = login + self.token = None + self.auth() + + if not isfile("db.json"): + with open("db.json", "w") as db: + json.dump({"users": [], "commands": []}, db) + + def auth(self): + if self.token: + try: + if requests.get(self.url+"/users", json=self.token).status_code == 401: + raise ValueError + except ValueError: + pass + except requests.exceptions.ConnectionError: + raise ConnectionError + else: + return self.token + + token = None + try: + token = requests.post(self.url+"/auth", json=self.login).json()["access_token"] + except requests.exceptions.MissingSchema: + raise InvalidUrl + except (json.JSONDecodeError, KeyError): + raise InvalidLogin + except requests.exceptions.ConnectionError: + raise ConnectionError + self.token = {"Authorization": f"jwt {token}"} + return self.token + + def get_data(self): + try: + users = requests.get(self.url+"/users", headers=self.auth()).json() + commands = requests.get(self.url+"/commands", headers=self.auth()).json() + except (json.JSONDecodeError, requests.exceptions.ConnectionError): + raise ConnectionError + return {"users": users, "commands": commands} + + def pull_data(self): + remote = self.get_data() + with open("db.json", "r") as db: + local = json.load(db) + + for key in remote: + for rdata in remote[key]: + found = False + for i, ldata in enumerate(local[key]): + if rdata["id"] == ldata["id"]: + found = True + if parse(rdata["last_update"]) > parse(ldata["last_update"]): + local[key][i] = rdata + break + if not found: + local[key].append(rdata) + + with open("db.json", "w") as db: + json.dump(local, db) + + def push_data(self): + remote = self.get_data() + with open("db.json", "r") as db: + local = json.load(db) + + for key in remote: + for ldata in local[key]: + found = False + for rdata in remote[key]: + if ldata["id"] == rdata["id"]: + found = True + if parse(ldata["last_update"]) > parse(rdata["last_update"]): + requests.put(self.url+"/"+key+"/"+str(ldata["id"]), headers=self.auth(), json=ldata) + break + if not found: + requests.post(self.url+"/"+key, headers=self.auth(), json=ldata) + self.pull_data() + + +if __name__ == "__main__": + s = Sync("http://127.0.0.1:5000", {"id": 1, "password": "test"}) + s.pull_data() + with open("db.json", "r") as db: + local = json.load(db) + local["users"][0]["email"] = "ethanell@flifloo.fr" + local["users"][0]["last_update"] = str(datetime.now()) + with open("db.json", "w") as db: + json.dump(local, db) + s.push_data() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a13c99b --- /dev/null +++ b/setup.py @@ -0,0 +1,21 @@ +from setuptools import setup, find_packages + + +def readme(): + with open('README.md') as f: + return f.read() + + +setup(name="Kfet", + version="1.0.0", + description="", + long_description=readme(), + url="https://github.com/CharTero/Kfet", + author="flifloo", + author_email="flifloo@gmail.com", + license="MIT", + packages=find_packages(), + install_requires=[ + "", 'requests' + ], + zip_safe=False) \ No newline at end of file