def read_in_file(filename): """Read values from file into a list object. Expecting newline delimited. :param filename: the filename to read :returns: the list of values found in the file :raises: ValueError if incoming value is not a file that could be found """ result = None input_path = os.path.expanduser(os.path.expandvars(filename)) # pylint: disable=no-else-return if os.path.isfile(input_path): try: with open(input_path, 'r') as in_file: result = in_file.read().splitlines() except EnvironmentError as err: err_msg = t(messages.READ_FILE_ERROR % (input_path, err)) log.error(err_msg) return result else: raise ValueError(t(messages.NOT_A_FILE % input_path))
def validate_write_file(filename, param_name): """Write content to a file. :param filename: the filename to write :param param_name: the parameter name that provided the filename :raises: ValueError for validation errors """ input_path = os.path.expanduser(os.path.expandvars(filename)) if not input_path: raise ValueError(t(messages.REPORT_OUTPUT_CANNOT_BE_EMPTY % param_name)) if os.path.isdir(input_path): raise ValueError( t(messages.REPORT_OUTPUT_IS_A_DIRECTORY % (param_name, input_path))) directory = os.path.dirname(input_path) if directory and not os.path.exists(directory): raise ValueError( t(messages.REPORT_DIRECTORY_DOES_NOT_EXIST % directory))
def check_extension(extension, path): """Check if .json is in the file extension.""" if extension not in path: print(t(messages.OUTPUT_FILE_TYPE % extension)) sys.exit(1)