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

Commit 76bc950

Browse files
committed
Add existing
1 parent cee07be commit 76bc950

File tree

6 files changed

+81
-18
lines changed

6 files changed

+81
-18
lines changed

handlers/abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from os.path import abspath, expanduser, join
44

55

6-
class BaseHandler(metaclass=abc.ABCMeta):
6+
class BaseHandler(abc.ABC):
77
"""Base Handler class that defines abstract methods"""
88

99
@property

handlers/c.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
class CHandler(BaseHandler):
88
"""Compile and run C programs."""
99

10-
def run(self, code_file):
10+
def run(self, code_file, directory):
1111
try:
1212
check_call(["bash", "-c", 'which -s clang'])
1313
except Exception:
1414
compiler = "gcc"
1515
else:
1616
compiler = "clang"
17-
comp = run([compiler, code_file, "-o", "script"])
17+
comp = run([compiler, code_file, "-o", f"{directory}/script"])
1818
if comp.returncode != 0:
1919
exit(comp.returncode)
2020
else:
21-
exit(run(["bash", "-c", "./script"]).returncode)
21+
exit(run(["bash", "-c", f"{directory}/script"]).returncode)
2222

2323
def build(self, code, directory=None):
2424
with self.file("c", directory=directory) as f:
2525
f.write(code)
26-
self.run(f.name)
26+
self.run(f.name, directory=directory if directory else self.tempdir)

handlers/cpp.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from subprocess import check_call, run
2+
from sys import exit
3+
4+
from .abc import BaseHandler
5+
6+
7+
class CPPHandler(BaseHandler):
8+
"""Compile and run C++ programs."""
9+
10+
def run(self, code_file, directory):
11+
try:
12+
check_call(["bash", "-c", 'which -s clang++'])
13+
except Exception:
14+
compiler = "g++"
15+
else:
16+
compiler = "clang++"
17+
comp = run([compiler, code_file, "-o", f"{directory}/script"])
18+
if comp.returncode != 0:
19+
exit(comp.returncode)
20+
else:
21+
exit(run(["bash", "-c", f"{directory}/script"]).returncode)
22+
23+
def build(self, code, directory=None):
24+
with self.file("cpp", directory=directory) as f:
25+
f.write(code)
26+
self.run(f.name, directory=directory if directory else self.tempdir)

handlers/java.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import re
2+
from os.path import abspath, expanduser, join, dirname, basename
3+
from os import chdir
4+
from subprocess import CalledProcessError, check_call, run
5+
from sys import exit
6+
7+
from .abc import BaseHandler
8+
9+
10+
class JavaHandler(BaseHandler):
11+
"""Run a Java program."""
12+
13+
def run(self, code_file):
14+
try:
15+
check_call(["javac", code_file])
16+
except CalledProcessError as e:
17+
exit(e.returncode)
18+
chdir(dirname(code_file))
19+
code_file = basename(code_file)
20+
exit(run(["java", code_file.rpartition(".java")[0]]).returncode)
21+
22+
def build(self, code, directory=None):
23+
classname = re.search(r"public class ([A-Za-z0-9_]+)", code).group(1)
24+
directory = self.tempdir if directory is None else abspath(expanduser(directory))
25+
with open(join(directory, classname + ".java"), "w") as file:
26+
file.write(code)
27+
self.run(file.name)

main.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
import sys
55

66
from handlers.c import CHandler
7-
from handlers.python import PythonHandler, Python2Handler
7+
from handlers.cpp import CPPHandler
8+
from handlers.java import JavaHandler
9+
from handlers.python import Python2Handler, PythonHandler
810
from handlers.shell import BashShellHandler, ShellHandler, ZshShellHandler
11+
from util import reverse_dict
912

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,
13+
handlers_unwrapped = {
14+
BashShellHandler: ["--bash"],
15+
CHandler: ["--c"],
16+
CPPHandler: ["--cpp", "--c++", "--cplusplus"],
17+
JavaHandler: ["--java"],
18+
PythonHandler: ["--python", "--python3", "--py", "--py3"],
19+
Python2Handler: ["--python2", "--py2"],
20+
ShellHandler: ["--sh"],
21+
ZshShellHandler: ["--zsh"]
1922
}
23+
handlers = reverse_dict(handlers_unwrapped)
2024

2125

2226
def main(args=None):
@@ -35,13 +39,13 @@ def main(args=None):
3539
help="Give the stored file the speified name (does not do anything without --keep)",
3640
default="script",
3741
)
38-
for argname, handler in handlers.items():
42+
for handler, argnames in handlers_unwrapped.items():
3943
parser.add_argument(
40-
argname, action="store_true", default=None, help=handler.__doc__
44+
*argnames, action="store_true", default=None, help=handler.__doc__
4145
)
4246
ns = parser.parse_args(args)
4347
parsed_dict = {
44-
argname.split("-")[-1]: getattr(ns, argname.split("-")[-1])
48+
argname.split("-")[-1]: getattr(ns, argname.split("-")[-1], None)
4549
for argname in handlers.keys()
4650
}
4751
parse_count = [bool(val) for name, val in parsed_dict.items()].count(True)

util.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def reverse_dict(dictionary):
2+
d = {}
3+
for key, values in dictionary.items():
4+
for value in values:
5+
d[value] = key
6+
return d

0 commit comments

Comments
 (0)