Commit fbc61f92 authored by Devaev Maxim's avatar Devaev Maxim

Modified objects structure, added config loader

parent f35440fb
[service]
bus_type = session
......@@ -5,31 +5,33 @@ import dbus.service
import dbus.glib
import gobject
import settingsd.config
from settingsd import const
from settingsd import config
if __name__ == "__main__" :
# future config init here
config.loadConfigFiles()
bus_name = dbus.service.BusName(settingsd.config.GlobalConfig["service_name"], bus = dbus.SessionBus())
if config.value("service", "bus_type") == const.CONFIG_SERVICE_BUS_TYPE_SYSTEM :
bus = dbus.SystemBus()
else :
bus = dbus.SessionBus()
bus_name = dbus.service.BusName(config.value("service", "name"), bus = bus)
config.setValue("runtime", "bus_name", bus_name)
main_parent_class = type("MainParent", (object,), {})
setattr(main_parent_class, "busName", lambda self : bus_name)
main_parent = main_parent_class()
sys.path.append("plugins")
sys.path.append(const.PLUGINS_DIR)
services_list = []
for module_name in [ item[:-3] for item in os.listdir("plugins") if item.endswith(".py") ] :
services_list.append(__import__(module_name, globals(), locals(), [""]).Service(main_parent))
for module_name in [ item[:-3] for item in os.listdir(const.PLUGINS_DIR) if item.endswith(".py") ] :
services_list.append(__import__(module_name, globals(), locals(), [""]).Service())
services_list[-1].initService()
print "Initialized \"%s\"" % (module_name)
main_loop = gobject.MainLoop()
print >> sys.stderr, "Initialized"
try :
main_loop.run()
except KeyboardInterrupt :
for services_list_item in services_list :
services_list_item.closeService()
print "Closed \"%s\"" % (module_name)
main_loop.quit()
print >> sys.stderr, "\nClosed"
......@@ -18,7 +18,7 @@ class Hello(settingsd.service.NativeObject) :
class Service(settingsd.service.Service) :
def initService(self) :
self.addObject(Hello("hello", self))
self.addSharedObject(Hello("hello"))
def closeService(self) :
pass
......
GlobalConfig = {
"service_name" : "org.etersoft.settingsd",
"service_path" : "/org/etersoft/settingsd"
import os
import ConfigParser
import const
import config
ConfigDictObject = {
"service" : {
"name" : const.DEFAULT_CONFIG_SERVICE_NAME,
"path" : const.DEFAULT_CONFIG_SERVICE_PATH,
"bus_type" : (const.DEFAULT_CONFIG_SERVICE_BUS_TYPE, const.CONFIG_VALID_SERVICE_BUS_TYPES_LIST)
}
}
class ValueError(Exception) :
pass
def setValue(section, option, value) :
global ConfigDictObject
if not ConfigDictObject.has_key(section) :
ConfigDictObject[section] = {}
if ConfigDictObject[section].has_key(option) and ConfigDictObject[section][option].__class__.__name__ == "tuple" :
if not value in ConfigDictObject[section][option][1] :
raise ValueError("Argument of \"%s :: %s\" \"%s\" not in list %s" % (
section, option, value, str(ConfigDictObject[section][option][1] )))
ConfigDictObject[section][option] = (value, ConfigDictObject[section][option][1])
else :
ConfigDictObject[section][option] = value
def value(section, option) :
if ConfigDictObject[section][option].__class__.__name__ == "tuple" :
return ConfigDictObject[section][option][0]
return ConfigDictObject[section][option]
def validValues(section, option) :
if ConfigDictObject[section][option].__class__.__name__ == "tuple" :
return ConfigDictObject[section][option][1]
return None
def loadConfigFiles() :
for config_files_list_item in os.listdir(const.CONFIGS_DIR) :
if not config_files_list_item.endswith(const.CONFIG_FILE_POSTFIX) :
continue
config_parser = ConfigParser.ConfigParser()
config_parser.read(os.path.join(const.CONFIGS_DIR, config_files_list_item))
for section in config_parser.sections() :
for option in config_parser.options(section):
setValue(section, option, config_parser.get(section, option))
MY_NAME = "settingsd"
PLUGINS_DIR = "plugins"
CONFIGS_DIR = "configs"
CONFIG_FILE_POSTFIX = ".conf"
CONFIG_SERVICE_BUS_TYPE_SYSTEM = "system"
CONFIG_SERVICE_BUS_TYPE_SESSION = "session"
CONFIG_VALID_SERVICE_BUS_TYPES_LIST = (
CONFIG_SERVICE_BUS_TYPE_SYSTEM,
CONFIG_SERVICE_BUS_TYPE_SESSION
)
DEFAULT_CONFIG_SERVICE_NAME = "org.etersoft.%s" % (MY_NAME)
DEFAULT_CONFIG_SERVICE_PATH = "/org/etersoft/%s" % (MY_NAME)
DEFAULT_CONFIG_SERVICE_BUS_TYPE = CONFIG_SERVICE_BUS_TYPE_SYSTEM
......@@ -4,17 +4,14 @@ import dbus.glib
import abc
import config
import tools
class Service(object) :
__metaclass__ = abc.ABCMeta
def __init__(self, parent) :
object.__init__(self)
self.__parent = parent
self.__objects_list = []
def __init__(self) :
self.__shared_objects_list = []
@abc.abstractmethod
def initService(self) :
......@@ -24,38 +21,25 @@ class Service(object) :
def closeService(self) :
pass
def addObject(self, object) :
self.__objects_list.append(object)
def objectsList(self) :
return self.__objects_list
def addSharedObject(self, shared_object) :
self.__shared_objects_list = []
def parent(self) :
return self.__parent
def busName(self) :
return self.__parent.busName()
def sharedObjects(self) :
return self.__shared_objects_list
class CustomObject(dbus.service.Object) :
def __init__(self, object_path, parent) :
dbus.service.Object.__init__(self, parent.busName(), object_path)
def __init__(self, object_path) :
dbus.service.Object.__init__(self, config.value("runtime", "bus_name"), object_path)
self.__object_path = object_path
self.__parent = parent
def parent(self) :
return self.__parent
def busName(self) :
return self.__parent.busName()
def objectPath(self) :
return self.__object_path
self.__object_path
class NativeObject(CustomObject) :
def __init__(self, object_path, parent) :
CustomObject.__init__(self, "%s/%s" % (config.GlobalConfig["service_path"], object_path), parent)
def __init__(self, object_path) :
CustomObject.__init__(self, tools.joinPath(config.value("service", "path"), object_path))
def customMethod(interface_name) :
......@@ -65,6 +49,6 @@ def customMethod(interface_name) :
def nativeMethod(interface_name) :
def decorator(function) :
return customMethod("%s.%s" % (config.GlobalConfig["service_name"], interface_name))(function)
return customMethod(tools.joinMethod(config.value("service", "name"), interface_name))(function)
return decorator
def __joinItems(separator, first, *others_list) :
for others_list_item in others_list :
if len(first) == 0 :
first = others_list_item
else :
first += separator + others_list_item
return first
def joinPath(first, *others_list) :
return __joinItems("/", first, *others_list)
def joinMethod(first, *others_list) :
return __joinItems(".", first, *others_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