motom001/DoorPi

View on GitHub
doorpi/status/webserver_lib/request_handler.py

Summary

Maintainability
F
3 days
Test Coverage

File request_handler.py has 363 lines of code (exceeds 250 allowed). Consider refactoring.
Open

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
logger = logging.getLogger(__name__)
Severity: Minor
Found in doorpi/status/webserver_lib/request_handler.py - About 4 hrs to fix

    Function parse_content has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
    Open

        def parse_content(self, content, online_fallback = False, **mapping_table):
            try:
                matches = re.findall(r"{([^}\s]*)}", content)
                if not matches: return content
                #http://stackoverflow.com/questions/12897374/get-unique-values-from-a-list-in-python/12897491#12897491
    Severity: Minor
    Found in doorpi/status/webserver_lib/request_handler.py - About 3 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 do_control has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring.
    Open

        def do_control(self, control_order, para):
            result_object = dict(
                success = False,
                message = 'unknown error'
            )
    Severity: Minor
    Found in doorpi/status/webserver_lib/request_handler.py - About 3 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

    DoorPiWebRequestHandler has 26 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class DoorPiWebRequestHandler(BaseHTTPRequestHandler):
    
        @property
        def conf(self): return self.server.config
    
    
    Severity: Minor
    Found in doorpi/status/webserver_lib/request_handler.py - About 3 hrs to fix

      Function authentication_required has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def authentication_required(self):
              parsed_path = urlparse(self.path)
      
              public_resources = self.conf.get_keys(self.server.area_public_name, log = False)
              for public_resource in public_resources:
      Severity: Minor
      Found in doorpi/status/webserver_lib/request_handler.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 return_virtual_resource has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def return_virtual_resource(self, prepared_object, return_type = 'json'):
              if isinstance(return_type, list) and len(return_type) > 0: return_type = return_type[0]
      
              if return_type in ["json", "default"]:
                  return  self.return_message(json.dumps(prepared_object), "application/json; charset=utf-8")
      Severity: Minor
      Found in doorpi/status/webserver_lib/request_handler.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 create_virtual_resource has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

          def create_virtual_resource(self, path, raw_parameters):
              return_object = {}
              try:
                  if path.path == '/mirror':
                      return_object = self.create_mirror()
      Severity: Minor
      Found in doorpi/status/webserver_lib/request_handler.py - About 35 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 self.return_message(prepared_object, 'text/html; charset=utf-8')
      Severity: Major
      Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                    return self.return_message(repr(prepared_object))
        Severity: Major
        Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                      try:    return self.return_message(repr(prepared_object))
          Severity: Major
          Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                        except: return self.return_message(str(prepared_object))
            Severity: Major
            Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                          return self.return_message(str(prepared_object))
              Severity: Major
              Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                        return True
                Severity: Major
                Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                          else: return self.real_resource(parsed_path.path)
                  Severity: Major
                  Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                                    return False
                    Severity: Major
                    Found in doorpi/status/webserver_lib/request_handler.py - About 30 mins to fix

                      Function list_directory has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                      Open

                          def list_directory(self, path):
                              dirs = []
                              files = []
                              for item in os.listdir(path):
                                  if os.path.isfile(item): files.append(item)
                      Severity: Minor
                      Found in doorpi/status/webserver_lib/request_handler.py - About 25 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

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

                              for read_permission in user_session['readpermissions']:
                                  if re.match(read_permission, parsed_path.path):
                                      logger.info('user %s has read permissions: %s', user_session['username'], parsed_path.path)
                                      return False
                      Severity: Major
                      Found in doorpi/status/webserver_lib/request_handler.py and 1 other location - About 1 hr to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 346..349

                      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 47.

                      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

                              for write_permission in user_session['writepermissions']:
                                  if re.match(write_permission, parsed_path.path):
                                      logger.info('user %s has write permissions: %s', user_session['username'], parsed_path.path)
                                      return False
                      Severity: Major
                      Found in doorpi/status/webserver_lib/request_handler.py and 1 other location - About 1 hr to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 351..354

                      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 47.

                      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 3 locations. Consider refactoring.
                      Open

                                  elif control_order == "config_value_set":
                                      # section, key, value, password
                                      result_object['success'] = control_config_set_value(**para)
                                      result_object['message'] = "config_value_set %s" % (
                                          'success' if result_object['success'] else 'failed'
                      Severity: Major
                      Found in doorpi/status/webserver_lib/request_handler.py and 2 other locations - About 1 hr to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 113..117
                      doorpi/status/webserver_lib/request_handler.py on lines 119..123

                      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 45.

                      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 3 locations. Consider refactoring.
                      Open

                                  elif control_order == "config_value_delete":
                                      # section and key
                                      result_object['success'] = control_config_delete_key(**para)
                                      result_object['message'] = "config_value_delete %s" % (
                                          'success' if result_object['success'] else 'failed'
                      Severity: Major
                      Found in doorpi/status/webserver_lib/request_handler.py and 2 other locations - About 1 hr to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 107..111
                      doorpi/status/webserver_lib/request_handler.py on lines 119..123

                      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 45.

                      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 3 locations. Consider refactoring.
                      Open

                                  elif control_order == "config_save":
                                      # configfile
                                      result_object['success'] = control_config_save(**para)
                                      result_object['message'] = "config_save %s" % (
                                          'success' if result_object['success'] else 'failed'
                      Severity: Major
                      Found in doorpi/status/webserver_lib/request_handler.py and 2 other locations - About 1 hr to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 107..111
                      doorpi/status/webserver_lib/request_handler.py on lines 113..117

                      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 45.

                      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

                                          try: replace_with = self.read_from_file(self.server.www + '/dashboard/parts/' + mapping_table[match])
                      Severity: Minor
                      Found in doorpi/status/webserver_lib/request_handler.py and 1 other location - About 35 mins to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 423..427

                      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 33.

                      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 self.server.online_fallback:
                                                  replace_with = self.read_from_fallback(
                                                      self.server.online_fallback +
                                                      '/dashboard/parts/' +
                                                      mapping_table[match]
                      Severity: Minor
                      Found in doorpi/status/webserver_lib/request_handler.py and 1 other location - About 35 mins to fix
                      doorpi/status/webserver_lib/request_handler.py on lines 421..421

                      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 33.

                      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