Commit cf1eb01e authored by Devaev Maxim's avatar Devaev Maxim

Moved requisites to service class, refactoring

parent b6d81a29
[service]
[settingsd]
bus_type = session
log_level = 2
......
......@@ -22,7 +22,10 @@ class Hello(settingsd.service.FunctionObject) :
settingsd.shared.Functions.Hello.removeFromConnection()
settingsd.shared.Functions.removeSharedObject("Hello")
class Requisites(settingsd.service.Requisites) :
class Service(settingsd.service.Service) :
def initService(self) :
self.Functions.addSharedObject("Hello", Hello(self.serviceName()))
@classmethod
def serviceName(self) :
return "hello"
......@@ -30,13 +33,6 @@ class Requisites(settingsd.service.Requisites) :
@classmethod
def options(self) :
return [
("hello", "hello_string", "Hello, World!", str)
(self.serviceName(), "hello_string", "Hello, World!", str)
]
class Service(settingsd.service.Service) :
def init(self) :
self.Functions.addSharedObject("Hello", Hello("hello"))
def close(self) :
pass
......@@ -48,7 +48,6 @@ class Application(object) :
self.closeServices()
self._main_loop.quit()
def loadModules(self) :
sys.path.append(const.FUNCTIONS_DIR)
sys.path.append(const.ACTIONS_DIR)
......@@ -56,21 +55,17 @@ class Application(object) :
for modules_path_list_item in (const.FUNCTIONS_DIR, const.ACTIONS_DIR) :
for module_name in [ item[:-3] for item in os.listdir(modules_path_list_item) if item.endswith(".py") ] :
self._modules_list.append(__import__(module_name, globals(), locals(), [""]))
if self._modules_list[-1].Requisites.serviceName() != None :
self._services_dict[self._modules_list[-1].Requisites.serviceName()] = {
"requisites" : self._modules_list[-1].Requisites,
"service" : self._modules_list[-1].Service,
"instance" : None
self._services_dict[self._modules_list[-1].Service.serviceName()] = {
"service_class" : self._modules_list[-1].Service,
"service" : None
}
else :
print >> sys.stderr, "Anonymous modules does not acceped"
sys.path.remove(const.FUNCTIONS_DIR)
sys.path.remove(const.ACTIONS_DIR)
def loadConfigs(self) :
for service_name in self._services_dict.keys() :
service_options_list = list(self._services_dict[service_name]["requisites"].options())
service_options_list = list(self._services_dict[service_name]["service_class"].options())
service_options_list.append((service_name, "enabled", "no", validators.validBool))
for service_options_list_item in service_options_list :
......@@ -79,23 +74,23 @@ class Application(object) :
config.loadConfig()
def initBus(self) :
if config.value("service", "bus_type") == const.SERVICE_BUS_TYPE_SYSTEM :
if config.value(const.MY_NAME, "bus_type") == const.BUS_TYPE_SYSTEM :
bus = dbus.SystemBus()
else :
bus = dbus.SessionBus()
self._bus_name = dbus.service.BusName(config.value("service", "name"), bus = bus)
config.setValue("runtime", "bus_name", self._bus_name)
self._bus_name = dbus.service.BusName(config.value(const.MY_NAME, "service_name"), bus = bus)
config.setValue(const.RUNTIME_NAME, "bus_name", self._bus_name)
def initServices(self) :
for service_name in self._services_dict.keys() :
if config.value(service_name, "enabled") :
self._services_dict[service_name]["instance"] = self._services_dict[service_name]["service"]()
self._services_dict[service_name]["instance"].init()
self._services_dict[service_name]["service"] = self._services_dict[service_name]["service_class"]()
self._services_dict[service_name]["service"].initService()
def closeServices(self) :
for service_name in self._services_dict.keys() :
if self._services_dict[service_name]["instance"] != None :
self._services_dict[service_name]["instance"].close()
del self._services_dict[service_name]["instance"]
self._services_dict[service_name]["instance"] = None
if self._services_dict[service_name]["service"] != None :
self._services_dict[service_name]["service"].closeService()
del self._services_dict[service_name]["service"]
self._services_dict[service_name]["service"] = None
......@@ -11,10 +11,10 @@ import validators
#####
ConfigDictObject = {
"service" : {
"name" : (const.DEFAULT_SERVICE_NAME, str),
"path" : (const.DEFAULT_SERVICE_PATH, str),
"bus_type" : (const.DEFAULT_SERVICE_BUS_TYPE, ( lambda arg : validators.validRange(arg, const.ALL_SERVICE_BUS_TYPES_LIST) )),
const.MY_NAME : {
"service_name" : (const.DEFAULT_SERVICE_NAME, str),
"service_path" : (const.DEFAULT_SERVICE_PATH, str),
"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) ))
}
}
......
......@@ -2,6 +2,7 @@
MY_NAME = "settingsd"
RUNTIME_NAME = "runtime"
FUNCTIONS_DIR = "functions"
ACTIONS_DIR = "actions"
......@@ -9,11 +10,11 @@ CONFIGS_DIR = "configs"
CONFIG_FILE_POSTFIX = ".conf"
SERVICE_BUS_TYPE_SYSTEM = "system"
SERVICE_BUS_TYPE_SESSION = "session"
ALL_SERVICE_BUS_TYPES_LIST = (
SERVICE_BUS_TYPE_SYSTEM,
SERVICE_BUS_TYPE_SESSION
BUS_TYPE_SYSTEM = "system"
BUS_TYPE_SESSION = "session"
ALL_BUS_TYPES_LIST = (
BUS_TYPE_SYSTEM,
BUS_TYPE_SESSION
)
LOG_LEVEL_INFO = 0
......@@ -28,6 +29,6 @@ ALL_LOG_LEVELS_LIST = (
DEFAULT_SERVICE_NAME = "org.etersoft.%s" % (MY_NAME)
DEFAULT_SERVICE_PATH = "/org/etersoft/%s" % (MY_NAME)
DEFAULT_SERVICE_BUS_TYPE = SERVICE_BUS_TYPE_SYSTEM
DEFAULT_BUS_TYPE = BUS_TYPE_SYSTEM
DEFAULT_LOG_LEVEL = LOG_LEVEL_INFO
......@@ -37,7 +37,7 @@ def message(message_type, message) :
else :
raise UnknownMessageType("Message type \"%d\" not in list %s" % (message_type, ALL_MESSAGES_LIST))
if log_level <= config.value("service", "log_level") :
if log_level <= config.value(const.MY_NAME, "log_level") :
text_log_levels_list = (
"%s [ Info ]:" % (const.MY_NAME),
"%s [ Details ]:" % (const.MY_NAME),
......
......@@ -13,17 +13,6 @@ import logger
#####
class Requisites(object) :
@classmethod
def serviceName(self) :
return None
@classmethod
def options(self) :
return []
#####
class Service(object) :
__metaclass__ = abc.ABCMeta
......@@ -31,17 +20,26 @@ class Service(object) :
Actions = shared.Actions
@abc.abstractmethod
def init(self) :
def initService(self) :
pass
def close(self) :
def closeService(self) :
pass
@classmethod
@abc.abstractmethod
def serviceName(self) :
pass
@classmethod
def options(self) :
return []
#####
class CustomObject(dbus.service.Object) :
def __init__(self, object_path) :
dbus.service.Object.__init__(self, config.value("runtime", "bus_name"), object_path)
dbus.service.Object.__init__(self, config.value(const.RUNTIME_NAME, "bus_name"), object_path)
self._object_path = object_path
......@@ -56,18 +54,18 @@ class CustomObject(dbus.service.Object) :
class FunctionObject(CustomObject) :
def __init__(self, object_path) :
CustomObject.__init__(self, dbus_tools.joinPath(config.value("service", "path"), "functions", object_path))
CustomObject.__init__(self, dbus_tools.joinPath(config.value(const.MY_NAME, "service_path"), "functions", object_path))
class ActionObject(CustomObject) :
def __init__(self, object_path) :
CustomObject.__init__(self, dbus_tools.joinPath(config.value("service", "path"), "actions", object_path))
CustomObject.__init__(self, dbus_tools.joinPath(config.value(const.MY_NAME, "service_path"), "actions", object_path))
######
def tracer(function) :
def wrapper(self, *args_list, **kwargs_dict) :
return_value = function(self, *args_list, **kwargs_dict)
if config.value("service", "log_level") == const.LOG_LEVEL_DEBUG :
if config.value(const.MY_NAME, "log_level") == const.LOG_LEVEL_DEBUG :
logger.message(logger.DEBUG_MESSAGE, "Called \"%s::%s\" with args (%s, %s) --> %s" % (
self.__class__.__name__, function.__name__, str(args_list), str(kwargs_dict), str(return_value) ))
return return_value
......@@ -84,11 +82,11 @@ def customMethod(interface_name) :
def functionMethod(interface_name) :
def decorator(function) :
return customMethod(dbus_tools.joinMethod(config.value("service", "name"), "functions", interface_name))(function)
return customMethod(dbus_tools.joinMethod(config.value(const.MY_NAME, "service_name"), "functions", interface_name))(function)
return decorator
def actionsMethod(interface_name) :
def decorator(function) :
return customMethod(dbus_tools.joinMethod(config.value("service", "name"), "actions", interface_name))(function)
return customMethod(dbus_tools.joinMethod(config.value(const.MY_NAME, "service_name"), "actions", interface_name))(function)
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