Skip to content

Commit fe0a550

Browse files
authored
Fix linting warnings and errors from prospector tool (open-edge-platform#401)
Signed-off-by: Katakol, Rohit <rohit.katakol@intel.com>
1 parent 2c7077b commit fe0a550

File tree

1 file changed

+68
-64
lines changed

1 file changed

+68
-64
lines changed

microservices/dlstreamer-pipeline-server/src/model_updater.py

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ class ModelQueryParams(BaseModel):
3838
pipeline_element_name: str = None
3939

4040
@field_validator('*')
41-
def validate_params_not_empty(cls, value, info):
41+
def validate_params_not_empty(self, value, info):
42+
"""
43+
Check if the given `value` is None or an empty string.
44+
Raise error if `value` is empty
45+
"""
4246
if value is not None and value == '':
4347
raise ValueError(f'{info.field_name} cannot be empty')
4448
return value
@@ -149,7 +153,7 @@ def _get_env_var_or_default_value(self,
149153
return value
150154

151155
def _send_request(self, url: str, method: RequestMethod = RequestMethod.GET,
152-
params=None, data=None, stream: bool=False) -> Response:
156+
params = None, data = None, stream: bool = False) -> Response:
153157
"""Sends a HTTP/HTTPS request and retries the request 2 times while attempting
154158
to obtain a new JWT if the response's status code is 401
155159
@@ -231,7 +235,7 @@ def _get_model(self, params: ModelQueryParams) -> dict:
231235
raise ValueError(
232236
f"Received 0 or more than 1 model. " \
233237
f"Expected a single model with properties: " \
234-
f"{params_dict_str}")
238+
f"{params_dict_str}")
235239

236240
model = json_obj[0]
237241
self._logger.debug(
@@ -312,7 +316,7 @@ def get_model_path(self, pipelines_cfg: list) -> dict:
312316
list_pipeline_model_params = pipeline.get("model_params")
313317
if list_pipeline_model_params:
314318
for pipeline_model_params in list_pipeline_model_params:
315-
model_params = ModelQueryParams(**pipeline_model_params)
319+
model_params = ModelQueryParams(**pipeline_model_params)
316320
model_params = {k: v for k,
317321
v in model_params.model_dump().items() if v is not None}
318322
if model_params.get("deploy", False):
@@ -381,68 +385,68 @@ def download_models(self, pipelines_cfg: list):
381385
list_pipeline_model_params = pipeline.get("model_params")
382386
if list_pipeline_model_params:
383387
for pipeline_model_params in list_pipeline_model_params:
384-
msg = None
385-
params = ModelQueryParams(**pipeline_model_params)
386-
model = self._get_model(params)
387-
if not model:
388-
msg = "Model is not found."
389-
raise ValueError(msg)
388+
msg = None
389+
params = ModelQueryParams(**pipeline_model_params)
390+
model = self._get_model(params)
391+
if not model:
392+
msg = "Model is not found."
393+
raise ValueError(msg)
394+
395+
model_id = model["id"]
396+
397+
models_pipeline_dirpath = (self._saved_models_dir + \
398+
"/" + "_".join((model["name"],
399+
"m-"+model["version"],
400+
model["precision"][0]))).lower()
401+
402+
deployment_dirpath = (
403+
models_pipeline_dirpath + "/deployment").lower()
404+
405+
dir_info = ("Directory", models_pipeline_dirpath)
406+
is_already_saved = False
407+
408+
if not (os.path.exists(deployment_dirpath) or \
409+
os.path.exists(models_pipeline_dirpath)):
410+
411+
self._logger.info("Downloading model files...")
412+
zip_file_data = self._get_model_artifacts_zip_file_data(
413+
model_id)
414+
415+
if zip_file_data:
416+
if not os.path.exists(models_pipeline_dirpath):
417+
os.makedirs(models_pipeline_dirpath)
418+
419+
with zipfile.ZipFile(io.BytesIO(zip_file_data), 'r') as zip_ref:
420+
ignored_filenames = (".DS_Store", "__MACOSX")
421+
for file_info in zip_ref.infolist():
422+
file_root_dirname = zip_ref.filelist[0].filename
423+
fname = file_info.filename
424+
if not "deployment" in file_root_dirname:
425+
fname = fname.replace(
426+
file_root_dirname, "")
427+
if fname and \
428+
not file_info.is_dir() and \
429+
not any(name in fname for name in ignored_filenames):
430+
extract_path = os.path.join(models_pipeline_dirpath, fname)
431+
os.makedirs(os.path.dirname(extract_path), exist_ok=True)
432+
file_info.filename = os.path.basename(file_info.filename)
433+
zip_ref.extract(file_info, os.path.dirname(extract_path))
390434

391-
model_id = model["id"]
392-
393-
models_pipeline_dirpath = (self._saved_models_dir + \
394-
"/" + "_".join((model["name"],
395-
"m-"+model["version"],
396-
model["precision"][0]))).lower()
397-
398-
deployment_dirpath = (
399-
models_pipeline_dirpath + "/deployment").lower()
400-
401-
dir_info = ("Directory", models_pipeline_dirpath)
402-
is_already_saved = False
403-
404-
if not (os.path.exists(deployment_dirpath) or \
405-
os.path.exists(models_pipeline_dirpath)):
406-
407-
self._logger.info("Downloading model files...")
408-
zip_file_data = self._get_model_artifacts_zip_file_data(
409-
model_id)
410-
411-
if zip_file_data:
412-
if not os.path.exists(models_pipeline_dirpath):
413-
os.makedirs(models_pipeline_dirpath)
414-
415-
with zipfile.ZipFile(io.BytesIO(zip_file_data), 'r') as zip_ref:
416-
ignored_filenames = (".DS_Store", "__MACOSX")
417-
for file_info in zip_ref.infolist():
418-
file_root_dirname = zip_ref.filelist[0].filename
419-
fname = file_info.filename
420-
if not "deployment" in file_root_dirname:
421-
fname = fname.replace(
422-
file_root_dirname, "")
423-
if fname and \
424-
not file_info.is_dir() and \
425-
not any(name in fname for name in ignored_filenames):
426-
extract_path = os.path.join(models_pipeline_dirpath, fname)
427-
os.makedirs(os.path.dirname(extract_path), exist_ok=True)
428-
file_info.filename = os.path.basename(file_info.filename)
429-
zip_ref.extract(file_info, os.path.dirname(extract_path))
430-
431-
is_artifacts_saved = True
432-
else:
433-
msg = "Model artifacts are not found."
434-
else:
435-
is_already_saved = True
436435
is_artifacts_saved = True
437-
438-
if os.path.exists(deployment_dirpath):
439-
dir_info = ("Deployment directory", deployment_dirpath)
440-
441-
if msg is None:
442-
verb_phrase = "already exists " if is_already_saved else "was created "
443-
msg = f"{dir_info[0]} ({dir_info[1]}) {verb_phrase}" \
444-
f"for the {pipeline['name']} pipeline."
445-
self._logger.info(msg)
436+
else:
437+
msg = "Model artifacts are not found."
438+
else:
439+
is_already_saved = True
440+
is_artifacts_saved = True
441+
442+
if os.path.exists(deployment_dirpath):
443+
dir_info = ("Deployment directory", deployment_dirpath)
444+
445+
if msg is None:
446+
verb_phrase = "already exists " if is_already_saved else "was created "
447+
msg = f"{dir_info[0]} ({dir_info[1]}) {verb_phrase}" \
448+
f"for the {pipeline['name']} pipeline."
449+
self._logger.info(msg)
446450

447451
except Exception as e:
448452
if isinstance(e, PermissionError):

0 commit comments

Comments
 (0)