-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathCsFile.py
More file actions
executable file
·179 lines (156 loc) · 5.95 KB
/
CsFile.py
File metadata and controls
executable file
·179 lines (156 loc) · 5.95 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -- coding: utf-8 --
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
import re
import copy
class CsFile:
""" File editors """
def __init__(self, filename):
self.filename = filename
self.load()
def load(self):
self.new_config = []
self.config = []
try:
for line in open(self.filename):
self.new_config.append(line)
except IOError:
logging.debug("File %s does not exist" % self.filename)
else:
logging.debug("Reading file %s" % self.filename)
self.config = list(self.new_config)
def is_changed(self):
if set(self.config) != set(self.new_config):
return True
else:
return False
def __len__(self):
return len(self.config)
def empty(self):
self.config = []
self.new_config = []
def repopulate(self):
self.new_config = []
def commit(self):
if not self.is_changed():
logging.info("Nothing to commit. The %s file did not change" % self.filename)
return False
handle = open(self.filename, "w+")
for line in self.new_config:
handle.write(line)
handle.close()
logging.info("Wrote edited file %s" % self.filename)
self.config = list(self.new_config)
logging.info("Updated file in-cache configuration")
return True
def dump(self):
for line in self.new_config:
print(line)
def addeq(self, string):
""" Update a line in a file of the form token=something
match on token= and replace something if needed
Add line if token is not present
"""
token = string.split('=')[0] + '='
self.search(token, string)
def append(self, string, where=-1):
if where == -1:
self.new_config.append("%s\n" % string)
else:
self.new_config.insert(where, "%s\n" % string)
def add(self, string, where=-1):
for index, line in enumerate(self.new_config):
if line.strip() == string:
return False
if where == -1:
self.new_config.append("%s\n" % string)
else:
self.new_config.insert(where, "%s\n" % string)
return True
def section(self, start, end, content):
sind = -1
eind = -1
found = False
for index, line in enumerate(self.new_config):
if found and line.strip() == end:
eind = index
found = False
if line.strip() == start:
sind = index + 1
found = True
if sind == -1:
content.insert(0, start + "\n")
content.append(end + "\n")
self.new_config[sind:eind] = content
def greplace(self, search, replace):
logging.debug("Searching for %s and replacing with %s" % (search, replace))
self.new_config = [w.replace(search, replace) for w in self.new_config]
def replaceIfFound(self, search, replace):
found = False
replace_filtered = replace
if re.search("PSK \"", replace):
replace_filtered = re.sub(r'".*"', '"****"', replace)
logging.debug("Searching for %s and replacing with %s if found" % (search, replace_filtered))
for index, line in enumerate(self.new_config):
if line.lstrip().startswith("#"):
continue
if re.search(search, line):
if replace not in line:
self.new_config[index] = replace + "\n"
return False
def search(self, search, replace):
found = False
replace_filtered = replace
if re.search("PSK \"", replace):
replace_filtered = re.sub(r'".*"', '"****"', replace)
logging.debug("Searching for %s and replacing with %s" % (search, replace_filtered))
for index, line in enumerate(self.new_config):
if line.lstrip().startswith("#"):
continue
if re.search(search, line):
found = True
if replace not in line:
self.new_config[index] = replace + "\n"
if not found:
self.new_config.append(replace + "\n")
return True
return False
def searchString(self, search, ignoreLinesStartWith):
found = False
logging.debug("Searching for %s string " % search)
for index, line in enumerate(self.new_config):
if line.lstrip().startswith(ignoreLinesStartWith):
continue
if search in line:
found = True
break
return found
def deleteLine(self, search):
found = False
logging.debug("Searching for %s to remove the line " % search)
temp_config = []
for index, line in enumerate(self.new_config):
if line.lstrip().startswith("#"):
continue
if search not in line:
temp_config.append(line)
self.new_config = list(temp_config)
def compare(self, o):
result = (isinstance(o, self.__class__) and self.config == o.config)
logging.debug("Comparison of CsFiles content is ==> %s" % result)
return result