Remember active section in config.

portnov [2008-08-04 18:19:55]
Remember active section in config.
Filename
qwerty.py
diff --git a/qwerty.py b/qwerty.py
index a3d7624..ce99dcd 100755
--- a/qwerty.py
+++ b/qwerty.py
@@ -142,6 +142,9 @@ class Key(object):

 def parse_config():
     global sections
+    global defsection
+
+    defsection = 0

     def parse_line(key,line):
         key = key + ": "
@@ -151,6 +154,12 @@ def parse_config():
     context = GLOBAL
     for line in open(config_file):
         line = line.strip()
+
+        v = parse_line('DefaultSection', line)
+        if v:
+            defsection = int(v)
+            continue
+
         v = parse_line('Section',line)
         if v:
             S = Section(v)
@@ -210,6 +219,34 @@ def parse_config():
                 raise SyntaxError, "Unexpected 'Command' key in config"
             continue

+def save_config():
+    def write(key,value):
+        cf.write(key + ': ' + value + '\n')
+
+    def writeln():
+        cf.write('\n')
+
+    global sections
+    global defsection
+
+    cf = open(config_file, 'w')
+
+    write('DefaultSection', str(defsection))
+    for S in sections:
+        write('Section', S.title)
+        write('Icon', S.icon)
+        writeln()
+        for K in S.keys.values():
+            write('Key', K.key)
+            write('Icon', K.icon)
+            write('Title', K.title)
+            if hasattr(K, 'cls'):
+                write('Class', K.cls)
+            write('Command', K.command)
+            writeln()
+    cf.close()
+
+
 class GUI(object):
     def __init__(self):
         global sections
@@ -289,6 +326,7 @@ class GUI(object):
 #         self.by_xid = dict([(w.id,w) for w in self.clients_list])

     def exit(self,widget=None):
+        save_config()
         gtk.main_quit()

     def on_key(self,widget,event):
@@ -341,9 +379,12 @@ class GUI(object):

     def show_section(self,k):
         global sections
+        global defsection
+
         self.collect_windows()

         id = (int(k)-1)%10
+        defsection = id

         self.S = sections[id]
         for b in self.buttons:
@@ -362,5 +403,5 @@ class GUI(object):
 parse_config()

 g = GUI()
-g.show_section('1')
+g.show_section(defsection+1)
 gtk.main()
ViewGit