changing config.py for work with configuration file
author“Alexandr <“popov2al@gmail.com”>
Sun, 12 Apr 2009 09:48:06 +0000 (13:48 +0400)
committer“Alexandr <“popov2al@gmail.com”>
Sun, 12 Apr 2009 09:48:06 +0000 (13:48 +0400)
src/config.py

index 489a6ce..a7de788 100644 (file)
@@ -3,11 +3,13 @@
 # vim: sw=4 ts=4 expandtab ai
 
 import gtk
+from configobj import ConfigObj
+from validate import Validator
 
 class Config_Control(object):
 
     def __init__(self):
-        self.config = Config_Abstraction('findit.cfg')
+        self.config = Config_Abstraction('findit.ini', 'config_spec.ini')
         #conf_ui = Config_Presentation()
     
     def get(self, param):
@@ -15,8 +17,8 @@ class Config_Control(object):
 
 class Config_Abstraction(object):
 
-    def __init__(self, config_file):
-        self.load_config(config_file)
+    def __init__(self, config_file, config_spec_file):
+        self.load_config(config_file, config_spec_file)
 
     def get_val(self, param):
         return self.conf[param]
@@ -24,18 +26,17 @@ class Config_Abstraction(object):
     def set_val(self, param, value):
         self.conf[param] = value
 
-    def load_config(self, config_file):
-        # Здесь планируется загрузка конфига из файла - пока так присваивается
-        self.conf = {}
-        self.conf['default_start_dir'] = '/home/alex/Desktop/python/test/'
-        self.conf['default_count'] = 10
-        self.conf['ignore_dir_list'] = ['/dev', '/proc', '/sys', '/mnt']
-        self.conf['window_width'] = 575
-        self.conf['window_height'] = 345
-
-    def save_config(self, config_file):
-        pass
+    def load_config(self, config_file, config_spec_file):
+        self.conf = ConfigObj(config_file, configspec=config_spec_file)
+        val = Validator()
+        test = self.conf.validate(val)
+        if not test:
+            print 'Failed Load Config File'
 
+    def save_config(self, config_file=None):
+        if config_file:
+            self.conf.filename = config_file
+        self.conf.write()
 
 class Config_Presentation(gtk.Window):
     def __init__(self):
@@ -46,3 +47,11 @@ class Config_Presentation(gtk.Window):
     def run(self):
         self.show_all()
         gtk.main()
+
+
+if __name__ == '__main__':
+    cfg = Config_Control()
+    #cfg.config.set_val('default_pkg_count',15)
+    print cfg.config.conf
+    print cfg.get('window_width'), type(cfg.get('window_width'))
+    cfg.config.save_config()