saltstack/salt

View on GitHub
salt/utils/stringutils.py

Summary

Maintainability
F
4 days
Test Coverage

File stringutils.py has 492 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# -*- coding: utf-8 -*-
'''
Functions for manipulating or otherwise processing strings
'''

Severity: Minor
Found in salt/utils/stringutils.py - About 7 hrs to fix

    Function check_include_exclude has a Cognitive Complexity of 35 (exceeds 5 allowed). Consider refactoring.
    Open

    def check_include_exclude(path_str, include_pat=None, exclude_pat=None):
        '''
        Check for glob or regexp patterns for include_pat and exclude_pat in the
        'path_str' string and return True/False conditions as follows.
          - Default: return 'True' if no include_pat or exclude_pat patterns are
    Severity: Minor
    Found in salt/utils/stringutils.py - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function to_str has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
    Open

    def to_str(s, encoding=None, errors='strict', normalize=False):
        '''
        Given str, bytes, bytearray, or unicode (py2), return str
        '''
        def _normalize(s):
    Severity: Minor
    Found in salt/utils/stringutils.py - About 4 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function to_unicode has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

    def to_unicode(s, encoding=None, errors='strict', normalize=False):
        '''
        Given str or unicode, return unicode (str for python 3)
        '''
        def _normalize(s):
    Severity: Minor
    Found in salt/utils/stringutils.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function to_bytes has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

    def to_bytes(s, encoding=None, errors='strict'):
        '''
        Given bytes, bytearray, str, or unicode (python 2), return bytes (str for
        python 2)
        '''
    Severity: Minor
    Found in salt/utils/stringutils.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function check_whitelist_blacklist has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

    def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
        '''
        Check a whitelist and/or blacklist to see if the value matches it.
    
        value
    Severity: Minor
    Found in salt/utils/stringutils.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function is_binary has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

    def is_binary(data):
        '''
        Detects if the passed string of data is binary or text
        '''
        if not data or not isinstance(data, (six.string_types, six.binary_type)):
    Severity: Minor
    Found in salt/utils/stringutils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function print_cli has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
    Open

    def print_cli(msg, retries=10, step=0.01):
        '''
        Wrapper around print() that suppresses tracebacks on broken pipes (i.e.
        when salt output is piped to less and less is stopped prematurely).
        '''
    Severity: Minor
    Found in salt/utils/stringutils.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function human_to_bytes has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

    def human_to_bytes(size):
        '''
        Given a human-readable byte string (e.g. 2G, 30M),
        return the number of bytes.  Will return 0 if the argument has
        unexpected form.
    Severity: Minor
    Found in salt/utils/stringutils.py - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function camel_to_snake_case has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

    def camel_to_snake_case(camel_input):
        '''
        Converts camelCase (or CamelCase) to snake_case.
        From https://codereview.stackexchange.com/questions/185966/functions-to-convert-camelcase-strings-to-snake-case
    
    
    Severity: Minor
    Found in salt/utils/stringutils.py - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Avoid too many return statements within this function.
    Open

        return False
    Severity: Major
    Found in salt/utils/stringutils.py - About 30 mins to fix

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

          if blacklist:
              if isinstance(blacklist, six.string_types):
                  blacklist = [blacklist]
              if not hasattr(blacklist, '__iter__'):
                  raise TypeError(
      Severity: Major
      Found in salt/utils/stringutils.py and 1 other location - About 2 hrs to fix
      salt/utils/stringutils.py on lines 428..438

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 61.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

          if whitelist:
              if isinstance(whitelist, six.string_types):
                  whitelist = [whitelist]
              if not hasattr(whitelist, '__iter__'):
                  raise TypeError(
      Severity: Major
      Found in salt/utils/stringutils.py and 1 other location - About 2 hrs to fix
      salt/utils/stringutils.py on lines 416..426

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 61.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

              if isinstance(s, (bytes, bytearray)):
                  for enc in encoding:
                      try:
                          return _normalize(s.decode(enc, errors))
                      except UnicodeDecodeError as err:
      Severity: Major
      Found in salt/utils/stringutils.py and 1 other location - About 1 hr to fix
      salt/utils/stringutils.py on lines 148..160

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 50.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

              if isinstance(s, unicode):  # pylint: disable=incompatible-py3-code,undefined-variable
                  return _normalize(s)
              elif isinstance(s, (str, bytearray)):
                  for enc in encoding:
                      try:
      Severity: Major
      Found in salt/utils/stringutils.py and 1 other location - About 1 hr to fix
      salt/utils/stringutils.py on lines 92..102

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 50.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      There are no issues that match your filters.

      Category
      Status