-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadStateContextBuilder.py
68 lines (57 loc) · 2.03 KB
/
DownloadStateContextBuilder.py
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
from DownloadStateContext import DownloadStateContext
from RequestState import RequestState
from DownloadState import DownloadState
from ResponseState import ResponseState
import config as cfg
import operator
class DownloadStateContextBuilder:
def __init__(self):
self.downloadStateContext = DownloadStateContext()
def construct(self):
requestState = self.buildRequestState()
self.downloadStateContext.defineRequestState(requestState)
downloadState = self.buildDownloadState()
self.downloadStateContext.defineDownloadState(downloadState)
responseState = self.buildResponseState()
self.downloadStateContext.defineResponseState(responseState)
def buildRequestState(self):
module = __import__("RequestHandler")
requestState = RequestState()
#sort RequestHandler by their Priority (lowest first)
sortedRequestHandler = sorted(cfg.requestHandler.items(), key=operator.itemgetter(1))
setFirst = True
base = requestState
for handler in sortedRequestHandler:
nextHandlerClass = getattr(module, handler[0])
nextHandler = nextHandlerClass()
nextHandler.setContext(self.downloadStateContext)
if setFirst:
base.setFirstHandler(nextHandler)
setFirst = False
else:
base.setSuccessor(nextHandler)
base = nextHandler
base.setSuccessor(None)
return requestState
def buildDownloadState(self):
downloadState = DownloadState()
return downloadState
def buildResponseState(self):
module = __import__("ResponseHandler")
responseState = ResponseState()
#sort ResponseHandler by their Priority (lowest first)
sortedResponseHandler = sorted(cfg.responseHandler.items(), key=operator.itemgetter(1))
setFirst = True
base = responseState
for handler in sortedResponseHandler:
nextHandlerClass = getattr(module, handler[0])
nextHandler = nextHandlerClass()
nextHandler.setContext(self.downloadStateContext)
if setFirst:
base.setFirstHandler(nextHandler)
setFirst = False
else:
base.setSuccessor(nextHandler)
base = nextHandler
base.setSuccessor(None)
return responseState