aboutsummaryrefslogtreecommitdiffstats
path: root/shenanigans.py
diff options
context:
space:
mode:
authorgarret <garret@airmail.cc>2024-08-19 00:52:14 +0100
committergarret <garret@airmail.cc>2024-08-19 00:52:14 +0100
commitbf66a282fd6268475faaed427ebe966ee0dc2d17 (patch)
tree9772755dbeb7ccf275db33f56450e3179eab2d39 /shenanigans.py
parentb016274644532e09b77a7d295c97b1d35aee5770 (diff)
downloadaegisub-scripts-bf66a282fd6268475faaed427ebe966ee0dc2d17.tar.gz
aegisub-scripts-bf66a282fd6268475faaed427ebe966ee0dc2d17.tar.bz2
aegisub-scripts-bf66a282fd6268475faaed427ebe966ee0dc2d17.zip
add python shenanigan script that we actually use
Diffstat (limited to 'shenanigans.py')
-rw-r--r--shenanigans.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/shenanigans.py b/shenanigans.py
new file mode 100644
index 0000000..c4a5856
--- /dev/null
+++ b/shenanigans.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+import re
+import ass
+import argparse
+import sys
+from io import StringIO
+import os.path
+
+SHENAN_PATTERN = re.compile("shenan ([^;]*)")
+
+def decode_path(dialogue_path, shenan_path):
+ abspath = os.path.abspath(dialogue_path)
+ directory = os.path.dirname(abspath)
+ path = os.path.join(directory, shenan_path)
+ return path
+
+def main(sub, dialogue_path):
+ imports = {}
+
+ i = 0
+
+ while i < len(sub):
+ line = sub[i]
+ if line.effect == "import":
+ with open(decode_path(dialogue_path, line.text), encoding='utf-8-sig') as f:
+ imported_sub = ass.parse(f)
+
+ for imported_line in imported_sub.events:
+ match = SHENAN_PATTERN.search(imported_line.effect)
+ if match != None:
+ name = match.group(1)
+ if not name in imports:
+ imports[name] = []
+ imports[name].append(imported_line)
+
+ match = SHENAN_PATTERN.search(line.effect)
+ if match != None:
+ name = match.group(1)
+ if name in imports:
+ shenans = imports[name]
+ del sub[i]
+ if shenans != None:
+ sub = [*sub[:i], *shenans, *sub[i:]]
+ i += len(shenans)
+ imports[name] = None
+ i -= 1
+
+ i += 1
+ return sub
+
+def insert_shenanigans(infile, outfile):
+ doc = ass.parse(infile)
+ doc.events = main(doc.events, infile.name)
+ doc.dump_file(outfile)
+
+def insert_inplace(fi):
+ with open(fi, "r", encoding='utf-8-sig') as f:
+ doc = ass.parse(f)
+ doc.events = main(doc.events, f.name)
+ with open(fi, "w", encoding='utf-8-sig') as f:
+ doc.dump_file(f)
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description=('Shenanigan script'))
+
+ parser.add_argument('input', type=argparse.FileType('r', encoding='utf-8-sig'))
+ parser.add_argument('output', type=argparse.FileType('w', encoding='utf-8-sig'), nargs="?", default=sys.stdout)
+
+ args = parser.parse_args()
+
+ insert_shenanigans(args.input, args.output)