From 2d0813de810ac1aba5d282dd743c8e74c81678ed Mon Sep 17 00:00:00 2001 From: Chris Mackey Date: Wed, 11 Dec 2024 12:44:03 -0800 Subject: [PATCH] fix(commandutil): Add a function for processing CLI --output-files I was just finding myself copay/pasting this same code in several repos. So it seemed like it should have a place in ladybug. --- ladybug/commandutil.py | 28 ++++++++++++++++++++++++++++ ladybug/datacollection.py | 6 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ladybug/commandutil.py b/ladybug/commandutil.py index f91ea7c4..4fea015e 100644 --- a/ladybug/commandutil.py +++ b/ladybug/commandutil.py @@ -1,5 +1,6 @@ # coding=utf-8 """Utility functions for running the functions of CLI commands outside of the CLI.""" +import os def run_command_function(command_function, arguments=None, options=None): @@ -29,3 +30,30 @@ def run_command_function(command_function, arguments=None, options=None): # run the command using arguments and keyword arguments return command_function(*args, **kwargs) + + +def process_content_to_output(content_str, output_file=None): + """Process output strings from commands for various formats of output_files. + + Args: + content_str: A text string for file contents that are being output from + a command. + output_file: Any of the typically supported --output-file types of the + CLI. This can be a string for a file path, a file object, or the stdout + file object used by click. If None, the string is simply returned from + this method. (Default: None). + """ + if output_file is None: + return content_str + elif isinstance(output_file, str): + dir_name = os.path.dirname(os.path.abspath(output_file)) + if not os.path.isdir(dir_name): + os.makedirs(dir_name) + with open(output_file, 'w') as of: + of.write(content_str) + else: + if 'stdout' not in str(output_file): + dir_name = os.path.dirname(os.path.abspath(output_file.name)) + if not os.path.isdir(dir_name): + os.makedirs(dir_name) + output_file.write(content_str) diff --git a/ladybug/datacollection.py b/ladybug/datacollection.py index dc3564c9..06dc90ce 100644 --- a/ladybug/datacollection.py +++ b/ladybug/datacollection.py @@ -495,11 +495,11 @@ def _timestep_cull(self, timestep): return new_ap, new_values, new_datetimes def _check_analysis_period(self, analysis_period): - assert self.header.analysis_period.timestep == analysis_period.timestep,\ + assert self.header.analysis_period.timestep == analysis_period.timestep, \ 'analysis_period timestep must match that on the'\ 'Collection header. {} != {}'.format( analysis_period.timestep, self.header.analysis_period.timestep) - assert self.header.analysis_period.is_leap_year is analysis_period.is_leap_year,\ + assert self.header.analysis_period.is_leap_year is analysis_period.is_leap_year, \ 'analysis_period is_leap_year must match that on the'\ 'Collection header. {} != {}'.format( analysis_period.is_leap_year, self.header.analysis_period.is_leap_year) @@ -1253,7 +1253,7 @@ def validate_analysis_period(self): return new_coll def _check_analysis_period(self, analysis_period): - assert self.header.analysis_period.is_leap_year is analysis_period.is_leap_year,\ + assert self.header.analysis_period.is_leap_year is analysis_period.is_leap_year, \ 'analysis_period is_leap_year must match that on the'\ 'Collection header. {} != {}'.format( analysis_period.is_leap_year, self.header.analysis_period.is_leap_year)