omitted files work

This commit is contained in:
Greg DiCristofaro 2020-07-21 08:28:49 -04:00
parent 2c5bab645c
commit 7d3b36f44e

View File

@ -0,0 +1,60 @@
import os
from typing import Union, Tuple
def get_path_pieces(orig_path: str) -> Tuple[str, Union[str, None], Union[str, None]]:
"""Retrieves path pieces. This is a naive approach as it determines if a file is present based on the
presence of an extension.
Args:
orig_path: The original path to deconstruct.
Returns: A tuple of directory, filename and extension. If no extension is present, filename and extension are None.
"""
potential_parent_dir, orig_file = os.path.split(orig_path)
filename, file_extension = os.path.splitext(orig_file)
if file_extension is None or len(file_extension) < 1:
return orig_path, None, None
else:
return potential_parent_dir, filename, file_extension
def get_new_path(orig_path: str, new_filename: str) -> str:
"""Obtains a new path. This tries to determine if the provided path is a directory or filename (has an
extension containing '.') then constructs the new path with the old parent directory and the new filename.
Args:
orig_path (str): The original path.
new_filename (str): The new filename to use.
Returns:
str: The new path.
"""
parent_dir, filename, ext = get_path_pieces(orig_path)
return os.path.join(parent_dir, new_filename)
# For use with creating csv filenames for entries that have been omitted.
OMITTED_ADDITION = '-omitted'
def get_filename_addition(orig_path: str, filename_addition: str) -> str:
"""Gets filename with addition. So if item is '/path/name.ext' and the filename_addition is '-add', the new result
would be '/path/name-add.ext'.
Args:
orig_path (str): The original path.
filename_addition (str): The new addition.
Returns: The altered path.
"""
parent_dir, filename, extension = get_path_pieces(orig_path)
if filename is None:
return orig_path + filename_addition
else:
ext = '' if extension is None else extension
return os.path.join(parent_dir, '{0}{1}.{2}'.format(filename, filename_addition, ext))