Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavitsharma committed Jul 7, 2023
1 parent bdfc5c8 commit 08b1aa7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 18 additions & 1 deletion automata/core/base/patterns/singleton.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from typing import Any, Dict
from typing import Any, Dict, List


class Singleton(abc.ABCMeta, type):
Expand All @@ -14,3 +14,20 @@ def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class Doubleton(abc.ABCMeta, type):
"""
Doubleton metaclass for ensuring at most two instances of a class.
"""

_instances: Dict[str, List[Any]] = {}

def __call__(cls, *args, **kwargs):
"""Call method for the doubleton metaclass."""
if cls not in cls._instances:
cls._instances[cls] = []
if len(cls._instances[cls]) < 2:
instance = super(Doubleton, cls).__call__(*args, **kwargs)
cls._instances[cls].append(instance)
return cls._instances[cls][-1] # Always return the last created instance
9 changes: 7 additions & 2 deletions automata/singletons/py_module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from redbaron import RedBaron

from automata.core.base.patterns.singleton import Singleton
from automata.core.base.patterns.singleton import Doubleton
from automata.core.utils import get_root_fpath, get_root_py_fpath
from automata.navigation.py.dotpath_map import DotPathMap

Expand All @@ -23,7 +23,12 @@ class ParsingStrategy(Enum):
AstType = Union[RedBaron, Module]


class PyModuleLoader(metaclass=Singleton):
# Doubleton metaclass for ensuring at most two instances of a class.
# This is used to ensure that we can have two different parsing strategies
# for the same project.
# This is just a temporary solution to avoid breaking existing code.
# Will be changed back to Singleton in the future.
class PyModuleLoader(metaclass=Doubleton):
"""
A Singleton with a lazy dictionary mapping dotpaths to their corresponding RedBaron FST objects.
Loads and caches modules in memory as they are accessed
Expand Down

0 comments on commit 08b1aa7

Please sign in to comment.