-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfgread.py
More file actions
90 lines (77 loc) · 1.9 KB
/
cfgread.py
File metadata and controls
90 lines (77 loc) · 1.9 KB
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
87
88
89
90
# A simple model to handel reading and writeing simple config files.
# by Ben
str_lines = []
str_keys = []
str_values = []
cfgFilename = ''
def keyIndex(key):
idx = -1
x = 0
if key is None : return -1
for x in range(len(str_keys)):
if str_keys[x] == key.upper():
idx = x
break
return idx
def keyExists(key):
return keyIndex(key) != -1
def readKeys():
return str_keys
def loadCfg(filename):
global cfgFilename
cfgFilename = filename
fp = open(cfgFilename,"r")
if fp.mode == "r":
# Clear arrays
str_values.clear()
str_keys.clear()
str_lines.clear()
for s in fp:
s = s.strip()
if len(s) > 0:
s_pos = s.index("=")
if s_pos > 0:
str_keys.append(s[:s_pos].strip().upper())
str_values.append(s[s_pos + 1:].strip())
fp.close()
return True
def readVal(key):
k_idx = keyIndex(key)
if k_idx == -1:
return ""
else:
# Extract key value
return str_values[k_idx]
def setVal(key, value):
idx = keyIndex(key)
if idx == -1:
return False
str_values[idx] = value
return True
def delKey(key):
idx = keyIndex(key)
if idx == -1:
return False
del str_keys[idx]
del str_values[idx]
return True
def appendKey(key, value):
# Append new key and value
idx = keyIndex(key)
if idx > -1:
return False
# Apend
str_keys.append(key.upper())
str_values.append(value)
return True
def update():
x = 0
buffer = ""
for x in range(len(str_keys)):
buffer += str_keys[x] + "=" + str_values[x] + "\n"
fp = open(cfgFilename,"w")
fp.write(buffer)
fp.close()
return True
# Clear buffer
buffer = ""