Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, sys, copy
- def manual_name(buttonname):
- button = buttonname.split("_")
- button = map(str.capitalize, button)
- allcaps = ["Rc", "Ff"]
- for caps in allcaps:
- if caps in button:
- button[button.index(caps)] = caps.upper()
- button = "\\" + "".join(button)
- return button
- if __name__ == "__main__":
- text = None
- if len(sys.argv) < 2:
- print "usage: %s <filename> or - for stdin" % (sys.argv[0])
- sys.exit(-1)
- if sys.argv[1] == "-":
- text = sys.stdin.read()
- else:
- with open(sys.argv[1]) as f:
- text = f.read()
- text = re.sub(r'(\s)+', r'\1', text.strip())
- text = text.replace("# ", "#")
- text = re.split(r'(#define|#elif|#ifdef|#ifndef|#endif|\|\||\(|\)|\/\/|\||\s)', text)
- text = filter(lambda a: len(a) > 0 and a != " ", text)
- keypads = {}
- keys = {}
- actions = {}
- print text
- for (i,token) in zip(range(len(text)),text):
- if token == "#elif" or token == "#if": # beginning of a keymap
- rest = text[i+1:]
- current_keypads = [] # the keypads this keymap refers to
- if "#endif" in rest and not "#elif" in rest: # we only see an #endif - this is the last keymap
- keymap = rest[:rest.index("#endif")]
- else: # this is not the last keymap
- keymap = rest[:rest.index("#elif")]
- i = 0
- while i < len(keymap):
- if len(keymap[i].strip()) == 0 or keymap[i] == "||" or keymap[i] == "\\" or keymap[i] == ")":
- i = i + 1
- elif keymap[i] == "//":
- if "\n" in keymap:
- i += keymap[i:].index("\n")
- else:
- i = len(keymap) + 1
- elif keymap[i] == "CONFIG_KEYPAD" or (keymap[i] == "(" and keymap[i+1] == "CONFIG_KEYPAD"):
- if keymap[i] == "(": # (, CONFIG_KEYPAD, ==, X, )
- current_keypads.append(keymap[i+3])
- i = i + 5
- else: # CONFIG_KEYPAD, ==, X
- current_keypads.append(keymap[i+2])
- i = i + 3
- elif keymap[i] == "#define":
- pads = tuple(current_keypads)
- action = keymap[i+1]
- if keymap[i+2] == "(": # this is a key combination: #define, ACTION, (, X, |, Y, )
- key = (keymap[i+3], keymap[i+5])
- i = i + 7
- else: # #define, ACTION, X
- key = keymap[i+2]
- i = i + 3
- """ # Additional lookup methods
- if pads in keypads: # keypads is a map [keypads] -> (action, key) (lookup by keypad)
- keypads[pads].append((action, key))
- else:
- keypads[pads] = [(action, key)]
- if (action,key) in keys: # keys is a map (action, key) -> [pads] (lookup for key used in the same way in several keypads)
- keys[(action,key)] += current_keypads
- else:
- keys[(action, key)] = copy.deepcopy(current_keypads)
- """
- if action in actions: # actions is a map action -> key -> [pads]
- if key in actions[action]:
- actions[action][key] += current_keypads
- #print action, "->", key, "->", actions[action][key]
- else:
- actions[action][key] = copy.deepcopy(current_keypads)
- else:
- actions[action] = {key: copy.deepcopy(current_keypads)}
- elif keymap[i] == "#else": # this is the last thing in every keymap and it's an #error clause
- break # end the loop
- else:
- print "Unknown token \"",keymap[i], "\""
- print i
- print keymap
- sys.exit(-2)
- print r"""\begin{table}
- \begin{btnmap}{}{}"""
- for action in actions.iterkeys():
- for key in actions[action]:
- if type(key) is tuple:
- keyname = manual_name(key[0])+"+"+manual_name(key[1])
- else:
- keyname = manual_name(key)
- actions[action][key].sort()
- print " \opt{%s}{%s}" % (",".join(actions[action][key]), keyname)
- print " & %s \\\\" % (action,)
- print "%"
- print r""" \end{btnmap}
- \end{table}"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement