Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JournalLock: Check for journal_dir of None #913

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions journal_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class JournalLockResult(Enum):
JOURNALDIR_NOTEXIST = 2
JOURNALDIR_READONLY = 3
ALREADY_LOCKED = 4
JOURNALDIR_IS_NONE = 5


class JournalLock:
Expand All @@ -33,18 +34,33 @@ class JournalLock:
def __init__(self) -> None:
"""Initialise where the journal directory and lock file are."""
self.journal_dir: str = config.get('journaldir') or config.default_journal_dir
self.journal_dir_path = pathlib.Path(self.journal_dir)
self.journal_dir_path: Optional[pathlib.Path] = None
self.set_path_from_journaldir()
self.journal_dir_lockfile_name: Optional[pathlib.Path] = None
# We never test truthiness of this, so let it be defined when first assigned. Avoids type hint issues.
# self.journal_dir_lockfile: Optional[IO] = None
self.locked = False

def set_path_from_journaldir(self):
if self.journal_dir is None:
self.journal_dir_path = None

else:
try:
self.journal_dir_path = pathlib.Path(self.journal_dir)

except Exception:
logger.exception("Couldn't make pathlib.Path from journal_dir")

def obtain_lock(self) -> JournalLockResult:
"""
Attempt to obtain a lock on the journal directory.

:return: LockResult - See the class Enum definition
"""
if self.journal_dir_path is None:
return JournalLockResult.JOURNALDIR_IS_NONE

self.journal_dir_lockfile_name = self.journal_dir_path / 'edmc-journal-lock.txt'
logger.trace(f'journal_dir_lockfile_name = {self.journal_dir_lockfile_name!r}')
try:
Expand Down Expand Up @@ -100,7 +116,7 @@ def release_lock(self) -> bool:
"""
Release lock on journal directory.

:return: bool - Success of unlocking operation.
:return: bool - Whether we're now unlocked.
"""
if not self.locked:
return True # We weren't locked, and still aren't
Expand Down Expand Up @@ -222,8 +238,9 @@ def update_lock(self, parent: tk.Tk) -> None:
self.release_lock()

self.journal_dir = current_journaldir
self.journal_dir_path = pathlib.Path(self.journal_dir)
if not self.obtain_lock():
self.set_path_from_journaldir()

if self.obtain_lock() == JournalLockResult.ALREADY_LOCKED:
# Pop-up message asking for Retry or Ignore
self.retry_popup = self.JournalAlreadyLocked(parent, self.retry_lock)

Expand All @@ -241,7 +258,7 @@ def retry_lock(self, retry: bool, parent: tk.Tk) -> None:

current_journaldir = config.get('journaldir') or config.default_journal_dir
self.journal_dir = current_journaldir
self.journal_dir_path = pathlib.Path(self.journal_dir)
if not self.obtain_lock():
self.set_path_from_journaldir()
if self.obtain_lock() == JournalLockResult.ALREADY_LOCKED:
# Pop-up message asking for Retry or Ignore
self.retry_popup = self.JournalAlreadyLocked(parent, self.retry_lock)