|
3 | 3 | from ..services.location.jhu import JhuLocationService |
4 | 4 | from ..services.location.nyt import NYTLocationService |
5 | 5 |
|
6 | | -# Mapping of services to data-sources. |
7 | | -DATA_SOURCES = { |
8 | | - "jhu": JhuLocationService(), |
9 | | - "csbs": CSBSLocationService(), |
10 | | - "nyt": NYTLocationService(), |
11 | | -} |
| 6 | +class DataSourceSingletonMeta(type): |
| 7 | + """ |
| 8 | + access point to the DataSourceSingleton |
| 9 | + """ |
| 10 | + _instances = {} |
12 | 11 |
|
| 12 | + def __call__(cls, *args, **kwargs): |
| 13 | + """ |
| 14 | + Possible changes to the value of the `__init__` argument do not affect |
| 15 | + the returned instance. |
| 16 | + """ |
| 17 | + if cls not in cls._instances: |
| 18 | + instance = super().__call__(*args, **kwargs) |
| 19 | + cls._instances[cls] = instance |
| 20 | + return cls._instances[cls] |
13 | 21 |
|
14 | | -def data_source(source): |
15 | | - """ |
16 | | - Retrieves the provided data-source service. |
17 | 22 |
|
18 | | - :returns: The service. |
19 | | - :rtype: LocationService |
20 | | - """ |
21 | | - return DATA_SOURCES.get(source.lower()) |
| 23 | +class DataSourceSingleton(metaclass=DataSourceSingletonMeta): |
| 24 | + DATA_SOURCES = { |
| 25 | + "jhu": JhuLocationService(), |
| 26 | + "csbs": CSBSLocationService(), |
| 27 | + "nyt": NYTLocationService(), |
| 28 | + } |
| 29 | + def get_data_source(self, dataSource): |
| 30 | + return self.DATA_SOURCES.get(dataSource.lower()) |
| 31 | + ... |
| 32 | + |
| 33 | + def get_data_source_list(self): |
| 34 | + return self.DATA_SOURCES |
| 35 | + |
| 36 | + |
0 commit comments