-
Notifications
You must be signed in to change notification settings - Fork 10
Change MerginProject constructor to force one instance per project #283
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,9 @@ | |
| import os | ||
| import re | ||
| import shutil | ||
| import uuid | ||
| import tempfile | ||
| import uuid | ||
| import weakref | ||
| from datetime import datetime | ||
| from dateutil.tz import tzlocal | ||
|
|
||
|
|
@@ -41,7 +42,30 @@ class MerginProject: | |
| Linked to existing local directory, with project metadata (mergin.json) and backups located in .mergin directory. | ||
| """ | ||
|
|
||
| # To make sure we don't have multiple instances for a single project that | ||
| # then have out-of-date information, we keep this map of absolute project | ||
| # directory paths to instances. | ||
| # The dictionary is a WeakValueDictionary so we don't cause memory leaks | ||
| # (when the instance is GC'd, the entry is deleted). | ||
| project_cache: weakref.WeakValueDictionary[str, "MerginProject"] = weakref.WeakValueDictionary() | ||
|
|
||
| def __new__(cls, directory): | ||
| directory = os.path.abspath(directory) | ||
| if instance := cls.project_cache.get(directory): | ||
| return instance | ||
|
|
||
| instance = super().__new__(cls) | ||
| cls.project_cache[directory] = instance | ||
|
||
| return instance | ||
|
|
||
| def __init__(self, directory): | ||
| # __init__ still gets called after __new__, even if it returns a | ||
| # pre-existing object. Work around this by checking whether we've been | ||
| # initialised. | ||
| if hasattr(self, "_initialised"): | ||
MarcelGeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| self._initialised = True | ||
|
|
||
| self.dir = os.path.abspath(directory) | ||
| if not os.path.exists(self.dir): | ||
| raise InvalidProject("Project directory does not exist") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.