Commit 3237b4e7 authored by Devaev Maxim's avatar Devaev Maxim

New parsers for ntp_config

parent d381ba9d
...@@ -24,26 +24,50 @@ class NtpConfig(service.FunctionObject) : ...@@ -24,26 +24,50 @@ class NtpConfig(service.FunctionObject) :
@service.functionMethod(NTP_METHODS_NAMESPACE, in_signature="as") @service.functionMethod(NTP_METHODS_NAMESPACE, in_signature="as")
def setServers(self, servers_list) : def setServers(self, servers_list) :
self.setConfigValue("server", servers_list)
@service.functionMethod(NTP_METHODS_NAMESPACE, out_signature="as")
def servers(self) :
return self.configValue("server")
###
@service.functionMethod(NTP_METHODS_NAMESPACE)
def request(self) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "ntpdate_prog_path"), " ".join(self.servers()))
tools.execProcess(proc_args)
### Private ###
def setConfigValue(self, variable_name, values_list, replace_flag = True) :
if not type(values_list).__name__ in ("list", "tuple") :
values_list = [values_list]
if not os.access(config.value(SERVICE_NAME, "ntp_config_file_path"), os.F_OK) : if not os.access(config.value(SERVICE_NAME, "ntp_config_file_path"), os.F_OK) :
open(config.value(SERVICE_NAME, "ntp_config_file_path"), "w").close() open(config.value(SERVICE_NAME, "ntp_config_file_path"), "w").close()
ntp_config_file = open(config.value(SERVICE_NAME, "ntp_config_file_path"), "r+") ntp_config_file = open(config.value(SERVICE_NAME, "ntp_config_file_path"), "r+")
ntp_config_file_data = ntp_config_file.read() ntp_config_file_data = ntp_config_file.read()
ntp_config_file_data = re.sub(r"(\n|\A)server[\s\t]+[^\n]+", "", ntp_config_file_data) variable_regexp = re.compile(r"(((\n|\A)%s[\s\t]+[^\n]*)+)" % (variable_name))
for servers_list_item in servers_list : variable = "\n".join([ "%s %s" % (variable_name, values_list_item) for values_list_item in values_list ])
ntp_config_file_data += "server %s\n" % (servers_list_item) if variable_regexp.search(ntp_config_file_data) :
if len(variable) != 0 :
variable = ( "\n" if replace_flag else "\\1\n" )+variable
ntp_config_file_data = variable_regexp.sub(variable, ntp_config_file_data)
elif len(variable) != 0 :
ntp_config_file_data += ( "\n" if ntp_config_file_data[-1] != "\n" else "" )+variable+"\n"
ntp_config_file.seek(0) ntp_config_file.seek(0)
ntp_config_file.truncate() ntp_config_file.truncate()
ntp_config_file.write(ntp_config_file_data) ntp_config_file.write(ntp_config_file_data)
try : try :
ntp_config_file.close() ntp_config_file_data.close()
except : pass except : pass
@service.functionMethod(NTP_METHODS_NAMESPACE, out_signature="as") def configValue(self, variable_name) :
def servers(self) :
if os.access(config.value(SERVICE_NAME, "ntp_config_file_path"), os.F_OK) : if os.access(config.value(SERVICE_NAME, "ntp_config_file_path"), os.F_OK) :
ntp_config_file = open(config.value(SERVICE_NAME, "ntp_config_file_path"), "r") ntp_config_file = open(config.value(SERVICE_NAME, "ntp_config_file_path"), "r")
ntp_config_file_list = ntp_config_file.read().split("\n") ntp_config_file_list = ntp_config_file.read().split("\n")
...@@ -51,21 +75,18 @@ class NtpConfig(service.FunctionObject) : ...@@ -51,21 +75,18 @@ class NtpConfig(service.FunctionObject) :
ntp_config_file.close() ntp_config_file.close()
except : pass except : pass
servers_list = [] variable_regexp = re.compile(r"^%s[\s\t]+([^\n]*)" % (variable_name))
variables_list = []
for ntp_config_file_list_item in ntp_config_file_list : for ntp_config_file_list_item in ntp_config_file_list :
if len(ntp_config_file_list_item) == 0 : if len(ntp_config_file_list_item) == 0 :
continue continue
if re.match(r"^server[\s\t]+", ntp_config_file_list_item) != None : variable_match = variable_regexp.match(ntp_config_file_list_item)
servers_list.append(re.split(r"[\s\t]+", ntp_config_file_list_item)[1]) if variable_match != None :
return servers_list variables_list.append(variable_match.group(1))
return variables_list
return [] return []
###
@service.functionMethod(NTP_METHODS_NAMESPACE)
def request(self) :
proc_args = "%s %s" % (config.value(SERVICE_NAME, "ntpdate_prog_path"), " ".join(self.servers()))
tools.execProcess(proc_args)
##### Public classes ##### ##### Public classes #####
......
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