Skip to content

Commit

Permalink
fix(commandutil): Add a function for processing CLI --output-files
Browse files Browse the repository at this point in the history
I was just finding myself copay/pasting this same code in several repos. So it seemed like it should have a place in ladybug.
  • Loading branch information
chriswmackey committed Dec 11, 2024
1 parent e484da3 commit 2d0813d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 28 additions & 0 deletions ladybug/commandutil.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions ladybug/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2d0813d

Please sign in to comment.