-
Notifications
You must be signed in to change notification settings - Fork 0
/
hlhl
executable file
·66 lines (50 loc) · 2.33 KB
/
hlhl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import json
import os
import tempfile
from argparse import ArgumentParser
from zipfile import ZipFile
CHARACTER_HIGHLIGHTING_FILENAME = "characterHighlighting.json"
DEFAULT_COLOR = "D1E3F4"
def highlight_character(file_path, character, clear_others=False, color=DEFAULT_COLOR):
"""Highlights a character in a Highland document."""
print(f"Highlighting {character} in {file_path}.")
# We write the updated script to a temporary file then replace the original later.
temp_fd, temp_path = tempfile.mkstemp()
os.close(temp_fd)
with ZipFile(file_path, "r") as in_zip, ZipFile(temp_path, "w") as out_zip:
character_highlighting_path = os.path.join(
os.path.dirname(in_zip.namelist()[0]),
CHARACTER_HIGHLIGHTING_FILENAME,
)
character_highlighting = {}
# Copy items from the original zip file to the temporary one.
# But load characterHighlighting.json when we find it.
for item in in_zip.infolist():
content = in_zip.read(item.filename)
if item.filename == character_highlighting_path:
character_highlighting = json.loads(content)
else:
out_zip.writestr(item, content)
if clear_others:
character_highlighting.clear()
# Now write characterHighlighting.json.
character_highlighting[character.upper()] = color
character_highlighting_json = json.dumps(character_highlighting)
out_zip.writestr(character_highlighting_path, character_highlighting_json)
# Overwrite the original file with the temporary one we created.
os.remove(file_path)
os.rename(temp_path, file_path)
if __name__ == "__main__":
parser = ArgumentParser(description="Highlights a character in a Highland screenplay.")
parser.add_argument("file_path", help="Highland file", metavar="FILE")
parser.add_argument("character", help="character to highlight", metavar="CHARACTER")
parser.add_argument("--clear-others", action="store_true", help="remove other highlights")
parser.add_argument("--color", default=DEFAULT_COLOR, help=f"hex color value (default: {DEFAULT_COLOR})")
args = parser.parse_args()
highlight_character(
args.file_path,
args.character,
clear_others=args.clear_others,
color=args.color,
)