def db_exists(self): import os.path if hasattr(pyco, 'pyco_home'): db_file = '%s/%s' % (pyco.pyco_home, DEVICE_REPO) else: db_file = '/tmp/%s' % DEVICE_REPO try: if not os.path.isfile(db_file): log.debug('creating cache [%s] ...' % db_file) self.createDB('sqlite://%s' % db_file) except Exception as e: log.error('fatal error: %s' % e)
def patternMatch(self, target, checkPoint, patternsExt, maxWaitTime): target.currentEvent = Event('do-nothing-event') log.debug("entering patternMatch, checkpoint is [%s]" % (checkPoint)) log.debug("exactPatternMatch [%s]" % target.exactPatternMatch) response = '' while not (checkPoint (target) or target.currentEvent.isTimeout()): patterns = target.patterns(target.state) + patternsExt # expect and match try: log.debug("[%s] matching [%s]" % (target.state, patterns)) #log.debug("PRE exp before: [%s] - after: [%s]" % (self.pipe.before, self.pipe.after)) if target.exactPatternMatch: index = self.pipe.expect_exact(patterns, maxWaitTime) else: index = self.pipe.expect(patterns, maxWaitTime) try: target.currentEvent = Event(target.get_event(patterns[index])) except Exception as e: if patterns[index] == TIMEOUT: log.debug("[%s]: exception timeout triggered" % target.name) target.currentEvent = Event('timeout', propagateToFsm = True) else: log.error("[%s]: event not registered for pattern: [%s]" % (target.name, patterns[index])) raise log.debug("matched [%s] pattern [%s] --> [%s]" % (index, patterns[index], target.currentEvent.name)) log.debug("before: [%s] - after: [%s]" % (self.pipe.before, self.pipe.after)) except EOF: log.debug("[%s] connection unexpectedly closed (%s)" % (target.name, self.pipe.before)) target.currentEvent = Event('eof') except TIMEOUT: log.debug("[%s] connection timed out, unmatched output: [%s]" % (target.name, self.pipe.before)) target.currentEvent = Event('timeout') #log.debug("detected event [%s]" % target.currentEvent) if target.has_event_handlers(target.currentEvent): log.debug("[%s] got [%s] event; invoking handlers: [%s]" % (target.name, target.currentEvent.name, target.get_event_handlers(target.currentEvent))) for eh in target.get_event_handlers(target.currentEvent): eh(target) stateChanged = target.process(target.currentEvent) response += self.pipe.before if isinstance(self.pipe.after, str) and not target.currentEvent.isPromptMatch(): response += self.pipe.after return response
def patternMatch(self, target, checkPoint, patternsExt, maxWaitTime): target.currentEvent = Event('do-nothing-event') log.debug("entering patternMatch, checkpoint is [%s]" % (checkPoint)) log.debug("exactPatternMatch [%s]" % target.exactPatternMatch) response = '' while not (checkPoint(target) or target.currentEvent.isTimeout()): patterns = target.patterns(target.state) + patternsExt # expect and match try: log.debug("[%s] matching [%s]" % (target.state, patterns)) #log.debug("PRE exp before: [%s] - after: [%s]" % (self.pipe.before, self.pipe.after)) if target.exactPatternMatch: index = self.pipe.expect_exact(patterns, maxWaitTime) else: index = self.pipe.expect(patterns, maxWaitTime) try: target.currentEvent = Event( target.get_event(patterns[index])) except Exception as e: if patterns[index] == TIMEOUT: log.debug("[%s]: exception timeout triggered" % target.name) target.currentEvent = Event('timeout', propagateToFsm=True) else: log.error( "[%s]: event not registered for pattern: [%s]" % (target.name, patterns[index])) raise log.debug("matched [%s] pattern [%s] --> [%s]" % (index, patterns[index], target.currentEvent.name)) log.debug("before: [%s] - after: [%s]" % (self.pipe.before, self.pipe.after)) except EOF: log.debug("[%s] connection unexpectedly closed (%s)" % (target.name, self.pipe.before)) target.currentEvent = Event('eof') except TIMEOUT: log.debug("[%s] connection timed out, unmatched output: [%s]" % (target.name, self.pipe.before)) target.currentEvent = Event('timeout') #log.debug("detected event [%s]" % target.currentEvent) if target.has_event_handlers(target.currentEvent): log.debug("[%s] got [%s] event; invoking handlers: [%s]" % (target.name, target.currentEvent.name, target.get_event_handlers(target.currentEvent))) for eh in target.get_event_handlers(target.currentEvent): eh(target) stateChanged = target.process(target.currentEvent) response += self.pipe.before if isinstance(self.pipe.after, str) and not target.currentEvent.isPromptMatch(): response += self.pipe.after return response