Commit 0589180e authored by Devaev Maxim's avatar Devaev Maxim

Added library for editing config files and universal class for plain configs

parent e2d06d71
# -*- coding: utf-8 -*-
__all__ = ["dbus", "process"]
__all__ = ["dbus", "process", "editors"]
# -*- coding: utf-8 -*-
import os
import re
import shutil
##### Exceptions #####
class NotAssociated(Exception) :
pass
class AlreadyAssociated(Exception) :
pass
##### Public classes #####
class PlainEditor(object) :
def __init__(self, delimiter = "=", spaces_list = ["\\s"], quotes_list= ["\"", "\'"], comments_list = ["#"]) :
object.__init__(self)
#####
self.__delimiter = delimiter
self.__spaces_list = list(spaces_list)
self.__quotes_list = list(quotes_list)
self.__comments_list = list(comments_list)
#####
self.__config_file = None
self.__config_file_data_list = None
###
spaces = ( "[%s]*" % ("".join(self.__spaces_list)) if len(self.__spaces_list) > 0 else "" )
comments = "".join(self.__comments_list)
self.__comments_regexp = re.compile(r"%s(?<!\\)[%s]%s" % (spaces, comments, spaces))
self.__variable_regexp = re.compile(r"%s%s%s" % (spaces, self.__delimiter, spaces))
### Public ###
def open(self, config_file_path, config_sample_file_path = None) :
if self.__config_file != None :
raise AlreadyAssociated("This parser already associated with config \"%s\"" % self.__config_file.name)
if not os.access(config_file_path, os.F_OK) :
if config_sample_file_path != None and os.access(config_sample_file_path, os.F_OK) :
shutil.copy2(config_sample_file_path, config_file_path)
else :
open(config_file_path, "w").close()
self.__config_file = open(config_file_path, "r+")
self.__config_file_data_list = self.__config_file.read().split("\n")
def save(self) :
if self.__config_file == None :
raise NotAssociated("This parser is not associated with config")
self.__config_file.seek(0)
self.__config_file.truncate()
self.__config_file.write("\n".join(self.__config_file_data_list))
self.__config_file.flush()
def close(self) :
if self.__config_file == None :
raise NotAssociated("This parser is not associated with config")
try :
self.__config_file.close()
except : pass
self.__config_file = None
self.__config_file_data_list = None
###
def setValue(self, variable_name, values_list) :
if self.__config_file == None :
raise NotAssociated("This parser is not associated with config")
if not type(values_list).__name__ in ("list", "tuple") :
values_list = [values_list]
last_variable_index = len(self.__config_file_data_list) - 1
count = 0
while count < len(self.__config_file_data_list) :
variable = self.__comments_regexp.split(self.__config_file_data_list[count].strip(), 1)[0]
variable_parts_list = self.__variable_regexp.split(variable, 1)
if variable_parts_list[0] == variable_name :
self.__config_file_data_list.pop(count)
last_variable_index = count
else :
count += 1
space = ( " " if len(self.__spaces_list) > 0 else "" )
quote = ( self.__quotes_list[0] if len(self.__quotes_list) > 0 else "" )
for count in xrange(len(values_list)) :
variable = variable_name + space + self.__delimiter + space + quote + str(values_list[count]) + quote
self.__config_file_data_list.insert(last_variable_index + count, variable)
def value(self, variable_name) :
if self.__config_file == None :
raise NotAssociated("This parser is not associated with config")
values_list = []
for config_file_data_list_item in self.__config_file_data_list :
variable = self.__comments_regexp.split(config_file_data_list_item.strip(), 1)[0]
variable_parts_list = self.__variable_regexp.split(variable, 1)
if variable_parts_list[0] == variable_name :
if len(variable_parts_list) > 1 :
value = variable_parts_list[1]
for quotes_list_item in self.__quotes_list :
if len(value) > 2 and value[0] == value[-1] == quotes_list_item :
value = value[1:-1]
values_list.append(value)
else :
values_list.append("")
return values_list
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