[POS-commit] r4038 - in stoq/trunk: bin

Johan Dahlin jdahlin at async.com.br
Mon Jul 31 15:34:53 BRT 2006


Author: jdahlin
Date: Mon Jul 31 15:34:53 2006
New Revision: 4038

Modified:
   stoq/trunk/bin/stoqdbadmin
   stoq/trunk/stoq/lib/configparser.py

Log:
Add a configure option which creates a stoq.conf file

Modified: stoq/trunk/bin/stoqdbadmin
==============================================================================
--- stoq/trunk/bin/stoqdbadmin	(original)
+++ stoq/trunk/bin/stoqdbadmin	Mon Jul 31 15:34:53 2006
@@ -31,11 +31,12 @@
     def __init__(self, prog_name):
         self.prog_name = prog_name
 
-    def _read_config(self, options):
+    def _read_config(self, options, create=False):
         from stoq.lib.configparser import StoqConfig
         from stoq.lib.startup import setup
         config = StoqConfig(options.filename)
-        config.check_connection()
+        if create:
+            config.create()
         setup(config, options)
         return config
 
@@ -94,6 +95,16 @@
                          action='store_true',
                          dest='create_examples')
 
+    def cmd_configure(self, options):
+        if not options.dbname:
+            print 'dbname missing'
+            return 1
+        if not options.address:
+            print 'address missing'
+            return 1
+        config = self._read_config(options, create=True)
+        config.flush()
+
 def main(args):
     pname = args[0]
     args = args[1:]

Modified: stoq/trunk/stoq/lib/configparser.py
==============================================================================
--- stoq/trunk/stoq/lib/configparser.py	(original)
+++ stoq/trunk/stoq/lib/configparser.py	Mon Jul 31 15:34:53 2006
@@ -185,24 +185,46 @@
                                    (self._filename, name))
 
     def _get_rdbms_name(self):
+        if not self._has_option('rdbms', section='Database'):
+            return 'postgres'
         return self._get_option('rdbms', section='Database')
 
     def _get_address(self):
         return self._get_option('address', section='Database')
 
     def _get_port(self):
+        if not self._has_option('port', section='Database'):
+            return '5432'
         return self._get_option('port', section='Database')
 
     def _get_dbname(self):
+        if not self._has_option('dbname', section='Database'):
+            return self._get_username()
         return self._get_option('dbname', section='Database')
 
     def _get_username(self):
+        if not self._has_option('dbusername', section='Database'):
+            import pwd
+            return pwd.getpwuid(os.getuid())[0]
         return self._get_option('dbusername', section='Database')
 
     #
     # Public API
     #
 
+    def create(self):
+        config_dir = self.get_config_directory()
+        if not os.path.exists(config_dir):
+            os.mkdir(config_dir)
+        self._filename = os.path.join(
+            config_dir, StoqConfig.domain + '.conf')
+
+        if not self._config.has_section('General'):
+            self._config.add_section('General')
+
+        if not self._config.has_section('Database'):
+            self._config.add_section('Database')
+
     def flush(self):
         """
         Writes the current configuration data to disk.
@@ -277,6 +299,10 @@
         @rtype: id
         """
 
+        # XXX: Remove
+        if not self._has_option('station_id', section='General'):
+            return 0
+
         station_id = self._get_option('station_id', section='General')
         try:
             return int(station_id)
@@ -320,7 +346,7 @@
         """
 
         if options.address:
-            self._config.set('Database', 'address', options.hostname)
+            self._config.set('Database', 'address', options.address)
         if options.port:
             self._config.set('Database', 'port', options.port)
         if options.dbname:


More information about the POS-commit mailing list