Skip to content

Commit

Permalink
Improved efficiency
Browse files Browse the repository at this point in the history
No longer doing inefficient string addition (strings are immutable).

New windows binary
  • Loading branch information
2Cas committed Apr 27, 2018
1 parent 1dc0b32 commit 85bf20f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 62 deletions.
Binary file modified bin/Win/q2k_util.exe
Binary file not shown.
129 changes: 69 additions & 60 deletions q2k/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ def __init__(self, gui):
def error(self, info, fatal=True):
''' Prints non-fatal and fatal error messages to console'''
if self.gui:
msg = ''
msg = ['']
for info, line in enumerate(info):
msg += line+' \n'
msg += [line, ' \n']
if not info:
warning=line
self.errors.append(line)
print('❌ ERROR: ' +line)
print('❌ ERROR:', line)
else:
self.errors.append(line)
print('• '+line)
print('•', line)
if fatal:
tk.messagebox.showerror('Error', msg)
tk.messagebox.showerror('Error', ''.join(msg))
raise RuntimeError(warning)
else:
error_msg = tc.colored('❌ ERROR:', 'red', attrs=['reverse', 'bold'])
Expand All @@ -108,42 +108,45 @@ def error(self, info, fatal=True):
if not info:
self.errors.append(line)
line = tc.colored(line, 'red')
print(error_msg + ' ' +line)
print(error_msg, line)
else:
self.errors.append(line)
print(e_bullet + ' '+line)
print(e_bullet, line)
if fatal:
exit()

def bad_kc(self, kc_type, code):
""" Prints bad keycode warnings to console"""
if self.gui:
message = 'Invalid '+kc_type+': '+code
bad_kc_msg = '❌ Invalid '+kc_type+':'
print(bad_kc_msg+' '+code)
self.errors.append(message)

bad_kc_msg = ['❌ ', 'Invalid ', kc_type, ': ', code]
print(''.join(bad_kc_msg))
del bad_kc_msg[0]
self.errors.append(''.join(bad_kc_msg))
else:
message = 'Invalid '+kc_type+': '+code
bad_kc_msg = tc.colored('❌ Invalid '+kc_type+':', 'cyan')
print(bad_kc_msg+' '+code)
self.errors.append(message)
message = ['❌ ', 'Invalid ', kc_type, ':']
bad_kc_msg = [ tc.colored(''.join(message), 'cyan') ]
bad_kc_msg += [' ', code]
print(''.join(bad_kc_msg))
message += [' ', code]
self.errors.append(''.join(message))

def warning(self, info, pause=False):
""" Prints warnings and interactive warnings to console"""

if self.gui:
msg = ''
msg = ['']
for info, line in enumerate(info):
msg += line+' \n'
msg += [line, ' \n']
if not info:
warning=line
self.errors.append(line)
print('▲ WARNING: ' +line)
print('▲ WARNING:', line)
else:
self.errors.append(line)
print('• '+line)
print('•', line)
if pause:
if not tk.messagebox.askyesno('Warning', msg+'\nContinue?'):
if not tk.messagebox.askyesno('Warning', ''.join(msg)+'\nContinue?'):
raise RuntimeWarning(warning)

else:
Expand All @@ -154,12 +157,12 @@ def warning(self, info, pause=False):
if not info:
self.errors.append(line)
line = tc.colored(line, 'yellow')
print(warning_msg + ' ' +line)
print(warning_msg, line)
else:
self.errors.append(line)
print(w_bullet + ' '+line)
print(w_bullet, line)
if pause:
print(w_bullet+' Press [ENTER] to continue')
print(w_bullet, 'Press [ENTER] to continue')
input()


Expand All @@ -177,9 +180,9 @@ def note(self, info):

for info, line in enumerate(info):
if not info:
print(note_msg + ' ' +line)
print(note_msg, line)
else:
print(n_bullet + ' '+line)
print(n_bullet, line)

def clear(self):

