From 3acc5678807ea93a2421c9b32c13b04b12540065 Mon Sep 17 00:00:00 2001 From: grdn1337 Date: Wed, 6 Apr 2022 11:32:43 +0200 Subject: [PATCH] removed caching, added some comments. --- pypaperless/model.py | 104 +++++++++++++++++++-------------------- pypaperless/paperless.py | 94 ++++++----------------------------- setup.py | 2 +- 3 files changed, 67 insertions(+), 133 deletions(-) diff --git a/pypaperless/model.py b/pypaperless/model.py index 3553b3e..dade2bd 100644 --- a/pypaperless/model.py +++ b/pypaperless/model.py @@ -5,119 +5,119 @@ class Correspondent: - """Class that represents a Light object in the ExampleHub API.""" + """Class that represents a correspondent object in the paperless API.""" def __init__(self, raw_data: dict, auth: Auth): - """Initialize a light object.""" + """Initialize a correspondent object.""" self.raw_data = raw_data self.auth = auth @property def id(self) -> int: - """Return the count.""" + """Return the id.""" return self.raw_data["id"] @property def slug(self) -> str: - """Return the count.""" + """Return the slug.""" return self.raw_data["slug"] @property def name(self) -> str: - """Return the count.""" + """Return the name.""" return self.raw_data["name"] @property def match(self) -> str: - """Return the count.""" + """Return the match.""" return self.raw_data["match"] @property def matching_algorithm(self) -> int: - """Return the count.""" + """Return the matching_algorithm.""" return self.raw_data["matching_algorithm"] @property def is_insensitive(self) -> bool: - """Return the count.""" + """Return the is_insensitive flag.""" return self.raw_data["is_insensitive"] @property def document_count(self) -> int: - """Return the count.""" + """Return the document_count.""" return self.raw_data["document_count"] @property def last_correspondence(self) -> datetime: - """Return the count.""" + """Return the last_correspondence date.""" return self.raw_data["last_correspondence"] class DocumentType: - """Class that represents a Light object in the ExampleHub API.""" + """Class that represents a document type object in the paperless API.""" def __init__(self, raw_data: dict, auth: Auth): - """Initialize a light object.""" + """Initialize a document type object.""" self.raw_data = raw_data self.auth = auth @property def id(self) -> int: - """Return the count.""" + """Return the id.""" return self.raw_data["id"] @property def slug(self) -> str: - """Return the count.""" + """Return the slug.""" return self.raw_data["slug"] @property def name(self) -> str: - """Return the count.""" + """Return the name.""" return self.raw_data["name"] @property def match(self) -> str: - """Return the count.""" + """Return the match.""" return self.raw_data["match"] @property def matching_algorithm(self) -> int: - """Return the count.""" + """Return the matching_algorithm.""" return self.raw_data["matching_algorithm"] @property def is_insensitive(self) -> bool: - """Return the count.""" + """Return the is_insensitive flag.""" return self.raw_data["is_insensitive"] @property def document_count(self) -> int: - """Return the count.""" + """Return the document_count.""" return self.raw_data["document_count"] class Tag: - """Class that represents a Light object in the ExampleHub API.""" + """Class that represents a tag object in the paperless API.""" def __init__(self, raw_data: dict, auth: Auth): - """Initialize a light object.""" + """Initialize a tag object.""" self.raw_data = raw_data self.auth = auth @property def id(self) -> int: - """Return the count.""" + """Return the id.""" return self.raw_data["id"] @property def slug(self) -> str: - """Return the count.""" + """Return the slug.""" return self.raw_data["slug"] @property def name(self) -> str: - """Return the count.""" + """Return the name.""" return self.raw_data["name"] @property @@ -127,138 +127,138 @@ def colour(self) -> int: @property def match(self) -> str: - """Return the count.""" + """Return the match.""" return self.raw_data["match"] @property def matching_algorithm(self) -> int: - """Return the count.""" + """Return the matching_algorithm.""" return self.raw_data["matching_algorithm"] @property def is_insensitive(self) -> bool: - """Return the count.""" + """Return the is_insensitive flag.""" return self.raw_data["is_insensitive"] @property def is_inbox_tag(self) -> bool: - """Return the count.""" + """Return the is_inbox_tag flag.""" return self.raw_data["is_inbox_tag"] @property def document_count(self) -> int: - """Return the count.""" + """Return the document_count.""" return self.raw_data["document_count"] class SavedView: - """Class that represents a Light object in the ExampleHub API.""" + """Class that represents a saved view object in the paperless API.""" def __init__(self, raw_data: dict, auth: Auth): - """Initialize a light object.""" + """Initialize a saved view object.""" self.raw_data = raw_data self.auth = auth @property def id(self) -> int: - """Return the count.""" + """Return the id.""" return self.raw_data["id"] @property def name(self) -> str: - """Return the count.""" + """Return the name.""" return self.raw_data["name"] @property def show_on_dashboard(self) -> bool: - """Return the count.""" + """Return the show_on_dashboard flag.""" return self.raw_data["show_on_dashboard"] @property def show_in_sidebar(self) -> bool: - """Return the count.""" + """Return the show_in_sidebar flag.""" return self.raw_data["show_in_sidebar"] @property def sort_field(self) -> str: - """Return the count.""" + """Return the sort_field.""" return self.raw_data["sort_field"] @property def sort_reverse(self) -> bool: - """Return the count.""" + """Return the sort_reverse flag.""" return self.raw_data["sort_reverse"] @property def filter_rules(self) -> List[dict]: - """Return the count.""" + """Return the filter_rules dict.""" return self.raw_data["filter_rules"] class Document: - """Class that represents a Light object in the ExampleHub API.""" + """Class that represents a document object in the paperless API.""" def __init__(self, raw_data: dict, auth: Auth): - """Initialize a light object.""" + """Initialize a document object.""" self.raw_data = raw_data self.auth = auth @property def id(self) -> int: - """Return the count.""" + """Return the id.""" return self.raw_data["id"] @property def correspondent(self) -> int: - """Return the count.""" + """Return the correspondent id.""" return self.raw_data["correspondent"] @property def document_type(self) -> int: - """Return the count.""" + """Return the document_type id.""" return self.raw_data["document_type"] @property def title(self) -> str: - """Return the count.""" + """Return the title.""" return self.raw_data["title"] @property def content(self) -> str: - """Return the count.""" + """Return the content.""" return self.raw_data["content"] @property def tags(self) -> List[int]: - """Return the count.""" + """Return the list of mapped tags.""" return self.raw_data["tags"] @property def created(self) -> datetime: - """Return the count.""" + """Return the created date.""" return self.raw_data["created"] @property def modified(self) -> datetime: - """Return the count.""" + """Return the modified date.""" return self.raw_data["modified"] @property def added(self) -> datetime: - """Return the count.""" + """Return the added date.""" return self.raw_data["added"] @property def archive_serial_number(self) -> str: - """Return the count.""" + """Return the archive_serial_number.""" return self.raw_data["archive_serial_number"] @property def original_file_name(self) -> str: - """Return the count.""" + """Return the original_file_name.""" return self.raw_data["original_file_name"] @property def archived_file_name(self) -> str: - """Return the count.""" + """Return the archived_file_name.""" return self.raw_data["archived_file_name"] diff --git a/pypaperless/paperless.py b/pypaperless/paperless.py index 330860f..59a1512 100644 --- a/pypaperless/paperless.py +++ b/pypaperless/paperless.py @@ -7,26 +7,14 @@ class PaperlessAPI: - """Class to communicate with the API.""" + """Class to communicate with the paginated API.""" - def __init__(self, endpoint: str, token: str, sync_delay: int = 300): - """Initialize the API and store the auth so we can make requests.""" + def __init__(self, endpoint: str, token: str): + """Initialize the API and store the auth so we can make token-based requests.""" self.auth = Auth(endpoint, token) - self.sync_delay = sync_delay - - self.__correspontents_cache = None - self.__correspontents_last_sync = None - self.__document_types_cache = None - self.__document_types_last_sync = None - self.__tags_cache = None - self.__tags_last_sync = None - self.__saved_views_cache = None - self.__saved_views_last_sync = None - self.__documents_cache = None - self.__documents_last_sync = None async def __get_paginated_results(self, path: str, isa: object) -> List[object]: - """ REturn bla.""" + """Walk through the pagination and return a list of objects of type .""" json = {} results = [] @@ -51,75 +39,21 @@ async def __get_paginated_results(self, path: str, isa: object) -> List[object]: return results async def get_correspondents(self) -> List[Correspondent]: - """Return the correspondents.""" - try: - delta = (datetime.now() - - self.__correspontents_last_sync).total_seconds() - except: - delta = 0 - - if self.__correspontents_last_sync is None or delta > self.sync_delay: - self.__correspontents_cache = await self.__get_paginated_results( - "correspondents", Correspondent) - self.__correspontents_last_sync = datetime.now() - - return self.__correspontents_cache + """Return all correspondents.""" + return await self.__get_paginated_results("correspondents", Correspondent) async def get_document_types(self) -> List[DocumentType]: - """Return the correspondents.""" - try: - delta = (datetime.now() - - self.__correspontents_last_sync).total_seconds() - except: - delta = 0 - - if self.__document_types_last_sync is None or delta > self.sync_delay: - self.__document_types_cache = await self.__get_paginated_results( - "document_types", DocumentType) - self.__document_types_last_sync = datetime.now() - - return self.__document_types_cache + """Return all document types.""" + return await self.__get_paginated_results("document_types", DocumentType) async def get_tags(self) -> List[Tag]: - """Return the correspondents.""" - try: - delta = (datetime.now() - - self.__tags_last_sync).total_seconds() - except: - delta = 0 - - if self.__tags_last_sync is None or delta > self.sync_delay: - self.__tags_cache = await self.__get_paginated_results("tags", Tag) - self.__tags_last_sync = datetime.now() - - return self.__tags_cache + """Return all tags.""" + return await self.__get_paginated_results("tags", Tag) async def get_saved_views(self) -> List[SavedView]: - """Return the correspondents.""" - try: - delta = (datetime.now() - - self.__saved_views_last_sync).total_seconds() - except: - delta = 0 - - if self.__saved_views_last_sync is None or delta > self.sync_delay: - self.__saved_views_cache = await self.__get_paginated_results( - "saved_views", SavedView) - self.__saved_views_last_sync = datetime.now() - - return self.__saved_views_cache + """Return all saved views.""" + return await self.__get_paginated_results("saved_views", SavedView) async def get_documents(self) -> List[Document]: - """Return the correspondents.""" - try: - delta = (datetime.now() - - self.__documents_last_sync).total_seconds() - except: - delta = 0 - - if self.__documents_last_sync is None or delta > self.sync_delay: - self.__documents_cache = await self.__get_paginated_results( - "documents", Document) - self.__documents_last_sync = datetime.now() - - return self.__documents_cache + """Return all documents.""" + return await self.__get_paginated_results("documents", Document) diff --git a/setup.py b/setup.py index 87496f0..36158d4 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pypaperless", - version="0.0.6", + version="0.0.7", author="tbsch", author_email="info@tbsch.de", description="A small API wrapper for paperless-ngx dms.",