def __init__(self, basetime, filename, mode='a'): """Creates a logger utility, will store results in a python list, and dump them at the end when the log is closed...""" self.filename = filename self.baseTime = basetime self.mode = 'a' self.logList = [] if self.filename is 'nolog': self.logging = False return ts = time.time() - self.baseTime try: with open(self.filename, self.mode) as output: output.write("Log created at: ") output.write("%07.5f:EOL\n" % ts) self.logging = True except Exception, e: printOut( "Cannot create log file at " + self.filename + "this log file will not be valid:", 0) self.logging = False print e
def stopLog(self): """ Stop logging. Opens the file (usually in append mode), dumps the string list, closes the file. :return: None """ if (not self.logging) and (self.filename != 'nolog'): printOut("Trying to stop an invalid logger\n", 0) return ts = time.time() - self.baseTime text = ''.join([s for s in self.logList]) try: with open(self.filename, self.mode) as output: # fastest method to concatenate strings in Python # then do ONE single write to file output.write(text) output.write("%07.5f:EOL\n" % ts) # file is closed after "with" clause except Exception, e: print e printOut("Warning: Failed writing to log file %s" % self.filename, 0) printOut("Trying temporary file...", 0) tempName = str(random.randint) with open(tempName, mode='w') as tempfile: tempfile.write(text) tempfile.write("%07.5f:EOL\n" % ts) printOut("Log dumped to file %s\n" % tempName, 0)
def __init__(self, newTransitions, elements): self.transitions = newTransitions self.states = elements # fix end transition to do nothing self.transitions['end'] = {} # check that the FSM is correct, meaning at least # that all states mentioned in the transitions exist. missing = [ el for el in self.transitions.keys() if el not in self.states.keys()] if len(missing) > 0: printOut("Fatal error, the FSM has names that are not defined",0) printOut("Missing elements: %s" % missing,0) self.valid = False else: self.valid = True
def splitTransitionString(transition): try: if ':' in transition: events = transition.split(':')[1].strip() events = events.split(',') else: events = ['auto'] # parse the transition "fromA,fromB @ toA,toB : whenEvt" temp = transition.split('@') temp[1] = temp[1].split(':')[0] fromStates = [f.strip() for f in temp[0].strip().split(',')] toStates = [f.strip() for f in temp[1].strip().split(',')] return (fromStates, toStates, events) except: printOut("Malformed transition: " + transition, 0) printOut("fix the file and press ctrl+R")
def __init__(self, **kwargs): # build basic element # this sets all the attributes specified in the generic XML super(Calib, self).__init__(**kwargs) # window ratio set during World constructor. window_ratio = self.config.world.camera.ratio # speed from point to point self.moveSpeed = float(self.xmlConfig.moveSpeed) # fixation time self.fixTime = float(self.xmlConfig.fixationTime) # load background for calibration if exists if (self.xmlConfig.backTexture is not "None"): node = loader.loadModel("models/plane") node.setName("background") try: texture = loader.loadTexture(self.xmlConfig.backTexture) #texture.setMinfilter(Texture.FTLinearMipmapLinear) #texture.setAnisotropicDegree(2) node.setTexture(texture) node.reparentTo(aspect2d) # in Aspect2D, the size is 2*ratio in width and 2 in height node.setScale(2 * window_ratio, 1.0, 2.0) node.reparentTo(self.hudNP) except: printOut( "Failed to load texture: " + self.xmlConfig.backTexture, 2) sys.exit() # creates an empty calibration point calPoint = calStimuli(self.hudNP) # loads textures for the stimuli: (check out the XML calconfig.xml) textures = self.xmlConfig.stimuliTex.texture for t in textures: # load textures texture = loader.loadTexture(t.name) scale = float(t.scale) order = int(t.order) name = t.nodename if (t.shrink == 'None'): shrink = None else: shrink = float(t.shrink) if (texture): calPoint.addTexture(name, texture, scale, order, shrink) # once textured have been loaded, create scale (up/down) intervals # and store them in the calPoint class. They can be used regardless of # the movement. calPoint.createScaleIntervals(self.fixTime) # keep a reference self.calPoint = calPoint # read calibration points from config file self.calPositions = [] self.calPositions.append(Vec2(0, 0)) # On how to use EVAL safely: http://lybniz2.sourceforge.net/safeeval.html try: randomCalPoints = eval(self.calConfig.calPoints.random) except: # if it's not True then default to False randomCalPoints = False if (not randomCalPoints): xmlPos = self.xmlConfig.calPoints.calPoint # convert to floats and append to list # also rescale them # Rescale width from 0..1 to -ratio..ratio # Rescale height from 0..1 to -1..1 for p in xmlPos: p = map(float, p.pos.split(',')) p[0] = (p[0] * 2 * window_ratio) - window_ratio p[1] = 1 - (p[1] * 2) self.calPositions.append(Vec2(p[0], p[1])) # animation string when moving from one calib point to another self.animCurve = self.xmlConfig.animation # initial position for the calibration point (centre ?) pos = self.calPositions[0] self.calPoint.setPosition(pos.x, pos.y) self.currCalPoint = 1