Skip to content
Snippets Groups Projects
Commit e5a4d5bd authored by Mayr, Hannes's avatar Mayr, Hannes
Browse files

Fix bug in validate_list func which didnt overwrite self.figure_ids so that...

Fix bug in validate_list func which didnt overwrite self.figure_ids so that publish iterated over a string instead of a list.
parent be61e775
No related branches found
No related tags found
1 merge request!29Move list validation to own function
Pipeline #812487 waiting for manual action
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Contains the PlotOptions and PlotIDTransfer classes.""" """Contains the PlotOptions and PlotIDTransfer classes."""
import os
class PlotOptions: class PlotOptions:
""" """
...@@ -10,7 +12,10 @@ class PlotOptions: ...@@ -10,7 +12,10 @@ class PlotOptions:
Methods Methods
------- -------
__init__ __init__
validate_input : Check if input is correct type. validate_input
Check if input is correct type.
validate_list
Check if all elements of a given list are of certain type.
Attributes Attributes
---------- ----------
...@@ -100,7 +105,55 @@ class PlotIDTransfer: ...@@ -100,7 +105,55 @@ class PlotIDTransfer:
def __init__(self, figs, figure_ids): def __init__(self, figs, figure_ids):
self.figs = figs self.figs = figs
self.figure_ids = figure_ids self.figure_ids = figure_ids
self.figure_ids = validate_list(self.figure_ids)
def __str__(self): def __str__(self):
"""Representation if an object of this class is printed.""" """Representation if an object of this class is printed."""
return str(self.__class__) + ": " + str(self.__dict__) return str(self.__class__) + ": " + str(self.__dict__)
def validate_list(list_var, elt_type=str, is_file=False):
"""
Validate if contents of a list are of specific type.
Parameters
----------
list_var : list or str
List or single string which contents will be validated.
elt_type : datatype, optional
Datatype of which the list elements must be type of. Otherwise
an Error will be raised. The default is str.
is_file : boolean, optional
Flag to indicate if the list contains paths to files. If True the
strings will be checked if they correspond to an existing file.
The default is False.
Raises
------
TypeError
If one of the list elements is not of type elt_type.
FileNotFoundError
If strings are also checked for existing files and one of the files
does not exist.
Returns
-------
list_var as list
"""
if isinstance(list_var, elt_type):
list_var = [list_var]
if isinstance(list_var, list):
for elt in list_var:
if not isinstance(elt, elt_type):
raise TypeError(f'The list of {list_var} contains an '
f'object which is not of type {elt_type}.')
if is_file:
# Check if directory and files exist
if not os.path.exists(elt):
raise FileNotFoundError('The specified directory'
f'/file {elt} does not exist.')
else:
raise TypeError(f'The specified {list_var} are neither a '
f'{elt_type} nor a list of {elt_type}.')
return list_var
...@@ -17,7 +17,7 @@ import shutil ...@@ -17,7 +17,7 @@ import shutil
import sys import sys
import warnings import warnings
from plotid.save_plot import save_plot from plotid.save_plot import save_plot
from plotid.plotoptions import PlotIDTransfer from plotid.plotoptions import PlotIDTransfer, validate_list
class PublishOptions: class PublishOptions:
...@@ -29,8 +29,6 @@ class PublishOptions: ...@@ -29,8 +29,6 @@ class PublishOptions:
__init__ __init__
validate_input validate_input
Check if input is correct type. Check if input is correct type.
validate_list
Check if all elements of a given list are of certain type.
export export
Export the plot and copy specified files to the destiantion folder. Export the plot and copy specified files to the destiantion folder.
""" """
...@@ -53,51 +51,6 @@ class PublishOptions: ...@@ -53,51 +51,6 @@ class PublishOptions:
"""Representation if an object of this class is printed.""" """Representation if an object of this class is printed."""
return str(self.__class__) + ": " + str(self.__dict__) return str(self.__class__) + ": " + str(self.__dict__)
def validate_list(self, list_var, elt_type=str, is_file=False):
"""
Validate if contents of a list are of specific type.
Parameters
----------
list_var : list
List which contents will be validated.
elt_type : datatype, optional
Datatype of which the list elements must be type of. Otherwise
an Error will be raised. The default is str.
is_file : boolean, optional
Flag to indicate if the list contains paths to files. If True the
strings will be checked if they correspond to an existing file.
The default is False.
Raises
------
TypeError
If one of the list elements is not of type elt_type.
FileNotFoundError
If strings are also checked for existing files and one of the files
does not exist.
Returns
-------
None.
"""
if isinstance(list_var, elt_type):
list_var = [list_var]
if isinstance(list_var, list):
for elt in list_var:
if not isinstance(elt, elt_type):
raise TypeError(f'The list of {list_var} contains an '
f'object which is not of type {elt_type}.')
if is_file:
# Check if directory and files exist
if not os.path.exists(elt):
raise FileNotFoundError('The specified directory'
f'/file {elt} does not exist.')
else:
raise TypeError(f'The specified {list_var} are neither a '
f'{elt_type} nor a list of {elt_type}.')
def validate_input(self): def validate_input(self):
""" """
Validate if input for PublishOptions is correct type. Validate if input for PublishOptions is correct type.
...@@ -116,10 +69,10 @@ class PublishOptions: ...@@ -116,10 +69,10 @@ class PublishOptions:
""" """
# Check if IDs are str # Check if IDs are str
self.validate_list(self.figure_ids) self.figure_ids = validate_list(self.figure_ids)
# Check if plot_name is a string or a list of strings # Check if plot_name is a string or a list of strings
self.validate_list(self.plot_names) self.plot_names = validate_list(self.plot_names)
if not os.path.isfile(sys.argv[0]): if not os.path.isfile(sys.argv[0]):
raise FileNotFoundError('Cannot copy original python script. ' raise FileNotFoundError('Cannot copy original python script. '
...@@ -127,7 +80,8 @@ class PublishOptions: ...@@ -127,7 +80,8 @@ class PublishOptions:
'possible.') 'possible.')
# Check if self.src_datapaths are strings and existing files. # Check if self.src_datapaths are strings and existing files.
self.validate_list(self.src_datapaths, is_file=True) self.src_datapaths = validate_list(self.src_datapaths,
is_file=True)
# Check if destination directory is allowed path # Check if destination directory is allowed path
if not os.path.exists(self.dst_path_head): if not os.path.exists(self.dst_path_head):
...@@ -157,7 +111,6 @@ class PublishOptions: ...@@ -157,7 +111,6 @@ class PublishOptions:
""" """
# Export plot figure to picture. # Export plot figure to picture.
plot_paths = save_plot(self.figure, self.plot_names) plot_paths = save_plot(self.figure, self.plot_names)
print(plot_paths)
match self.data_storage: match self.data_storage:
case 'centralized': case 'centralized':
self.centralized_data_storage() self.centralized_data_storage()
......
...@@ -27,7 +27,7 @@ def save_plot(figures, plot_names, extension='png'): ...@@ -27,7 +27,7 @@ def save_plot(figures, plot_names, extension='png'):
Returns Returns
------- -------
plot_path : str or list of str plot_path : list of str
Names of the created pictures. Names of the created pictures.
""" """
# Check if figs is a valid figure or a list of valid figures # Check if figs is a valid figure or a list of valid figures
......
...@@ -47,7 +47,10 @@ class TestPublish(unittest.TestCase): ...@@ -47,7 +47,10 @@ class TestPublish(unittest.TestCase):
'from a Python script. Therefore, the script cannot be ' 'from a Python script. Therefore, the script cannot be '
'copied.') 'copied.')
def test_publish(self): def test_publish(self):
""" Test publish and check if an exported picture file exists. """ """
Test publish and check if an exported picture file exists.
The destination path is given with trailing slash.
"""
publish(PlotIDTransfer(FIG, 'testID'), SRC_DIR, DST_PATH + '/', publish(PlotIDTransfer(FIG, 'testID'), SRC_DIR, DST_PATH + '/',
PIC_NAME, data_storage='individual') PIC_NAME, data_storage='individual')
assert os.path.isfile(os.path.join(DST_PATH, 'testID', assert os.path.isfile(os.path.join(DST_PATH, 'testID',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment