From d95becec238ecb90d5e4f3d85c68814bfdcf868d Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Wed, 27 Oct 2021 06:33:59 -0500 Subject: [PATCH] Updates for issue #644 Refactoring case_utils to improve readability. --- vsg/rules/case_utils.py | 236 ++++++++++++++++++++++++++-------------- 1 file changed, 157 insertions(+), 79 deletions(-) diff --git a/vsg/rules/case_utils.py b/vsg/rules/case_utils.py index c26091d7..e891eb8c 100644 --- a/vsg/rules/case_utils.py +++ b/vsg/rules/case_utils.py @@ -2,95 +2,33 @@ from vsg import violation -def is_exception_enabled(lList): - if len(lList) == 0: - return False - return True - - def check_for_case_violation(oToi, self, check_prefix=False, check_suffix=False, iIndex=0, iLine=None): sObjectValue = oToi.get_tokens()[iIndex].get_value() iMyLine = get_violation_line(oToi, iLine) - if self.case == 'lower' and not check_prefix and not check_suffix: - if not sObjectValue.islower(): - return create_case_violation(sObjectValue, sObjectValue.lower(), oToi, iIndex, iMyLine) + if is_lower_case_without_prefix_or_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_lower_case(sObjectValue, oToi, iIndex, iMyLine) - if self.case == 'lower' and check_prefix and not check_suffix: - if prefix_detected(sObjectValue, self.prefix_exceptions): - sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) - sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) - sConstant = remove_prefix(sObjectValue, sActualPrefix) - sExpected = sDesiredPrefix + sConstant.lower() - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.islower(): - return create_case_violation(sObjectValue, sObjectValue.lower(), oToi, iIndex, iMyLine) + if is_lower_case_with_prefix_exception(self.case, check_prefix, check_suffix): + return check_for_lower_case_with_prefix_exception(sObjectValue, self, oToi, iIndex, iMyLine) - if self.case == 'lower' and not check_prefix and check_suffix: - if suffix_detected(sObjectValue, self.suffix_exceptions): - sDesiredSuffix = get_matched_suffix(sObjectValue, self.suffix_exceptions) - sActualSuffix = extract_suffix(sObjectValue, sDesiredSuffix) - sConstant = remove_suffix(sObjectValue, sActualSuffix) - sExpected = sConstant.lower() + sDesiredSuffix - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.islower(): - return create_case_violation(sObjectValue, sObjectValue.lower(), oToi, iIndex, iMyLine) + if is_lower_case_with_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_lower_case_with_suffix_exception(sObjectValue, self, oToi, iIndex, iMyLine) - if self.case == 'lower' and check_prefix and check_suffix: - if prefix_detected(sObjectValue, self.prefix_exceptions) and suffix_detected(sObjectValue, self.suffix_exceptions): - sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) - sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) - sConstant = remove_prefix(sObjectValue, sActualPrefix) - sDesiredSuffix = get_matched_suffix(sConstant, self.suffix_exceptions) - sActualSuffix = extract_suffix(sConstant, sDesiredSuffix) - sConstant = remove_suffix(sConstant, sActualSuffix) - sExpected = sDesiredPrefix + sConstant.lower() + sDesiredSuffix - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.islower(): - return create_case_violation(sObjectValue, sObjectValue.lower(), oToi, iIndex, iMyLine) + if is_lower_case_with_prefix_and_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_lower_case_with_prefix_and_suffix_exceptions(sObjectValue, self, oToi, iIndex, iMyLine) - if self.case == 'upper' and not check_prefix and not check_suffix: - if not sObjectValue.isupper(): - return create_case_violation(sObjectValue, sObjectValue.upper(), oToi, iIndex, iMyLine) + if is_upper_case_without_prefix_or_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_upper_case(sObjectValue, oToi, iIndex, iMyLine) - if self.case == 'upper' and check_prefix and not check_suffix: - if prefix_detected(sObjectValue, self.prefix_exceptions): - sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) - sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) - sConstant = remove_prefix(sObjectValue, sActualPrefix) - sExpected = sDesiredPrefix + sConstant.upper() - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.isupper(): - return create_case_violation(sObjectValue, sObjectValue.upper(), oToi, iIndex, iMyLine) + if is_upper_case_with_prefix_exception(self.case, check_prefix, check_suffix): + return check_for_upper_case_with_prefix_exception(sObjectValue, self, oToi, iIndex, iMyLine) - if self.case == 'upper' and not check_prefix and check_suffix: - if suffix_detected(sObjectValue, self.suffix_exceptions): - sDesiredSuffix = get_matched_suffix(sObjectValue, self.suffix_exceptions) - sActualSuffix = extract_suffix(sObjectValue, sDesiredSuffix) - sConstant = remove_suffix(sObjectValue, sActualSuffix) - sExpected = sConstant.upper() + sDesiredSuffix - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.isupper(): - return create_case_violation(sObjectValue, sObjectValue.upper(), oToi, iIndex, iMyLine) + if is_upper_case_with_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_upper_case_with_suffix_exception(sObjectValue, self, oToi, iIndex, iMyLine) - if self.case == 'upper' and check_prefix and check_suffix: - if prefix_detected(sObjectValue, self.prefix_exceptions) and suffix_detected(sObjectValue, self.suffix_exceptions): - sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) - sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) - sConstant = remove_prefix(sObjectValue, sActualPrefix) - sDesiredSuffix = get_matched_suffix(sConstant, self.suffix_exceptions) - sActualSuffix = extract_suffix(sConstant, sDesiredSuffix) - sConstant = remove_suffix(sConstant, sActualSuffix) - sExpected = sDesiredPrefix + sConstant.upper() + sDesiredSuffix - if sObjectValue != sExpected: - return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iMyLine) - elif not sObjectValue.isupper(): - return create_case_violation(sObjectValue, sObjectValue.upper(), oToi, iIndex, iMyLine) + if is_upper_case_with_prefix_and_suffix_exception(self.case, check_prefix, check_suffix): + return check_for_upper_case_with_prefix_and_suffix_exceptions(sObjectValue, self, oToi, iIndex, iMyLine) def prefix_detected(sString, lPrefixes): @@ -156,5 +94,145 @@ def get_violation_line(oToi, iLine): return oToi.get_line_number() else: return iLine - + +def is_exception_enabled(lList): + if len(lList) == 0: + return False + return True + + +def is_lower_case_without_prefix_or_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'lower' and not bPrefix and not bSuffix: + return True + return False + + +def is_lower_case_with_prefix_exception(sCase, bPrefix, bSuffix): + if sCase == 'lower' and bPrefix and not bSuffix: + return True + return False + + +def is_lower_case_with_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'lower' and not bPrefix and bSuffix: + return True + return False + + +def is_lower_case_with_prefix_and_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'lower' and bPrefix and bSuffix: + return True + return False + + +def is_upper_case_without_prefix_or_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'upper' and not bPrefix and not bSuffix: + return True + return False + + +def is_upper_case_with_prefix_exception(sCase, bPrefix, bSuffix): + if sCase == 'upper' and bPrefix and not bSuffix: + return True + return False + + +def is_upper_case_with_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'upper' and not bPrefix and bSuffix: + return True + return False + + +def is_upper_case_with_prefix_and_suffix_exception(sCase, bPrefix, bSuffix): + if sCase == 'upper' and bPrefix and bSuffix: + return True + return False + + +def check_for_lower_case(sObjectValue, oToi, iIndex, iLine): + if not sObjectValue.islower(): + return create_case_violation(sObjectValue, sObjectValue.lower(), oToi, iIndex, iLine) + + +def check_for_upper_case(sObjectValue, oToi, iIndex, iLine): + if not sObjectValue.isupper(): + return create_case_violation(sObjectValue, sObjectValue.upper(), oToi, iIndex, iLine) + + +def check_for_lower_case_with_prefix_exception(sObjectValue, self, oToi, iIndex, iLine): + if prefix_detected(sObjectValue, self.prefix_exceptions): + sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) + sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) + sConstant = remove_prefix(sObjectValue, sActualPrefix) + sExpected = sDesiredPrefix + sConstant.lower() + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_lower_case(sObjectValue, oToi, iIndex, iLine) + + +def check_for_upper_case_with_prefix_exception(sObjectValue, self, oToi, iIndex, iLine): + if prefix_detected(sObjectValue, self.prefix_exceptions): + sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) + sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) + sConstant = remove_prefix(sObjectValue, sActualPrefix) + sExpected = sDesiredPrefix + sConstant.upper() + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_upper_case(sObjectValue, oToi, iIndex, iLine) + + +def check_for_lower_case_with_suffix_exception(sObjectValue, self, oToi, iIndex, iLine): + if suffix_detected(sObjectValue, self.suffix_exceptions): + sDesiredSuffix = get_matched_suffix(sObjectValue, self.suffix_exceptions) + sActualSuffix = extract_suffix(sObjectValue, sDesiredSuffix) + sConstant = remove_suffix(sObjectValue, sActualSuffix) + sExpected = sConstant.lower() + sDesiredSuffix + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_lower_case(sObjectValue, oToi, iIndex, iLine) + + +def check_for_upper_case_with_suffix_exception(sObjectValue, self, oToi, iIndex, iLine): + if suffix_detected(sObjectValue, self.suffix_exceptions): + sDesiredSuffix = get_matched_suffix(sObjectValue, self.suffix_exceptions) + sActualSuffix = extract_suffix(sObjectValue, sDesiredSuffix) + sConstant = remove_suffix(sObjectValue, sActualSuffix) + sExpected = sConstant.upper() + sDesiredSuffix + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_upper_case(sObjectValue, oToi, iIndex, iLine) + + +def check_for_lower_case_with_prefix_and_suffix_exceptions(sObjectValue, self, oToi, iIndex, iLine): + if prefix_detected(sObjectValue, self.prefix_exceptions) and suffix_detected(sObjectValue, self.suffix_exceptions): + sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) + sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) + sConstant = remove_prefix(sObjectValue, sActualPrefix) + sDesiredSuffix = get_matched_suffix(sConstant, self.suffix_exceptions) + sActualSuffix = extract_suffix(sConstant, sDesiredSuffix) + sConstant = remove_suffix(sConstant, sActualSuffix) + sExpected = sDesiredPrefix + sConstant.lower() + sDesiredSuffix + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_lower_case(sObjectValue, oToi, iIndex, iLine) + + +def check_for_upper_case_with_prefix_and_suffix_exceptions(sObjectValue, self, oToi, iIndex, iLine): + if prefix_detected(sObjectValue, self.prefix_exceptions) and suffix_detected(sObjectValue, self.suffix_exceptions): + sDesiredPrefix = get_matched_prefix(sObjectValue, self.prefix_exceptions) + sActualPrefix = extract_prefix(sObjectValue, sDesiredPrefix) + sConstant = remove_prefix(sObjectValue, sActualPrefix) + sDesiredSuffix = get_matched_suffix(sConstant, self.suffix_exceptions) + sActualSuffix = extract_suffix(sConstant, sDesiredSuffix) + sConstant = remove_suffix(sConstant, sActualSuffix) + sExpected = sDesiredPrefix + sConstant.upper() + sDesiredSuffix + if sObjectValue != sExpected: + return create_case_violation(sObjectValue, sExpected, oToi, iIndex, iLine) + else: + return check_for_upper_case(sObjectValue, oToi, iIndex, iLine)