class ShortcutDict(dict): index = 0 def __init__(self,name=None): if name is None: ShortcutDict.index += 1 name = "Untitled{}".format(ShortcutDict.index) self.name = name def setAction(self,key,action): self[key]=action def getAction(self,key): return self[key] def hasAction(self,key): return key in self class ShortcutStack(list): def __init__(self): pass def top(self): return self[0] def setAction(self,key,action): 'Sets an action -- can be any object' self.top().setAction(key,action) def getAction(self,key): 'Gets an action' for x in self: if x.hasAction(key): return x.getAction(key) else: raise KeyError(key) def hasAction(self,key): for x in self: if x.hasAction(key): return True def push(self,sdict): 'Pushes sdict onto the top of the stack' self[0:0] = [sdict] def pop(self): 'Removes top element of the stack and returns it' s = self[0] # Will raise IndexError del(self[0]) return s def peek(self): 'Returns top element of the stack' return self[0] def drop(self): del(self[0]) def __str__(self): 'Returns a list of the names of the ShortcutDicts' return "Shortcuts: {}".format(", ".join(list(map(lambda t: t.name,self)))) def pushStack(self,sstack): for x in reversed(sstack): self.push(x) def dropMany(self,n): for i in range(n): self.drop() def spliceStack(self,n,sstack): self.dropMany(n) self.pushStack(sstack) def getIndexOf(self,name): for i,x in enumerate(self): if x.name == name: return i else: raise ValueError("ShortcutDict '{}' not found".format(name)) def printShortcuts(self): d = {} for x in self: for y in x: if not y in d: d[y] = x[y] for x in sorted(d.keys()): print("{:16s} : {}".format(x,d[x])) def main(): print("Create dicts a, b, c") a = ShortcutDict() b = ShortcutDict("Hello") c = ShortcutDict() s = ShortcutStack() s.push(a) s.push(b) s.push(c) print("Stack looks like: {}".format(s)) s.push(s.pop()) # These should be mutual inverses print("Stack should look the same") print("Stack looks like: {}".format(s)) s.push(b) print("Test pop.push {} should be Hello".format(s.pop().name)) print("Test a with empty b and c") a.setAction("Alt-X","Say Hello") a.setAction("Alt-Y","Say Goodbye") print("{} should be {}".format(s.getAction("Alt-X"),"Say Hello")) print("{} should be {}".format(s.getAction("Alt-Y"),"Say Goodbye")) print("Test with nonempty c, by assigning via c") c.setAction("Alt-X","Say Boo") print("{} should be {}".format(s.getAction("Alt-X"),"Say Boo")) print("Test by assigning via s") s.setAction("Alt-Y","Say Goose") print("{} should be {}".format(s.getAction("Alt-Y"),"Say Goose")) print("Get index of 'Hello', should be {}, got {}".format(1,s.getIndexOf('Hello'))) print("Stack looks like: {}".format(s)) print("Create new dict d") d = ShortcutDict() ss = ShortcutStack() ss.push(d) print("Splice d in at index of 'Hello'") s.spliceStack(s.getIndexOf('Hello'),ss) print("{} should be {}".format(s.getAction("Alt-X"),"Say Hello")) print("{} should be {}".format(s.getAction("Alt-Y"),"Say Goodbye")) print("Assign to d") d.setAction("Alt-Y","Bounce") print("{} should be {}".format(s.getAction("Alt-Y"),"Bounce")) print("Stack looks like: {}".format(s)) s.printShortcuts() print("Done test1") # Creating keymap looks like def createStack(basename,basemap): s = ShortcutStack() a = ShortcutDict(basename) a.update(basemap) return s def createDict(dictname,keymap): a = ShortcutDict(dictname) a.update(keymap) return a def test2(): s = createStack("Flibble",{"Alt-X":"Begin","Meta-Y":"End"}) a = createDict("Mr",{"A":"Hexvision","Shift-B":"FryPiggy"}) s.push(a) s.printShortcuts() print("Done test2") if __name__ == '__main__': main() # Basic test test2()