[rtems-source-builder commit] sb: Introduce formatter classes

Sebastian Huber sebh at rtems.org
Thu Dec 11 07:10:22 UTC 2014


Module:    rtems-source-builder
Branch:    master
Commit:    5d565849b33e54c087699da1d949923e080cab2d
Changeset: http://git.rtems.org/rtems-source-builder/commit/?id=5d565849b33e54c087699da1d949923e080cab2d

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Thu Dec  4 06:22:15 2014 +0100

sb: Introduce formatter classes

Use inheritance instead of ifs.

---

 source-builder/sb/reports.py | 56 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 12 deletions(-)

diff --git a/source-builder/sb/reports.py b/source-builder/sb/reports.py
index ae0cd5d..40c8beb 100644
--- a/source-builder/sb/reports.py
+++ b/source-builder/sb/reports.py
@@ -49,6 +49,41 @@ except:
     print 'error: unknown application load error'
     sys.exit(1)
 
+class formatter(object):
+    def format(self):
+        raise error.general('internal error: formatter.format() not implemented')
+
+    def ext(self):
+        raise error.general('internal error: formatter.ext() not implemented')
+
+class asciidoc_formatter(formatter):
+    def format(self):
+        return 'asciidoc'
+
+    def ext(self):
+        return '.txt'
+
+class ini_formatter(formatter):
+    def format(self):
+        return 'ini'
+
+    def ext(self):
+        return '.ini'
+
+class html_formatter(formatter):
+    def format(self):
+        return 'html'
+
+    def ext(self):
+        return '.html'
+
+class text_formatter(formatter):
+    def format(self):
+        return 'text'
+
+    def ext(self):
+        return '.txt'
+
 def _tree_name(path_):
     return path.splitext(path.basename(path_))[0]
 
@@ -65,8 +100,9 @@ class report:
 
     line_len = 78
 
-    def __init__(self, format, _configs, opts, macros = None):
-        self.format = format
+    def __init__(self, formatter, _configs, opts, macros = None):
+        self.formatter = formatter
+        self.format = formatter.format()
         self.configs = _configs
         self.opts = opts
         if macros is None:
@@ -581,8 +617,7 @@ def run(args):
             output = opts.get_arg('--output')
             if output is not None:
                 output = output[1]
-            format = 'text'
-            ext = '.txt'
+            formatter = text_formatter()
             format_opt = opts.get_arg('--format')
             if format_opt:
                 if len(format_opt) != 2:
@@ -590,20 +625,17 @@ def run(args):
                 if format_opt[1] == 'text':
                     pass
                 elif format_opt[1] == 'asciidoc':
-                    format = 'asciidoc'
-                    ext = '.txt'
+                    formatter = asciidoc_formatter()
                 elif format_opt[1] == 'html':
-                    format = 'html'
-                    ext = '.html'
+                    formatter = html_formatter()
                 elif format_opt[1] == 'ini':
-                    format = 'ini'
-                    ext = '.ini'
+                    formatter = ini_formatter()
                 else:
                     raise error.general('invalid format: %s' % (format_opt[1]))
-            r = report(format, configs, opts)
+            r = report(formatter, configs, opts)
             for _config in opts.params():
                 if output is None:
-                    outname = path.splitext(_config)[0] + ext
+                    outname = path.splitext(_config)[0] + formatter.ext()
                     outname = outname.replace('/', '-')
                 else:
                     outname = output



More information about the vc mailing list