Skip to content
Snippets Groups Projects
Commit 7a3ba3c8 authored by Mayr, Hannes's avatar Mayr, Hannes Committed by Hock, Martin
Browse files

Move list validation to own function

parent df7f25c2
No related branches found
No related tags found
4 merge requests!41Include latest changes in main branch,!37Merge dev upstream changes into improve/metadata,!34Include architecture diagram in docs,!29Move list validation to own function
......@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
"""Contains the PlotOptions and PlotIDTransfer classes."""
import os
class PlotOptions:
"""
......@@ -10,7 +12,10 @@ class PlotOptions:
Methods
-------
__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
----------
......@@ -99,7 +104,55 @@ class PlotIDTransfer:
def __init__(self, figs, figure_ids):
self.figs = figs
self.figure_ids = figure_ids
self.figure_ids = validate_list(self.figure_ids)
def __str__(self):
"""Representation if an object of this class is printed."""
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
import sys
import warnings
from plotid.save_plot import save_plot
from plotid.plotoptions import PlotIDTransfer
from plotid.plotoptions import PlotIDTransfer, validate_list
class PublishOptions:
......@@ -69,53 +69,25 @@ class PublishOptions:
"""
# Check if IDs are str
if isinstance(self.figure_ids, str):
self.figure_ids = [self.figure_ids]
if isinstance(self.figure_ids, list):
for identifier in self.figure_ids:
if not isinstance(identifier, str):
raise TypeError('The list of figure_ids contains an object'
' which is not a string.')
else:
raise TypeError('The specified figure_ids are neither a string nor'
' a list of strings.')
self.figure_ids = validate_list(self.figure_ids)
# Check if plot_name is a string or a list of strings
self.plot_names = validate_list(self.plot_names)
if not os.path.isfile(sys.argv[0]):
raise FileNotFoundError('Cannot copy original python script. '
'Running publish from a shell is not '
'possible.')
if isinstance(self.src_datapaths, str):
self.src_datapaths = [self.src_datapaths]
if isinstance(self.src_datapaths, list):
for path in self.src_datapaths:
if not isinstance(path, str):
raise TypeError(f'{path} is not a string.')
# Check if source directory and files exist
if not os.path.exists(path):
raise FileNotFoundError('The specified source directory'
f'/file {path} does not exist.')
else:
raise TypeError('The source directory/files are neither '
'a string nor a list.')
# Check if self.src_datapaths are strings and existing files.
self.src_datapaths = validate_list(self.src_datapaths,
is_file=True)
# Check if destination directory is allowed path
if not os.path.exists(self.dst_path_head):
raise FileNotFoundError('The specified destination directory '
f'{self.dst_path_head} does not exist.')
# Check if plot_name is a string or a list of strings
if isinstance(self.plot_names, str):
self.plot_names = [self.plot_names]
if isinstance(self.plot_names, list):
for name in self.plot_names:
if not isinstance(name, str):
raise TypeError('The list of plot_names contains an object'
' which is not a string.')
else:
raise TypeError('The specified plot_names is neither a string nor'
' a list of strings.')
# Check if data_storage is a string
if not isinstance(self.data_storage, str):
raise TypeError('The specified data_storage method is not a '
......@@ -139,7 +111,6 @@ class PublishOptions:
"""
# Export plot figure to picture.
plot_paths = save_plot(self.figure, self.plot_names)
print(plot_paths)
match self.data_storage:
case 'centralized':
self.centralized_data_storage()
......
......@@ -27,7 +27,7 @@ def save_plot(figures, plot_names, extension='png'):
Returns
-------
plot_path : str or list of str
plot_path : list of str
Names of the created pictures.
"""
# Check if figs is a valid figure or a list of valid figures
......
......@@ -47,7 +47,10 @@ class TestPublish(unittest.TestCase):
'from a Python script. Therefore, the script cannot be '
'copied.')
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 + '/',
PIC_NAME, data_storage='individual')
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