Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Last active July 17, 2024 21:39
Show Gist options
  • Save sergiobuj/4fa8026a5545279d3e96996ecddfc818 to your computer and use it in GitHub Desktop.
Save sergiobuj/4fa8026a5545279d3e96996ecddfc818 to your computer and use it in GitHub Desktop.
Cli parse kv params / encode/decode file
#!/usr/bin/env python3
import argparse
import base64
import random
from pathlib import Path
def update_process(process_file: Path):
raw = ""
with open(process_file, "rb") as f:
raw = f.read()
encoded = base64.b64encode(raw)
with open("encoded.txt", "w") as f:
f.write(encoded.decode("utf-8"))
def create_instance(process_file: Path, params: dict):
pass
def run_process_with_params(process_file: Path, params: dict):
process = update_process(process_file)
instance = create_instance(process, params)
def _parse_kv_params(kvargs: list[str]) -> dict[str, str]:
values = {}
for kv in kvargs:
k, v = kv.split("=", maxsplit=1)
values[k] = v
return values
def recreate_file(encoded_bytes: str):
raw_bytes = base64.b64decode(encoded_bytes)
_id = random.randint(0, 100)
filename = f"flag_{_id}.png"
with open(filename, "wb") as f:
f.write(raw_bytes)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--bpmn", type=Path, required=True)
parser.add_argument("-p", "--param", metavar="k=v", required=True, action="append")
parser.add_argument("--raw", type=str)
args = parser.parse_args()
bpmn = args.bpmn
params = _parse_kv_params(args.param)
print(f"{bpmn=}")
print(f"{params=}")
run_process_with_params(bpmn, params)
recreate_file(args.raw)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment