

def sanitize_path_fragment(original_fragment, filename_extension='', target_file_systems={'btrfs', 'ext', 'ext2', 'ext3', 'ext3cow', 'ext4', 'exfat', 'fat32', 'hfs+', 'ntfs_win32', 'reiser4', 'reiserfs', 'xfs', 'zfs'}, sanitization_method='underscore', truncate=True, replacement='_', additional_illegal_characters=[]):
    unicode_args = ([original_fragment, filename_extension, replacement] + additional_illegal_characters)
    if (not _are_unicode(unicode_args)):
        raise ValueError('`original_fragment`, `filename_extension`, `replacement`, and `additional_illegal_characters` must be of the unicode type under Python 2 or str type under Python 3.')
    if (len(replacement) > 1):
        warnings.warn("The replacement is longer than one character. The length of the resulting string cannot be guaranteed to fit the target file systems' length limit.", ReplacementLengthWarning)
    sanitized_fragment = unicodedata.normalize('NFC', original_fragment)
    if (len(filename_extension) > 0):
        filename_extension = unicodedata.normalize('NFC', ('.' + filename_extension))
    if (sanitization_method == 'underscore'):
        illegal_characters = {
            'btrfs': {'\x00', '/'},
            'ext': {'\x00', '/'},
            'ext2': {'\x00', '/'},
            'ext3': {'\x00', '/'},
            'ext3cow': {'\x00', '/', '@'},
            'ext4': {'\x00', '/'},
            'exfat': {'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', '/', '\\', ':', '*', '?', '"', '<', '>', '|'},
            'fat32': {'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', '/', '\\', ':', '*', '?', '"', '<', '>', '|'},
            'hfs+': {'\x00', '/', ':'},
            'ntfs_win32': {'\x00', '/', '\\', ':', '*', '?', '"', '<', '>', '|'},
            'ntfs_posix': {'\x00', '/'},
            'reiser4': {'\x00', '/'},
            'reiserfs': {'\x00', '/'},
            'xfs': {'\x00', '/'},
            'zfs': {'\x00', '/'},
            'additional_illegal_characters': set(additional_illegal_characters),
        }
        _temp_target_file_systems = set.union(target_file_systems, {'additional_illegal_characters'})
        illegal_character_set = set.union(*(illegal_characters[file_system] for file_system in _temp_target_file_systems))
        if any(((character in replacement) for character in illegal_character_set)):
            raise ValueError('The replacement contains a character that would be illegal in the target file system(s).')
        for character in illegal_character_set:
            sanitized_fragment = sanitized_fragment.replace(character, replacement)
            filename_extension = filename_extension.replace(character, replacement)
        if target_file_systems.intersection({'fat32', 'ntfs_win32'}):
            windows_reserved_names = ('CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9')
            if (sanitized_fragment in windows_reserved_names):
                sanitized_fragment = ((replacement + sanitized_fragment) + replacement)
            if (filename_extension in windows_reserved_names):
                filename_extension = ((replacement + filename_extension) + replacement)
        if truncate:
            max_lengths = {
                'btrfs': (255, 'bytes', 'utf-8', 'NFC'),
                'ext': (255, 'bytes', 'utf-8', 'NFC'),
                'ext2': (255, 'bytes', 'utf-8', 'NFC'),
                'ext3': (255, 'bytes', 'utf-8', 'NFC'),
                'ext3cow': (255, 'bytes', 'utf-8', 'NFC'),
                'ext4': (255, 'bytes', 'utf-8', 'NFC'),
                'exfat': (255, 'characters', 'utf-16', 'NFC'),
                'fat32': (255, 'characters', 'utf-16', 'NFC'),
                'hfs+': (255, 'characters', 'utf-16', 'NFD'),
                'ntfs_win32': (255, 'characters', 'utf-16', 'NFC'),
                'ntfs_posix': (255, 'characters', 'utf-16', 'NFC'),
                'reiser4': (3976, 'bytes', 'utf-8', 'NFC'),
                'reiserfs': (4032, 'bytes', 'utf-8', 'NFC'),
                'xfs': (255, 'bytes', 'utf-8', 'NFC'),
                'zfs': (255, 'bytes', 'utf-8', 'NFC'),
            }
            for file_system in target_file_systems:
                if (max_lengths[file_system][1] == 'bytes'):
                    extension_bytes = unicodedata.normalize(max_lengths[file_system][3], filename_extension).encode(max_lengths[file_system][2])
                    temp_fragment = bytearray()
                    for character in sanitized_fragment:
                        encoded_bytes = unicodedata.normalize(max_lengths[file_system][3], character).encode(max_lengths[file_system][2])
                        if (((len(temp_fragment) + len(encoded_bytes)) + len(extension_bytes)) <= max_lengths[file_system][0]):
                            temp_fragment = (temp_fragment + encoded_bytes)
                        else:
                            break
                    sanitized_fragment = unicodedata.normalize('NFC', temp_fragment.decode(max_lengths[file_system][2]))
                else:
                    temp_fragment = ''
                    if (file_system == 'hfs+'):
                        normalize = unicodedata.ucd_3_2_0.normalize
                    else:
                        normalize = unicodedata.normalize
                    normalized_extension = normalize(max_lengths[file_system][3], filename_extension)
                    for character in sanitized_fragment:
                        normalized_character = normalize(max_lengths[file_system][3], character)
                        if (((len(temp_fragment) + len(normalized_character)) + len(normalized_extension)) <= max_lengths[file_system][0]):
                            temp_fragment += normalized_character
                        else:
                            break
                    sanitized_fragment = unicodedata.normalize('NFC', temp_fragment)
        sanitized_fragment = (sanitized_fragment + filename_extension)
        if (target_file_systems.intersection({'fat32', 'ntfs_win32'}) and (sanitized_fragment.endswith('.') or sanitized_fragment.endswith(' '))):
            if (replacement.endswith('.') or replacement.endswith(' ')):
                raise ValueError('The sanitized string ends with a dot or space, and the replacement also ends with a dot or space. Therefore the string cannot be sanitized for fat32 or ntfs_win32.')
            while (sanitized_fragment.endswith('.') or sanitized_fragment.endswith(' ')):
                sanitized_fragment = (sanitized_fragment[:(- 1)] + replacement)
    else:
        raise ValueError('sanitization_method must be a valid sanitization method.')
    return sanitized_fragment

dataset/ETHPy150Open ksze/sanitize/sanitize.py sanitize_path_fragment/original