{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp project" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# project\n", "\n", "> A simple API for handling time spent in a project" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| hide\n", "from nbdev.showdoc import *\n", "from fastcore.test import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "from fastcore.utils import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| hide\n", "import nbdev; nbdev.nbdev_export()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "from googleapiclient.discovery import build\n", "from google.oauth2 import service_account" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "class __Project():\n", " \"Project (time) data handler\"\n", " def __init__(self,\n", " scopes: list, # google apis authentication scopes,\n", " fn: str, # service account file name\n", " sid: str): # spreadsheet id\n", " creds = service_account.Credentials.from_service_account_file(fn, scopes=scopes)\n", " service = build('sheets', 'v4', credentials=creds)\n", " self.sheet = service.spreadsheets()\n", " self.sid = sid\n", " def __str__(self): return f\"{self.sid}\"\n", " __repr__=__str__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "SERVICE_ACCOUNT_FILE = '../keys.json'\n", "SCOPES = ['https://www.googleapis.com/auth/spreadsheets']\n", "SPREADSHEET_ID = '1DNoNf4glcuMxKoVzHVrFo-MktmsVji1wf4IHeraWH84'\n", "\n", "class Project(__Project):\n", " def __init__(self,\n", " fn:str=SERVICE_ACCOUNT_FILE): # service account file name\n", " super(Project, self).__init__(SCOPES, fn, SPREADSHEET_ID)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Project() prints spread sheet id, `sid`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pr = Project(SERVICE_ACCOUNT_FILE)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def get(self:Project,\n", " sname:str=\"\"): # a worksheet name can be specified. If not the 1st sheet is choosen.\n", " range = f\"{sname}!\" if sname else \"\"\n", " range += \"A1:Z1000\"\n", " result = self.sheet.values().get(spreadsheetId=self.sid,\n", " range=range).execute()\n", " values = result.get('values', [])\n", " return values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For instance, here's some tests of equality" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['3/1/2022', '4000'],\n", " ['4/4/2022', '3000'],\n", " ['7/12/2022', '7000'],\n", " ['7/12/2022', '9999'],\n", " ['7/12/2022', '9999']]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pr.get(\"Test\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def put(self:Project,\n", " data:list,\n", " sname:str=\"\"): # a worksheet name can be specified. If not the 1st sheet is choosen.\n", " range = f\"{sname}!\" if sname else \"\"\n", " range += \"A1\"\n", " request = self.sheet.values().update(spreadsheetId=self.sid,\n", " range=range,\n", " valueInputOption=\"USER_ENTERED\",\n", " body={\"values\":data})\n", " response = request.execute()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['3/1/2022', '4000'],\n", " ['4/4/2022', '3000'],\n", " ['7/12/2022', '7000'],\n", " ['7/12/2022', '9999'],\n", " ['7/12/2022', '9999']]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pr.get(\"Test\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = [[\"3/1/2022\", \"4000\"],[\"4/4/2022\", \"3000\"],[\"7/12/2022\", \"7000\"]]\n", "pr.put(data, \"Test\")\n", "test_eq(pr.get(\"Test\")[:3], data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def append(self:Project,\n", " data:list,\n", " sname:str=\"\") -> None: # a worksheet name can be specified. If not the 1st sheet is choosen.\n", " range = f\"{sname}!\" if sname else \"\"\n", " range += \"A:E\"\n", " request = self.sheet.values().append(spreadsheetId=self.sid,\n", " range=range,\n", " valueInputOption=\"USER_ENTERED\",\n", " body={\"values\":data})\n", " response = request.execute()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = [\"7/12/2022\", \"9999\"]\n", "pr.append([data], \"Test\")\n", "test_eq(pr.get(\"Test\")[-1], data)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }