Use configparser to store/load configuration

This commit is contained in:
iabdalkader 2014-10-05 01:59:08 +02:00
parent 1706cbdb83
commit a1699f5f3b
2 changed files with 40 additions and 22 deletions

View File

@ -12,12 +12,18 @@ import usb.core
import usb.util
import numpy as np
from os.path import expanduser
try:
# 3.x name
import configparser
except ImportError:
# 2.x name
configparser = __import__("ConfigParser")
UI_PATH =os.path.dirname(os.path.realpath(__file__))+"/openmv-ide.glade"
CONFIG_PATH = expanduser("~")+"/.openmvide.config"
EXAMPLE_PATH = os.path.dirname(os.path.realpath(__file__))+"/examples"
SCRIPTS_PATH = os.path.dirname(os.path.realpath(__file__))+"/scripts"
IDE_PATH = os.path.dirname(os.path.realpath(__file__))
GLADE_PATH = IDE_PATH+"/openmv-ide.glade"
CONFIG_PATH = IDE_PATH+"/openmv.config"
EXAMPLE_PATH = IDE_PATH+"/examples"
SCRIPTS_PATH = IDE_PATH+"/scripts"
FWBIN_PATH = "/home/mux/src/c/stm32f4/openmv/src/build/openmv.bin"
DFU_CMD = "dfu-util -d 0483:df11 -c 1 -i 0 -a 0 -s 0x08000000 -D %s"
@ -30,7 +36,7 @@ class OMVGtk:
def __init__(self):
#Set the Glade file
self.builder = gtk.Builder()
self.builder.add_from_file(UI_PATH)
self.builder.add_from_file(GLADE_PATH)
# get top window
self.window = self.builder.get_object("top_window")
@ -120,13 +126,18 @@ class OMVGtk:
}
self.builder.connect_signals(signals)
# open last opened file
# load config
self.config = configparser.ConfigParser()
try:
self.config.read(CONFIG_PATH)
except Exception as e:
print ("Failed to open config file %s"%(e))
# current file path
self.file_path= None
if os.path.isfile(CONFIG_PATH):
with open(CONFIG_PATH, "r") as file:
path = file.read()
if os.path.isfile(path):
self._load_file(path)
# path = self.config.get("main", "last_opened_file")
# if os.path.isfile(path):
# self._load_file(path)
# build examples menu
if os.path.isdir(EXAMPLE_PATH):
@ -157,11 +168,11 @@ class OMVGtk:
self.terminal = self.builder.get_object('terminal')
try:
# open VCP and configure the terminal
self.fd = os.open("/dev/openmvcam", os.O_RDWR)
self.fd = os.open(self.config.get("main", "serial_port"), os.O_RDWR)
self.terminal.reset(True, True)
self.terminal.set_size(80,24)
self.terminal.set_pty(self.fd)
except Exception, e:
except Exception as e:
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
return
@ -172,7 +183,7 @@ class OMVGtk:
# interrupt any running code
openmv.stop_script()
sleep(0.1)
except Exception, e:
except Exception as e:
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
return
@ -365,7 +376,7 @@ class OMVGtk:
try:
# read drawingarea
fb = openmv.fb_dump()
except Exception, e:
except Exception as e:
self.disconnect()
self._update_title()
return True
@ -390,6 +401,12 @@ class OMVGtk:
def on_ctrl_scale_value_changed(self, adjust):
openmv.set_attr(adjust.attr, int(adjust.value))
def save_config(self):
# TODO set config items from GUI
#self.config.set("section", "key", value)
with open(CONFIG_PATH, "w") as file:
self.config.write(file)
def _update_title(self):
if (self.file_path==None):
title = "Untitled"
@ -508,10 +525,7 @@ class OMVGtk:
# disconnect
self.disconnect()
if (self.file_path):
# write last opened file
with open(CONFIG_PATH, "w") as file:
file.write(self.file_path)
self.save_config()
# exit
sys.exit(0)

4
usr/openmv.config Normal file
View File

@ -0,0 +1,4 @@
[main]
board = openmv1
serial_port = /dev/openmvcam