Skip to content

Commit 60a42a8

Browse files
committed
Apply black formatting
1 parent 2b1b628 commit 60a42a8

File tree

2 files changed

+90
-62
lines changed

2 files changed

+90
-62
lines changed

src/sasctl/pzmm/gitIntegration.py

Lines changed: 87 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from .._services.model_repository import ModelRepository as mr
1313
from ..core import RestObj
1414

15+
1516
def getZippedModel(model, gPath, project=None):
16-
'''Retrieve a zipped file containing all of the model contents or a specified
17+
"""Retrieve a zipped file containing all of the model contents or a specified
1718
model. The project argument is only needed if the model argument is not a valid
1819
UUID or RestObj.
1920
@@ -27,9 +28,9 @@ def getZippedModel(model, gPath, project=None):
2728
project : string or RestObj, optional
2829
Project identifier, which is required when only the model name is supplied. Default
2930
is None.
30-
'''
31-
params = {'format': 'zip'}
32-
modelZip = mr.get('models/%s' % (model), params=params, format_='content')
31+
"""
32+
params = {"format": "zip"}
33+
modelZip = mr.get("models/%s" % (model), params=params, format_="content")
3334
modelName = mr.get_model(model).name
3435
# Check if the provided project variable is a REST object
3536
if isinstance(project, RestObj):
@@ -38,25 +39,32 @@ def getZippedModel(model, gPath, project=None):
3839
projectName = mr.get_project(project).name
3940
# Check to see if project folder exists
4041
if (Path(gPath) / projectName).exists():
41-
#Check to see if model folder exists
42+
# Check to see if model folder exists
4243
if (Path(gPath) / projectName / modelName).exists():
43-
with open(Path(gPath) / projectName / modelName / (modelName + '.zip'), 'wb') as zFile:
44+
with open(
45+
Path(gPath) / projectName / modelName / (modelName + ".zip"), "wb"
46+
) as zFile:
4447
zFile.write(modelZip)
4548
else:
4649
newDir = Path(gPath) / projectName / modelName
4750
newDir.mkdir(parents=True, exist_ok=True)
48-
with open(Path(gPath) / projectName / modelName / (modelName + '.zip'), 'wb') as zFile:
51+
with open(
52+
Path(gPath) / projectName / modelName / (modelName + ".zip"), "wb"
53+
) as zFile:
4954
zFile.write(modelZip)
5055
else:
5156
newDir = Path(gPath) / projectName
5257
newDir.mkdir(parents=True, exist_ok=True)
5358
newDir = Path(gPath) / projectName / modelName
5459
newDir.mkdir(parents=True, exist_ok=True)
55-
with open(Path(gPath) / (projectName + '/' + modelName + '.zip'), 'wb') as zFile:
60+
with open(
61+
Path(gPath) / (projectName + "/" + modelName + ".zip"), "wb"
62+
) as zFile:
5663
zFile.write(modelZip)
5764

5865
return modelName, projectName
5966

67+
6068
def project_exists(response, project):
6169
"""Checks if project exists on SAS Viya. If the project does not exist, then a new
6270
project is created or an error is raised.
@@ -93,7 +101,8 @@ def project_exists(response, project):
93101
return response
94102
else:
95103
return response
96-
104+
105+
97106
def model_exists(project, name, force):
98107
"""Checks if model already exists and either raises an error or deletes the redundant model.
99108
@@ -138,6 +147,8 @@ def model_exists(project, name, force):
138147
project.name
139148
)
140149
)
150+
151+
141152
class GitIntegrate:
142153
@classmethod
143154
def pullViyaModel(
@@ -146,14 +157,14 @@ def pullViyaModel(
146157
gPath,
147158
project=None,
148159
):
149-
'''Send an API request in order to pull a model from a project in
160+
"""Send an API request in order to pull a model from a project in
150161
SAS Model Manager in a zipped format. The contents of the zip file
151162
include all files found in SAS Model Manager's model UI, except that
152163
read-only json files are updated to match the current state of the model.
153-
164+
154165
After pulling down the zipped model, unpack the file in the model folder.
155166
Overwrites files with the same name.
156-
167+
157168
If supplying a model name instead of model UUID, a project name or uuid must
158169
be supplied as well. Models in the model repository are allowed duplicate
159170
names, therefore we need a method of parsing the returned models.
@@ -166,7 +177,7 @@ def pullViyaModel(
166177
Base directory of the git repository.
167178
project : string or RestObj, optional
168179
A string or JSON response representing the project the model exists in, default is None.
169-
'''
180+
"""
170181
# Try to pull down the model assuming a UUID or RestObj is provided
171182
try:
172183
if isinstance(model, RestObj):
@@ -190,24 +201,30 @@ def pullViyaModel(
190201
try:
191202
if model["name"] == model:
192203
modelId = model.id
193-
modelName, projectName = getZippedModel(modelId, gPath, projectName)
204+
modelName, projectName = getZippedModel(
205+
modelId, gPath, projectName
206+
)
194207
except TypeError:
195208
if projectModels["name"] == model:
196209
modelId = projectModels.id
197-
modelName, projectName = getZippedModel(modelId, gPath, projectName)
198-
210+
modelName, projectName = getZippedModel(
211+
modelId, gPath, projectName
212+
)
213+
199214
# Unpack the pulled down zip model and overwrite any duplicate files
200-
mPath = Path(gPath) / '{projectName}/{modelName}'.format(projectName=projectName, modelName=modelName)
201-
with zipfile.ZipFile(str(mPath / (modelName + '.zip')), mode='r') as zFile:
215+
mPath = Path(gPath) / "{projectName}/{modelName}".format(
216+
projectName=projectName, modelName=modelName
217+
)
218+
with zipfile.ZipFile(str(mPath / (modelName + ".zip")), mode="r") as zFile:
202219
zFile.extractall(str(mPath))
203-
220+
204221
# Delete the zip model objects in the directory to minimize confusion when uploading back to SAS Model Manager
205-
for zipFile in mPath.glob('*.zip'):
222+
for zipFile in mPath.glob("*.zip"):
206223
zipFile.unlink()
207-
224+
208225
@classmethod
209226
def pushGitModel(cls, gPath, modelName=None, projectName=None):
210-
'''Push a single model in the git repository up to SAS Model Manager. This function
227+
"""Push a single model in the git repository up to SAS Model Manager. This function
211228
creates an archive of all files in the directory and imports the zipped model.
212229
213230
Parameters
@@ -218,29 +235,31 @@ def pushGitModel(cls, gPath, modelName=None, projectName=None):
218235
Name of model to be imported, by default None
219236
projectName : string, optional
220237
Name of project the model is imported from, by default None
221-
'''
238+
"""
222239
if modelName is None and projectName is None:
223240
modelDir = gPath
224241
modelName = modelDir.name
225242
projectName = modelDir.parent.name
226243
else:
227-
modelDir = Path(gPath) / (projectName + '/' + modelName)
228-
for zipFile in modelDir.glob('*.zip'):
229-
zipFile.unlink()
244+
modelDir = Path(gPath) / (projectName + "/" + modelName)
245+
for zipFile in modelDir.glob("*.zip"):
246+
zipFile.unlink()
230247
fileNames = []
231-
fileNames.extend(sorted(Path(modelDir).glob('*')))
232-
with zipfile.ZipFile(str(modelDir / (modelDir.name + '.zip')), mode='w') as zFile:
248+
fileNames.extend(sorted(Path(modelDir).glob("*")))
249+
with zipfile.ZipFile(
250+
str(modelDir / (modelDir.name + ".zip")), mode="w"
251+
) as zFile:
233252
for file in fileNames:
234253
zFile.write(str(file), arcname=file.name)
235-
with open(modelDir / (modelDir.name + '.zip'), 'rb') as zFile:
254+
with open(modelDir / (modelDir.name + ".zip"), "rb") as zFile:
236255
zipIOFile = io.BytesIO(zFile.read())
237256
# Check if model with same name already exists in project. Delete if it exists.
238257
model_exists(projectName, modelName, True)
239258
mr.import_model_from_zip(modelName, projectName, zipIOFile)
240-
259+
241260
@classmethod
242-
def gitRepoPush(cls, gPath, commitMessage, branch='origin'):
243-
'''Create a new commit with new files, then push changes from the local repository to a remote
261+
def gitRepoPush(cls, gPath, commitMessage, branch="origin"):
262+
"""Create a new commit with new files, then push changes from the local repository to a remote
244263
branch. The default remote branch is origin.
245264
246265
Parameters
@@ -251,32 +270,32 @@ def gitRepoPush(cls, gPath, commitMessage, branch='origin'):
251270
Commit message for the new commit
252271
branch : str, optional
253272
Branch name for the remote repository, by default 'origin'
254-
'''
273+
"""
255274
repo = Repo(gPath)
256275
repo.git.add(all=True)
257276
repo.index.commit(commitMessage)
258277
pushBranch = repo.remote(name=branch)
259278
pushBranch.push()
260-
279+
261280
@classmethod
262-
def gitRepoPull(cls, gPath, branch='origin'):
263-
'''Pull down any changes from a remote branch of the git repository. The default branch is
264-
origin.
281+
def gitRepoPull(cls, gPath, branch="origin"):
282+
"""Pull down any changes from a remote branch of the git repository. The default branch is
283+
origin.
265284
266285
Parameters
267286
----------
268287
gPath : string or Path
269288
Base directory of the git repository.
270289
branch : string
271290
Branch name for the remote repository, by default 'origin'
272-
'''
291+
"""
273292
repo = Repo(gPath)
274293
pullBranch = repo.remote(name=branch)
275294
pullBranch.pull()
276-
295+
277296
@classmethod
278297
def pullGitProject(cls, gPath, project=None):
279-
'''Using a user provided project name, search for the project in the specified git repository,
298+
"""Using a user provided project name, search for the project in the specified git repository,
280299
check if the project already exists on SAS Model Manager (create a new project if it does not),
281300
then upload each model found in the git project to SAS Model Manager
282301
@@ -286,33 +305,40 @@ def pullGitProject(cls, gPath, project=None):
286305
Base directory of the git repository or the project directory.
287306
project : string or RestObj
288307
Project name, UUID, or JSON response from SAS Model Manager.
289-
'''
308+
"""
290309
# Check to see if provided project argument is a valid project on SAS Model Manager
291310
projectResponse = mr.get_project(project)
292311
project = project_exists(projectResponse, project)
293312
projectName = project.name
294-
313+
295314
# Check if project exists in git path and produce an error if it does not
296315
pPath = Path(gPath) / projectName
297316
if pPath.exists():
298-
models = [x for x in pPath.glob('*') if x.is_dir()]
317+
models = [x for x in pPath.glob("*") if x.is_dir()]
299318
if len(models) == 0:
300-
print('No models were found in project {}.'.format(projectName))
301-
print('{numModels} models were found in project {projectName}.'.format(numModels=len(models), projectName=projectName))
319+
print("No models were found in project {}.".format(projectName))
320+
print(
321+
"{numModels} models were found in project {projectName}.".format(
322+
numModels=len(models), projectName=projectName
323+
)
324+
)
302325
else:
303-
raise FileNotFoundError('No directory with the name {} was found in the specified git path.'.format(project))
304-
326+
raise FileNotFoundError(
327+
"No directory with the name {} was found in the specified git path.".format(
328+
project
329+
)
330+
)
331+
305332
# Loop through paths of models and upload each to SAS Model Manager
306333
for model in models:
307334
# Remove any extra zip objects in the directory
308-
for zipFile in model.glob('*.zip'):
335+
for zipFile in model.glob("*.zip"):
309336
zipFile.unlink()
310337
cls.pushGitModel(model)
311-
312-
338+
313339
@classmethod
314340
def pullMMProject(cls, gPath, project):
315-
'''Following the user provided project argument, pull down all models from the
341+
"""Following the user provided project argument, pull down all models from the
316342
corresponding SAS Model Manager project into the mapped git directories.
317343
318344
Parameters
@@ -321,32 +347,34 @@ def pullMMProject(cls, gPath, project):
321347
Base directory of the git repository.
322348
project : string or RestObj
323349
The name or id of the model project, or a RestObj representation of the project.
324-
'''
350+
"""
325351
# Check to see if provided project argument is a valid project on SAS Model Manager
326352
projectResponse = mr.get_project(project)
327353
project = project_exists(projectResponse, project)
328354
projectName = project.name
329-
355+
330356
# Check if project exists in git path and create it if it does not
331357
pPath = Path(gPath) / projectName
332358
if not pPath.exists():
333359
Path(pPath).mkdir(parents=True, exist_ok=True)
334-
360+
335361
# Return a list of model names from SAS Model Manager project
336-
modelResponse = mr.get('projects/{}/models'.format(project.id))
362+
modelResponse = mr.get("projects/{}/models".format(project.id))
337363
if modelResponse == []:
338-
raise FileNotFoundError('No models were found in the specified project. A new project folder ' +
339-
'has been created if it did not already exist within the git repository.')
364+
raise FileNotFoundError(
365+
"No models were found in the specified project. A new project folder "
366+
+ "has been created if it did not already exist within the git repository."
367+
)
340368
modelNames = []
341369
modelId = []
342370
for model in modelResponse:
343371
modelNames.append(model.name)
344372
modelId.append(model.id)
345-
373+
346374
# For each model, search for an appropriate model directory in the project directory and pull down the model
347375
for name, id in zip(modelNames, modelId):
348376
mPath = pPath / name
349377
# If the model directory does not exist, create one in the project directory
350378
if not mPath.exists():
351379
Path(mPath).mkdir(parents=True, exist_ok=True)
352-
cls.pullViyaModel(id, mPath.parents[1])
380+
cls.pullViyaModel(id, mPath.parents[1])

src/sasctl/pzmm/importModel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def pzmmImportModel(
113113
isH2OModel=False,
114114
force=False,
115115
binaryString=None,
116-
missingValues=False
116+
missingValues=False,
117117
):
118118
"""Import model to SAS Model Manager using pzmm submodule.
119119
@@ -240,7 +240,7 @@ def getFiles(extensions):
240240
isH2OModel=isH2OModel,
241241
isBinaryModel=binaryModel,
242242
binaryString=binaryString,
243-
missingValues=missingValues
243+
missingValues=missingValues,
244244
)
245245
print(
246246
"Model score code was written successfully to {}.".format(
@@ -304,7 +304,7 @@ def getFiles(extensions):
304304
isH2OModel=isH2OModel,
305305
isBinaryModel=binaryModel,
306306
binaryString=binaryString,
307-
missingValues=missingValues
307+
missingValues=missingValues,
308308
)
309309
print(
310310
"Model score code was written successfully to {} and uploaded to SAS Model Manager".format(

0 commit comments

Comments
 (0)