def __init__(self, actionDict, validButtonNames, validClickTypeNames): self.actionDict = actionDict self.validActionNames = actionDict.getActionLambdaDict().keys() self.validConditionNames = actionDict.getConditionLambdaDict().keys() self.validButtonNames = validButtonNames self.validClickTypeNames = validClickTypeNames self.dbusButton = DbusButton() self.proximityButton = ProximityButton() self.lastTimeStamp = None self.resetConfig() self.initRegex()
class Config(): def __init__(self, actionDict, validButtonNames, validClickTypeNames): self.actionDict = actionDict self.validActionNames = actionDict.getActionLambdaDict().keys() self.validConditionNames = actionDict.getConditionLambdaDict().keys() self.validButtonNames = validButtonNames self.validClickTypeNames = validClickTypeNames self.dbusButton = DbusButton() self.proximityButton = ProximityButton() self.lastTimeStamp = None self.resetConfig() self.initRegex() def checkConfigFile(self): timestamp = self.getTimeStamp() print >> sys.stderr, timestamp if self.lastTimeStamp == None or self.lastTimeStamp != timestamp: try: print >> sys.stderr, "refreshing config" self.parseConfigFile() except: print >> sys.stderr, "INVALID CONFIG, USING DEFAULT" self.parse(self.getDefaultConfig()) self.lastTimeStamp = timestamp def getTimeStamp(self): if os.path.isfile(getConfigFilePath()): cmdArr = ["stat", "-t", getConfigFilePath()] out, err = subprocess.Popen(cmdArr, stdout=subprocess.PIPE).communicate() if err == None or len(err) == 0: return out else: return "error" else: return "missing" def getDefaultConfig(self): return ("#DEFAULT CONFIG\n" + "torchAutoShutOffTimeMs=300000\n" + "cameraDisabled=1\n" + "longClickDelayMs=400\n" + "doubleClickDelayMs=400\n" + "trebleClickDelayMs=600\n" + "dbusBufferMs=800\n" + "action=musicPlayPause,volumeUp,singleClick,screenLocked\n" + "action=musicNext,volumeDown,singleClick,screenLocked\n" + "action=musicPrev,volumeDown,doubleClick,screenLocked\n" + "action=clickCameraFocus,volumeUp,longClickStart,cameraAppFocused\n" + "action=clickCameraSnap,volumeUp,longClickStop,cameraAppFocused\n" + "action=clickCameraSnap,volumeUp,singleClick,cameraAppFocused\n" + "action=clickCameraFocus,proximitySensor,proximityEnter,cameraAppFocused\n" + "action=tap(69x67,69x67),volumeUp,singleClick,appFocused(frontcameravideo)\n" + "action=tap(802x253,802x253),volumeUp,singleClick,appFocused(rawcam)\n" ) def resetConfig(self): self.torchAutoShutOffTimeMs=300000 self.cameraDisabled=1 self.longClickDelayMs=400 self.doubleClickDelayMs=400 self.trebleClickDelayMs=600 self.dbusButton.setRepeatBufferMs(800) self.actionMapSet = ActionMapSet([]) def initRegex(self): self.integerRe = re.compile( "^\\s*(?P<key>[a-zA-Z0-9]+)" + "\\s*=\\s*" + "(?P<value>\d+)\\s*(#.*)?$") self.strRe = re.compile( "^\\s*(?P<key>[a-zA-Z0-9]+)" + "\\s*=\\s*" + "(?P<value>.*?)\\s*(#.*)?$") self.commentRe = re.compile("^\\s*#") self.emptyRe = re.compile("^\\s*$") self.actionMapRe = re.compile("" + "^" + "\\s*action\\s*=\\s*" + "(?P<actionName>" + "|".join(self.validActionNames) + ")" + "(?:" + "\(" + "(?P<actionParam>[^)]*)" + "\)" + ")?" + "\\s*,\\s*" + "(?P<button>" + "|".join(self.validButtonNames) + ")" + "(?:" + "\(" + "(?P<buttonParam>[^)]*)" + "\)" + ")?" + "\\s*,\\s*" + "(?P<clickType>" + "|".join(self.validClickTypeNames) + ")" + "\\s*,\\s*" + "(?P<condName>" + "|".join(self.validConditionNames) + ")" + "(?:" + "\(" + "(?P<condParam>[^)]*)" + "\)" + ")?" + "\\s*(#.*)?" + "$" ) def getConfigFileContent(self): if os.path.isfile(getConfigFilePath()): return open(getConfigFilePath(),"rb").read() else: return None def parseConfigFile(self): confText = self.getConfigFileContent() if confText == None: confText = self.getDefaultConfig() print "WARNING: no config file at '" + getConfigFilePath() + "'" print "Using default config:\n" + confText self.parse(confText) def parse(self, confText): self.resetConfig() actionMaps = [] for line in confText.splitlines(): actionMapMatch = self.actionMapRe.match(line) integerMatch = self.integerRe.match(line) commentMatch = self.commentRe.match(line) emptyMatch = self.emptyRe.match(line) key = None if integerMatch != None: key = integerMatch.group("key") val = int(integerMatch.group("value")) if actionMapMatch != None: actionMaps.append(ActionMap( self.actionDict, actionName = actionMapMatch.group("actionName"), actionParam = actionMapMatch.group("actionParam"), condName = actionMapMatch.group("condName"), condParam = actionMapMatch.group("condParam"), button = actionMapMatch.group("button"), buttonParam = actionMapMatch.group("buttonParam"), clickType = actionMapMatch.group("clickType"), )) elif key == "torchAutoShutOffTimeMs": self.torchAutoShutOffTimeMs = val elif key == "cameraDisabled": self.cameraDisabled = val elif key == "longClickDelayMs": self.longClickDelayMs = val elif key == "doubleClickDelayMs": self.doubleClickDelayMs = val elif key == "trebleClickDelayMs": self.trebleClickDelayMs = val elif key == "dbusBufferMs": self.dbusButton.setRepeatBufferMs(val) elif commentMatch == None and emptyMatch == None: print >> sys.stderr, "Unparseable config entry: " + line raise Exception("Unparseable config entry: " + line) self.actionMapSet = ActionMapSet(actionMaps) def getActionMapSet(self): return self.actionMapSet
class Config(): def __init__(self, actionDict, validButtonNames, validClickTypeNames): self.actionDict = actionDict self.validActionNames = actionDict.getActionLambdaDict().keys() self.validConditionNames = actionDict.getConditionLambdaDict().keys() self.validButtonNames = validButtonNames self.validClickTypeNames = validClickTypeNames self.dbusButton = DbusButton() self.proximityButton = ProximityButton() self.lastTimeStamp = None self.resetConfig() self.initRegex() def checkConfigFile(self): timestamp = self.getTimeStamp() if self.lastTimeStamp == None or self.lastTimeStamp != timestamp: print >> sys.stderr, "refreshing config" self.ensureUserConf() try: self.parse(self.readConf(getUserConfigFilePath())) except: print >> sys.stderr, "INVALID CONFIG, ATTEMPTING TO USE DEFAULT" self.parse(self.readConf(getSystemConfigFilePath())) self.lastTimeStamp = timestamp def getTimeStamp(self): if os.path.isfile(getUserConfigFilePath()): cmdArr = ["stat", "-t", getUserConfigFilePath()] out, err = subprocess.Popen(cmdArr, stdout=subprocess.PIPE).communicate() if err == None or len(err) == 0: return out else: return "error" else: return "missing" def resetConfig(self): self.torchAutoShutOffTimeMs=300000 self.cameraDisabled=0 self.quickSnapShutterSound=CAMERA_SOUND self.quickSnapSaveSound='' self.quickSnapShutterLedPattern=LedPattern('blink') self.quickSnapSaveLedPattern=LedPattern('doubleblink') self.longClickDelayMs=400 self.doubleClickDelayMs=400 self.trebleClickDelayMs=600 self.dbusButton.setPatternDelayMs(1500) self.dbusButton.clearButtonSyns() self.actionMapSet = ActionMapSet([]) def initRegex(self): self.intRe = re.compile( "^\\s*(?P<key>[a-zA-Z0-9]+)" + "\\s*=\\s*" + "(?P<value>\d+)\\s*(#.*)?$") self.strRe = re.compile( "^\\s*(?P<key>[a-zA-Z0-9]+)" + "\\s*=\\s*" + "(?P<value>.*?)\\s*(#.*)?$") self.commentRe = re.compile("^\\s*#") self.emptyRe = re.compile("^\\s*$") self.actionMapRe = re.compile("" + "^" + "\\s*action\\s*=\\s*" + "(?P<actionName>" + "|".join(self.validActionNames) + ")" + "(?:" + "\(" + "(?P<actionParam>[^)]*)" + "\)" + ")?" + "\\s*,\\s*" + "(?P<button>" + "|".join(self.validButtonNames) + ")" + "(?:" + "\(" + "(?P<buttonParam>[^)]*)" + "\)" + ")?" + "\\s*,\\s*" + "(?P<clickType>" + "|".join(self.validClickTypeNames) + ")" + "\\s*,\\s*" + "(?P<condName>" + "|".join(self.validConditionNames) + ")" + "(?:" + "\(" + "(?P<condParam>[^)]*)" + "\)" + ")?" + "\\s*(#.*)?" + "$" ) self.dbusButtonSynRe = re.compile("" + "^" + "\\s*dbusButtonSyn\\s*=\\s*" + "(?P<syn>[a-zA-Z0-9_\\-]+)" + "\\s*(?:=>|,|->)\\s*" + "(?P<target>[a-zA-Z0-9_\\-]+)" + "$" ) def readConf(self, confFile): if os.path.isfile(confFile): return open(confFile,"rb").read() else: return None def ensureUserConf(self): userConf = getUserConfigFilePath() if not os.path.isfile(userConf): print "WARNING: no config file at '" + userConf + "'" print "Copying default config:\n" os.system("cp " + getSystemConfigFilePath() + " " + userConf) def parse(self, confText): self.resetConfig() actionMaps = [] for line in confText.splitlines(): actionMapMatch = self.actionMapRe.match(line) dbusButtonSynMatch = self.dbusButtonSynRe.match(line) intMatch = self.intRe.match(line) strMatch = self.strRe.match(line) commentMatch = self.commentRe.match(line) emptyMatch = self.emptyRe.match(line) intKey = None if intMatch != None: intKey = intMatch.group("key") intVal = int(intMatch.group("value")) strKey = None if strMatch != None: strKey = strMatch.group("key") strVal = strMatch.group("value") if actionMapMatch != None: actionMaps.append(ActionMap( self.actionDict, actionName = actionMapMatch.group("actionName"), actionParam = actionMapMatch.group("actionParam"), condName = actionMapMatch.group("condName"), condParam = actionMapMatch.group("condParam"), button = actionMapMatch.group("button"), buttonParam = actionMapMatch.group("buttonParam"), clickType = actionMapMatch.group("clickType"), )) elif dbusButtonSynMatch != None: self.dbusButton.addButtonSyn( dbusButtonSynMatch.group("syn"), dbusButtonSynMatch.group("target")) elif intKey == "torchAutoShutOffTimeMs": self.torchAutoShutOffTimeMs = intVal elif intKey == "cameraDisabled": self.cameraDisabled = intVal elif intKey == "longClickDelayMs": self.longClickDelayMs = intVal elif intKey == "doubleClickDelayMs": self.doubleClickDelayMs = intVal elif intKey == "trebleClickDelayMs": self.trebleClickDelayMs = intVal elif intKey == "dbusPatternDelayMs": self.dbusButton.setPatternDelayMs(intVal) elif strKey == "quickSnapShutterSound": self.quickSnapShutterSound = strVal elif strKey == "quickSnapSaveSound": self.quickSnapSaveSound = strVal elif strKey == "quickSnapShutterLedPattern": self.quickSnapShutterLedPattern = LedPattern(strVal) elif strKey == "quickSnapSaveLedPattern": self.quickSnapSaveLedPattern = LedPattern(strVal) elif commentMatch == None and emptyMatch == None: print >> sys.stderr, "Unparseable config entry: " + line raise Exception("Unparseable config entry: " + line) self.actionMapSet = ActionMapSet(actionMaps) def getActionMapSet(self): return self.actionMapSet