def _should_write_file(self, filepath):
# type: (str) -> bool
"""Determines whether a specific file should be written.
:param str filepath: Full file path to file in question
:rtype: bool
"""
if not os.path.isfile(filepath):
# The file does not exist, nothing to overwrite
return True
if self.no_overwrite:
# The file exists and the caller specifically asked us not to overwrite anything
_LOGGER.warning('Skipping existing output file because of "no overwrite" option: %s', filepath)
return False
if self.interactive:
# The file exists and the caller asked us to be consulted on action before overwriting
decision = six.moves.input( # type: ignore # six.moves confuses mypy
'Overwrite existing output file "{}" with new contents? [y/N]:'.format(filepath)
)
try:
if decision.lower()[0] == 'y':
_LOGGER.warning('Overwriting existing output file based on interactive user decision: %s', filepath)
return True
return False
except IndexError:
# No input is interpreted as 'do not overwrite'
_LOGGER.warning('Skipping existing output file based on interactive user decision: %s', filepath)
return False
# If we get to this point, the file exists and we should overwrite it
_LOGGER.warning('Overwriting existing output file because no action was specified otherwise: %s', filepath)
return True
评论列表
文章目录