Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 12th, 2010  |  syntax: None  |  size: 4.74 KB  |  views: 113  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import re, sys, copy
  2.  
  3. def manual_name(buttonname):
  4.     button = buttonname.split("_")
  5.     button = map(str.capitalize, button)
  6.     allcaps = ["Rc", "Ff"]
  7.     for caps in allcaps:
  8.         if caps in button:
  9.             button[button.index(caps)] = caps.upper()
  10.     button = "\\" + "".join(button)
  11.  
  12.     return button
  13.    
  14. if __name__ == "__main__":
  15.     text = None
  16.     if len(sys.argv) < 2:
  17.         print "usage: %s <filename> or - for stdin" % (sys.argv[0])
  18.         sys.exit(-1)
  19.     if sys.argv[1] == "-":
  20.         text = sys.stdin.read()
  21.     else:
  22.         with open(sys.argv[1]) as f:
  23.             text = f.read()
  24.        
  25.     text = re.sub(r'(\s)+', r'\1', text.strip())
  26.     text = text.replace("# ", "#")
  27.     text = re.split(r'(#define|#elif|#ifdef|#ifndef|#endif|\|\||\(|\)|\/\/|\||\s)', text)
  28.     text = filter(lambda a: len(a) > 0 and a != " ", text)
  29.    
  30.    
  31.     keypads = {}
  32.     keys = {}
  33.     actions = {}
  34.     print text
  35.     for (i,token) in zip(range(len(text)),text):
  36.         if token == "#elif" or token == "#if": # beginning of a keymap
  37.             rest = text[i+1:]
  38.             current_keypads = [] # the keypads this keymap refers to
  39.  
  40.             if "#endif" in rest and not "#elif" in rest: # we only see an #endif - this is the last keymap
  41.                 keymap = rest[:rest.index("#endif")]
  42.             else: # this is not the last keymap
  43.                 keymap = rest[:rest.index("#elif")]
  44.             i = 0
  45.             while i < len(keymap):
  46.                 if len(keymap[i].strip()) == 0 or keymap[i] == "||" or keymap[i] == "\\" or keymap[i] == ")":
  47.                     i = i + 1
  48.                 elif keymap[i] == "//":
  49.                     if "\n" in keymap:
  50.                         i += keymap[i:].index("\n")
  51.                     else:
  52.                         i = len(keymap) + 1
  53.                 elif keymap[i] == "CONFIG_KEYPAD" or (keymap[i] == "(" and keymap[i+1] == "CONFIG_KEYPAD"):
  54.                     if keymap[i] == "(": # (, CONFIG_KEYPAD, ==, X, )
  55.                         current_keypads.append(keymap[i+3])
  56.                         i = i + 5
  57.                     else: # CONFIG_KEYPAD, ==, X
  58.                         current_keypads.append(keymap[i+2])
  59.                         i = i + 3
  60.                 elif keymap[i] == "#define":
  61.                     pads = tuple(current_keypads)
  62.                     action = keymap[i+1]
  63.                     if keymap[i+2] == "(": # this is a key combination: #define, ACTION, (, X, |, Y, )
  64.                         key = (keymap[i+3], keymap[i+5])
  65.                         i = i + 7
  66.                     else: # #define, ACTION, X
  67.                         key = keymap[i+2]
  68.                         i = i + 3
  69.                    
  70.                     """ # Additional lookup methods
  71.                     if pads in keypads: # keypads is a map [keypads] -> (action, key) (lookup by keypad)
  72.                         keypads[pads].append((action, key))
  73.                     else:
  74.                         keypads[pads] = [(action, key)]
  75.                        
  76.                     if (action,key) in keys: # keys is a map  (action, key) -> [pads] (lookup for key used in the same way in several keypads)
  77.                         keys[(action,key)] += current_keypads
  78.                     else:
  79.                         keys[(action, key)] = copy.deepcopy(current_keypads)
  80.                     """
  81.                    
  82.                     if action in actions: # actions is a map  action -> key -> [pads]
  83.                         if key in actions[action]:
  84.                             actions[action][key] += current_keypads
  85.                             #print action, "->", key, "->", actions[action][key]
  86.                         else:
  87.                             actions[action][key] = copy.deepcopy(current_keypads)
  88.                     else:
  89.                         actions[action] = {key: copy.deepcopy(current_keypads)}
  90.                 elif keymap[i] == "#else": # this is the last thing in every keymap and it's an #error clause
  91.                     break # end the loop
  92.                 else:
  93.                     print "Unknown token \"",keymap[i], "\""
  94.                     print i
  95.                     print keymap
  96.                     sys.exit(-2)
  97.    
  98.    
  99.     print r"""\begin{table}
  100.   \begin{btnmap}{}{}"""
  101.  
  102.     for action in actions.iterkeys():
  103.         for key in actions[action]:
  104.             if type(key) is tuple:
  105.                 keyname = manual_name(key[0])+"+"+manual_name(key[1])
  106.             else:
  107.                 keyname = manual_name(key)
  108.            
  109.             actions[action][key].sort()
  110.             print "    \opt{%s}{%s}" % (",".join(actions[action][key]), keyname)
  111.         print "    & %s \\\\" % (action,)
  112.         print "%"
  113.     print r"""  \end{btnmap}
  114. \end{table}"""
clone this paste RAW Paste Data