Commit ca308c0d authored by Devaev Maxim's avatar Devaev Maxim

Refactoring

parent 0f0c92ba
...@@ -87,15 +87,16 @@ if __name__ == "__main__" : ...@@ -87,15 +87,16 @@ if __name__ == "__main__" :
app.loadApplicationConfigs() app.loadApplicationConfigs()
except : except :
logger.error("Initialization error") logger.error("Initialization error")
logger.attachException()
sys.exit(1) sys.exit(1)
if bus_type != None : if bus_type != None :
config.setValue(const.MY_NAME, "bus_type", bus_type) config.setValue(config.APPLICATION_SECTION, "bus_type", bus_type)
if log_level != None : if log_level != None :
config.setValue(const.MY_NAME, "log_level", log_level) config.setValue(config.APPLICATION_SECTION, "log_level", log_level)
if use_syslog_flag : if use_syslog_flag :
syslog.openlog(const.MY_NAME, syslog.LOG_PID, syslog.LOG_USER) syslog.openlog(config.APPLICATION_SECTION, syslog.LOG_PID, syslog.LOG_USER)
config.setValue(const.RUNTIME_NAME, "use_syslog", True) config.setValue(config.RUNTIME_SECTION, "use_syslog", True)
try : try :
app.loadModules() app.loadModules()
...@@ -105,6 +106,7 @@ if __name__ == "__main__" : ...@@ -105,6 +106,7 @@ if __name__ == "__main__" :
logger.info("Initialized") logger.info("Initialized")
except : except :
logger.error("Initialization error") logger.error("Initialization error")
logger.attachException()
sys.exit(1) sys.exit(1)
try : try :
......
...@@ -61,7 +61,7 @@ class Application(object) : ...@@ -61,7 +61,7 @@ class Application(object) :
sys.path.remove(const.ACTIONS_DIR) sys.path.remove(const.ACTIONS_DIR)
def loadApplicationConfigs(self) : def loadApplicationConfigs(self) :
config.loadConfigs(only_sections_list = (const.MY_NAME,)) config.loadConfigs(only_sections_list = (config.APPLICATION_SECTION,))
def loadServicesConfigs(self) : def loadServicesConfigs(self) :
for service_name in self._services_dict.keys() : for service_name in self._services_dict.keys() :
...@@ -75,14 +75,14 @@ class Application(object) : ...@@ -75,14 +75,14 @@ class Application(object) :
logger.error("Error on set options tuple %s" % (str(service_options_list_item))) logger.error("Error on set options tuple %s" % (str(service_options_list_item)))
logger.attachException() logger.attachException()
config.loadConfigs(exclude_sections_list = (const.MY_NAME,)) config.loadConfigs(exclude_sections_list = (config.APPLICATION_SECTION,))
def initBus(self) : def initBus(self) :
bus_type = config.value(const.MY_NAME, "bus_type") bus_type = config.value(config.APPLICATION_SECTION, "bus_type")
service_name = config.value(const.MY_NAME, "service_name") service_name = config.value(config.APPLICATION_SECTION, "service_name")
try : try :
config.setValue(const.RUNTIME_NAME, "bus_name", dbus.service.BusName(service_name, config.setValue(config.RUNTIME_SECTION, "bus_name", dbus.service.BusName(service_name,
( dbus.SystemBus() if bus_type == const.BUS_TYPE_SYSTEM else dbus.SessionBus() ))) ( dbus.SystemBus() if bus_type == const.BUS_TYPE_SYSTEM else dbus.SessionBus() )))
except : except :
logger.error("Could not connect to D-Bus \"%s\"" % (bus_type)) logger.error("Could not connect to D-Bus \"%s\"" % (bus_type))
......
...@@ -9,15 +9,25 @@ import config ...@@ -9,15 +9,25 @@ import config
import validators import validators
##### Public constants #####
APPLICATION_SECTION = const.MY_NAME
RUNTIME_SECTION = "runtime"
ALL_SECTIONS_LIST = (
APPLICATION_SECTION,
RUNTIME_SECTION
)
##### Private objects ##### ##### Private objects #####
ConfigDictObject = { ConfigDictObject = {
const.MY_NAME : { APPLICATION_SECTION : {
"service_name" : (const.DEFAULT_SERVICE_NAME, str), "service_name" : (const.DEFAULT_SERVICE_NAME, str),
"service_path" : (const.DEFAULT_SERVICE_PATH, str), "service_path" : (const.DEFAULT_SERVICE_PATH, str),
"bus_type" : (const.DEFAULT_BUS_TYPE, ( lambda arg : validators.validRange(arg, const.ALL_BUS_TYPES_LIST) )), "bus_type" : (const.DEFAULT_BUS_TYPE, ( lambda arg : validators.validRange(arg, const.ALL_BUS_TYPES_LIST) )),
"log_level" : (const.DEFAULT_LOG_LEVEL, ( lambda arg : validators.validRange(int(arg), const.ALL_LOG_LEVELS_LIST) )) "log_level" : (const.DEFAULT_LOG_LEVEL, ( lambda arg : validators.validRange(int(arg), const.ALL_LOG_LEVELS_LIST) ))
}, },
const.RUNTIME_NAME : { RUNTIME_SECTION : {
"bus_name" : (None, None), "bus_name" : (None, None),
"use_syslog" : (False, None) "use_syslog" : (False, None)
} }
...@@ -51,7 +61,7 @@ def validator(section, option) : ...@@ -51,7 +61,7 @@ def validator(section, option) :
def loadConfigs(only_sections_list = (), exclude_sections_list = ()) : def loadConfigs(only_sections_list = (), exclude_sections_list = ()) :
only_sections_list = list(only_sections_list) only_sections_list = list(only_sections_list)
exclude_sections_list = list(exclude_sections_list) exclude_sections_list = list(exclude_sections_list)
exclude_sections_list.append(const.RUNTIME_NAME) exclude_sections_list.append(RUNTIME_SECTION)
for config_files_list_item in os.listdir(const.CONFIGS_DIR) : for config_files_list_item in os.listdir(const.CONFIGS_DIR) :
if not config_files_list_item.endswith(const.CONFIG_FILE_POSTFIX) : if not config_files_list_item.endswith(const.CONFIG_FILE_POSTFIX) :
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
##### Public constants ##### ##### Public constants #####
MY_NAME = "settingsd" MY_NAME = "settingsd"
RUNTIME_NAME = "runtime"
VERSION = "0.1" VERSION = "0.1"
......
...@@ -61,12 +61,12 @@ def log(message_type, message) : ...@@ -61,12 +61,12 @@ def log(message_type, message) :
if not message_type in ALL_MESSAGES_LIST : if not message_type in ALL_MESSAGES_LIST :
raise UnknownMessageType("Message type \"%d\" not in list %s" % (message_type, ALL_MESSAGES_LIST)) raise UnknownMessageType("Message type \"%d\" not in list %s" % (message_type, ALL_MESSAGES_LIST))
if message_type[2] <= config.value(const.MY_NAME, "log_level") : if message_type[2] <= config.value(config.APPLICATION_SECTION, "log_level") :
message_type_texts_list = ("Error", "Warning", "Notice", "Info", "Details", "Debug") message_type_texts_list = ("Error", "Warning", "Notice", "Info", "Details", "Debug")
message = "[ %s ]: %s" % (message_type_texts_list[message_type[0]], message) message = "[ %s ]: %s" % (message_type_texts_list[message_type[0]], message)
print >> sys.stderr, const.MY_NAME, message print >> sys.stderr, const.MY_NAME, message
if config.value(const.RUNTIME_NAME, "use_syslog") : if config.value(config.RUNTIME_SECTION, "use_syslog") :
syslog.syslog(message_type[1], message) syslog.syslog(message_type[1], message)
...@@ -52,7 +52,7 @@ class Service(ServiceInterface, ServiceRequisitesInterface) : ...@@ -52,7 +52,7 @@ class Service(ServiceInterface, ServiceRequisitesInterface) :
##### #####
class CustomObject(dbus.service.Object) : class CustomObject(dbus.service.Object) :
def __init__(self, object_path) : def __init__(self, object_path) :
dbus.service.Object.__init__(self, config.value(const.RUNTIME_NAME, "bus_name"), object_path) dbus.service.Object.__init__(self, config.value(config.RUNTIME_SECTION, "bus_name"), object_path)
self._object_path = object_path self._object_path = object_path
...@@ -71,18 +71,20 @@ class CustomObject(dbus.service.Object) : ...@@ -71,18 +71,20 @@ class CustomObject(dbus.service.Object) :
class FunctionObject(CustomObject) : class FunctionObject(CustomObject) :
def __init__(self, object_path) : def __init__(self, object_path) :
CustomObject.__init__(self, dbus_tools.joinPath(config.value(const.MY_NAME, "service_path"), "functions", object_path)) CustomObject.__init__(self, dbus_tools.joinPath(config.value(config.APPLICATION_SECTION,
"service_path"), "functions", object_path))
class ActionObject(CustomObject) : class ActionObject(CustomObject) :
def __init__(self, object_path) : def __init__(self, object_path) :
CustomObject.__init__(self, dbus_tools.joinPath(config.value(const.MY_NAME, "service_path"), "actions", object_path)) CustomObject.__init__(self, dbus_tools.joinPath(config.value(config.APPLICATION_SECTION,
"service_path"), "actions", object_path))
##### Private decorators ##### ##### Private decorators #####
def tracer(function) : def tracer(function) :
def wrapper(self, *args_list, **kwargs_dict) : def wrapper(self, *args_list, **kwargs_dict) :
return_value = function(self, *args_list, **kwargs_dict) return_value = function(self, *args_list, **kwargs_dict)
if config.value(const.MY_NAME, "log_level") == const.LOG_LEVEL_DEBUG : if config.value(config.APPLICATION_SECTION, "log_level") == const.LOG_LEVEL_DEBUG :
logger.debug("Called \"%s::%s\" with args (%s, %s) --> %s" % (self.__class__.__name__, function.__name__, logger.debug("Called \"%s::%s\" with args (%s, %s) --> %s" % (self.__class__.__name__, function.__name__,
str(args_list), str(kwargs_dict), str(return_value) )) str(args_list), str(kwargs_dict), str(return_value) ))
return return_value return return_value
...@@ -102,11 +104,13 @@ def customMethod(interface_name) : ...@@ -102,11 +104,13 @@ def customMethod(interface_name) :
def functionMethod(interface_name) : def functionMethod(interface_name) :
def decorator(function) : def decorator(function) :
return customMethod(dbus_tools.joinMethod(config.value(const.MY_NAME, "service_name"), "functions", interface_name))(function) return customMethod(dbus_tools.joinMethod(config.value(config.APPLICATION_SECTION,
"service_name"), "functions", interface_name))(function)
return decorator return decorator
def actionsMethod(interface_name) : def actionsMethod(interface_name) :
def decorator(function) : def decorator(function) :
return customMethod(dbus_tools.joinMethod(config.value(const.MY_NAME, "service_name"), "actions", interface_name))(function) return customMethod(dbus_tools.joinMethod(config.value(config.APPLICATION_SECTION,
"service_name"), "actions", interface_name))(function)
return decorator return decorator
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