diff options
Diffstat (limited to 'src/argaze/DataFeatures.py')
-rw-r--r-- | src/argaze/DataFeatures.py | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/src/argaze/DataFeatures.py b/src/argaze/DataFeatures.py index c7624e5..29a63e0 100644 --- a/src/argaze/DataFeatures.py +++ b/src/argaze/DataFeatures.py @@ -36,6 +36,32 @@ import matplotlib.pyplot as mpyplot import matplotlib.patches as mpatches from colorama import Style, Fore +# Define global working directory used to load file using relative path +WORKING_DIRECTORY = [None] + +def get_working_directory() -> str: + """Get global working directory.""" + + # Check global working directory + if WORKING_DIRECTORY[0] is None: + + raise(ValueError(f'No working directory.')) + + return WORKING_DIRECTORY[0] + +def set_working_directory(working_directory: str): + """Set global working directory.""" + + # Forget former global working directory + if WORKING_DIRECTORY[0] is not None: + + sys.path.remove(WORKING_DIRECTORY[0]) + + # Append new working directory to Python path + sys.path.append(working_directory) + + WORKING_DIRECTORY[0] = working_directory + def module_path(obj) -> str: """ Get object module path. @@ -472,7 +498,6 @@ def PipelineStepAttributeSetter(method): new_value: value used to set attribute. unwrap: call wrapped method directly. """ - if unwrap: return method(self, new_value) @@ -487,7 +512,7 @@ def PipelineStepAttributeSetter(method): except KeyError: - raise(ValueError(f'Missing annotations in {method.__name__}: {method.__annotations__}')) + raise(ValueError(f'Annotations are missing for {method.__name__}: {method.__annotations__}')) logging.debug('@PipelineStepAttributeSetter %s.%s.setter(%s) with %s', type(self).__name__, method.__name__, expected_value_type.__name__, new_value_type.__name__) @@ -546,8 +571,8 @@ def PipelineStepAttributeSetter(method): file_format = split_point[-1] logging.debug('\t> %s is a path to a %s file', new_value, file_format.upper()) - - filepath = os.path.join(self.working_directory, new_value) + + filepath = os.path.join(get_working_directory(), new_value) # Load image from JPG and PNG formats if file_format == 'jpg' or file_format == 'png': @@ -595,11 +620,10 @@ class PipelineStepObject(): def __init__(self, **kwargs): """Initialize PipelineStepObject.""" - logging.debug('PipelineStepObject.__init__ %s %s', type(self).__name__, kwargs['name'] if 'name' in kwargs else '') + logging.debug('PipelineStepObject.__init__ %s', type(self).__name__) # Init private attribute self.__name = None - self.__working_directory = None self.__observers = [] self.__execution_times = {} @@ -654,23 +678,6 @@ class PipelineStepObject(): self.__name = name @property - def working_directory(self) -> str: - """Get pipeline step object's working directory. - This path will be joined to relative file path.""" - return self.__working_directory - - @working_directory.setter - def working_directory(self, working_directory: str): - """Set pipeline step object's working directory.""" - - # Append working directory to the Python path - if working_directory is not None: - - sys.path.append(working_directory) - - self.__working_directory = working_directory - - @property def parent(self) -> object: """Get pipeline step object's parent object.""" return self.__parent @@ -713,6 +720,9 @@ class PipelineStepObject(): """ Load instance from .json file. + !!! note + The directory where configuration file is will be the global working directory. + Parameters: configuration_filepath: path to json configuration file patch_filepath: path to json patch file to modify any configuration entries @@ -720,15 +730,15 @@ class PipelineStepObject(): logging.debug('%s.from_json', cls.__name__) + # Edit working directory + set_working_directory(os.path.dirname(os.path.abspath(configuration_filepath))) + + logging.debug('\t> set global working directory as %s', get_working_directory()) + # Load configuration from JSON file with open(configuration_filepath) as configuration_file: - # Edit object_data with working directory as first key - object_data = { - 'working_directory': os.path.dirname(configuration_filepath) - } - - object_data.update(json.load(configuration_file)) + object_data = json.load(configuration_file) # Apply patch to configuration if required if patch_filepath is not None: @@ -760,6 +770,8 @@ class PipelineStepObject(): object_data = update(object_data, patch_data) # Instanciate class + logging.debug('\t+ create %s object from configuration updated by patch', cls.__name__) + return cls(**object_data) def to_json(self, json_filepath: str = None): |