Commit 4903930d authored by Devaev Maxim's avatar Devaev Maxim

fmod_dnsmasq_config uses PlainEditor

parent d6728db4
......@@ -15,6 +15,9 @@ from settingsd import logger
import settingsd.validators as validators
import settingsd.validators.network
import settingsd.tools as tools
import settingsd.tools.editors
##### Private constants #####
SERVICE_NAME = "dnsmasq_config"
......@@ -86,7 +89,13 @@ class SimpleDnsmasqConfig(service.FunctionObject) :
def addStaticHost(self, mac, ip, name) :
static_host = "%s,%s" % (validators.network.validMacAddress(mac)[0], validators.network.validIpv4Address(ip)[0])
static_host += ( ",%s" % (name) if len(name) != 0 else "" )
self.setConfigValue("dhcp-host", static_host, False)
dnsmasq_editor = tools.editors.PlainEditor(spaces_list = [], quotes_list = [])
dnsmasq_editor.open(config.value(SERVICE_NAME, "dnsmasq_config_file_path"),
config.value(SERVICE_NAME, "sample_dnsmasq_config_file_path"))
dnsmasq_editor.setValue("dhcp-host", dnsmasq_editor.value("dhcp_host") + [static_host])
dnsmasq_editor.save()
dnsmasq_editor.close()
@service.functionMethod(SIMPLE_METHODS_NAMESPACE, in_signature="s")
def removeStaticHost(self, identifier) :
......@@ -134,62 +143,20 @@ class SimpleDnsmasqConfig(service.FunctionObject) :
### Private ###
def setConfigValue(self, variable_name, values_list, replace_flag = True) :
if not type(values_list).__name__ in ("list", "tuple") :
values_list = [values_list]
dnsmasq_config_file_path = config.value(SERVICE_NAME, "dnsmasq_config_file_path")
###
dnsmasq_config_file_path_sample = config.value(SERVICE_NAME, "dnsmasq_config_file_path_sample")
if not os.access(dnsmasq_config_file_path, os.F_OK) :
if os.access(dnsmasq_config_file_path_sample, os.F_OK) :
shutil.copy2(dnsmasq_config_file_path_sample, dnsmasq_config_file_path)
else :
open(dnsmasq_config_file_path, "w").close()
###
dnsmasq_config_file = open(dnsmasq_config_file_path, "r+")
dnsmasq_config_file_data = dnsmasq_config_file.read()
variable_regexp = re.compile(r"(((\n|\A)%s[\s\t]*=[^\n]*)+)" % (variable_name))
variable = "\n".join([ "%s=%s" % (variable_name, values_list_item) for values_list_item in values_list ])
if variable_regexp.search(dnsmasq_config_file_data) :
if len(variable) != 0 :
variable = ( "\n" if replace_flag else "\\1\n" )+variable
dnsmasq_config_file_data = variable_regexp.sub(variable, dnsmasq_config_file_data)
elif len(variable) != 0 :
dnsmasq_config_file_data += ( "\n" if dnsmasq_config_file_data[-1] != "\n" else "" )+variable+"\n"
dnsmasq_config_file.seek(0)
dnsmasq_config_file.truncate()
dnsmasq_config_file.write(dnsmasq_config_file_data)
try :
dnsmasq_config_file_data.close()
except : pass
def setConfigValue(self, variable_name, values_list) :
dnsmasq_editor = tools.editors.PlainEditor(spaces_list = [], quotes_list = [])
dnsmasq_editor.open(config.value(SERVICE_NAME, "dnsmasq_config_file_path"),
config.value(SERVICE_NAME, "sample_dnsmasq_config_file_path"))
dnsmasq_editor.setValue(variable_name, values_list)
dnsmasq_editor.save()
dnsmasq_editor.close()
def configValue(self, variable_name) :
if os.access(config.value(SERVICE_NAME, "dnsmasq_config_file_path"), os.F_OK) :
dnsmasq_config_file = open(config.value(SERVICE_NAME, "dnsmasq_config_file_path"), "r")
dnsmasq_config_file_list = dnsmasq_config_file.read().split("\n")
try :
dnsmasq_config_file.close()
except : pass
variable_regexp = re.compile(r"^%s[\s\t]*=[\s\t]*([^\n]*)" % (variable_name))
variables_list = []
for dnsmasq_config_file_list_item in dnsmasq_config_file_list :
if len(dnsmasq_config_file_list_item) == 0 :
continue
variable_match = variable_regexp.match(dnsmasq_config_file_list_item)
if variable_match != None :
variables_list.append(variable_match.group(1))
return variables_list
return []
dnsmasq_editor = tools.editors.PlainEditor(spaces_list = [], quotes_list = [])
dnsmasq_editor.open(config.value(SERVICE_NAME, "dnsmasq_config_file_path"))
values_list = dnsmasq_editor.value(variable_name)
dnsmasq_editor.close()
return values_list
##### Public classes #####
......@@ -212,6 +179,6 @@ class Service(service.Service) :
return [
(SERVICE_NAME, "dnsmasq_config_file_path", "/etc/dnsmasq.conf", str),
(SERVICE_NAME, "dnsmasq_config_file_path_sample", os.path.join(const.FUNCTIONS_DATA_DIR, SERVICE_NAME, "dnsmasq.conf"), str)
(SERVICE_NAME, "sample_dnsmasq_config_file_path", os.path.join(const.FUNCTIONS_DATA_DIR, SERVICE_NAME, "dnsmasq.conf"), str)
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment