-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvariables.py
More file actions
59 lines (53 loc) · 1.89 KB
/
variables.py
File metadata and controls
59 lines (53 loc) · 1.89 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
'''
Created on 3 Dec 2015
@author: steph
'''
import logging
class Variables(object):
"""
Reads and writes the variables.txt file.
Keys and variables separated by a comma
"""
def readVariables(self, variableName):
"""
Accepts list of Variable names and returns list of variable data
or complete list if 'ALL' received
"""
self.logger = logging.getLogger("main.variables.readVariables")
data = self.variableData()
if variableName == 'ALL':
return data
output = []
for variable in variableName:
self.logger.debug("reading %s" % variable)
for lines in data:
line = lines.split(',')
if line[0] == variable:
try:
output.append(int(line[1]))
except:
output.append(str(line[1]))
if len(output) == 1:
output = output[0]
return output
def writeVariable(self, variables):
"""
Accepts List of Variable lists to change
"""
self.logger = logging.getLogger("main.variables.writeVariables")
self.logger.info("writing variables")
data = self.variableData()
for change in variables:
for i in range(len(data)):
if data[i].split(',')[0] == change[0]:
data[i] = "{},{}".format(change[0],change[1])
#print data
with open('variables.txt', 'w') as f:
for items in data:
if ',' in items:
self.logger.debug("Writing %s" % items)
print>>f, items
def variableData(self):
with open('variables.txt', 'r') as f:
data = f.read().split('\n')
return data