Skip to content

Commit 0d92a4c

Browse files
committed
Replace boolean methods with isinstance
1 parent 6730b6c commit 0d92a4c

File tree

8 files changed

+42
-108
lines changed

8 files changed

+42
-108
lines changed

src/reposcanner/contrib.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ class CommitInfoMiningRoutine(OfflineRepositoryRoutine):
3131
authorship information, the commit message, and which files were interacted with.
3232
"""
3333

34-
def getRequestType(self):
35-
return CommitInfoMiningRoutineRequest
36-
3734
def offlineImplementation(self, request, session):
3835

3936
factory = DataEntityFactory()
@@ -335,10 +332,8 @@ def __init__(self):
335332
try:
336333
import gambit
337334
except ImportError:
338-
self.gambitIsAvailable = False
339335
self.gambitImportRef = None
340336
else:
341-
self.gambitIsAvailable = True
342337
self.gambitImportRef = gambit
343338

344339
def getRequestType(self):
@@ -350,7 +345,7 @@ def getRequestType(self):
350345
def execute(self, request):
351346

352347
responseFactory = ResponseFactory()
353-
if not self.gambitIsAvailable:
348+
if not self.gambitImportRef is not None:
354349
return responseFactory.createFailureResponse(message="Gambit is not \
355350
installed, halting execution.")
356351

src/reposcanner/manager.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from reposcanner.git import CredentialKeychain
22
from reposcanner.data import DataEntityStore
33
from reposcanner.response import ResponseFactory
4+
from reposcanner.requests import AnalysisRequestModel, ExternalCommandLineToolRoutineRequest
45
from reposcanner.routines import RepositoryRoutine, ExternalCommandLineToolRoutine
56
from reposcanner.analyses import DataAnalysis
67
import warnings
@@ -63,7 +64,7 @@ def process(self, agents, store, notebook):
6364
if selectedAgent is not None:
6465
if notebook is not None:
6566
notebook.onTaskStart(self, store, selectedAgent)
66-
if self._request.isAnalysisRequestType():
67+
if isinstance(self._request, AnalysisRequestModel):
6768
self._request.fetchDataFromStore(store)
6869
self._response = selectedAgent.run(self._request)
6970
if notebook is not None:
@@ -73,7 +74,7 @@ def process(self, agents, store, notebook):
7374
self._response = responseFactory.createFailureResponse(
7475
message="No routine/analysis was found that could \
7576
execute the request ({requestType}).".format(
76-
requestType=type(request)))
77+
requestType=type(self._request)))
7778

7879
@abstractmethod
7980
def getResponseDescription(self):
@@ -348,27 +349,28 @@ def isGUIModeEnabled(self):
348349
def buildTask(self, projectID, projectName, url, routineOrAnalysis):
349350
"""Constructs a task to hold a request/response pair."""
350351
requestType = routineOrAnalysis.getRequestType()
351-
if requestType.isRoutineRequestType():
352-
if requestType.isExternalCommandLineToolRequestType():
353-
request = requestType(outputDirectory=self._outputDirectory)
354-
task = ManagerExternalCommandLineToolTask(request)
355-
return task
356-
else:
357-
if requestType.requiresOnlineAPIAccess():
358-
request = requestType(repositoryURL=url,
359-
outputDirectory=self._outputDirectory,
360-
keychain=self._keychain)
361-
else:
362-
request = requestType(repositoryURL=url,
363-
outputDirectory=self._outputDirectory,
364-
workspaceDirectory=self._workspaceDirectory)
365-
task = ManagerRepositoryRoutineTask(
366-
projectID=projectID, projectName=projectName, url=url, request=request)
367-
return task
368-
elif requestType.isAnalysisRequestType():
369-
request = requestType()
370-
task = ManagerAnalysisTask(request)
371-
return task
352+
if issubclass(requestType, ExternalCommandLineToolRoutineRequest):
353+
cmd_request = requestType(outputDirectory=self._outputDirectory)
354+
cmd_task = ManagerExternalCommandLineToolTask(cmd_request)
355+
return cmd_task
356+
elif issubclass(requestType, OnlineRoutineRequest):
357+
online_request = requestType(repositoryURL=url,
358+
outputDirectory=self._outputDirectory,
359+
keychain=self._keychain)
360+
online_task = ManagerRepositoryRoutineTask(
361+
projectID=projectID, projectName=projectName, url=url, request=online_request)
362+
return online_task
363+
elif issubclass(requestType, RepositoryRoutineRequestModel):
364+
repo_request = requestType(repositoryURL=url,
365+
outputDirectory=self._outputDirectory,
366+
workspaceDirectory=self._workspaceDirectory)
367+
repo_task = ManagerRepositoryRoutineTask(
368+
projectID=projectID, projectName=projectName, url=url, request=repo_request)
369+
return repo_task
370+
elif issubclass(requestType, AnalysisRequestModel):
371+
analysis_request = requestType()
372+
analysis_task = ManagerAnalysisTask(analysis_request)
373+
return analysis_task
372374
else:
373375
raise TypeError(
374376
"Encountered unrecognized request type when building task: {requestType}.".format(

src/reposcanner/provenance.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import subprocess
3131
import os
3232
import reposcanner.data as dataEntities
33+
from reposcanner.manager import ManagerRepositoryRoutineTask
34+
from reposcanner.requests import AnalysisRequestModel
3335

3436
"""
3537
trungdong/prov, a W3C-compliant provenance Data Model
@@ -265,8 +267,7 @@ def onTaskCreation(self, task):
265267
266268
task: The ManagerTask object.
267269
"""
268-
request = task.getRequest()
269-
if request.isRoutineRequestType():
270+
if isinstance(task, ManagerRepositoryRoutineTask):
270271
task = self._document.activity("rs:task{taskid}".format(taskid=id(task)), other_attributes=(
271272
('rs:requestType', task.getRequestClassName()),
272273
('rs:projectID', task.getProjectID()),
@@ -300,7 +301,7 @@ def onTaskStart(self, task, store, agent):
300301
# If the request is an analysis request, we can probe the request to see which
301302
# files it intends to grab from the data store.
302303
request = task.getRequest()
303-
if request.isAnalysisRequestType():
304+
if isinstance(request, AnalysisRequestModel):
304305
filesToBeUsedInAnalysis = store.getByCriteria(request.getDataCriteria())
305306
for entity in filesToBeUsedInAnalysis:
306307
entityID = None

src/reposcanner/requests.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,8 @@ def hasErrors(self):
2323
def getErrors(self):
2424
return self._errors
2525

26-
@classmethod
27-
def isRoutineRequestType(cls):
28-
return False
29-
30-
@classmethod
31-
def isAnalysisRequestType(cls):
32-
return False
33-
3426

3527
class AnalysisRequestModel(BaseRequestModel):
36-
37-
@classmethod
38-
def isAnalysisRequestType(cls):
39-
return True
40-
4128
def __init__(self, outputDirectory="./"):
4229
super().__init__()
4330
self._data = []
@@ -145,14 +132,6 @@ def __init__(self, outputDirectory):
145132
def getOutputDirectory(self):
146133
return self._outputDirectory
147134

148-
@classmethod
149-
def isRoutineRequestType(cls):
150-
return True
151-
152-
@classmethod
153-
def isExternalCommandLineToolRequestType(cls):
154-
return True
155-
156135

157136
class RepositoryRoutineRequestModel(BaseRequestModel):
158137
"""
@@ -213,29 +192,13 @@ def getRepositoryLocation(self):
213192
def getOutputDirectory(self):
214193
return self._outputDirectory
215194

216-
@classmethod
217-
def isRoutineRequestType(cls):
218-
return True
219-
220-
@classmethod
221-
def isExternalCommandLineToolRequestType(cls):
222-
return False
223-
224195

225196
class OnlineRoutineRequest(RepositoryRoutineRequestModel):
226197
"""
227198
The base class for requests to routines that use an online API to compute results.
228199
Request classes for OnlineRepositoryRoutine should inherit from this class.
229200
"""
230201

231-
@classmethod
232-
def requiresOnlineAPIAccess(cls):
233-
"""
234-
Tells the caller whether this request requires access to an online
235-
version control API.
236-
"""
237-
return True
238-
239202
def __init__(
240203
self,
241204
repositoryURL,
@@ -286,14 +249,6 @@ class OfflineRoutineRequest(RepositoryRoutineRequestModel):
286249
Request classes for OfflineRepositoryRoutine should inherit from this class.
287250
"""
288251

289-
@classmethod
290-
def requiresOnlineAPIAccess(cls):
291-
"""
292-
Tells the caller whether this request requires access to an online
293-
version control API.
294-
"""
295-
return False
296-
297252
def __init__(self, repositoryURL, outputDirectory, workspaceDirectory):
298253
"""
299254
Additional Parameters:

src/reposcanner/routines.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pygit2
55
from reposcanner.git import GitEntityFactory, RepositoryLocation
66
from reposcanner.response import ResponseFactory
7+
from reposcanner.requests import ExternalCommandLineToolRoutineRequest, OfflineRoutineRequest, OnlineRoutineRequest
78

89

910
class DataMiningRoutine(ABC):
@@ -112,14 +113,14 @@ def commandLineToolImplementation(self, request):
112113

113114
def execute(self, request):
114115
responseFactory = ResponseFactory()
115-
if not self.canHandleRequest(request):
116+
if not self.canHandleRequest(request) or not isinstance(request, ExternalCommandLineToolRoutineRequest):
116117
return responseFactory.createFailureResponse(
117118
message="The routine was passed a request of the wrong type.")
118119
elif request.hasErrors():
119120
return responseFactory.createFailureResponse(
120121
message="The request had errors in it and cannot be processed.",
121122
attachments=request.getErrors())
122-
elif not self.isExternalToolAvailable():
123+
elif not isinstance(request, ExternalCommandLineToolRoutineRequest):
123124
return responseFactory.createFailureResponse(
124125
message="The command-line tool required by this routine is not available or\
125126
is otherwise unable to be called.")
@@ -144,7 +145,7 @@ def execute(self, request):
144145
overriding that methods.
145146
"""
146147
responseFactory = ResponseFactory()
147-
if not self.canHandleRequest(request):
148+
if not self.canHandleRequest(request) or not isinstance(request, OfflineRoutineRequest):
148149
return responseFactory.createFailureResponse(
149150
message="The routine was passed a request of the wrong type.")
150151
elif request.hasErrors():
@@ -214,7 +215,7 @@ def execute(self, request):
214215
overriding those methods.
215216
"""
216217
responseFactory = ResponseFactory()
217-
if not self.canHandleRequest(request):
218+
if not self.canHandleRequest(request) or not isinstance(request, OnlineRoutineRequest):
218219
return responseFactory.createFailureResponse(
219220
message="The routine was passed a request of the wrong type.")
220221
elif request.hasErrors():
File renamed without changes.

tests/test_baseRoutines.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def canAlwaysHandleRequest(self, request):
136136
routines.OfflineRepositoryRoutine.canHandleRequest = canAlwaysHandleRequest
137137
genericRoutine = routines.OfflineRepositoryRoutine()
138138

139-
genericRequest = requests.RepositoryRoutineRequestModel(
140-
repositoryURL="https://github.com/owner/repo", outputDirectory="./")
139+
genericRequest = requests.OfflineRoutineRequest(
140+
repositoryURL="https://github.com/owner/repo", outputDirectory="./out", workspaceDirectory="./workspace")
141141
genericRequest.addError(message="Something has gone horribly wrong.")
142142
response = genericRoutine.run(genericRequest)
143143
assert(not response.wasSuccessful())
@@ -161,8 +161,8 @@ def canNeverHandleRequest(self, request):
161161
routines.OnlineRepositoryRoutine.canHandleRequest = canNeverHandleRequest
162162
genericRoutine = routines.OnlineRepositoryRoutine()
163163

164-
genericRequest = requests.RepositoryRoutineRequestModel(
165-
repositoryURL="https://github.com/owner/repo", outputDirectory="./")
164+
genericRequest = requests.OnlineRoutineRequest(
165+
repositoryURL="https://github.com/owner/repo", outputDirectory="./out")
166166
response = genericRoutine.run(genericRequest)
167167
assert(not response.wasSuccessful())
168168
assert(response.hasMessage())
@@ -179,7 +179,7 @@ def canAlwaysHandleRequest(self, request):
179179
routines.OnlineRepositoryRoutine.canHandleRequest = canAlwaysHandleRequest
180180
genericRoutine = routines.OnlineRepositoryRoutine()
181181

182-
genericRequest = requests.RepositoryRoutineRequestModel(
182+
genericRequest = requests.OnlineRoutineRequest(
183183
repositoryURL="https://github.com/owner/repo", outputDirectory="./")
184184
genericRequest.addError(message="Something has gone horribly wrong.")
185185
response = genericRoutine.run(genericRequest)
@@ -203,8 +203,8 @@ def canAlwaysHandleRequest(self, request):
203203
emptyAPICreator = gitEntityFactory.createVCSAPISessionCompositeCreator()
204204
genericRoutine.sessionCreator = emptyAPICreator
205205

206-
genericRequest = requests.RepositoryRoutineRequestModel(
207-
repositoryURL="https://github.com/owner/repo", outputDirectory="./")
206+
genericRequest = requests.OnlineRoutineRequest(
207+
repositoryURL="https://github.com/owner/repo", outputDirectory="./", username="foo", password="bar")
208208
response = genericRoutine.run(genericRequest)
209209
assert(not response.wasSuccessful())
210210
assert(response.hasMessage())

tests/test_requests.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ def test_AnalysisRequestModel_isDirectlyConstructible():
99
analysisRequest = requests.AnalysisRequestModel(outputDirectory="./")
1010

1111

12-
def test_AnalysisRequestModel_isAnAnalysisRequestType():
13-
analysisRequest = requests.AnalysisRequestModel(outputDirectory="./")
14-
assert(analysisRequest.isAnalysisRequestType())
15-
assert(not analysisRequest.isRoutineRequestType())
16-
17-
1812
def test_AnalysisRequestModel_hasNoErrorsForValidInput():
1913
analysisRequest = requests.AnalysisRequestModel(outputDirectory="./")
2014
assert(not analysisRequest.hasErrors())
@@ -52,13 +46,6 @@ def test_ExternalCommandLineToolRoutineRequest_isDirectlyConstructible():
5246
requests.ExternalCommandLineToolRoutineRequest(outputDirectory="./")
5347

5448

55-
def test_ExternalCommandLineToolRoutineRequest_isARoutineRequestType():
56-
commandLineToolRequest = requests.ExternalCommandLineToolRoutineRequest(
57-
outputDirectory="./")
58-
assert(not commandLineToolRequest.isAnalysisRequestType())
59-
assert(commandLineToolRequest.isRoutineRequestType())
60-
61-
6249
def test_ExternalCommandLineToolRoutineRequest_hasNoErrorsForValidInput():
6350
commandLineToolRequest = requests.ExternalCommandLineToolRoutineRequest(
6451
outputDirectory="./")
@@ -87,13 +74,6 @@ def test_RepositoryRoutineRequestModel_isDirectlyConstructible():
8774
outputDirectory="./")
8875

8976

90-
def test_AnalysisRequestModel_isARoutineRequestType():
91-
routineRequest = requests.RepositoryRoutineRequestModel(
92-
repositoryURL="https://github.com/owner/repo", outputDirectory="./")
93-
assert(not routineRequest.isAnalysisRequestType())
94-
assert(routineRequest.isRoutineRequestType())
95-
96-
9777
def test_RepositoryRoutineRequestModel_hasNoErrorsForValidInput():
9878
request = requests.RepositoryRoutineRequestModel(
9979
repositoryURL="https://github.com/owner/repo", outputDirectory="./")

0 commit comments

Comments
 (0)