Commit fd345f6f authored by Devaev Maxim's avatar Devaev Maxim

Class with startup modes

parent 8d7f618d
...@@ -6,10 +6,8 @@ import syslog ...@@ -6,10 +6,8 @@ import syslog
import getopt import getopt
from settingsd import const from settingsd import const
from settingsd import config
from settingsd import logger
from settingsd import validators from settingsd import validators
from settingsd import application from settingsd import startup
#from settingsd import daemon # TODO #from settingsd import daemon # TODO
...@@ -80,54 +78,6 @@ if __name__ == "__main__" : ...@@ -80,54 +78,6 @@ if __name__ == "__main__" :
##### #####
if not daemon_mode_flag : startup_proc = startup.Startup(log_level, use_syslog_flag, bus_type, daemon_mode_flag)
app = application.Application() startup_proc.run()
try :
app.loadApplicationConfigs()
except :
logger.error("Initialization error")
logger.attachException()
sys.exit(1)
if bus_type != None :
config.setValue(config.APPLICATION_SECTION, "bus_type", bus_type)
if log_level != None :
config.setValue(config.APPLICATION_SECTION, "log_level", log_level)
if use_syslog_flag :
syslog.openlog(config.APPLICATION_SECTION, syslog.LOG_PID, syslog.LOG_USER)
config.setValue(config.RUNTIME_SECTION, "use_syslog", True)
try :
app.loadModules()
app.loadServicesConfigs()
app.initBus()
app.initServices()
logger.info("Initialized")
except :
logger.error("Initialization error")
logger.attachException()
sys.exit(1)
try :
app.runLoop()
except (SystemExit, KeyboardInterrupt) :
try :
app.closeServices()
except :
logger.error("Critical error on services closing, abort all processes and go boom")
app.quitLoop()
logger.info("Closed")
except :
logger.error("Runtime error, trying to close services")
logger.attachException()
try :
app.closeServices()
except :
logger.error("Critical error on services closing, abort all processes and go boom")
app.quitLoop()
logger.info("Closed")
sys.exit(1)
else :
print "TODO"
# -*- coding: utf-8 -*-
import sys
import signal
import syslog
import const
import config
import logger
import application
#import daemon # TODO
##### Public classes #####
class Startup(object) :
def __init__(self, log_level, use_syslog_flag, bus_type, daemon_mode_flag) :
object.__init__(self)
#####
self._log_level = log_level
self._use_syslog_flag = use_syslog_flag
self._bus_type = bus_type
self._daemon_mode_flag = daemon_mode_flag
#####
self._app = application.Application()
### Public ###
def run(self) :
self.prepare()
if self._daemon_mode_flag :
self.runDaemon()
else :
self.runInteractive()
### Private ###
def prepare(self) :
if self._use_syslog_flag == None :
if self._daemon_mode_flag :
syslog.openlog(const.MY_NAME, syslog.LOG_PID, syslog.LOG_DAEMON)
config.setValue(config.RUNTIME_SECTION, "use_syslog", True)
else :
syslog.openlog(const.MY_NAME, syslog.LOG_PID, ( syslog.LOG_DAEMON if self._daemon_mode_flag else syslog.LOG_USER ))
config.setValue(config.RUNTIME_SECTION, "use_syslog", True)
try :
self._app.loadApplicationConfigs()
except :
logger.error("Initialization error")
logger.attachException()
sys.exit(1)
if self._bus_type != None :
config.setValue(config.APPLICATION_SECTION, "bus_type", self._bus_type)
if self._log_level != None :
config.setValue(config.APPLICATION_SECTION, "log_level", self._log_level)
###
def runInteractive(self) :
try :
self._app.loadModules()
self._app.loadServicesConfigs()
self._app.initBus()
self._app.initServices()
logger.info("Initialized")
except :
logger.error("Initialization error")
logger.attachException()
sys.exit(1)
try :
self._app.runLoop()
except (SystemExit, KeyboardInterrupt) :
try :
self._app.closeServices()
except :
logger.error("Critical error on services closing, abort all processes and go boom")
self._app.quitLoop()
logger.info("Closed")
except :
logger.error("Runtime error, trying to close services")
logger.attachException()
try :
self._app.closeServices()
except :
logger.error("Critical error on services closing, abort all processes and go boom")
self._app.quitLoop()
logger.error("Closed")
sys.exit(1)
def runDaemon(self) :
print "TODO" # TODO
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