Created
May 10, 2016 17:02
-
-
Save jjones646/c742aa769049e1b71fe3c075eaacde98 to your computer and use it in GitHub Desktop.
Python script that prints out json formatted stats from log files of the logkeys program on linux. https://apps.ubuntu.com/cat/applications/logkeys
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
Basic script that parses the default formatted log files from the | |
logkeys program on linux. Prints out json formatted stats. | |
https://apps.ubuntu.com/cat/applications/logkeys | |
""" | |
import re | |
import argparse | |
import json | |
from builtins import chr | |
def main(args): | |
"""Main login function for the script | |
""" | |
raw_lines = [] | |
with open(args.infile.name, 'r') as f: | |
for line in f: | |
raw_lines.append(line.rstrip()) | |
raw_lines = raw_lines[2:-3] | |
data = [] | |
for l in raw_lines: | |
data.append(l[27:]) | |
token_counts = dict() | |
tokens = re.compile(r'(<.*?>)') | |
for d in data: | |
matches = tokens.findall(d) | |
if matches is not None: | |
for t in matches: | |
d = re.sub(t, '', d) | |
if t not in token_counts: | |
token_counts[t] = 0 | |
token_counts[t] += 1 | |
raw_elems = d.split() | |
for w in raw_elems: | |
raw_chars = [ord(r) for r in list(w)] | |
for c in [chr(r) for r in raw_chars]: | |
if c not in token_counts: | |
token_counts[c] = 0 | |
token_counts[c] += 1 | |
token_counts = sorted(token_counts.items(), key=lambda x: x[1]) | |
print(json.dumps(token_counts, indent=4)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Parse log files from the logkeys program.') | |
parser.add_argument('infile', type=argparse.FileType('r'), help='the raw key log file') | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment