From bf72ac2c33516a951b7b156c10c4e753c781bfc2 Mon Sep 17 00:00:00 2001 From: Jason Gunthorpe Date: Wed, 5 Feb 2020 11:37:54 -0400 Subject: Lift Mailbox.cfg out of the sub classes Make sure it is always set Signed-off-by: Jason Gunthorpe --- cloud_mdir_sync/config.py | 8 ++++---- cloud_mdir_sync/gmail.py | 9 ++++----- cloud_mdir_sync/mailbox.py | 6 +++++- cloud_mdir_sync/maildir.py | 9 ++++----- cloud_mdir_sync/main.py | 2 +- cloud_mdir_sync/office365.py | 8 ++++---- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/cloud_mdir_sync/config.py b/cloud_mdir_sync/config.py index 85351d5..be64f57 100644 --- a/cloud_mdir_sync/config.py +++ b/cloud_mdir_sync/config.py @@ -89,8 +89,8 @@ class Config(object): """Create a cloud mailbox for Office365. Mailbox is the name of O365 mailbox to use, account should be the result of Office365_Account""" from .office365 import O365Mailbox - self.cloud_mboxes.append(O365Mailbox(mailbox, user=account[0], - tenant=account[1])) + self.cloud_mboxes.append( + O365Mailbox(self, mailbox, user=account[0], tenant=account[1])) return self.cloud_mboxes[-1] def GMail_Account(self, user): @@ -103,13 +103,13 @@ class Config(object): """Create a cloud mailbox for Office365. Mailbox is the name of O365 mailbox to use, account should be the result of Office365_Account""" from .gmail import GMailMailbox - self.cloud_mboxes.append(GMailMailbox(label, user=account[0])) + self.cloud_mboxes.append(GMailMailbox(self, label, user=account[0])) return self.cloud_mboxes[-1] def MailDir(self, directory): """Create a local maildir to hold messages""" from .maildir import MailDirMailbox - self.local_mboxes.append(MailDirMailbox(directory)) + self.local_mboxes.append(MailDirMailbox(self, directory)) return self.local_mboxes[-1] def _direct_message(self, msg): diff --git a/cloud_mdir_sync/gmail.py b/cloud_mdir_sync/gmail.py index 1975ab1..8492223 100644 --- a/cloud_mdir_sync/gmail.py +++ b/cloud_mdir_sync/gmail.py @@ -344,21 +344,20 @@ class GMailMailbox(mailbox.Mailbox): | messages.Message.FLAG_FLAGGED | messages.Message.FLAG_DELETED) timer = None - cfg: config.Config gmail: GmailAPI gmail_messages: Dict[str, GMailMessage] history_delta = None delete_action = "archive" # or delete - def __init__(self, label: str, user: str): - super().__init__() + def __init__(self, cfg: config.Config, label: str, user: str): + super().__init__(cfg) self.label_name = label self.user = user self.gmail_messages = {} - async def setup_mbox(self, cfg: config.Config): + async def setup_mbox(self): """Setup access to the authenticated API domain for this endpoint""" - self.cfg = cfg + cfg = self.cfg did = f"gmail-{self.user}" self.name = f"{self.user}:{self.label_name}" gmail = cfg.domains.get(did) diff --git a/cloud_mdir_sync/mailbox.py b/cloud_mdir_sync/mailbox.py index 24c64cd..10b0b64 100644 --- a/cloud_mdir_sync/mailbox.py +++ b/cloud_mdir_sync/mailbox.py @@ -42,9 +42,13 @@ class Mailbox(object): messages: "CHMsgDict_Type" = {} changed_event = asyncio.Event() need_update = True + cfg: "config.Config" + + def __init__(self, cfg: "config.Config"): + self.cfg = cfg @abstractmethod - async def setup_mbox(self, cfg: "config.Config") -> None: + async def setup_mbox(self) -> None: pass @abstractmethod diff --git a/cloud_mdir_sync/maildir.py b/cloud_mdir_sync/maildir.py index 7f298fb..ffb9d77 100644 --- a/cloud_mdir_sync/maildir.py +++ b/cloud_mdir_sync/maildir.py @@ -24,15 +24,14 @@ class MailDirMailbox(mailbox.Mailbox): | messages.Message.FLAG_DELETED) cfg: config.Config - def __init__(self, directory): - super().__init__() + def __init__(self, cfg: config.Config, directory: str): + super().__init__(cfg) self.dfn = os.path.expanduser(directory) for sub in ["tmp", "cur", "new"]: os.makedirs(os.path.join(self.dfn, sub), mode=0o700, exist_ok=True) - async def setup_mbox(self, cfg: config.Config): - self.cfg = cfg - cfg.watch_manager.add_watch( + async def setup_mbox(self): + self.cfg.watch_manager.add_watch( path=[ os.path.join(self.dfn, "cur"), os.path.join(self.dfn, "new") diff --git a/cloud_mdir_sync/main.py b/cloud_mdir_sync/main.py index 816cf20..ab39676 100644 --- a/cloud_mdir_sync/main.py +++ b/cloud_mdir_sync/main.py @@ -59,7 +59,7 @@ async def synchronize_mail(cfg: config.Config): try: await cfg.web_app.go() - await asyncio.gather(*(mbox.setup_mbox(cfg) + await asyncio.gather(*(mbox.setup_mbox() for mbox in cfg.all_mboxes())) msgs = None diff --git a/cloud_mdir_sync/office365.py b/cloud_mdir_sync/office365.py index a13c7bb..7b6e7c8 100644 --- a/cloud_mdir_sync/office365.py +++ b/cloud_mdir_sync/office365.py @@ -362,21 +362,21 @@ class O365Mailbox(mailbox.Mailbox): loop: asyncio.AbstractEventLoop timer = None use_owa_subscribe = True - cfg: config.Config graph: GraphAPI def __init__(self, + cfg: config.Config, mailbox: str, user: Optional[str] = None, tenant="common"): - super().__init__() + super().__init__(cfg) self.mailbox = mailbox self.tenant = tenant self.user = user - async def setup_mbox(self, cfg: config.Config): + async def setup_mbox(self): """Setup access to the authenticated API domain for this endpoint""" - self.cfg = cfg + cfg = self.cfg self.loop = cfg.loop did = f"o365-{self.user}-{self.tenant}" graph = cfg.domains.get(did) -- cgit v1.2.3