1
0

b/c...py: substitute multiple variables at once

This commit is contained in:
Intel A80486DX2-66 2023-12-03 15:55:08 +03:00
parent 8df2334bc4
commit ca019b8fe8
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -46,8 +46,10 @@ def read_from_file_or_stdin(path: str) -> str:
print("The specified file doesn't exist")
raise SystemExit
def substitute_var(placeholder: str, replacement, text: str) -> str:
return text.replace(f"`{placeholder}`", str(replacement))
def substitute_vars(replacements: dict, text: str) -> str:
for placeholder, replacement in replacements.items():
text = text.replace(f"`{placeholder}`", str(replacement))
return text
CC = fetch("CC")
CC_FLAGS = fetch("CC_FLAGS")
@ -135,26 +137,17 @@ if __name__ == "__main__":
print("CLI: Count of samples should be greater than zero.")
raise SystemExit
substitute = read_file(PATHS["template"])
substitute = substitute_var("bytebeat_contents",
bytebeat_contents, substitute)
substitute = substitute_var("sample_rate",
args.sample_rate, substitute)
substitute = substitute_var("final_sample_rate_code",
final_sample_rate_code, substitute)
substitute = substitute_var("bit_depth",
args.bit_depth, substitute)
substitute = substitute_var("is_signed",
"1" if args.signed else "0", substitute)
substitute = substitute_var("channels",
args.channels, substitute)
substitute = substitute_var("length",
samples, substitute)
substitute = substitute_var("silent_mode",
"true" if args.silent else "false", substitute)
substitute = substitute_var("verbose_mode",
"true" if args.verbose and not args.silent else "false", substitute)
rewrite_file(PATHS["substitute"], substitute)
rewrite_file(PATHS["substitute"], substitute_vars({
"bytebeat_contents": bytebeat_contents,
"sample_rate": args.sample_rate,
"final_sample_rate_code": final_sample_rate_code,
"bit_depth": args.bit_depth,
"is_signed": "1" if args.signed else "0",
"channels": args.channels,
"length": samples,
"silent_mode": "true" if args.silent else "false",
"verbose_mode": "true" if args.verbose and not args.silent else "false",
}, read_file(PATHS["template"])))
# Compile by invoking the shell script
print("Compiling")