Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit ea699be

Browse files
committed
Write main program
1 parent 9f725f0 commit ea699be

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import argparse
2+
import os
3+
import pathlib
4+
import sys
5+
6+
from handlers.c import CHandler
7+
from handlers.python import PythonHandler, Python2Handler
8+
from handlers.shell import BashShellHandler, ShellHandler, ZshShellHandler
9+
10+
handlers = {
11+
"--bash": BashShellHandler,
12+
"--c": CHandler,
13+
"--python": PythonHandler,
14+
"--py": PythonHandler,
15+
"--py2": Python2Handler,
16+
"--python2": Python2Handler,
17+
"--sh": ShellHandler,
18+
"--zsh": ZshShellHandler,
19+
}
20+
21+
22+
def main(args=None):
23+
parser = argparse.ArgumentParser(
24+
description="Reads stdin and executes the code as a script."
25+
)
26+
parser.add_argument(
27+
"--keep",
28+
nargs=1,
29+
type=pathlib.Path,
30+
metavar="DIRECTORY",
31+
default=None,
32+
help="Store the script in the given directory",
33+
)
34+
parser.add_argument(
35+
"--filename",
36+
nargs=1,
37+
help="Give the stored file the speified name (does not do anything without --keep)",
38+
default="script",
39+
)
40+
for argname, handler in handlers.items():
41+
parser.add_argument(
42+
argname, action="store_true", default=None, help=handler.__doc__
43+
)
44+
ns = parser.parse_args(args)
45+
parsed_dict = {
46+
argname.split("-")[-1]: getattr(ns, argname.split("-")[-1])
47+
for argname in handlers.keys()
48+
}
49+
parse_count = [bool(val) for name, val in parsed_dict.items()].count(True)
50+
if parse_count > 1:
51+
print("Only one type of script can be specified.", file=sys.stderr)
52+
parser.print_help(sys.stderr)
53+
sys.exit(1)
54+
elif parse_count < 1:
55+
print("A script type needs to be specified.", file=sys.stderr)
56+
parser.print_help(sys.stderr)
57+
sys.exit(1)
58+
for key, value in parsed_dict.items():
59+
if value:
60+
parser = handlers["--" + key]
61+
break
62+
code = sys.stdin.read()
63+
if ns.keep:
64+
dir = ns.keep
65+
os.makedirs(dir, exist_ok=True)
66+
parser().build(code, str(dir.joinpath(ns.filename)))
67+
else:
68+
parser().build(code)
69+
70+
71+
if __name__ == "__main__":
72+
main()

0 commit comments

Comments
 (0)