1. Olemis Lang
  2. bloodhound-trac

Commits

g...@13f79535-47bb-0310-9956-ffa450edef68  committed d1204a8 Draft

provide functions to determine if a component is overridden by a subclass - uses this to determine whether a ReportModule is active

  • Participants
  • Parent commits 54b0f4e
  • Branches default

Comments (0)

Files changed (3)

File trac/ticket/query.py

View file
  • Ignore whitespace
 from trac.util.presentation import Paginator
 from trac.util.text import empty, shorten_line, quote_query_string
 from trac.util.translation import _, tag_, cleandoc_
+from trac.util.introspection import get_enabled_component_subclass
 from trac.web import arg_list_to_args, parse_arg_list, IRequestHandler
 from trac.web.href import Href
 from trac.web.chrome import (INavigationContributor, Chrome,
     def get_navigation_items(self, req):
         from trac.ticket.report import ReportModule
         if 'TICKET_VIEW' in req.perm and \
-                not self.env.is_component_enabled(ReportModule):
+                get_enabled_component_subclass(self.env, ReportModule) is None:
             yield ('mainnav', 'tickets',
                    tag.a(_('View Tickets'), href=req.href.query()))
 

File trac/util/introspection.py

View file
  • Ignore whitespace
+
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+"""Helper functions for introspection functionality"""
+
+def subclasses(cls):
+    """recursively get subclasses of a class"""
+    for sub in cls.__subclasses__():
+        for subsub in subclasses(sub):
+            yield subsub
+        yield sub
+
+def get_enabled_component_subclass(env, cls):
+    """if the cls is not enabled, attempts to find a subclass which is"""
+    if env.is_component_enabled(cls):
+        return cls
+    for subcls in subclasses(cls):
+        if env.is_component_enabled(subcls):
+            return subcls
+    return None

File trac/util/tests/introspection.py

View file
  • Ignore whitespace
+
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+import unittest
+from trac.util.subclasses import subclasses
+
+class BaseClass(object):
+    """Common base class for tests"""
+class SubClass(BaseClass):
+    """Common sub class of BaseClass for tests"""
+
+class SubClassTestCase(unittest.TestCase):
+    def test_discover_subclass(self):
+        subs = list(subclasses(BaseClass))
+        self.assertEqual(len(subs), 1)
+        self.assertIs(subs[0], SubClass)
+
+    def test_discover_subsubclass(self):
+        class SubSubClass(SubClass):
+            """Sub class of SubClass"""
+        subs = list(subclasses(BaseClass))
+        self.assertEqual(len(subs), 2)
+        self.assertIn(SubClass, subs)
+        self.assertIn(SubSubClass, subs)
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(SubClassTestCase, 'test'))
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='suite')