[Osaas-commits] r4 - in trunk: . server/osaas/http server/test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Aug 27 19:26:12 CEST 2007


Author: bh
Date: 2007-08-27 19:26:12 +0200 (Mon, 27 Aug 2007)
New Revision: 4

Modified:
   trunk/ChangeLog
   trunk/server/osaas/http/run.py
   trunk/server/test/test_config.py
   trunk/server/test/test_run.py
Log:
Rework where the default options are defined

* server/osaas/http/run.py (ProgramWithOptions.create_option_parser)
(ServerProgram.create_option_parser): Move most of the option
definitions from ProgramWithOptions to the new
create_option_parser method of ServerProgram where they make more
sense.  Only the config-file option remains in ProgramWithOptions
(ServerProgram.default_port): New class variable to specify the
default port.  The default port no longer needs to be passed to
various methods.
(ProgramWithOptions.parse_options): Adapt to create_option_parser
changes.
(ServerProgram.main): Adapt to parse_options changes.

* server/test/test_config.py (TestConfigParsing.test)
(TestConfigParsing_MissingEntries.test)
(TestConfigParsing_AmbiguousEntries.test): Adapt to parse_options
changes.

* server/test/test_run.py (TestProgramWithOptions.test_default_options)
(TestProgramWithOptions.test_options)
(TestProgramWithOptions.test_adding_options)
(TestProgramWithOptions.test_adding_options)
(TestProgramWithOptions.test_read_config_file_method)
(TestProgramWithOptions.test_optparse_option_class): Adapt to
changes in server/osaas/http/run.py


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-08-27 17:15:38 UTC (rev 3)
+++ trunk/ChangeLog	2007-08-27 17:26:12 UTC (rev 4)
@@ -1,3 +1,32 @@
 2007-08-27  Bernhard Herzog  <bh at intevation.de>
 
+	Rework where the default options are defined
+
+	* server/osaas/http/run.py (ProgramWithOptions.create_option_parser)
+	(ServerProgram.create_option_parser): Move most of the option
+	definitions from ProgramWithOptions to the new
+	create_option_parser method of ServerProgram where they make more
+	sense.  Only the config-file option remains in ProgramWithOptions
+	(ServerProgram.default_port): New class variable to specify the
+	default port.  The default port no longer needs to be passed to
+	various methods.
+	(ProgramWithOptions.parse_options): Adapt to create_option_parser
+	changes.
+	(ServerProgram.main): Adapt to parse_options changes.
+
+	* server/test/test_config.py (TestConfigParsing.test)
+	(TestConfigParsing_MissingEntries.test)
+	(TestConfigParsing_AmbiguousEntries.test): Adapt to parse_options
+	changes.
+
+	* server/test/test_run.py (TestProgramWithOptions.test_default_options)
+	(TestProgramWithOptions.test_options)
+	(TestProgramWithOptions.test_adding_options)
+	(TestProgramWithOptions.test_adding_options)
+	(TestProgramWithOptions.test_read_config_file_method)
+	(TestProgramWithOptions.test_optparse_option_class): Adapt to
+	changes in server/osaas/http/run.py
+
+2007-08-27  Bernhard Herzog  <bh at intevation.de>
+
 	* ChangeLog: started ChangeLog

Modified: trunk/server/osaas/http/run.py
===================================================================
--- trunk/server/osaas/http/run.py	2007-08-27 17:15:38 UTC (rev 3)
+++ trunk/server/osaas/http/run.py	2007-08-27 17:26:12 UTC (rev 4)
@@ -39,15 +39,11 @@
 
     optparse_option_class = None
 
-    def create_option_parser(self, default_port):
+    def create_option_parser(self):
         kw = {}
         if self.optparse_option_class is not None:
             kw["option_class"] = self.optparse_option_class
         parser = optparse.OptionParser(**kw)
-        parser.set_defaults(port=default_port)
-        parser.add_option("--port", type="int")
-        parser.add_option("--access-log")
-        parser.add_option("--error-log")
         parser.add_option("--config-file")
         return parser
 
@@ -65,8 +61,8 @@
         name of a configuration file on the command line.
         """
 
-    def parse_options(self, args, default_port):
-        parser = self.create_option_parser(default_port=default_port)
+    def parse_options(self, args):
+        parser = self.create_option_parser()
 
         default_opts = parser.get_default_values()
         configfile_opts = optparse.Values()
@@ -80,9 +76,19 @@
 
     """Main program of the server"""
 
+    default_port = 8989
+
     def __init__(self):
         self.server = None
 
+    def create_option_parser(self, **kw):
+        parser = ProgramWithOptions.create_option_parser(self, **kw)
+        parser.set_defaults(port=self.default_port)
+        parser.add_option("--port", type="int")
+        parser.add_option("--access-log")
+        parser.add_option("--error-log")
+        return parser
+
     def setup_signals(self):
         """Set handle_term_signal as signal handler for SIGTERM"""
         signal.signal(signal.SIGTERM, self.handle_term_signal)
@@ -120,7 +126,7 @@
 
     def main(self, server_class=httpserver.HTTPServer,
              default_port=8989, bind_host="127.0.0.1", **kw):
-        opts, rest = self.parse_options(sys.argv[1:], default_port=default_port)
+        opts, rest = self.parse_options(sys.argv[1:])
 
         self.setup_signals()
         self.setup_logging(opts)

Modified: trunk/server/test/test_config.py
===================================================================
--- trunk/server/test/test_config.py	2007-08-27 17:15:38 UTC (rev 3)
+++ trunk/server/test/test_config.py	2007-08-27 17:26:12 UTC (rev 4)
@@ -40,8 +40,7 @@
 
     def test(self):
         prog = OSAASServerProgram()
-        opts, rest = prog.parse_options(["--config-file=%s" % self.filename],
-                                        default_port=9090)
+        opts, rest = prog.parse_options(["--config-file=%s" % self.filename])
         self.assertEquals(opts.port, 6688)
         self.assertEquals(opts.access_log, "/var/log/osaas-access.log")
         self.assertEquals(opts.error_log, "/var/log/osaas-error.log")
@@ -58,9 +57,8 @@
 
     def test(self):
         prog = OSAASServerProgram()
-        opts, rest = prog.parse_options(["--config-file=%s" % self.filename],
-                                        default_port=9090)
-        self.assertEquals(opts.port, 9090)
+        opts, rest = prog.parse_options(["--config-file=%s" % self.filename])
+        self.assertEquals(opts.port, 8989)
         self.assertEquals(opts.access_log, None)
         self.assertEquals(opts.error_log, "/var/log/osaas-error.log")
 
@@ -79,5 +77,4 @@
         prog = OSAASServerProgram()
         self.assertRaises(ConfigXMLPathError,
                           prog.parse_options,
-                          ["--config-file=%s" % self.filename],
-                          default_port=9090)
+                          ["--config-file=%s" % self.filename])

Modified: trunk/server/test/test_run.py
===================================================================
--- trunk/server/test/test_run.py	2007-08-27 17:15:38 UTC (rev 3)
+++ trunk/server/test/test_run.py	2007-08-27 17:26:12 UTC (rev 4)
@@ -19,19 +19,13 @@
 
     def test_default_options(self):
         prog = ProgramWithOptions()
-        opts, rest = prog.parse_options([], default_port=12345)
-        self.assertEquals(opts.port, 12345)
-        self.assertEquals(opts.error_log, None)
-        self.assertEquals(opts.access_log, None)
+        opts, rest = prog.parse_options([])
+        self.assertEquals(opts.config_file, None)
 
     def test_options(self):
         prog = ProgramWithOptions()
-        opts, rest = prog.parse_options(["--port=4321",
-                                         "--error-log=/var/log/testerror.log"],
-                                        default_port=12345)
-        self.assertEquals(opts.port, 4321)
-        self.assertEquals(opts.error_log, "/var/log/testerror.log")
-        self.assertEquals(opts.access_log, None)
+        opts, rest = prog.parse_options(["--config-file=/etc/myconfig"])
+        self.assertEquals(opts.config_file, "/etc/myconfig")
 
     def test_adding_options(self):
         class Program(ProgramWithOptions):
@@ -43,41 +37,46 @@
 
         # defaults
         prog = Program()
-        opts, rest = prog.parse_options([], default_port=12345)
-        self.assertEquals(opts.port, 12345)
-        self.assertEquals(opts.error_log, None)
-        self.assertEquals(opts.access_log, None)
+        opts, rest = prog.parse_options([])
+        self.assertEquals(opts.config_file, None)
         self.assertEquals(opts.num_threads, 5)
 
         # some options supplied
         prog = Program()
-        opts, rest = prog.parse_options(["--port=6543", "--num-threads=10"],
-                                        default_port=12345)
-        self.assertEquals(opts.port, 6543)
-        self.assertEquals(opts.error_log, None)
-        self.assertEquals(opts.access_log, None)
+        opts, rest = prog.parse_options(["--config-file=/etc/config2",
+                                         "--num-threads=10"])
+        self.assertEquals(opts.config_file, "/etc/config2")
         self.assertEquals(opts.num_threads, 10)
 
     def test_read_config_file_method(self):
         class Program(ProgramWithOptions):
+            def create_option_parser(self, **kw):
+                parser = ProgramWithOptions.create_option_parser(self, **kw)
+                parser.add_option("--num-threads", type="int")
+                parser.add_option("--error-log")
+                parser.add_option("--port", type="int")
+                parser.set_defaults(num_threads=5, error_log="/var/log/error",
+                                    port=7777)
+                return parser
             def read_config_file(self, opts):
+                opts.set_file_option("num_threads", 10)
                 opts.set_file_option("port", 9988)
-                opts.set_file_option("access_log", "/home/test/access.log")
 
         prog = Program()
-        opts, rest = prog.parse_options(["--port=8877"], default_port=12345)
+        opts, rest = prog.parse_options(["--port=8899"])
 
         # The port has been given on the command line, so that value
         # should win:
-        self.assertEquals(opts.port, 8877)
+        self.assertEquals(opts.port, 8899)
 
-        # The access_log has been set in read_config_file but not on the
-        # command line, so the value from read_config_file wins:
-        self.assertEquals(opts.access_log, "/home/test/access.log")
+        # The num_threads option has been set in read_config_file but
+        # not on the command line, so the value from read_config_file
+        # wins:
+        self.assertEquals(opts.num_threads, 10)
 
         # The error_log has only been set in the defaults (with the
-        # default default value of None).
-        self.assertEquals(opts.error_log, None)
+        # default default value of "/var/log/error").
+        self.assertEquals(opts.error_log, "/var/log/error")
 
     def test_optparse_option_class(self):
         class MyOption(optparse.Option):
@@ -95,7 +94,7 @@
 
         prog = Program()
         try:
-            prog.parse_options([], default_port=12345)
+            prog.parse_options([])
         except:
             self.fail("Unexpected exception when parsing options:\n%s"
                       % "".join(traceback.format_exception(*sys.exc_info())))



More information about the Osaas-commits mailing list