Skip to content

Commit

Permalink
Refactored Office 365 Plugin (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc authored Dec 7, 2024
1 parent e9020e6 commit 4d21759
Show file tree
Hide file tree
Showing 5 changed files with 1,085 additions and 211 deletions.
56 changes: 40 additions & 16 deletions apprise/attachment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,26 @@ def exists(self, retrieve_if_missing=True):
cache = self.template_args['cache']['default'] \
if self.cache is None else self.cache

if self.download_path and os.path.isfile(self.download_path) \
and cache:
try:
if self.download_path and os.path.isfile(self.download_path) \
and cache:

# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True
# We have enough reason to look further into our cached content
# and verify it has not expired.
if cache is True:
# return our fixed content as is; we will always cache it
return True

# Verify our cache time to determine whether we will get our
# content again.
try:
age_in_sec = time.time() - os.stat(self.download_path).st_mtime
# Verify our cache time to determine whether we will get our
# content again.
age_in_sec = \
time.time() - os.stat(self.download_path).st_mtime
if age_in_sec <= cache:
return True

except (OSError, IOError):
# The file is not present
pass
except (OSError, IOError):
# The file is not present
pass

return False if not retrieve_if_missing else self.download()

Expand Down Expand Up @@ -359,12 +360,27 @@ def download(self):

def open(self, mode='rb'):
"""
return our file pointer and track it (we'll auto close later
return our file pointer and track it (we'll auto close later)
"""
pointer = open(self.path, mode=mode)
self.__pointers.add(pointer)
return pointer

def chunk(self, size=5242880):
"""
A Generator that yield chunks of a file with the specified size.
By default the chunk size is set to 5MB (5242880 bytes)
"""

with self.open() as file:
while True:
chunk = file.read(size)
if not chunk:
break

yield chunk

def __enter__(self):
"""
support with keyword
Expand Down Expand Up @@ -431,7 +447,15 @@ def __len__(self):
Returns the filesize of the attachment.
"""
return os.path.getsize(self.path) if self.path else 0
if not self:
return 0

try:
return os.path.getsize(self.path) if self.path else 0

except OSError:
# OSError can occur if the file is inaccessible
return 0

def __bool__(self):
"""
Expand Down
6 changes: 5 additions & 1 deletion apprise/attachment/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def download(self, **kwargs):
# Ensure any existing content set has been invalidated
self.invalidate()

if not os.path.isfile(self.dirty_path):
try:
if not os.path.isfile(self.dirty_path):
return False

except OSError:
return False

if self.max_file_size > 0 and \
Expand Down
Loading

0 comments on commit 4d21759

Please sign in to comment.