from shareplum import Site, Office365
from shareplum.site import Version

import json
import os


ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = "\\".join([ROOT_DIR, "config.json"])

# read config file
with open(config_path) as config_file:
    config = json.load(config_file)
    config = config["share_point"]

USERNAME = config["user"]
PASSWORD = config["password"]
SHAREPOINT_URL = config["url"]
SHAREPOINT_SITE = config["site"]
SHAREPOINT_DOC = config["doc_library"]


class SharePoint:
    def auth(self):
        self.authcookie = Office365(SHAREPOINT_URL, username=USERNAME, password=PASSWORD).GetCookies()
        self.site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=self.authcookie)

        return self.site

    def connect_folder(self, folder_name):
        self.auth_site = self.auth()

        self.sharepoint_dir = "/".join([SHAREPOINT_DOC, folder_name])
        self.folder = self.auth_site.Folder(self.sharepoint_dir)

        return self.folder

    def upload_file(self, file, file_name, folder_name):
        folder = self.connect_folder(folder_name)

        with open(file, mode="rb") as file_obj:
            file_content = file_obj.read()

        folder.upload_file(file_content, file_name)

    def delete_file(self, file_name, folder_name):
        folder = self.connect_folder(folder_name)
        folder.delete_file(file_name)


if __name__ == "__main__":
    sp = SharePoint()
    sp.auth()
    sp.upload_file("tox.ini", "tox.ini", "")