Skip to content

Commit 85bf20f

Browse files
author
2Cas
committed
Improved efficiency
No longer doing inefficient string addition (strings are immutable). New windows binary
1 parent 1dc0b32 commit 85bf20f

File tree

4 files changed

+71
-62
lines changed

4 files changed

+71
-62
lines changed

bin/Win/q2k_util.exe

1.61 MB
Binary file not shown.

q2k/core.py

+69-60
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ def __init__(self, gui):
8787
def error(self, info, fatal=True):
8888
''' Prints non-fatal and fatal error messages to console'''
8989
if self.gui:
90-
msg = ''
90+
msg = ['']
9191
for info, line in enumerate(info):
92-
msg += line+' \n'
92+
msg += [line, ' \n']
9393
if not info:
9494
warning=line
9595
self.errors.append(line)
96-
print('❌ ERROR: ' +line)
96+
print('❌ ERROR:', line)
9797
else:
9898
self.errors.append(line)
99-
print('• '+line)
99+
print('•', line)
100100
if fatal:
101-
tk.messagebox.showerror('Error', msg)
101+
tk.messagebox.showerror('Error', ''.join(msg))
102102
raise RuntimeError(warning)
103103
else:
104104
error_msg = tc.colored('❌ ERROR:', 'red', attrs=['reverse', 'bold'])
@@ -108,42 +108,45 @@ def error(self, info, fatal=True):
108108
if not info:
109109
self.errors.append(line)
110110
line = tc.colored(line, 'red')
111-
print(error_msg + ' ' +line)
111+
print(error_msg, line)
112112
else:
113113
self.errors.append(line)
114-
print(e_bullet + ' '+line)
114+
print(e_bullet, line)
115115
if fatal:
116116
exit()
117117

118118
def bad_kc(self, kc_type, code):
119119
""" Prints bad keycode warnings to console"""
120120
if self.gui:
121-
message = 'Invalid '+kc_type+': '+code
122-
bad_kc_msg = '❌ Invalid '+kc_type+':'
123-
print(bad_kc_msg+' '+code)
124-
self.errors.append(message)
121+
122+
bad_kc_msg = ['❌ ', 'Invalid ', kc_type, ': ', code]
123+
print(''.join(bad_kc_msg))
124+
del bad_kc_msg[0]
125+
self.errors.append(''.join(bad_kc_msg))
125126
else:
126-
message = 'Invalid '+kc_type+': '+code
127-
bad_kc_msg = tc.colored('❌ Invalid '+kc_type+':', 'cyan')
128-
print(bad_kc_msg+' '+code)
129-
self.errors.append(message)
127+
message = ['❌ ', 'Invalid ', kc_type, ':']
128+
bad_kc_msg = [ tc.colored(''.join(message), 'cyan') ]
129+
bad_kc_msg += [' ', code]
130+
print(''.join(bad_kc_msg))
131+
message += [' ', code]
132+
self.errors.append(''.join(message))
130133

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

134137
if self.gui:
135-
msg = ''
138+
msg = ['']
136139
for info, line in enumerate(info):
137-
msg += line+' \n'
140+
msg += [line, ' \n']
138141
if not info:
139142
warning=line
140143
self.errors.append(line)
141-
print('▲ WARNING: ' +line)
144+
print('▲ WARNING:', line)
142145
else:
143146
self.errors.append(line)
144-
print('• '+line)
147+
print('•', line)
145148
if pause:
146-
if not tk.messagebox.askyesno('Warning', msg+'\nContinue?'):
149+
if not tk.messagebox.askyesno('Warning', ''.join(msg)+'\nContinue?'):
147150
raise RuntimeWarning(warning)
148151

149152
else:
@@ -154,12 +157,12 @@ def warning(self, info, pause=False):
154157
if not info:
155158
self.errors.append(line)
156159
line = tc.colored(line, 'yellow')
157-
print(warning_msg + ' ' +line)
160+
print(warning_msg, line)
158161
else:
159162
self.errors.append(line)
160-
print(w_bullet + ' '+line)
163+
print(w_bullet, line)
161164
if pause:
162-
print(w_bullet+' Press [ENTER] to continue')
165+
print(w_bullet, 'Press [ENTER] to continue')
163166
input()
164167

