Skip to content

Instantly share code, notes, and snippets.

@heyjoeway
Created December 28, 2023 20:57
Show Gist options
  • Save heyjoeway/a452c55ba5899db99538b80f76421f61 to your computer and use it in GitHub Desktop.
Save heyjoeway/a452c55ba5899db99538b80f76421f61 to your computer and use it in GitHub Desktop.
For https://privacy.sexy/: Split privacy-script.bat into individual batch scripts
# https://stackoverflow.com/a/7406369
def make_filename_safe(filename):
keepcharacters = (' ','.','_')
return "".join(c for c in filename if c.isalnum() or c in keepcharacters).rstrip()
def split_privacy_script(path):
def new_item():
return {
"title": "",
"contents": ""
}
item = None
items = []
def append_item():
nonlocal item
nonlocal items
if not item:
item = new_item()
return
item["contents"] = item["contents"].strip("\n")
if item["contents"]:
if not item["title"]:
item["title"] = f"Untitled {hash(item['contents'])}"
items.append(item)
item = new_item()
# For each line in "privacy-script.bat"
with open(path, "r") as f:
for line in f:
line = line.strip("\n")
if line.startswith(":: "):
is_separator = line[3:] == ("-" * len(line[3:]))
if is_separator:
append_item()
continue
if not item:
continue
if not item["title"]:
item["title"] = line[3:].strip("- ")
continue
if not item:
continue
item["contents"] += line + "\n"
return items
def save_split_privacy_script(path_in, path_out):
import os
import shutil
# Recursively delete directory if it exists
if os.path.exists(path_out):
shutil.rmtree(path_out)
# Ensure directory exists
os.makedirs(path_out)
for item in split_privacy_script(path_in):
filename = make_filename_safe(item["title"])
with open(f"{path_out}/{filename}.bat", "w") as f:
f.write("@echo off\n\n" + item["contents"])
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("in", help="Path to privacy-script.bat")
parser.add_argument("out", help="Path to output directory")
args = parser.parse_args()
path_out = args["out"]
path_in = args["in"]
save_split_privacy_script(path_in, path_out)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment