edgewall/trac

View on GitHub
trac/mimeview/api.py

Summary

Maintainability
F
6 days
Test Coverage

File api.py has 895 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# -*- coding: utf-8 -*-
#
# Copyright (C) 2004-2023 Edgewall Software
# Copyright (C) 2004 Daniel Lundin <daniel@edgewall.com>
# Copyright (C) 2005-2006 Christopher Lenz <cmlenz@gmx.de>
Severity: Major
Found in trac/mimeview/api.py - About 2 days to fix

    Function render has a Cognitive Complexity of 50 (exceeds 5 allowed). Consider refactoring.
    Open

        def render(self, context, mimetype, content, filename=None, url=None,
                   annotations=None, force_source=False):
            """Render an XHTML preview of the given `content`.
    
            `content` is the same as an `IHTMLPreviewRenderer.render`'s
    Severity: Minor
    Found in trac/mimeview/api.py - About 7 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 convert_content has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
    Open

        def convert_content(self, req, mimetype, content, key, filename=None,
                            url=None, iterable=False):
            """Convert the given content to the target MIME type represented by
            `key`, which can be either a MIME type or a key. Returns a tuple of
            (content, output_mime_type, extension)."""
    Severity: Minor
    Found in trac/mimeview/api.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 get_mimetype has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
    Open

    def get_mimetype(filename, content=None, mime_map=MIME_MAP,
                     mime_map_patterns={}):
        """Guess the most probable MIME type of a file with the given name.
    
        `filename` is either a filename (the lookup will then use the suffix)
    Severity: Minor
    Found in trac/mimeview/api.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 mime_map has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
    Open

        def mime_map(self):
            # Extend default extension to MIME type mappings with configured ones
            if not self._mime_map:
                self._mime_map = MIME_MAP.copy()
                # augment mime_map from `IHTMLPreviewRenderer`s
    Severity: Minor
    Found in trac/mimeview/api.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 _render_source has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

        def _render_source(self, context, lines, annotations):
            from trac.web.chrome import add_warning
            annotators, labels, titles = {}, {}, {}
            for annotator in self.annotators:
                atype, alabel, atitle = annotator.get_annotation_type()
    Severity: Minor
    Found in trac/mimeview/api.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 send_converted has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        def send_converted(self, req, in_type, content, selector, filename='file'):
            """Helper method for converting `content` and sending it directly.
    
            `selector` can be either a key or a MIME Type."""
            from trac.web.chrome import Chrome
    Severity: Minor
    Found in trac/mimeview/api.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 preview_data has 8 arguments (exceeds 4 allowed). Consider refactoring.
    Open

        def preview_data(self, context, content, length, mimetype, filename,
    Severity: Major
    Found in trac/mimeview/api.py - About 1 hr to fix

      Function mime_map_patterns has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

          def mime_map_patterns(self):
              if not self._mime_map_patterns:
                  self._mime_map_patterns = {}
                  for mapping in self.config['mimeviewer'] \
                          .getlist('mime_map_patterns'):
      Severity: Minor
      Found in trac/mimeview/api.py - About 55 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 render has 7 arguments (exceeds 4 allowed). Consider refactoring.
      Open

          def render(self, context, mimetype, content, filename=None, url=None,
      Severity: Major
      Found in trac/mimeview/api.py - About 50 mins to fix

        Function convert_content has 7 arguments (exceeds 4 allowed). Consider refactoring.
        Open

            def convert_content(self, req, mimetype, content, key, filename=None,
        Severity: Major
        Found in trac/mimeview/api.py - About 50 mins to fix

          Avoid deeply nested control flow statements.
          Open

                                  for keyword in kwds:
                                      self._mime_map[keyword] = mimetype
                      # augment/override mime_map from trac.ini
                      for mapping in self.config['mimeviewer'].getlist('mime_map'):
          Severity: Major
          Found in trac/mimeview/api.py - About 45 mins to fix

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

                def get_supported_conversions(self, mimetype):
                    """Return a list of target MIME types as instances of the `namedtuple`
                    `MimeConversion`. Output is ordered from best to worst quality.
            
                    The `MimeConversion` `namedtuple` has fields: key, name, extension,
            Severity: Minor
            Found in trac/mimeview/api.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 annotate_row has 5 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                def annotate_row(self, context, row, lineno, line, data):
            Severity: Minor
            Found in trac/mimeview/api.py - About 35 mins to fix

              Function render has 5 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                  def render(self, context, mimetype, content, filename=None, url=None):
              Severity: Minor
              Found in trac/mimeview/api.py - About 35 mins to fix

                Function render has 5 arguments (exceeds 4 allowed). Consider refactoring.
                Open

                    def render(self, context, mimetype, content, filename=None, url=None):
                Severity: Minor
                Found in trac/mimeview/api.py - About 35 mins to fix

                  Function send_converted has 5 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                      def send_converted(self, req, in_type, content, selector, filename='file'):
                  Severity: Minor
                  Found in trac/mimeview/api.py - About 35 mins to fix

                    Function render has 5 arguments (exceeds 4 allowed). Consider refactoring.
                    Open

                        def render(self, context, mimetype, content, filename=None, url=None):
                    Severity: Minor
                    Found in trac/mimeview/api.py - About 35 mins to fix

                      Avoid too many return statements within this function.
                      Open

                                          return self._render_source(context, result, annotations)
                      Severity: Major
                      Found in trac/mimeview/api.py - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                                return mimetype
                        Severity: Major
                        Found in trac/mimeview/api.py - About 30 mins to fix

                          Avoid too many return statements within this function.
                          Open

                                              return tag.div(class_='code')(tag.pre(result))
                          Severity: Major
                          Found in trac/mimeview/api.py - About 30 mins to fix

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

                                def is_binary(self, mimetype=None, filename=None, content=None):
                                    """Check if a file must be considered as binary."""
                                    if not mimetype and filename:
                                        mimetype = self.get_mimetype(filename, content)
                                    if mimetype:
                            Severity: Minor
                            Found in trac/mimeview/api.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

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

                                def get_charset(self, content='', mimetype=None):
                                    """Infer the character encoding from the `content` or the `mimetype`.
                            
                                    `content` is either a `bytes` or a `str` object.
                            
                            
                            Severity: Minor
                            Found in trac/mimeview/api.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

                            There are no issues that match your filters.

                            Category
                            Status