165168

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

178181
for info, line in enumerate(info):
179182
if not info:
180-
print(note_msg + ' ' +line)
183+
print(note_msg, line)
181184
else:
182-
print(n_bullet + ' '+line)
185+
print(n_bullet, line)
183186

184187
def clear(self):
185188

@@ -217,7 +220,9 @@ def layout_headers(data):
217220
while tokens.name in names:
218221
i += 1
219222
if not tokens.name.endswith(')'):
220-
tokens.name += '('+str(i)+')'
223+
dupl_name = [ tokens.name ]
224+
dupl_name += ['(', str(i), ')']
225+
tokens.name = ''.join(dupl_name)
221226
else:
222227
tokens.name = tokens.name[:-2]+str(i)+')'
223228
names.append(tokens.name)
@@ -646,19 +651,21 @@ def __func(self, qmk_func, layer_names, functions, console):
646651
elif qfunc in ref.keyp_mods.keys() and ')' in func_target:
647652
target = func_target
648653
# Init functions with the first function
649-
functions = ref.keyp_mods[qfunc]
654+
functions = [ ref.keyp_mods[qfunc] ]
650655
# Recursively pipe func into functions until no more functions are left.
651656
while ')' in target:
652657
br = target.index('(')+1
653658
func = target[:br]
654659
target = target[br:-1]
660+
655661
if func in ref.keyp_mods.keys():
656-
functions += ref.keyp_mods[func]
662+
functions.append(ref.keyp_mods[func])
657663
# Check that this LAST element is a keycode
658664
if target in ref.keyp_kc.keys():
659665
final_kc = self.__keycode(target, functions, console, allow_quotes=False)
660666
# Wrap with quotes -> '[func]' - note: keyplus Format is [TAP]>[HOLD]
661-
keyp_func = "'"+functions+'-'+final_kc+"'"
667+
keyp_func_list = [ "'", ''.join(functions), '-', final_kc, "'" ]
668+
keyp_func = ''.join(keyp_func_list)
662669
return keyp_func
663670

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

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

10531060
def __save_cache(self):
@@ -1426,12 +1433,11 @@ def __get_keycodes(self, DEBUG=False):
14261433
# Todo: Implement in pyparsing instead (proper fix)
14271434
for i, element in enumerate(row):
14281435
if ')' in element and '(' not in element:
1429-
func = ''
1430-
func = row[i-1]+','+row[i]
1436+
func = [ row[i-1], ',', row[i] ]
14311437
del row[i-1]
14321438
i -= 1
14331439
del row[i]
1434-
row.insert(i, func)
1440+
row.insert(i, ''.join(func))
14351441
if len(row) > curr_layer.matrix_cols:
14361442
num_col = len(row)
14371443
curr_layer.keymap += (list(row))
@@ -1648,23 +1654,24 @@ def __create_keyplus_yaml(self, DEBUG=False):
16481654

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

16561662
rev = self.build_rev
16571663
layers = rev.build_layout
16581664

1659-
errors = ''
1665+
errors = ['']
16601666
for error in self.console.errors:
1661-
errors += '# '+error+'\n'
1667+
error = ''.join(['# ', error, '\n'])
1668+
errors.append(error)
16621669

16631670
template_matrix = layers[0].matrix_map
16641671
if rev_n:
1665-
name = kb_n+'_'+rev_n
1672+
name = [kb_n, rev_n]
16661673
else:
1667-
name = kb_n
1674+
name = [kb_n]
16681675

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

1678-
template = ''
1685+
template = ['']
16791686
for i, row in enumerate(template_matrix):
16801687
for col in row:
1681-
template += (col+', ')
1688+
template += [col, ', ']
16821689
if i+1 < len(template_matrix):
1683-
template += '\n '
1690+
template.append('\n ')
16841691