Expand Down Expand Up @@ -217,7 +220,9 @@ def layout_headers(data):
while tokens.name in names:
i += 1
if not tokens.name.endswith(')'):
tokens.name += '('+str(i)+')'
dupl_name = [ tokens.name ]
dupl_name += ['(', str(i), ')']
tokens.name = ''.join(dupl_name)
else:
tokens.name = tokens.name[:-2]+str(i)+')'
names.append(tokens.name)
Expand Down Expand Up @@ -646,19 +651,21 @@ def __func(self, qmk_func, layer_names, functions, console):
elif qfunc in ref.keyp_mods.keys() and ')' in func_target:
target = func_target
# Init functions with the first function
functions = ref.keyp_mods[qfunc]
functions = [ ref.keyp_mods[qfunc] ]
# Recursively pipe func into functions until no more functions are left.
while ')' in target:
br = target.index('(')+1
func = target[:br]
target = target[br:-1]

if func in ref.keyp_mods.keys():
functions += ref.keyp_mods[func]
functions.append(ref.keyp_mods[func])
# Check that this LAST element is a keycode
if target in ref.keyp_kc.keys():
final_kc = self.__keycode(target, functions, console, allow_quotes=False)
# Wrap with quotes -> '[func]' - note: keyplus Format is [TAP]>[HOLD]
keyp_func = "'"+functions+'-'+final_kc+"'"
keyp_func_list = [ "'", ''.join(functions), '-', final_kc, "'" ]
keyp_func = ''.join(keyp_func_list)
return keyp_func

# Legacy TMK-style QMK Functions e.g. FUNC(x)
Expand Down Expand Up @@ -1034,7 +1041,7 @@ def __validate_mcu(self, mcu_list, kbo, revo, DEBUG=False):
if revo.is_rev:
self.__console.warning([os.path.join(kb_n, rev_n)+' might have invalid MCU(s) '+', '.join(bad_mcu)])
else:
self.__console.warning([kb_n+' might have Invalid MCU(s) '+', '.join(bad_mcu)])
self.__console.warning([kb_n+' might have invalid MCU(s) '+', '.join(bad_mcu)])
return True

else:
Expand All @@ -1047,7 +1054,7 @@ def __validate_mcu(self, mcu_list, kbo, revo, DEBUG=False):
if revo.is_rev:
self.__console.error([os.path.join(kb_n, rev_n)+' has invalid MCU(s) '+', '.join(bad_mcu)], fatal=False)
else:
self.__console.error([kb_n+' has Invalid MCU(s) '+', '.join(bad_mcu)], fatal=False)
self.__console.error([kb_n+' has invalid MCU(s) '+', '.join(bad_mcu)], fatal=False)
return False

def __save_cache(self):
Expand Down Expand Up @@ -1426,12 +1433,11 @@ def __get_keycodes(self, DEBUG=False):
# Todo: Implement in pyparsing instead (proper fix)
for i, element in enumerate(row):
if ')' in element and '(' not in element:
func = ''
func = row[i-1]+','+row[i]
func = [ row[i-1], ',', row[i] ]
del row[i-1]
i -= 1
del row[i]
row.insert(i, func)
row.insert(i, ''.join(func))
if len(row) > curr_layer.matrix_cols:
num_col = len(row)
curr_layer.keymap += (list(row))
Expand Down Expand Up @@ -1648,23 +1654,24 @@ def __create_keyplus_yaml(self, DEBUG=False):

# Can't simply dump to yaml as we want to keep layout (array) as a human readable matrix (2D 'array').
out_dir = self.dirs['Keyplus YAML output']
kb_n = self.build_kb.name
kb_n = self.build_kb.name.replace('/', '_').replace('\\', '_')
rev_n = self.build_rev.name
if rev_n == 'n/a': rev_n = ''
keymap = self.build_kb.build_keymap

rev = self.build_rev
layers = rev.build_layout

errors = ''
errors = ['']
for error in self.console.errors:
errors += '# '+error+'\n'
error = ''.join(['# ', error, '\n'])
errors.append(error)

