]> Pileus Git - ~andy/csm213a-hw/commitdiff
Update settings
authorAndy Spencer <andy753421@gmail.com>
Mon, 3 Feb 2014 06:38:04 +0000 (06:38 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 3 Feb 2014 06:42:06 +0000 (06:42 +0000)
.gitignore
vis/config.py
vis/settings.cfg [deleted file]
vis/visual.py
vis/visual.ui

index 536d98d01522b7016f8381a861715369ec2a9017..eec85d625969bdca772320ce1029e46bbae2622a 100644 (file)
@@ -9,3 +9,4 @@
 __pycache__
 xively
 config.mk
+settings.cfg
index 71dd99e5e35c98fe9cb8cbb54033541d53783927..50c5f7724c137c6217ad27e7d8bec0f9f77baf64 100644 (file)
@@ -5,36 +5,57 @@ class Config:
        filename = "settings.cfg"
 
        defaults = {
-               'baudrate': '/dev/ttyACM0',
-               'baudrate': '9600',
-               'parity':   'N',
-               'databits': '8',
-               'stopbits': '1',
-               'username': '<username>',
-               'password': '<password>',
-               'apikey':   '<apikey>',
+               'serial': {
+                       'device':   '/dev/ttyACM0',
+                       'baudrate': '9600',
+                       'parity':   'N',
+                       'databits': '8',
+                       'stopbits': '1',
+               },
+               'xively': {
+                       'feedid':   '<feedid>',
+                       'apikey':   '<apikey>',
+                       'maxrate':  '6',
+               }
        }
 
        # Constructor
        def __init__(self):
-               self.parser = ConfigParser(defaults=self.defaults)
+               self.parser = ConfigParser()
                self.parser.add_section('serial')
                self.parser.add_section('xively')
                self.load()
 
        # Methods
        def load(self):
-               self.parser.read(self.filename)
+               def get(sect, name):
+                       if self.parser.has_option(sect, name):
+                               return self.parser.get(sect, name)
+                       else:
+                               return self.defaults[sect][name]
 
-               self.device   = self.parser.get('serial', 'device')
-               self.baudrate = self.parser.getint('serial', 'baudrate')
-               self.parity   = self.parser.get('serial', 'parity')
-               self.databits = self.parser.getint('serial', 'databits')
-               self.stopbits = self.parser.getint('serial', 'stopbits')
+               self.parser.read(self.filename)
 
-               self.username = self.parser.get('xively', 'username')
-               self.password = self.parser.get('xively', 'password')
-               self.apikey   = self.parser.get('xively', 'apikey')
+               self.device   =     get('serial', 'device')
+               self.baudrate = int(get('serial', 'baudrate'))
+               self.parity   =     get('serial', 'parity')
+               self.databits = int(get('serial', 'databits'))
+               self.stopbits = int(get('serial', 'stopbits'))
+                                                                
+               self.feedid   =     get('xively', 'feedid')
+               self.apikey   =     get('xively', 'apikey')
+               self.maxrate  = int(get('xively', 'maxrate'))
 
        def save(self):
-               self.parser.write(self.filename)
+               self.parser.set('serial', 'device',           self.device)
+               self.parser.set('serial', 'baudrate', str(int(self.baudrate)))
+               self.parser.set('serial', 'parity',           self.parity)
+               self.parser.set('serial', 'databits', str(int(self.databits)))
+               self.parser.set('serial', 'stopbits', str(int(self.stopbits)))
+
+               self.parser.set('xively', 'feedid',           self.feedid)
+               self.parser.set('xively', 'apikey',           self.apikey)
+               self.parser.set('xively', 'maxrate',  str(int(self.maxrate)))
+
+               with open(self.filename, 'w') as fd:
+                       self.parser.write(fd)
diff --git a/vis/settings.cfg b/vis/settings.cfg
deleted file mode 100644 (file)
index 8757c24..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-[serial]
-device   = /dev/ttyACM0
-buadrate = 9600
-parity   = N
-databits = 8
-stopbits = 1
-
-[xively]
-username = andy
-password = open sesame
-apikey   = magic
index 5d0ff3976025271bdc1080b62e6440036af9a597..6d06ba4444bf6a478cdafd0dbef54a1157f94fc9 100644 (file)
@@ -56,6 +56,16 @@ class Visual:
                print("Unimplemented: on_rate")
                return True
 
+       def on_config(self, obj):
+               name = Gtk.Buildable.get_name(obj)
+               if isinstance(obj, Gtk.SpinButton):
+                       value = obj.get_value()
+               else:
+                       value = obj.get_text()
+               if getattr(self.config, name) != value:
+                       setattr(self.config, name, value)
+                       self.config.save()
+
        def on_key(self, win, ev):
                if ev.string == 'q':
                        Gtk.main_quit();
@@ -78,14 +88,12 @@ class Visual:
                stopb.set_value(self.config.stopbits)
 
                # Xively settings
-               user  = self.builder.get_object("username")
-               pwd   = self.builder.get_object("password")
+               feed  = self.builder.get_object("feedid")
                api   = self.builder.get_object("apikey")
-               user.set_text(self.config.username)
-               pwd.set_text(self.config.password)
+               rate  = self.builder.get_object("maxrate")
+               feed.set_text(self.config.feedid)
                api.set_text(self.config.apikey)
-
-               # TODO - send changes back to config
+               rate.set_value(self.config.maxrate)
 
        def update(self, state):
                def setxyz(objs, vals):
@@ -139,4 +147,3 @@ class Visual:
                        return
                status = self.builder.get_object("status")
                status.push(text=('Error: ' + msg), context_id=0)
-
index bb981763bb368af24dcb6b74300acea56002fed8..f4795b7848d82cbd7cb006f426d222df75c7a90f 100644 (file)
@@ -2,6 +2,16 @@
 <interface>
   <requires lib="gtk+" version="2.24"/>
   <!-- interface-naming-policy project-wide -->
+  <object class="GtkToggleAction" id="serial_con">
+    <property name="label" translatable="yes">Serial</property>
+    <property name="stock_id">gtk-connect</property>
+    <signal name="activate" handler="on_serial" swapped="no"/>
+  </object>
+  <object class="GtkAction" id="settings_act">
+    <property name="label" translatable="yes">Settings</property>
+    <property name="stock_id">gtk-preferences</property>
+    <signal name="activate" handler="on_show" swapped="no"/>
+  </object>
   <object class="GtkAdjustment" id="a2d_adj">
     <property name="upper">100</property>
     <property name="value">1</property>
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <object class="GtkTextBuffer" id="serial_buf"/>
-  <object class="GtkToggleAction" id="serial_con">
-    <property name="label" translatable="yes">Serial</property>
-    <property name="stock_id">gtk-connect</property>
-    <signal name="activate" handler="on_serial" swapped="no"/>
-  </object>
-  <object class="GtkAction" id="settings_act">
-    <property name="label" translatable="yes">Settings</property>
-    <property name="stock_id">gtk-preferences</property>
-    <signal name="activate" handler="on_show" swapped="no"/>
-  </object>
-  <object class="GtkToggleAction" id="xively_con">
-    <property name="label" translatable="yes">Xively</property>
-    <property name="stock_id">gtk-connect</property>
-    <signal name="activate" handler="on_xively" swapped="no"/>
+  <object class="GtkAdjustment" id="rate_adj">
+    <property name="upper">100</property>
+    <property name="step_increment">1</property>
+    <property name="page_increment">10</property>
   </object>
+  <object class="GtkTextBuffer" id="serial_buf"/>
   <object class="GtkDialog" id="settings">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
                         <property name="adjustment">baud_adj</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
                         <property name="adjustment">stopb_adj</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
                         <property name="adjustment">datab_adj</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                         <property name="secondary_icon_activatable">False</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                         <property name="secondary_icon_activatable">False</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                     <property name="n_columns">2</property>
                     <property name="column_spacing">20</property>
                     <child>
-                      <object class="GtkLabel" id="user_lbl">
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="xalign">0</property>
-                        <property name="label" translatable="yes">Username</property>
-                      </object>
-                      <packing>
-                        <property name="x_options">GTK_FILL</property>
-                        <property name="y_options">GTK_FILL</property>
-                      </packing>
-                    </child>
-                    <child>
-                      <object class="GtkLabel" id="pass_lbl">
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="xalign">0</property>
-                        <property name="label" translatable="yes">Password</property>
-                      </object>
-                      <packing>
-                        <property name="top_attach">1</property>
-                        <property name="bottom_attach">2</property>
-                        <property name="x_options">GTK_FILL</property>
-                        <property name="y_options">GTK_FILL</property>
-                      </packing>
-                    </child>
-                    <child>
-                      <object class="GtkLabel" id="api_lbl">
+                      <object class="GtkLabel" id="feed_llb">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <property name="xalign">0</property>
-                        <property name="label" translatable="yes">API Key</property>
+                        <property name="ypad">8</property>
+                        <property name="label" translatable="yes">Feed ID</property>
                       </object>
                       <packing>
-                        <property name="top_attach">2</property>
-                        <property name="bottom_attach">3</property>
                         <property name="x_options">GTK_FILL</property>
                         <property name="y_options">GTK_FILL</property>
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkEntry" id="username">
+                      <object class="GtkEntry" id="feedid">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">●</property>
                         <property name="secondary_icon_activatable">False</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkEntry" id="password">
+                      <object class="GtkEntry" id="apikey">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">●</property>
                         <property name="secondary_icon_activatable">False</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkEntry" id="apikey">
+                      <object class="GtkLabel" id="api_lbl">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="xalign">0</property>
+                        <property name="label" translatable="yes">API Key</property>
+                      </object>
+                      <packing>
+                        <property name="top_attach">1</property>
+                        <property name="bottom_attach">2</property>
+                        <property name="x_options">GTK_FILL</property>
+                        <property name="y_options">GTK_FILL</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkLabel" id="rate_lbl">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <property name="xalign">0</property>
+                        <property name="label" translatable="yes">Max Rate</property>
+                      </object>
+                      <packing>
+                        <property name="top_attach">2</property>
+                        <property name="bottom_attach">3</property>
+                        <property name="x_options">GTK_FILL</property>
+                        <property name="y_options">GTK_FILL</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkSpinButton" id="maxrate">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="invisible_char">●</property>
-                        <property name="invisible_char_set">True</property>
                         <property name="primary_icon_activatable">False</property>
                         <property name="secondary_icon_activatable">False</property>
                         <property name="primary_icon_sensitive">True</property>
                         <property name="secondary_icon_sensitive">True</property>
+                        <property name="adjustment">rate_adj</property>
+                        <signal name="changed" handler="on_config" swapped="no"/>
                       </object>
                       <packing>
                         <property name="left_attach">1</property>
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
+  <object class="GtkToggleAction" id="xively_con">
+    <property name="label" translatable="yes">Xively</property>
+    <property name="stock_id">gtk-connect</property>
+    <signal name="activate" handler="on_xively" swapped="no"/>
+  </object>
   <object class="GtkWindow" id="window">
     <property name="can_focus">False</property>
     <signal name="destroy" handler="on_destroy" swapped="no"/>
       </object>
     </child>
   </object>
+  <object class="GtkSizeGroup" id="settings_lbls">
+    <widgets>
+      <widget name="feed_llb"/>
+      <widget name="api_lbl"/>
+      <widget name="rate_lbl"/>
+      <widget name="device_lbl"/>
+    </widgets>
+  </object>
 </interface>