-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.py
More file actions
executable file
·122 lines (99 loc) · 3.13 KB
/
magic.py
File metadata and controls
executable file
·122 lines (99 loc) · 3.13 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""invoke magic spells from magic words inside a file
magic words are defined thusly: (must be all caps)
#__MAGICWORD__# echo 'followed by a shell command'
put something of that format inside a file to set up running that command
additionally, #__file__# will be substituted with the path of the file this is called on
#__dir__# is the file's containing directory
you can also call the script with a spell_idx argument
`magic.py magic.py 0`
TODO:
if no args:
look for other executables inside folder
look for .MAGIC or MAGIC.txt inside folder?
`magic.py % 0` is <leader>0
examples:
#__PUSH__# gist -u ed85631bcb75846d950258eb19cb6b2a #__file__#
#__RUN__# python ~/scripts/magic.py
"""
import os
import re
import sys
import subprocess
MAGIC_REGEX = re.compile(r"\s*#\s*__([A-Z0-9_]+)__#\s*(\S.*)")
KIRBY = "(๑•ᴗ•)⊃━"
STARS = "☆.*・。゚・"
def show_cast(name):
import time
print(KIRBY, end="")
print(STARS, end="", flush=True)
print(name)
def main():
if len(sys.argv) == 1:
print(__doc__)
exit()
if len(sys.argv) >= 2:
filename = sys.argv[1]
if os.path.isfile(filename):
with_arg(filename)
else:
print(f"no file {filename}")
sys.exit(1)
def with_arg(filename):
spells = []
spell_counter = 0
with open(filename, "r") as fileobj:
for line in fileobj:
matches = MAGIC_REGEX.search(line)
if matches:
name, command = matches.group(1), matches.group(2)
command = sub_magic(command, filename)
spells.append((name, command))
spell_counter += 1
if spell_counter == 0:
print(f"no spells found in {filename}")
if filename.endswith(".py"):
print("running python file")
show_cast("python3 " + filename)
subprocess.run(["python3", filename])
return
print("executing shebang at top")
with open(filename, "r") as fileobj:
first_line = fileobj.readline()
if not first_line.startswith("#!"):
print("no shebang found")
exit(1)
else:
show_cast("./" + filename)
subprocess.run(["perl", filename])
exit(0)
if len(sys.argv) >= 3:
spell_idx = int(sys.argv[2])
else:
spell_idx = choose_spell_idx(spells)
name, command = spells[spell_idx]
show_cast(name)
subprocess.call(command, shell=True)
def sub_magic(command, argfile):
file_abs = os.path.abspath(argfile)
file_dir = os.path.dirname(file_abs)
command = command.replace("#__file__#", file_abs)
command = command.replace("#__dir__#", file_dir)
return command
def choose_spell_idx(spells):
idx = 0
for name, command in spells:
print(f"{idx}.\t{KIRBY}{name}")
print(f"{command}")
print("-" * 5)
idx += 1
if len(spells) == 1:
return 0
inp = input("idx: ")
if not inp:
spell_idx = 0
else:
spell_idx = int(inp)
return spell_idx
if __name__ == "__main__":
main()