# (c) 2008, Marcin Kasperski # # Generate .html files out of WingIDE keymap files. # # Usage: # - modify the line below so it points to the directory where keymap files are saved # - chdir to the directory where result files are to be created # - run this script keymap_location = "/Applications/WingIDE.app/Contents/MacOS" keymap_files = [ 'keymap.basic', 'keymap.brief', 'keymap.emacs', 'keymap.normal', 'keymap.osx', 'keymap.vi', 'keymap.visualstudio' ] ########################################################################## import re import os class KeymapFile(object): # To speed up include handling, here we cache already loaded objects. This maps # keymap name to the KeymapFile object. _cached_files = {} @classmethod def load(cls, keymap_name): """ True constructor. map = KeymapFile.load("keymap.brief") """ obj = cls._cached_files.get(keymap_name, None) if obj: return obj obj = KeymapFile(keymap_name) cls._cached_files[keymap_name] = obj return obj def __init__(self, fname): """ Do not use this constructor, call KeymapFile.load() instead. """ # It seems comments are only at the start on the line, # may be mentioned as key re_comment = re.compile(r'^#') re_mapping = re.compile(r"^'([^']+)'\s*:\s*'([^']+)'") re_include = re.compile(r"^\%include\s+(\S+)") # List of assigned keys in order of reading self.keys = [] # Mapping of keys to commands self.commands = {} f = file(os.path.join(keymap_location, fname), "r") for line in f.readlines(): line = line.strip() if not line: continue if re_comment.match(line): continue m = re_mapping.match(line) if m: #print "Mapping %s -> %s" % (m.group(1), m.group(2)) self._add_mapping(m.group(1), m.group(2)) continue m = re_include.match(line) if m: #print "Include %s" % m.group(1) self._import_mappings(m.group(1)) continue raise Exception("Unknown line: %s" % line) f.close() def _add_mapping(self, key, command): # Removing previous key entry if any if key in self.keys: self.keys.remove(key) # Addding this one self.keys.append(key) # Saving command self.commands[key] = command def _import_mappings(self, importFromName): importFrom = self.load(importFromName) for key in importFrom.keys: self._add_mapping(key, importFrom.commands[key]) def text_report(self, filelike): for key in self.keys: filelike.write( "%s: %s\n" % (key, self.commands[key]) ) def html_report(self, filelike): filelike.write("") for key in self.keys: filelike.write( "" % (key, self.commands[key]) ) filelike.write("
KeyCommand
%s%s
") import sys for keymap_name in keymap_files: print "\n*** %s ***\n" % keymap_name m = KeymapFile.load(keymap_name) #m.text_report(sys.stdout) m.html_report(file (keymap_name + '.html', 'w'))