-
-
Save unknownpwn-zz/9a510a571fb48fa16117 to your computer and use it in GitHub Desktop.
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
from configobj import ConfigObj | |
import sys | |
import StringIO | |
import re | |
from pprint import pprint | |
if len(sys.argv) < 3: | |
print 'Usage: ducky-convert.py <keyboard.properties> <layout.properties>' | |
sys.exit() | |
layout_file = open(sys.argv[2]).read() | |
#get rid of the trailing '//' since configobj doesn't like them | |
out = StringIO.StringIO(re.sub(r'//', '#', layout_file)) | |
layout = ConfigObj(out) | |
all_keys = ConfigObj(sys.argv[1]) | |
formatted = {} | |
for k,v in layout.items(): | |
if type(v) is list: | |
#print 'v is a list' | |
#print 'parsing ' + k + ' = ' + str(v) | |
v_list = v[::-1] | |
for key in v_list: | |
if key in all_keys.keys(): | |
#print 'looking up ' + key | |
code = all_keys[key] | |
#print key + ' = ' + code | |
nindex = v_list.index(key) | |
#print key + ' is at index ' + str(nindex) | |
try: | |
v_list[nindex] = hex(int(code)) | |
#print 'converted to hex' | |
except TypeError and ValueError: | |
v_list[nindex] = code | |
if len(v_list) is 2: | |
formatted["\\x" + k[-2:]] = "\\%s\\x00\\x00\\%s\\x00\\x00\\x00\\x00" % (v_list[0][-3:], v_list[1][-3:]) | |
elif len(v_list) is 3: | |
formatted["\\x" + k[-2:]] = "\\%s\\x00\\x00\\%s\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\%s\\x00\\x00\\x00\\x00" % (v_list[0][-3:], v_list[1][-3:], v_list[2][-3:]) | |
elif type(v) is str: | |
#print 'v is a string' | |
#print 'parsing ' + k + ' = ' + str(v) | |
if v in all_keys.keys(): | |
#print 'looking up ' + str(v) | |
code = all_keys[v] | |
#print v + ' = ' + code | |
try: | |
formatted["\\x" + k[-2:]] = "\\x00\\x00\\x00\\%s\\x00\\x00\\x00\\x00" % str(hex(int(code)))[-3:] | |
#print 'converted to hex' | |
except TypeError: | |
layout[k] = code | |
pprint(formatted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment