forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
109 lines (90 loc) · 2.8 KB
/
__init__.py
File metadata and controls
109 lines (90 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""app.data"""
from abc import ABC, abstractmethod
from ..services.location.csbs import CSBSLocationService
from ..services.location.jhu import JhuLocationService
from ..services.location.nyt import NYTLocationService
class DataSourceFactory(ABC):
def __init__(self):
pass
@abstractmethod
def getInstance():
pass
@abstractmethod
def getService():
pass
class JhuFactory(DataSourceFactory):
__instance = None
__service = None
def __init__(self):
if JhuFactory.__instance != None:
raise Exception("Factory is singleton!")
elif JhuFactory.__service != None:
raise Exception("Service is singleton!")
else:
JhuFactory.__instance = self
JhuFactory.__service = JhuLocationService()
@staticmethod
def getInstance():
if JhuFactory.__instance == None:
JhuFactory()
return JhuFactory.__instance
@staticmethod
def getService():
if JhuFactory.__service == None:
JhuFactory()
return JhuFactory.__service
class CSBSFactory(DataSourceFactory):
__instance = None
__service = None
def __init__(self):
if CSBSFactory.__instance != None:
raise Exception("Factory is singleton!")
elif CSBSFactory.__service != None:
raise Exception("Service is singleton!")
else:
CSBSFactory.__instance = self
CSBSFactory.__service = CSBSLocationService()
@staticmethod
def getInstance():
if CSBSFactory.__instance == None:
CSBSFactory()
return CSBSFactory.__instance
@staticmethod
def getService():
if CSBSFactory.__service == None:
CSBSFactory()
return CSBSFactory.__service
class NYTFactory(DataSourceFactory):
__instance = None
__service = None
def __init__(self):
if NYTFactory.__instance != None:
raise Exception("Factory is singleton!")
elif NYTFactory.__service != None:
raise Exception("Service is singleton!")
else:
NYTFactory.__instance = self
NYTFactory.__service = NYTLocationService()
@staticmethod
def getInstance():
if NYTFactory.__instance == None:
NYTFactory()
return NYTFactory.__instance
@staticmethod
def getService():
if NYTFactory.__service == None:
NYTFactory()
return NYTFactory.__service
# Mapping of factories to data-sources.
DATA_SOURCES = {
"jhu": JhuFactory.getInstance(),
"csbs": CSBSFactory.getInstance(),
"nyt": NYTFactory.getInstance(),
}
def data_source(source):
"""
Retrieves the provided data-source service.
:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower()).getService()