class TestConfig: def setup_method(self, method): self.contents = StringIO(''' [KEY_NAME] process = processName search = searchString needsKill = false [KILL_NAME] process = anotherProcessName needsKill = true ''') self.config = Config() self.config.load(self.contents) def testCommandsLoadedWithCorrectProcess(self): assert self.config.getCommand('KEY_NAME').process == 'processName' def testCommandsLoadedWithCorrectSearchString(self): assert self.config.getCommand('KEY_NAME').search == 'searchString' def testCommandsLoadedWithCorrectKillFlagWhenFalse(self): assert self.config.getCommand('KEY_NAME').needsKill == False def testCommandsLoadedWithCorrectKillFlagWhenTrue(self): assert self.config.getCommand('KILL_NAME').needsKill == True
class HtpcLauncherApp: def __init__(self, ostools = None, config = None): self.ostools = ostools if not self.ostools: self.ostools = OsTools() self.config = config if not self.config: self.config = Config() self._startLogging() self._loadConfig() self._startFileLogging() self.irReader = IrReader() self.processManager = ProcessManager() def run(self): try: self._processIrCode(self.config.getLaunchCommand()) while True: try: try: code = self.irReader.getNextCode() except IrReaderError: time.sleep(5) continue self._processIrCode(code) except KeyboardInterrupt: break except: self.log.exception('Unhandled exception') def _startFileLogging(self): formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s') filePath = os.path.expanduser(DEFAULT_LOG_FILE) self.log.info('Logging to %s', self.config.getLogPath()) handler = self.ostools.getRotatingLogHandler(self.config.getLogPath(), maxBytes = 1024000, backupCount = 5) handler.setFormatter(formatter) self.log.addHandler(handler) def _startLogging(self): self.log = logging.getLogger() self.log.setLevel(logging.DEBUG) self.log.addHandler(logging.StreamHandler()) def _loadConfig(self): self.log.info('Loading config file from home directory') configFile = self.ostools.openUserFile('.%s' % DEFAULT_CONF_FILE) if not configFile: self.log.info('Config not found in home directory; loading from system config') configFile = self.ostools.openSystemConfFile(DEFAULT_CONF_FILE) if not configFile: raise RuntimeError('Failed to find configuration file') self.config.load(configFile) def _processIrCode(self, code): command = self.config.getCommand(code) if not command: return self.processManager.execute(command)