예제 #1
0
def test_capital_case():
    res = tools.capital_case("PowerShell Remoting")
    assert res == "PowerShell Remoting"
    res = tools.capital_case("good life")
    assert res == "Good Life"
    res = tools.capital_case("good_life-here v2")
    assert res == "Good_life-here V2"
    res = tools.capital_case("")
    assert res == ""
예제 #2
0
    def format_pack_dir_name(name: str) -> str:
        """Formats a (pack) name to a valid value

        Specification:
            A valid pack name does not contain any whitespace and may only contain alphanumeric, underscore, and
            dash characters. The name must begin and end with an alphanumeric character. If it begins with an
            alphabetical character, that character must be capitalized.

        Behavior:
            Individual words are titlecased, whitespace is stripped, and disallowed punctuation and space
            characters are replaced with underscores.

        Args:
            name (str): The proposed pack name to convert to valid pack name format

        Returns:
            str: The reformatted pack name
        """
        temp = capital_case(name.strip().strip('-_'))
        punctuation_to_replace = punctuation.replace('-', '').replace('_', '')
        translation_dict = {x: '_' for x in punctuation_to_replace}
        translation_table = str.maketrans(translation_dict)
        temp = temp.translate(translation_table).strip('-_')
        temp = re.sub(r'-+', '-', re.sub(r'_+', '_', temp))
        comparator = capital_case(temp.replace('_', ' ').replace('-', ' '))
        result = ''
        i = j = 0
        while i < len(temp):
            temp_char = temp[i]
            comp_char = comparator[j]
            if temp_char.casefold() != comp_char.casefold():
                while temp_char in {' ', '_', '-'}:
                    result += f'{temp_char}'
                    i += 1
                    temp_char = temp[i]
                while comp_char in {' '}:
                    j += 1
                    comp_char = comparator[j]
            else:
                result += comparator[j]
                i += 1
                j += 1
        result = result.replace(' ', '')
        result = re.sub(r'-+', '-', re.sub(r'_+', '_', result))
        return result