Spaces:
Running
Running
Download modules/google/docs_gateway.py from sky-meilin/SkyOSFullCore: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/spaces/sky-meilin/SkyOSFullCore/resolve/main/modules/google/docs_gateway.py
- Command line
-
hf download hf://spaces/sky-meilin/SkyOSFullCore/modules/google/docs_gateway.py
-
curl -L -o docs_gateway.py https://huggingface.co/spaces/sky-meilin/SkyOSFullCore/resolve/main/modules/google/docs_gateway.py
3.49 kB
| """ | |
| docs_gateway.py | |
| SkyOS Google Docs Gateway | |
| Google Docs Schnittstelle für SkyOS. | |
| Autor: Sky Meilin | |
| Projekt: SkyOS Full Core | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| from Module.google.auth import docs | |
| logger = logging.getLogger("SkyOS.GoogleDocs") | |
| class DocsGateway: | |
| def __init__(self): | |
| self.service = docs() | |
| # -------------------------------------------------- | |
| # Dokument erstellen | |
| # -------------------------------------------------- | |
| def create_document(self, title: str): | |
| """Erstellt ein neues Google-Dokument.""" | |
| document = ( | |
| self.service.documents() | |
| .create( | |
| body={ | |
| "title": title | |
| } | |
| ) | |
| .execute() | |
| ) | |
| logger.info("Dokument erstellt: %s", title) | |
| return document | |
| # -------------------------------------------------- | |
| # Dokument abrufen | |
| # -------------------------------------------------- | |
| def get_document(self, document_id: str): | |
| """Lädt ein Google-Dokument.""" | |
| return ( | |
| self.service.documents() | |
| .get( | |
| documentId=document_id | |
| ) | |
| .execute() | |
| ) | |
| # -------------------------------------------------- | |
| # Text anhängen | |
| # -------------------------------------------------- | |
| def append_text( | |
| self, | |
| document_id: str, | |
| text: str, | |
| index: int = 1, | |
| ): | |
| """ | |
| Fügt Text in das Dokument ein. | |
| Standardmäßig am Dokumentanfang. | |
| """ | |
| requests = [ | |
| { | |
| "insertText": { | |
| "location": { | |
| "index": index | |
| }, | |
| "text": text, | |
| } | |
| } | |
| ] | |
| return ( | |
| self.service.documents() | |
| .batchUpdate( | |
| documentId=document_id, | |
| body={ | |
| "requests": requests | |
| }, | |
| ) | |
| .execute() | |
| ) | |
| # -------------------------------------------------- | |
| # Dokument ersetzen | |
| # -------------------------------------------------- | |
| def replace_text( | |
| self, | |
| document_id: str, | |
| search_text: str, | |
| replace_text: str, | |
| ): | |
| """Ersetzt Text im gesamten Dokument.""" | |
| requests = [ | |
| { | |
| "replaceAllText": { | |
| "containsText": { | |
| "text": search_text, | |
| "matchCase": True, | |
| }, | |
| "replaceText": replace_text, | |
| } | |
| } | |
| ] | |
| return ( | |
| self.service.documents() | |
| .batchUpdate( | |
| documentId=document_id, | |
| body={ | |
| "requests": requests | |
| }, | |
| ) | |
| .execute() | |
| ) | |
| # -------------------------------------------------- | |
| # Titel ändern | |
| # -------------------------------------------------- | |
| def rename_document( | |
| self, | |
| document_id: str, | |
| new_title: str, | |
| ): | |
| """ | |
| Titel ändern. | |
| Benötigt zusätzlich drive_gateway.py, | |
| da der Dateiname über Google Drive verwaltet wird. | |
| """ | |
| raise NotImplementedError( | |
| "rename_document erfolgt über drive_gateway.py" | |
| ) | |
| docs_gateway = DocsGateway() |