Commits

Kevin Qiu committed 9e6af83

Added the ability to specify reporter class name as report's 'output-format'.

This allows a custom reporter to be used to format reports.
Backwards-compatibility is maintained, so a user can still specify
the named reporters such as 'text' or 'parseable'.

Comments (0)

Files changed (3)

     * #4685: check for consistency of a module's __all__ variable,
       introducing new E0603 message
 
+    * #105337: allow custom reporter in output-format (patch by Kevin Jing Qiu)
+
     * #100654: fix grammatical error for W0332 message (using 'l' as
       long int identifier)
 
 from logilab.common.configuration import UnsupportedAction, OptionsManagerMixIn
 from logilab.common.optik_ext import check_csv
 from logilab.common.modutils import load_module_from_name
+from logilab.common.modutils import get_module_part
 from logilab.common.interface import implements
 from logilab.common.textutils import splitstrip
 from logilab.common.ureports import Table, Text, Section
 python modules names) to load, usually to register additional checkers.'}),
 
                 ('output-format',
-                 {'default': 'text', 'type': 'choice', 'metavar' : '<format>',
-                  'choices': REPORTER_OPT_MAP.keys(),
+                 {'default': 'text', 'type': 'string', 'metavar' : '<format>',
                   'short': 'f',
                   'group': 'Reports',
                   'help' : 'Set the output format. Available formats are text,\
                 else :
                     meth(value)
         elif optname == 'output-format':
-            self.set_reporter(REPORTER_OPT_MAP[value.lower()]())
+            if value.lower() in REPORTER_OPT_MAP:
+                self.set_reporter(REPORTER_OPT_MAP[value.lower()]())
+            else:
+                module = load_module_from_name(get_module_part(value))
+                class_name = value.split('.')[-1]
+                reporter_class = getattr(module, class_name)
+                self.set_reporter(reporter_class())
+
         try:
             BaseRawChecker.set_option(self, optname, value, action, optdict)
         except UnsupportedAction:

test/unittest_lint.py

         self.linter.enable('RP0001')
         self.assertEqual(self.linter.report_is_enabled('RP0001'), True)
 
+    def test_report_output_format_aliased(self):
+        self.linter.set_option('output-format', 'text')
+        self.assertEqual(self.linter.reporter.__class__.__name__, 'TextReporter')
+
+    def test_report_output_format_custom(self):
+        this_module = sys.modules[__name__]
+        class TestReporter(object):
+            pass
+        this_module.TestReporter = TestReporter
+        class_name = ".".join((this_module.__name__, 'TestReporter'))
+        self.linter.set_option('output-format', class_name)
+        self.assertEqual(self.linter.reporter.__class__.__name__, 'TestReporter')
+
     def test_set_option_1(self):
         linter = self.linter
         linter.set_option('disable', 'C0111,W0142')