-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigcontrols.py
More file actions
46 lines (43 loc) · 1.61 KB
/
Copy pathconfigcontrols.py
File metadata and controls
46 lines (43 loc) · 1.61 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
from __future__ import unicode_literals
import os
class ConfigControls( object ):
def __init__(self):
self.path = os.path.dirname(os.path.abspath(__file__))
def get_section(self,section,return_other_text=False):
'''Returns the section from the config file'''
section_text = []
other_text = [[],[]]
index = 0
flag_section = False
full_text = open(os.path.join(self.path,'config.txt'),'r')
for line in full_text:
if flag_section:
if '[' in line:
flag_section = False
other_text[index].append(line)
elif line != '\n':
section_text.append(line.strip())
else:
other_text[index].append(line)
elif '['+section.strip()+']' in line:
flag_section = True
other_text[index].append(line)
index += 1
else:
other_text[index].append(line)
if return_other_text:
return [section_text,other_text]
else:
return section_text
def set_section(self,section_name,section_list):
try:
[section_text,other_text] = self.get_section(section_name,True)
section_list.append('')
file = open(os.path.join(self.path,'config.txt'),'w')
file.write(''.join(other_text[0])
+'\n'.join(section_list)
+''.join(other_text[1]))
return True
except Exception as error:
print(error)
return False