in utils.py [0:0]
def open_input(filepath: str = None) -> str:
"""
Performs a check on the --filepath argument to attempt to find the
template file for the custom control and custom framework.
It looks in three locations.
* A complete path to the file
* A file in /frameworks
* An S3 URL
Args:
filepath (*string*, REQUIRED):
A string which defined the path to the template file for the
custom controls and custome framework
Defaults to None.
Raises:
FileNotFoundError: The FileNotFoundError will be raised if it cannot
locate the file
"""
script_dir = os.path.dirname(os.path.realpath(__file__))
framework_dir = os.path.join(script_dir, 'frameworks')
try:
if os.path.isfile(filepath) is True:
logging.info(
'Input file found at {}'.format(filepath))
if filepath.endswith(".json") is True:
contents = open_json(input_file=filepath)
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = open_yaml(input_file=filepath)
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
elif os.path.isfile(os.path.join(framework_dir, filepath)) is True:
filepath = os.path.join(framework_dir, filepath)
logging.info(
'Input file found in {} directory'.format(
os.path.join(framework_dir, filepath)))
if filepath.endswith('.json') is True:
contents = open_json(input_file=filepath)
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = open_yaml(input_file=filepath)
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
elif filepath.startswith('s3://') is True:
logging.info('Input file is in S3 bucket {}'.format(filepath))
if filepath.endswith('.json') is True:
contents = json.load(get_object_from_s3(filepath))
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = yaml.safe_load(get_object_from_s3(filepath))
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
else:
raise FileNotFoundError
except FileNotFoundError as error:
raise error