Commits

Kevin Qiu committed 1fffc9e Draft

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 (2)

 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:
+                components = value.split('.')
+                reporter_class = __import__(components[0])
+                for comp in components[1:]:
+                    reporter_class = getattr(reporter_class, comp)
+                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')