"""Utility methods to assist with file operations.""" from datetime import datetime import os def get_s3_file_key(*args): """Get the s3 file key from env and filename. Args: path (str): The path to use. filename (str): The filename to use. """ # return '{}/{}'.format(path, filename) return os.path.join(*args).replace('\\', '/') def create_path_with_todays_date(output_path, subdir=False, skip_date=False, timestamp=False): """Create and return tmp path. Choose whether or not to use this function, not passing args that eliminate it's main function. (i.e. skip_date arg) Args: output_path: (str) The path to prepend to the generated log path subdir: (bool) Whether to make the date-based folder a subdirectory of the output_path skip_date: (bool) Whether to skip the date portion of the path. timestamp: (bool) If True, the date component is a full timestamp Returns: str: The newly created path. """ if timestamp: date = datetime.today().strftime('%Y-%m-%d_%H_%M_%S') else: date = datetime.today().strftime('%Y-%m-%d') date = date.replace('-', '_') # Remove date portion (if present) for relevant switches. if skip_date or subdir: if '{date}' in output_path: output_path = output_path.replace('{date}', '') # Adjust for switches. if skip_date: # Pass path along temp_path = output_path elif subdir: # Use date as new directory leaf node temp_path = os.path.join(output_path, date) else: # Substitute the date where specified, or append if '{date}' not in output_path: if output_path[-1:] == '/': output_path = output_path[:-1] output_path = output_path + '_{date}' temp_path = output_path.format(date=date) if not os.path.isdir(temp_path): os.makedirs(temp_path) return temp_path