voicemeeter-compact/tools/rewriter.py

251 lines
9.8 KiB
Python
Raw Normal View History

2023-08-27 02:41:07 +01:00
import argparse
import logging
from pathlib import Path
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('vm-compact-rewriter')
2023-08-27 02:41:07 +01:00
PACKAGE_DIR = Path(__file__).parent.parent / 'vmcompact'
2023-08-27 02:41:07 +01:00
SRC_DIR = Path(__file__).parent / 'src'
2023-08-27 02:41:07 +01:00
def write_outs(output, outs: tuple):
for out in outs:
output.write(out)
def rewrite_app():
app_logger = logger.getChild('app')
app_logger.info('rewriting app.py')
infile = Path(SRC_DIR) / 'app.bk'
outfile = Path(PACKAGE_DIR) / 'app.py'
with open(infile, 'r') as input:
with open(outfile, 'w') as output:
2023-08-27 02:41:07 +01:00
for line in input:
match line:
# App init()
case ' def __init__(self, vmr):\n':
output.write(' def __init__(self, vmr, theme):\n')
case ' self._vmr = vmr\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' self._vmr = vmr\n',
' self._theme = theme\n',
2023-08-27 02:41:07 +01:00
' tcldir = Path.cwd() / "theme"\n',
' if not tcldir.is_dir():\n',
' tcldir = Path.cwd() / "_internal" / "theme"\n',
2023-08-27 02:41:07 +01:00
' self.tk.call("source", tcldir.resolve() / f"forest-{self._theme}.tcl")\n',
),
)
# def connect()
case 'def connect(kind_id: str, vmr) -> App:\n':
2023-08-27 02:41:07 +01:00
output.write(
'def connect(kind_id: str, vmr, theme="light") -> App:\n'
)
case ' return VMMIN_cls(vmr)\n':
output.write(' return VMMIN_cls(vmr, theme)\n')
2023-08-27 02:41:07 +01:00
case _:
output.write(line)
def rewrite_builders():
builders_logger = logger.getChild('builders')
builders_logger.info('rewriting builders.py')
infile = Path(SRC_DIR) / 'builders.bk'
outfile = Path(PACKAGE_DIR) / 'builders.py'
with open(infile, 'r') as input:
with open(outfile, 'w') as output:
2023-08-27 02:41:07 +01:00
ignore_next_lines = 0
for line in input:
if ignore_next_lines > 0:
builders_logger.info(f'ignoring: {line}')
2023-08-27 02:41:07 +01:00
ignore_next_lines -= 1
continue
match line:
# loading themes
case 'import sv_ttk\n':
output.write('#import sv_ttk\n')
case ' self.app.resizable(False, False)\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' self.app.resizable(False, False)\n'
' if _configuration.themes_enabled:\n',
2023-08-27 02:41:07 +01:00
' ttk.Style().theme_use(f"forest-{self.app._theme}")\n',
' self.logger.info(f"Forest Theme applied")\n',
),
)
ignore_next_lines = 6
# setting navframe button widths
case ' variable=self.navframe.submix,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.navframe.submix,\n'
' width=8,\n',
2023-08-27 02:41:07 +01:00
),
)
case ' variable=self.navframe.channel,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.navframe.channel,\n'
' width=8,\n',
2023-08-27 02:41:07 +01:00
),
)
case ' variable=self.navframe.extend,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.navframe.extend,\n'
' width=8,\n',
2023-08-27 02:41:07 +01:00
),
)
case ' variable=self.navframe.info,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.navframe.info,\n'
' width=8,\n',
2023-08-27 02:41:07 +01:00
),
)
# set channelframe button widths
case ' variable=self.labelframe.mute,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.labelframe.mute,\n'
' width=7,\n',
2023-08-27 02:41:07 +01:00
),
)
case ' variable=self.labelframe.conf,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.labelframe.conf,\n'
' width=7,\n',
2023-08-27 02:41:07 +01:00
),
)
case ' variable=self.labelframe.on,\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.labelframe.on,\n'
' width=7,\n',
2023-08-27 02:41:07 +01:00
),
)
# set stripconfigframe button widths
case ' self.configframe.phys_out_params.index(param)\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' self.configframe.phys_out_params.index(param)\n',
' ],\n',
' width=6,\n',
2023-08-27 02:41:07 +01:00
),
)
ignore_next_lines = 1
case ' self.configframe.virt_out_params.index(param)\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' self.configframe.virt_out_params.index(param)\n',
' ],\n',
' width=6,\n',
2023-08-27 02:41:07 +01:00
),
)
ignore_next_lines = 1
# This does both strip and bus param vars buttons
case ' variable=self.configframe.param_vars[i],\n':
2023-08-27 02:41:07 +01:00
write_outs(
output,
(
' variable=self.configframe.param_vars[i],\n',
' width=6,\n',
2023-08-27 02:41:07 +01:00
),
)
case _:
if 'Toggle.TButton' in line:
output.write(line.replace('Toggle.TButton', 'ToggleButton'))
2023-08-27 02:41:07 +01:00
else:
output.write(line)
def rewrite_menu():
menu_logger = logger.getChild('menu')
menu_logger.info('rewriting menu.py')
infile = Path(SRC_DIR) / 'menu.bk'
outfile = Path(PACKAGE_DIR) / 'menu.py'
with open(infile, 'r') as input:
with open(outfile, 'w') as output:
2023-08-27 02:41:07 +01:00
ignore_next_lines = 0
for line in input:
if ignore_next_lines > 0:
menu_logger.info(f'ignoring: {line}')
2023-08-27 02:41:07 +01:00
ignore_next_lines -= 1
continue
match line:
case 'import sv_ttk\n':
output.write('#import sv_ttk\n')
case ' # layout/themes\n':
2023-08-27 02:41:07 +01:00
ignore_next_lines = 14
case _:
output.write(line)
def prepare_for_build():
################# MOVE FILES FROM PACKAGE DIR INTO SRC DIR #########################
2023-08-28 05:17:54 +01:00
for file in (
PACKAGE_DIR / 'app.py',
PACKAGE_DIR / 'builders.py',
PACKAGE_DIR / 'menu.py',
2023-08-27 02:41:07 +01:00
):
2023-08-28 05:17:54 +01:00
if file.exists():
logger.debug(f'moving {str(file)}')
file.rename(SRC_DIR / f'{file.stem}.bk')
2023-08-27 02:41:07 +01:00
###################### RUN THE FILE REWRITER FOR EACH *.BK #########################
steps = (
rewrite_app,
rewrite_builders,
rewrite_menu,
)
[step() for step in steps]
def cleanup():
########################## RESTORE *.BK FILES #####################################
for file in (
PACKAGE_DIR / 'app.py',
PACKAGE_DIR / 'builders.py',
PACKAGE_DIR / 'menu.py',
2023-08-27 02:41:07 +01:00
):
file.unlink()
for file in (
SRC_DIR / 'app.bk',
SRC_DIR / 'builders.bk',
SRC_DIR / 'menu.bk',
2023-08-27 02:41:07 +01:00
):
file.rename(PACKAGE_DIR / f'{file.stem}.py')
2023-08-27 02:41:07 +01:00
if __name__ == '__main__':
2023-08-27 02:41:07 +01:00
parser = argparse.ArgumentParser()
parser.add_argument('--rewrite', action='store_true')
parser.add_argument('--restore', action='store_true')
2023-08-27 02:41:07 +01:00
args = parser.parse_args()
if args.rewrite:
logger.info('preparing files for build')
2023-08-27 02:41:07 +01:00
prepare_for_build()
elif args.restore:
logger.info('cleaning up files')
2023-08-27 02:41:07 +01:00
cleanup()