template_matrix = layers[0].matrix_map
if rev_n:
name = kb_n+'_'+rev_n
name = [kb_n, rev_n]
else:
name = kb_n
name = [kb_n]

if rev.build_m_row_pins and rev.build_m_col_pins:
rows = str(rev.build_m_row_pins)
Expand All @@ -1675,19 +1682,19 @@ def __create_keyplus_yaml(self, DEBUG=False):
cols = '# ------- Input col pins here ------- '
diodes = rev.build_diodes

template = ''
template = ['']
for i, row in enumerate(template_matrix):
for col in row:
template += (col+', ')
template += [col, ', ']
if i+1 < len(template_matrix):
template += '\n '
template.append('\n ')

layout = ''
layout = ['']
keycode_define = []
for i, layer in enumerate(layers):
layout += ' [ # layer '+str(i)+'\n ['
layout += [' [ # layer ',str(i),'\n [' ]
for row in layer.layout:
layout += '\n '
layout.append('\n ')
for keycode in row:
if len(keycode) < 4:
repeat = 4 - len(keycode)
Expand All @@ -1698,33 +1705,35 @@ def __create_keyplus_yaml(self, DEBUG=False):
if keycode not in keycode_define:
keycode_define.append(keycode)

layout += keycode+', '
layout +='\n ]\n ],\n'
layout+= [keycode, ', ']
layout.append('\n ]\n ],\n')

keycodes = ''
keycodes = ['']
if keycode_define:
keycodes = 'keycodes:'
keycodes.append('keycodes:')
for kc in keycode_define:
split = kc.split('>', 1)
tap = split[0][1:]
hold = split[1][:-1]
tap = split[0][1:]
hold = split[1][:-1]

kc_template = ref.keyplus_yaml_keycode_template
kc_template = kc_template.replace('<KEYCODE>', kc)
kc_template = kc_template.replace('<TAP>', tap)
kc_template = kc_template.replace('<HOLD>', hold)

keycodes += ref.keyplus_yaml_keycode_template
keycodes = keycodes.replace('<KEYCODE>', kc)
keycodes = keycodes.replace('<TAP>', tap)
keycodes = keycodes.replace('<HOLD>', hold)
keycodes.append(kc_template)

# Load Template
output_yaml_info = ref.keyplus_yaml_template
output_yaml_info = output_yaml_info.replace('<ERRORS>', errors)
output_yaml_info = output_yaml_info.replace('<KB_NAME>', name)
output_yaml_info = output_yaml_info.replace('<ERRORS>', ''.join(errors))
output_yaml_info = output_yaml_info.replace('<KB_NAME>', '_'.join(name))
output_yaml_info = output_yaml_info.replace('<LAYOUT_NAME>', keymap)
output_yaml_info = output_yaml_info.replace('<ROWS>', rows)
output_yaml_info = output_yaml_info.replace('<COLS>', cols)
output_yaml_info = output_yaml_info.replace('<DIODES>', diodes)
output_yaml_info = output_yaml_info.replace('<MATRIX_MAP>', template)
output_yaml_info = output_yaml_info.replace('<LAYOUT>', layout)
output_yaml_info = output_yaml_info.replace('<KEYCODES>', keycodes)
output_yaml_info = output_yaml_info.replace('<MATRIX_MAP>', ''.join(template))
output_yaml_info = output_yaml_info.replace('<LAYOUT>', ''.join(layout))
output_yaml_info = output_yaml_info.replace('<KEYCODES>', ''.join(keycodes))

kblibs = self.build_kb.libs
if rev_n:
Expand Down
2 changes: 1 addition & 1 deletion q2k/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
q2kversion = '1.2.0.a2'
q2kversion = '1.2.1.a1'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
license = 'MIT',
packages = find_packages(include=['q2k', 'q2k.*']),
python_requires = '>=3.0',
python_requires = '>=3.5',
install_requires = [
'pyyaml', 'pyparsing', 'termcolor'
],
Expand Down

0 comments on commit 85bf20f

Please sign in to comment.