Last active
October 19, 2016 18:00
-
-
Save georgedorn/97327345a17eb0909cce6918cea79e4c to your computer and use it in GitHub Desktop.
Parses a python codebase to find all event_types along with event_name subtypes where appropriate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import random | |
import re | |
import sys | |
from pprint import pprint | |
from subprocess import check_output | |
from collections import defaultdict | |
ACK_CMD = 'ack' # could also be 'ack-grep' on some systems | |
try: | |
print check_output([ACK_CMD, random.choice(['--thpppt', '--bar', '--cathy'])]) | |
except OSError: | |
print "Check to ensure ACK_CMD is set correctly." | |
sys.exit(1) | |
# regex used in ack command | |
regex = """(\{.*?('|\")event_type('|\"):.*?\})""" | |
# regex used on cleaned output of ack command to extract event_type | |
event_type_regex = """'event_type':'([A-Z_]+)'""" | |
event_name_regex = """'event_name':'([A-Z_]+)'""" | |
def clean_line(line): | |
res = line.replace(' ', '') | |
res = res.replace('"', "'") | |
res = res.replace("\'", "'") | |
return res | |
def get_event_deets_from_line(line): | |
et = en = None | |
line = clean_line(line) | |
res = re.search(event_type_regex, line) | |
if res: | |
et = res.groups()[0] | |
res = re.search(event_name_regex, line) | |
if res: | |
en = res.groups()[0] | |
return et, en | |
if __name__ == '__main__': | |
search = check_output([ACK_CMD, '--python', '-oh', regex]) | |
lines = search.split("\n") | |
events = defaultdict(set) | |
for line in lines: | |
et, en = get_event_deets_from_line(line) | |
if et: | |
events[et].add(en) | |
for et in sorted(events.keys()): | |
print et, ':', | |
pprint( sorted(list(events[et]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment