mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
* Replace OpenDevin with OpenHands * Update CONTRIBUTING.md * Update README.md * Update README.md * update poetry lock; move opendevin folder to openhands * fix env var * revert image references in docs * revert permissions * revert permissions --------- Co-authored-by: Xingyao Wang <xingyao6@illinois.edu>
38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
import dataclasses
|
|
|
|
from openhands.core import logger
|
|
|
|
|
|
class Singleton(type):
|
|
_instances: dict = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
if cls not in cls._instances:
|
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
else:
|
|
# allow updates, just update existing instance
|
|
# perhaps not the most orthodox way to do it, though it simplifies client code
|
|
# useful for pre-defined groups of settings
|
|
instance = cls._instances[cls]
|
|
for key, value in kwargs.items():
|
|
if hasattr(instance, key):
|
|
setattr(instance, key, value)
|
|
else:
|
|
logger.openhands_logger.warning(
|
|
f'Unknown key for {cls.__name__}: "{key}"'
|
|
)
|
|
return cls._instances[cls]
|
|
|
|
@classmethod
|
|
def reset(cls):
|
|
# used by pytest to reset the state of the singleton instances
|
|
for instance_type, instance in cls._instances.items():
|
|
print('resetting... ', instance_type)
|
|
for field_info in dataclasses.fields(instance_type):
|
|
if dataclasses.is_dataclass(field_info.type):
|
|
setattr(instance, field_info.name, field_info.type())
|
|
elif field_info.default_factory is not dataclasses.MISSING:
|
|
setattr(instance, field_info.name, field_info.default_factory())
|
|
else:
|
|
setattr(instance, field_info.name, field_info.default)
|