-
Notifications
You must be signed in to change notification settings - Fork 11
/
ConfigHelper.py
86 lines (70 loc) · 2.73 KB
/
ConfigHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#-*- coding: utf-8 -*-
import const
from ConfigParser import ConfigParser
class ConfigHelper:
SEC_UPLOAD = "upload"
SEC_UI = "ui"
KEY_HEX = "hex"
KEY_COM = "port"
KEY_BOARD = "board"
KEY_STYLE = "style"
KEY_PROGPAPER = "progpaper"
def __init__(self, ui, cfgini=const.config):
self.ui = ui
self.cfgini = cfgini
self.cfg = ConfigParser()
self.readCfg()
def readCfg(self):
return len(self.cfg.read(self.cfgini)) > 0
def getVal(self, section, key):
if not self.cfg.has_section(section):
return ""
if not self.cfg.has_option(section, key):
return ""
return self.cfg.get(section, key).strip()
def setVal(self, section, key, val):
if not self.cfg.has_section(section):
self.cfg.add_section(section)
self.cfg.set(section, key, val)
def writeCfg(self):
try:
self.cfg.write(open(self.cfgini, "w"))
except IOError:
pass
def __updateUiAboutUpload(self):
hex = self.getVal(self.SEC_UPLOAD, self.KEY_HEX)
if hex != "":
self.ui.hexfileCombox.addItem(hex)
board = self.getVal(self.SEC_UPLOAD, self.KEY_BOARD)
if board != "":
index = self.ui.mcuCombox.findText(board)
if index != -1:
self.ui.mcuCombox.setCurrentIndex(index)
com = self.getVal(self.SEC_UPLOAD, self.KEY_COM)
if com != "":
index = self.ui.portCombox.findText(com)
if index != -1:
self.ui.portCombox.setCurrentIndex(index)
else:
self.ui.portCombox.addItem(com)
self.ui.portCombox.setCurrentIndex(self.ui.portCombox.count() - 1)
def __updateUiAboutUi(self):
style = self.getVal(self.SEC_UI, self.KEY_STYLE)
if style != "":
from PyQt4.QtGui import QApplication
QApplication.setStyle(style)
progpaper = self.getVal(self.SEC_UI, self.KEY_PROGPAPER)
if progpaper != "":
from os import path as OSPath
from PyQt4.QtGui import QPixmap
progpaper = unicode(progpaper, "gbk")
if OSPath.exists(progpaper):
self.ui.headLabel.setPixmap(QPixmap(progpaper).scaled(const.width, const.height))
def updateUiByConfig(self):
self.__updateUiAboutUpload()
self.__updateUiAboutUi()
def updateConfig(self, hex="", com="", board=""):
self.setVal(self.SEC_UPLOAD, self.KEY_HEX, hex)
self.setVal(self.SEC_UPLOAD, self.KEY_COM, com)
self.setVal(self.SEC_UPLOAD, self.KEY_BOARD, board)
self.writeCfg()