def putCode(keyhandles, parser, commands, added_qualifiers): keys = parser.getKeys() + parser.getQualifiersWithoutFocusAndMouse( ) + added_qualifiers firstkey = keys.pop(0) command = " ".join(commands) pythoncall = commands[0] + "(" + ",".join(commands[1:]) + ")" #print "addkeybinding:", commands, keysub[firstkey] + " " + " ".join(sorted(map(lambda x:keysub[x],keys))), pythoncall ra.addKeybinding( command, keysub[firstkey] + " " + " ".join(sorted(map(lambda x: keysub[x], keys)))) #pythoncall=commands.pop(0) #pythoncall+="(" #while len(commands)>1: # pythoncall+=commands.pop(0)+"," # if len(commands)>0: # pythoncall+=commands.pop(0) # pythoncall+=")" if pythoncall == "ra.undo()": print "adding \"%s\", line: %d, firstkey: %d, keys: %s" % ( pythoncall, parser.getCurrLineNum(), firstkey, keys) if keyhandles[firstkey].addHandle( keys, compile(pythoncall, '<string>', 'single')) == False: # Note. This doesn't happen anymore. Redefining a keybinding is allowed. message = "Keybindings for command \"%s\" in line %d is already used" % ( pythoncall, parser.getCurrLineNum()) print message ra.addMessage(message) return False return True
def parse_user_keys(): import codecs if not ra.hasConfPath("keys"): return user_key_file = ra.getConfPath("keys") try: f = open(user_key_file, 'r') filecontent = f.read() f.close() except: try: filecontent = codecs.open(user_key_file, "r", "latin-1").read() except: return for line in filecontent.split('\n'): line = line.split('#')[0].strip() if len(line) == 0: continue elif '=' not in line: print '"Error: Malformed line in "' + user_key_file + '": ' + "'" + line + "'" ra.addMessage('"Error: Malformed line in "' + user_key_file + '": ' + "'" + line + "'") sys.exit(-1) else: key, value = line.split("=") code2read[key.strip()] = value.strip()
def putCode(keyhandles,parser,commands,added_qualifiers): keys=parser.getKeys()+parser.getQualifiers()+added_qualifiers firstkey=keys.pop(0) command = " ".join(commands) pythoncall = commands[0] + "(" + ",".join(commands[1:]) + ")" #print "addkeybinding:", commands, keysub[firstkey] + " " + " ".join(sorted(map(lambda x:keysub[x],keys))), pythoncall ra.addKeybinding(command, keysub[firstkey] + " " + " ".join(sorted(map(lambda x:keysub[x],keys)))) #pythoncall=commands.pop(0) #pythoncall+="(" #while len(commands)>1: # pythoncall+=commands.pop(0)+"," # if len(commands)>0: # pythoncall+=commands.pop(0) # pythoncall+=")" if pythoncall=="ra.undo()": print "adding \"%s\", line: %d, firstkey: %d, keys: %s" % (pythoncall,parser.getCurrLineNum(),firstkey,keys) if keyhandles[firstkey].addHandle(keys,compile(pythoncall,'<string>','single'))==False: # Note. This doesn't happen anymore. Redefining a keybinding is allowed. message = "Keybindings for command \"%s\" in line %d is already used" % (pythoncall , parser.getCurrLineNum()) print message ra.addMessage(message) return False return True
def __init__(self, filename): if not '_keybindingsdict' in dir(ra): ra.addMessage("Error. Unable to generate menues.") try: keybindingsdict = ra._keybindingsdict except: print sys.exc_info() message = traceback.format_exc() print message ra.addMessage("Unable to generate menues. (" + str(sys.exc_info()) + ")") #print "AAAAAAAAAAAA",keybindingsdict file = open(filename, 'r') self.lines = map( lambda x: self.constructmenuitemstring(string.split(x, "|"), keybindingsdict), filter(lambda x: len(x) > 0 and x[0] != "#", map(lambda x: string.rstrip(x), file.readlines()))) file.close() self.currline = 0
def parse_and_show_errors(): global keyhandles old_stdout = sys.stdout old_stderr = sys.stderr if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()): sys.stdout = NullWriter() sys.stderr = NullWriter() ret = False old_keyhandles = keyhandles try: resetKeyHandles() ret = parse() except: print sys.exc_info() #radium.addMessage("Couldn't create keybindings dict. ("+str(sys.exc_info())+")") message = traceback.format_exc() print message #radium.addMessage("Loading "+filename+" failed.") # If this is a valid module file, please send it to [email protected] ("+str(e)+")") # for m in message.split("\n"): ra.addMessage("Couldn't parse keybindings file.\n\nBacktrace:<pre>"+message+"</pre>") keyhandles = old_keyhandles if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()): sys.stdout = old_stdout sys.stderr = old_stderr return ret
def get_file_handles(): try: infilehandle=open(sys.argv[1],'r') except: print "Cant open1 -%s-" % sys.argv[1] ra.addMessage("Cant open %s" % sys.argv[1]) sys.exit(1) try: infilehandle2=open(sys.argv[2],'r') except: print "Cant open2 -%s-" % sys.argv[2] infilehandle2 = None #print "Cant open %s" % sys.argv[2], infilehandle2 #sys.exit(-1) try: outfilehandle=open(os.path.join(os.path.expanduser("~"), ".radium", "generated_keybinding_code.py"),'w+') outfilehandle.write("#Do not edit. This file is automatically generated from keybindings.config.\n") except: message = "Could not write to \"" + os.path.join(os.path.expanduser("~"), ".radium", "generated_keybinding_code.py") + "\"." print message ra.addMessage(message) outfilehandle = False return infilehandle, infilehandle2, outfilehandle
def get_file_handles(): try: infilehandle = open(sys.argv[1], 'r') except: print "Cant open1 -%s-" % sys.argv[1] ra.addMessage("Cant open %s" % sys.argv[1]) sys.exit(1) try: infilehandle2 = open(sys.argv[2], 'r') except: print "Cant open2 -%s-" % sys.argv[2] infilehandle2 = None #print "Cant open %s" % sys.argv[2], infilehandle2 #sys.exit(-1) try: outfilehandle = open( os.path.join(os.path.expanduser("~"), ".radium", "generated_keybinding_code.py"), 'w+') outfilehandle.write( "#Do not edit. This file is automatically generated from keybindings.config.\n" ) except: message = "Could not write to \"" + os.path.join( os.path.expanduser("~"), ".radium", "generated_keybinding_code.py") + "\"." print message ra.addMessage(message) outfilehandle = False return infilehandle, infilehandle2, outfilehandle
def parse_and_show_errors(): global keyhandles old_stdout = sys.stdout old_stderr = sys.stderr if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()): sys.stdout = NullWriter() sys.stderr = NullWriter() ret = False old_keyhandles = keyhandles try: resetKeyHandles() ret = parse() except: print sys.exc_info() #radium.addMessage("Couldn't create keybindings dict. ("+str(sys.exc_info())+")") message = traceback.format_exc() print message #radium.addMessage("Loading "+filename+" failed.") # If this is a valid module file, please send it to [email protected] ("+str(e)+")") # for m in message.split("\n"): ra.addMessage("Couldn't parse keybindings file.\n\nBacktrace:<pre>" + message + "</pre>") keyhandles = old_keyhandles if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()): sys.stdout = old_stdout sys.stderr = old_stderr return ret
def gotKey(windownum,key,keys): try: global keyhandles #print "*********** key: " + keysub[key] + ". keys: " + str(map(lambda k:keysub[k], keys)) return keyhandles[key].exe(windownum,keys); except: traceback.print_exc(file=sys.stdout) ra.addMessage("Keybinding error.\n\nBacktrace:<pre>" + traceback.format_exc() + "</pre>") return True
def get_tracks(filename): if filename=="": filename = radium.getLoadFilename("Choose midi file", "*.mid *.MID *.midi *.MIDI") if not filename or filename=="": return False try: tracks = midi.read_midifile(filename) except: radium.addMessage("Could not read "+filename+". Either file doesn't exist, or it could not be read as a standard midi file."); return False return tracks
def putCode(keyhandles, parser, codestring, added_qualifiers): keys = parser.getKeys() + parser.getQualifiers() + added_qualifiers firstkey = keys.pop(0) #print "adding \"%s\", line: %d, firstkey: %d, keys: %s" % (codestring,parser.getCurrLineNum(),firstkey,keys) if keyhandles[firstkey].addHandle( keys, compile(codestring, '<string>', 'single')) == False: # Note. This doesn't happen anymore. Redefining a keybinding is allowed. message = "Keybindings for command \"%s\" in line %d is already used" % ( codestring, parser.getCurrLineNum()) print message radium.addMessage(message) return False return True
def get_tracks(filename): if filename == "": filename = radium.getLoadFilename("Choose midi file", "*.mid *.MID *.midi *.MIDI") if not filename or filename == "": return False try: tracks = midi.read_midifile(filename) except: radium.addMessage( "Could not read " + filename + ". Either file doesn't exist, or it could not be read as a standard midi file." ) return False return tracks
def get_tracks(filename): if filename == "": filename = radium.getLoadFilename("Choose midi file", "*.mid *.MID *.midi *.MIDI") if not filename or filename == "" or radium.isIllegalFilepath(filename): return False try: filename = radium.getPathString(filename) tracks = midi.read_midifile(filename) except: message = traceback.format_exc() radium.addMessage( "Gakk. Could not read " + filename + ". Either file doesn't exist, or it could not be read as a standard midi file.\n\n" + message) return False return tracks
def nextLine(self): if not self.readNextLine(): return "ENDFILE" if self.currline[0:1] == '*': if self.linetype == "GOINGTOINSERTCODE": self.linetype = "INSERTCODELAST" else: if self.linetype == "INSERTCODE": self.linetype = "INSERTCODELAST" else: if self.linetype == "INSERTCLEANCODE": self.linetype = "INSERTCLEANCODELAST" else: self.linetype = "GOINGTOINSERTCLEANCODE" else: if self.linetype == "GOINGTOINSERTCODE": self.linetype = "INSERTCODE" if self.linetype == "GOINGTOINSERTCLEANCODE": self.linetype = "INSERTCLEANCODE" if self.linetype == "INSERTCODELAST" or self.linetype == "INSERTCLEANCODELAST": self.linetype = "NORMAL" if self.linetype == "NORMAL" or self.linetype == "SKIP": if self.currline[-2:] == "* ": self.linetype = "GOINGTOINSERTCODE" parts = self.parser.getParts() keys = [] self.command = [] for lokke in range(len(parts)): if parts[lokke] == ":": keys = parts[:lokke] self.command = parts[lokke + 1:] break lokke = 0 while lokke < len(keys): key = tuple_has_key(keysub, keys[lokke]) if key == -1: key = tuple_has_key(qualsub, keys[lokke]) if (key != -1): key += tuple_has_key(keysub, "CTRL_L") if key == -1: message = "Unknown key \"" + keys[ lokke] + "\" in line %d in keyconfig file." % self.linenum print message ra.addMessage(message) del keys[lokke] self.linetype = "ERROR" return "OK" else: keys[lokke] = key lokke += 1 keys.sort() new_keys = [] self.qualifiers = [] for key in keys: if key_is_qualifier(key): self.qualifiers.append(key) else: new_keys.append(key) new_keys.sort() self.qualifiers.sort() if len(keys) > len(self.qualifiers): # ???????? self.keys = new_keys if self.linetype != "GOINGTOINSERTCODE": if len(self.command) == 0: self.linetype = "SKIP" else: self.linetype = "NORMAL" #print "------------------------------------------>" #print "command: %s" % self.command #print "self.qualifiers: %s" % str(map(lambda k:keysub[k], self.qualifiers)) #print "self.keys: %s " % str(map(lambda k:keysub[k], self.keys)) #print "<------------------------------------------" return "OK"
def parse(): global keyhandles keybindingsdict = {} filehandle, filehandle2, outfilehandle = get_file_handles() parser = Parser(filehandle, filehandle2) defnum = 0 reader = protoconfparser.Read() while parser.nextLine() != "ENDFILE": if outfilehandle: if parser.getLineType() == "GOINGTOINSERTCODE": outfilehandle.write("def radium_generated_keycodedef%d():\n" % defnum) if parser.getLineType() == "INSERTCODE": outfilehandle.write(parser.getCurrLine() + "\n") if parser.getLineType() == "INSERTCODELAST": #outfilehandle.write("\treturn\n") outfilehandle.write("\n") addIt(keyhandles, parser, "keybinding.radium_generated_keycodedef%d()" % defnum) defnum += 1 if parser.getLineType() == "INSERTCLEANCODE": outfilehandle.write(parser.getCurrLine() + "\n") if parser.getLineType() == "ERROR": return False if parser.getLineType() == "NORMAL": commands = parser.getCommands() lokke = 2 while lokke < len(commands): if commands[lokke][0] == "+": add = int(commands.pop(lokke)[1:]) commands[lokke - 1] = "%d" % (int(commands[lokke - 1]) + add) elif commands[lokke][0] == "-": add = int(commands.pop(lokke)[1:]) commands[lokke - 1] = "%d" % (int(commands[lokke - 1]) - add) elif commands[lokke][0] == "*": add = int(commands.pop(lokke)[1:]) commands[lokke - 1] = "%d" % (int(commands[lokke - 1]) * add) elif commands[lokke][0] == "/": add = int(commands.pop(lokke)[1:]) commands[lokke - 1] = "%d" % (int(commands[lokke - 1]) / add) lokke += 1 command = commands[0] for c in commands[1:]: command += " " + c keybindingsdict[command] = [ map(lambda x: keysub[x], parser.getKeys()), map(lambda x: keysub[x], parser.getQualifiers()) ] #print "commands", commands pythoncall = commands.pop(0) pythoncall += "(" while len(commands) > 1: pythoncall += commands.pop(0) + "," if len(commands) > 0: pythoncall += commands.pop(0) pythoncall += ")" keys = parser.getKeys() + parser.getQualifiers() firstkey = keys.pop(0) #printsak(0,keyhandles,parser,command) addIt(keyhandles, parser, pythoncall) try: radium._keybindingsdict = keybindingsdict except: print sys.exc_info() radium.addMessage("Couldn't create keybindings dict. (" + str(sys.exc_info()) + ")") try: if filehandle: filehandle.close() if filehandle2: filehandle2.close() if outfilehandle: outfilehandle.close() except: print "Could not close file. Out of disk space?" #sys.exit(3) reload(keybinding ) # i.e re-do "import generated_keybinding_code as keybinding" #sys.exit(1) #print "BBBBBB",radium._keybindingsdict return True
ra=radium print "Parsing keybindings.conf..." #profile.run("KeyConfer(infilehandle,outfilehandle)","fooprof") try: keybindingsparser.init(sys.argv[1], sys.argv[2]) except: print sys.exc_info() #radium.addMessage("Couldn't create keybindings dict. ("+str(sys.exc_info())+")") message = traceback.format_exc() #radium.addMessage("Loading "+filename+" failed.") # If this is a valid module file, please send it to [email protected] ("+str(e)+")") # for m in message.split("\n"): radium.addMessage("Couldn't parse keybindings file.\n\nBacktrace:"+message) sys.exit(6) # Wait parsing keybindings. If we do it here, we run code in keybindings.conf, but the program isn't finished initializing at this point. #if keybindingsparser.parse_and_show_errors()==False: # sys.exit(5) # Hack to fix generated_keybinding_code to load during startup. This is not necessary when calling reloadKeybindings(), so I don't know why this is. Maybe some file cache thing. # Didn't work (I thought it worked once, but maybe not). Instead we call reloadKeybindings() in Qt_Main.cpp after startup.. #if keybindingsparser.parse_and_show_errors()==False: # sys.exit(50) #import os #os.system("/usr/bin/givertcap")
ra = radium print "Parsing keybindings.conf..." #profile.run("KeyConfer(infilehandle,outfilehandle)","fooprof") try: keybindingsparser.init(sys.argv[1], sys.argv[2]) except: print sys.exc_info() #radium.addMessage("Couldn't create keybindings dict. ("+str(sys.exc_info())+")") message = traceback.format_exc() #radium.addMessage("Loading "+filename+" failed.") # If this is a valid module file, please send it to [email protected] ("+str(e)+")") # for m in message.split("\n"): radium.addMessage("Couldn't parse keybindings file.\n\nBacktrace:" + message) sys.exit(6) if keybindingsparser.parse_and_show_errors() == False: sys.exit(5) import generated_keybinding_code as keybinding #import os #os.system("/usr/bin/givertcap") pid = os.getpid() # Seems like Ctrl+C is handled properly without this code now. This block is also disabled now since it sometimes caused the main process hang if program crashed, and you had to run "killall -9 radium" or similar. if False: #hasattr(os,'fork') and os.fork()==0: import signal, time
def nextLine(self): if not self.readNextLine(): return "ENDFILE" if self.currline[0:1]=='*': if self.linetype=="GOINGTOINSERTCODE": self.linetype="INSERTCODELAST" else: if self.linetype=="INSERTCODE": self.linetype="INSERTCODELAST" else: if self.linetype=="INSERTCLEANCODE": self.linetype="INSERTCLEANCODELAST" else: self.linetype="GOINGTOINSERTCLEANCODE" else: if self.linetype=="GOINGTOINSERTCODE": self.linetype="INSERTCODE" if self.linetype=="GOINGTOINSERTCLEANCODE": self.linetype="INSERTCLEANCODE" if self.linetype=="INSERTCODELAST" or self.linetype=="INSERTCLEANCODELAST": self.linetype="NORMAL" if self.linetype=="NORMAL" or self.linetype=="SKIP": if self.currline[-2:]=="* ": self.linetype="GOINGTOINSERTCODE" parts=self.parser.getParts() keys=[] self.command=[] for lokke in range(len(parts)): if parts[lokke]==":": keys=parts[:lokke] self.command=parts[lokke+1:] break lokke=0 while lokke<len(keys): key=tuple_has_key(keysub,keys[lokke]) if key==-1: key=tuple_has_key(qualsub,keys[lokke]) if(key!=-1): key+=tuple_has_key(keysub,"CTRL_L") if key==-1: message = "Unknown key \""+keys[lokke] +"\" in line %d in keyconfig file." % self.linenum print message ra.addMessage(message) del keys[lokke] self.linetype="ERROR" return "OK" else: keys[lokke]=key lokke+=1 keys.sort() new_keys = [] self.qualifiers = [] for key in keys: if key_is_qualifier(key): self.qualifiers.append(key) else: new_keys.append(key) new_keys.sort() self.qualifiers.sort() if len(keys)>len(self.qualifiers): # ???????? self.keys=new_keys if self.linetype!="GOINGTOINSERTCODE": if len(self.command)==0: self.linetype="SKIP" else: self.linetype="NORMAL" #print "------------------------------------------>" #print "command: %s" % self.command #print "self.qualifiers: %s" % str(map(lambda k:keysub[k], self.qualifiers)) #print "self.keys: %s " % str(map(lambda k:keysub[k], self.keys)) #print "<------------------------------------------" return "OK"