1685-
layout = ''
1692+
layout = ['']
16861693
keycode_define = []
16871694
for i, layer in enumerate(layers):
1688-
layout += ' [ # layer '+str(i)+'\n ['
1695+
layout += [' [ # layer ',str(i),'\n [' ]
16891696
for row in layer.layout:
1690-
layout += '\n '
1697+
layout.append('\n ')
16911698
for keycode in row:
16921699
if len(keycode) < 4:
16931700
repeat = 4 - len(keycode)
@@ -1698,33 +1705,35 @@ def __create_keyplus_yaml(self, DEBUG=False):
16981705
if keycode not in keycode_define:
16991706
keycode_define.append(keycode)
17001707

1701-
layout += keycode+', '
1702-
layout +='\n ]\n ],\n'
1708+
layout+= [keycode, ', ']
1709+
layout.append('\n ]\n ],\n')
17031710

1704-
keycodes = ''
1711+
keycodes = ['']
17051712
if keycode_define:
1706-
keycodes = 'keycodes:'
1713+
keycodes.append('keycodes:')
17071714
for kc in keycode_define:
17081715
split = kc.split('>', 1)
1709-
tap = split[0][1:]
1710-
hold = split[1][:-1]
1716+
tap = split[0][1:]
1717+
hold = split[1][:-1]
1718+
1719+
kc_template = ref.keyplus_yaml_keycode_template
1720+
kc_template = kc_template.replace('<KEYCODE>', kc)
1721+
kc_template = kc_template.replace('<TAP>', tap)
1722+
kc_template = kc_template.replace('<HOLD>', hold)
17111723

1712-
keycodes += ref.keyplus_yaml_keycode_template
1713-
keycodes = keycodes.replace('<KEYCODE>', kc)
1714-
keycodes = keycodes.replace('<TAP>', tap)
1715-
keycodes = keycodes.replace('<HOLD>', hold)
1724+
keycodes.append(kc_template)
17161725

17171726
# Load Template
17181727
output_yaml_info = ref.keyplus_yaml_template
1719-
output_yaml_info = output_yaml_info.replace('<ERRORS>', errors)
1720-
output_yaml_info = output_yaml_info.replace('<KB_NAME>', name)
1728+
output_yaml_info = output_yaml_info.replace('<ERRORS>', ''.join(errors))
1729+
output_yaml_info = output_yaml_info.replace('<KB_NAME>', '_'.join(name))
17211730
output_yaml_info = output_yaml_info.replace('<LAYOUT_NAME>', keymap)
17221731
output_yaml_info = output_yaml_info.replace('<ROWS>', rows)
17231732
output_yaml_info = output_yaml_info.replace('<COLS>', cols)
17241733
output_yaml_info = output_yaml_info.replace('<DIODES>', diodes)
1725-
output_yaml_info = output_yaml_info.replace('<MATRIX_MAP>', template)
1726-
output_yaml_info = output_yaml_info.replace('<LAYOUT>', layout)
1727-
output_yaml_info = output_yaml_info.replace('<KEYCODES>', keycodes)
1734+
output_yaml_info = output_yaml_info.replace('<MATRIX_MAP>', ''.join(template))
1735+
output_yaml_info = output_yaml_info.replace('<LAYOUT>', ''.join(layout))
1736+
output_yaml_info = output_yaml_info.replace('<KEYCODES>', ''.join(keycodes))
17281737

17291738
kblibs = self.build_kb.libs
17301739
if rev_n:

q2k/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
q2kversion = '1.2.0.a2'
1+
q2kversion = '1.2.1.a1'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
license = 'MIT',
2626
packages = find_packages(include=['q2k', 'q2k.*']),
27-
python_requires = '>=3.0',
27+
python_requires = '>=3.5',
2828
install_requires = [
2929
'pyyaml', 'pyparsing', 'termcolor'
3030
],

0 commit comments

Comments
 (0)