You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
3.7 KiB

#!/usr/bin/env python3
"""Register a new channel type in SDRangel's aggregate SWG classes by cloning
every block of an existing type (SWGChannelSettings, SWGChannelReport,
SWGChannelActions, SWGModelFactory). Each block that mentions the template
type is copied right after itself with the names substituted.
./swg_clone_channel.py ~/sdrangel/swagger/sdrangel/code/qt5/client
(default: template SELCALDemod -> ChirpModemDemod, as for the Rx plugin)
./swg_clone_channel.py ~/sdrangel/swagger/sdrangel/code/qt5/client \\
--template PacketMod --snake packet_mod --new ChirpModemMod --new-snake chirpmodem_mod \\
--skip _payload
The template's setters are copied as they are. Stock types (PacketMod) only
assign; SELCAL's delete the old object, so never use a SELCAL-style setter in
WebAPIRequestMapper::reset*(), which nulls members after cleanup().
"""
import argparse
import re
from pathlib import Path
FILES = ["SWGChannelSettings.h", "SWGChannelSettings.cpp", "SWGChannelReport.h",
"SWGChannelReport.cpp", "SWGChannelActions.h", "SWGChannelActions.cpp", "SWGModelFactory.h"]
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("client", help="swagger/sdrangel/code/qt5/client")
ap.add_argument("--template", default="SELCALDemod", help="existing type, as in SWG<template>Settings")
ap.add_argument("--snake", default="selcal_demod", help="its member name stem, as in <snake>_settings")
ap.add_argument("--new", default="ChirpModemDemod")
ap.add_argument("--new-snake", default="chirpmodem_demod")
ap.add_argument("--skip", help="regex: blocks matching it are not cloned")
a = ap.parse_args()
# Accessors capitalise only the first letter: getSelcalDemodSettings, getPacketModSettings
accessor = a.template[0] + a.template[1:].lower() if a.template.isupper() or "SELCAL" in a.template else a.template
if a.template == "SELCALDemod":
accessor = "SelcalDemod"
subst = [(a.template, a.new), (accessor, a.new), (a.snake, a.new_snake)]
match = re.compile(re.escape(a.template) + "|" + re.escape(accessor) + "|" + re.escape(a.snake))
skip = re.compile(a.skip) if a.skip else None
def transform(line):
for old, new in subst:
line = line.replace(old, new)
return line
def clone(lines):
out, i = [], 0
while i < len(lines):
line = lines[i]
starts_setter = line.strip() == "void" and i + 1 < len(lines) and match.search(lines[i + 1])
if not (match.search(line) or starts_setter):
out.append(line)
i += 1
continue
stripped = line.strip()
is_function = starts_setter or (stripped.startswith("SWG" + a.template) and stripped.endswith("*"))
j, depth, opened = i, 0, False
while True:
depth += lines[j].count("{") - lines[j].count("}")
opened = opened or "{" in lines[j]
if depth == 0 and (opened or not is_function):
break
j += 1
block = lines[i:j + 1]
out += block
if not (skip and any(skip.search(b) for b in block)):
out += [transform(b) for b in block]
i = j + 1
return out
client = Path(a.client).expanduser()
for name in FILES:
path = client / name
text = path.read_text()
if a.new in text:
print(f"{name}: already has {a.new}")
continue
lines = text.split("\n")
new = clone(lines)
path.write_text("\n".join(new))
print(f"{name}: +{len(new) - len(lines)} lines")
if __name__ == "__main__":
main()