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

[ref] Use singleton when creating context objects #3193

Merged
merged 1 commit into from
Mar 3, 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
3 changes: 2 additions & 1 deletion analyzer/codechecker_analyzer/analyzer_context.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import sys

from codechecker_common import logger
from codechecker_common.singleton import Singleton
from codechecker_common.util import load_json_or_empty

from . import env
@@ -247,7 +248,7 @@ def available_guidelines(self):


# -----------------------------------------------------------------------------
class Context(object):
class Context(object, metaclass=Singleton):
""" Generic package specific context. """

def __init__(self, package_root, pckg_layout, cfg_dict):
11 changes: 11 additions & 0 deletions codechecker_common/singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Singleton(type):
""" Helper type to create singleton classes. """

_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = \
super(Singleton, cls).__call__(*args, **kwargs)

return cls._instances[cls]
3 changes: 2 additions & 1 deletion web/codechecker_web/shared/webserver_context.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import sys

from codechecker_common import logger
from codechecker_common.singleton import Singleton
from codechecker_common.util import load_json_or_empty

LOG = logger.get_logger('system')
@@ -49,7 +50,7 @@ def __len__(self):


# -----------------------------------------------------------------------------
class Context(object):
class Context(object, metaclass=Singleton):
""" Generic package specific context. """

def __init__(self, package_root, pckg_layout, cfg_dict):