edgewall/trac

View on GitHub
trac/attachment.py

Summary

Maintainability
F
1 wk
Test Coverage

File attachment.py has 928 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2023 Edgewall Software
# Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com>
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
Severity: Major
Found in trac/attachment.py - About 2 days to fix

    Function process_request has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
    Open

        def process_request(self, req):
            parent_realm = req.args.get('realm')
            path = req.args.get('path')
    
            if not parent_realm or not path:
    Severity: Minor
    Found in trac/attachment.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 _move has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
    Open

        def _move(self, new_realm=None, new_id=None, new_filename=None,
                  new_parent_must_exist=False):
            """Move the attachment, changing one or more of its parent realm,
            parent id and filename.
    
    
    Severity: Minor
    Found in trac/attachment.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

    AttachmentModule has 24 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class AttachmentModule(Component):
    
        implements(IRequestHandler, INavigationContributor, IWikiSyntaxProvider,
                   IResourceManager)
    
    
    Severity: Minor
    Found in trac/attachment.py - About 2 hrs to fix

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

          def _do_save(self, req, attachment):
              req.perm(attachment.resource).require('ATTACHMENT_CREATE')
              parent_resource = attachment.resource.parent
      
              if 'cancel' in req.args:
      Severity: Minor
      Found in trac/attachment.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_permission has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          def check_permission(self, action, username, resource, perm):
              perm_map = self._perm_maps.get(action)
              if not perm_map or not resource or resource.realm != self.realm:
                  return
      
      
      Severity: Minor
      Found in trac/attachment.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 _format_link has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

          def _format_link(self, formatter, ns, target, label):
              link, params, fragment = formatter.split_link(target)
              ids = link.split(':', 2)
              attachment = None
              if len(ids) == 3:
      Severity: Minor
      Found in trac/attachment.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 _render_view has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

          def _render_view(self, req, attachment):
              req.perm(attachment.resource).require('ATTACHMENT_VIEW')
              can_delete = 'ATTACHMENT_DELETE' in req.perm(attachment.resource)
              req.check_modified(attachment.date, str(can_delete))
      
      
      Severity: Minor
      Found in trac/attachment.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_unique_file has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

          def _create_unique_file(self, dir, filename):
              parts = os.path.splitext(filename)
              flags = os.O_CREAT + os.O_WRONLY + os.O_EXCL
              if hasattr(os, 'O_BINARY'):
                  flags += os.O_BINARY
      Severity: Minor
      Found in trac/attachment.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 _download_as_zip has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

          def _download_as_zip(self, req, parent, attachments=None):
              if attachments is None:
                  attachments = self.viewable_attachments(web_context(req, parent))
              total_size = sum(attachment.size for attachment in attachments)
              if total_size > self.max_zip_size:
      Severity: Minor
      Found in trac/attachment.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 _from_database has 5 arguments (exceeds 4 allowed). Consider refactoring.
      Open

          def _from_database(self, filename, description, size, time, author):
      Severity: Minor
      Found in trac/attachment.py - About 35 mins to fix

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

            def reparent_all(cls, env, parent_realm, parent_id, new_realm, new_id):
        Severity: Minor
        Found in trac/attachment.py - About 35 mins to fix

          Function insert has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

              def insert(self, filename, fileobj, size, t=None):
                  """Create a new Attachment record and save the file content.
                  """
                  self.size = int(size) if size else 0
                  self.filename = None
          Severity: Minor
          Found in trac/attachment.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 _("Attachments of %(parent)s",
          Severity: Major
          Found in trac/attachment.py - About 30 mins to fix

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

                        try:
                            return filename, os.fdopen(os.open(path, flags, 0o666), 'wb')
                        except OSError as e:
                            if e.errno != errno.EEXIST:
                                raise
            Severity: Major
            Found in trac/attachment.py and 1 other location - About 6 hrs to fix
            trac/util/__init__.py on lines 310..320

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

            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

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

                    def write_partial(fileobj, start):
                        end = fileobj.tell()
                        fileobj.seek(start, 0)
                        remaining = end - start
                        while remaining > 0:
            Severity: Major
            Found in trac/attachment.py and 1 other location - About 5 hrs to fix
            trac/versioncontrol/web_ui/util.py on lines 216..225

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

            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 manipulator in self.manipulators:
                        for field, message in manipulator.validate_attachment(req,
                                                                              attachment):
                            valid = False
                            if field:
            Severity: Major
            Found in trac/attachment.py and 1 other location - About 4 hrs to fix
            trac/wiki/web_ui.py on lines 210..219

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

            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

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

                    if attachment_dir:
                        try:
                            os.rmdir(attachment_dir)
                        except OSError as e:
                            env.log.error("Can't delete attachment directory %s: %s",
            Severity: Major
            Found in trac/attachment.py and 1 other location - About 1 hr to fix
            trac/attachment.py on lines 861..867

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

            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

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

                    if attachment_dir:
                        try:
                            os.rmdir(attachment_dir)
                        except OSError as e:
                            env.log.error("Can't delete attachment directory %s: %s",
            Severity: Major
            Found in trac/attachment.py and 1 other location - About 1 hr to fix
            trac/attachment.py on lines 844..850

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

            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

                    if not resource_exists(self.env, parent):
                        raise ResourceNotFound(
                            _("Parent resource %(parent)s doesn't exist",
                              parent=get_resource_name(self.env, parent)))
            Severity: Major
            Found in trac/attachment.py and 2 other locations - About 55 mins to fix
            trac/attachment.py on lines 790..793
            trac/attachment.py on lines 904..907

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

            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 ids[0] in known_realms:
                                attachment = Resource(ids[0], ids[1]).child(self.realm,
                                                                            ids[2])
            Severity: Minor
            Found in trac/attachment.py and 1 other location - About 55 mins to fix
            trac/attachment.py on lines 605..607

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

            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

                        if not resource_exists(self.env, new_parent_resource):
                            raise ResourceNotFound(
                                _("%(target)s doesn't exist, can't move attachment",
                                  target=get_resource_name(self.env, new_parent_resource)))
            Severity: Major
            Found in trac/attachment.py and 2 other locations - About 55 mins to fix
            trac/attachment.py on lines 195..198
            trac/attachment.py on lines 790..793

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

            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 ids[1] in known_realms:
                            attachment = Resource(ids[1], ids[2]).child(self.realm,
                                                                        ids[0])
            Severity: Minor
            Found in trac/attachment.py and 1 other location - About 55 mins to fix
            trac/attachment.py on lines 609..611

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

            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

                    if not resource_exists(self.env, parent_resource):
                        raise ResourceNotFound(
                            _("%(parent)s doesn't exist, can't create attachment",
                              parent=get_resource_name(self.env, parent_resource)))
            Severity: Major
            Found in trac/attachment.py and 2 other locations - About 55 mins to fix
            trac/attachment.py on lines 195..198
            trac/attachment.py on lines 904..907

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

            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 action == 'new':
                            data = self._do_save(req, attachment)
                        elif action == 'delete':
                            self._do_delete(req, attachment)
                        else:
            Severity: Minor
            Found in trac/attachment.py and 1 other location - About 30 mins to fix
            trac/ticket/roadmap.py on lines 763..768

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

            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