Skip to content

Commit 49c186a

Browse files
Merge pull request #229 from reventlov/embossc_cleanup
`embossc`: Remove unnecessary module path manipulation, and format with Black.
2 parents 52779b4 + fccb41c commit 49c186a

File tree

1 file changed

+68
-67
lines changed

1 file changed

+68
-67
lines changed

embossc

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,85 +20,86 @@ import argparse
2020
import os
2121
import sys
2222

23+
from compiler.back_end.cpp import emboss_codegen_cpp, header_generator
24+
from compiler.front_end import emboss_front_end
25+
2326

2427
def _parse_args(argv):
25-
parser = argparse.ArgumentParser(description="Emboss compiler")
26-
parser.add_argument("--color-output",
27-
default="if_tty",
28-
choices=["always", "never", "if_tty", "auto"],
29-
help="Print error messages using color. 'auto' is a "
30-
"synonym for 'if_tty'.")
31-
parser.add_argument("--import-dir", "-I",
32-
dest="import_dirs",
33-
action="append",
34-
default=["."],
35-
help="A directory to use when searching for imported "
36-
"embs. If no import-dirs are specified, the "
37-
"current directory will be used.")
38-
parser.add_argument("--generate",
39-
nargs=1,
40-
choices=["cc"],
41-
default="cc",
42-
help="Which back end to use. Currently only C++ is "
43-
"supported.")
44-
parser.add_argument("--output-path",
45-
nargs=1,
46-
default=".",
47-
help="""Prefix path to use for the generated output file.
48-
Defaults to '.'""")
49-
parser.add_argument("--output-file",
50-
nargs=1,
51-
help="""File name to be used for the generated output
52-
file. Defaults to input_file suffixed by '.h'""")
53-
parser.add_argument("--cc-enum-traits",
54-
action=argparse.BooleanOptionalAction,
55-
default=True,
56-
help="""Controls generation of EnumTraits by the C++
57-
backend""")
58-
parser.add_argument("input_file",
59-
type=str,
60-
nargs=1,
61-
help=".emb file to compile.")
62-
return parser.parse_args(argv[1:])
28+
parser = argparse.ArgumentParser(description="Emboss compiler")
29+
parser.add_argument(
30+
"--color-output",
31+
default="if_tty",
32+
choices=["always", "never", "if_tty", "auto"],
33+
help="Print error messages using color. 'auto' is a synonym for 'if_tty'.",
34+
)
35+
parser.add_argument(
36+
"--import-dir",
37+
"-I",
38+
dest="import_dirs",
39+
action="append",
40+
default=["."],
41+
help="""A directory to use when searching for imported embs. If no
42+
import-dirs are specified, the current directory will be used.""",
43+
)
44+
parser.add_argument(
45+
"--generate",
46+
nargs=1,
47+
choices=["cc"],
48+
default="cc",
49+
help="Which back end to use. Currently only C++ is supported.",
50+
)
51+
parser.add_argument(
52+
"--output-path",
53+
nargs=1,
54+
default=".",
55+
help="Prefix path to use for the generated output file. Defaults to '.'",
56+
)
57+
parser.add_argument(
58+
"--output-file",
59+
nargs=1,
60+
help="""File name to be used for the generated output file. Defaults to
61+
input_file suffixed by '.h'""",
62+
)
63+
parser.add_argument(
64+
"--cc-enum-traits",
65+
action=argparse.BooleanOptionalAction,
66+
default=True,
67+
help="Controls generation of EnumTraits by the C++ backend",
68+
)
69+
parser.add_argument("input_file", type=str, nargs=1, help=".emb file to compile.")
70+
return parser.parse_args(argv[1:])
6371

6472

6573
def main(argv):
66-
flags = _parse_args(argv)
67-
base_path = os.path.dirname(__file__) or "."
68-
sys.path.append(base_path)
69-
70-
from compiler.back_end.cpp import ( # pylint:disable=import-outside-toplevel
71-
emboss_codegen_cpp, header_generator
72-
)
73-
from compiler.front_end import ( # pylint:disable=import-outside-toplevel
74-
emboss_front_end
75-
)
74+
flags = _parse_args(argv)
7675

77-
ir, _, errors = emboss_front_end.parse_and_log_errors(
78-
flags.input_file[0], flags.import_dirs, flags.color_output)
76+
ir, _, errors = emboss_front_end.parse_and_log_errors(
77+
flags.input_file[0], flags.import_dirs, flags.color_output
78+
)
7979

80-
if errors:
81-
return 1
80+
if errors:
81+
return 1
8282

83-
config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
84-
header, errors = emboss_codegen_cpp.generate_headers_and_log_errors(
85-
ir, flags.color_output, config)
83+
config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
84+
header, errors = emboss_codegen_cpp.generate_headers_and_log_errors(
85+
ir, flags.color_output, config
86+
)
8687

87-
if errors:
88-
return 1
88+
if errors:
89+
return 1
8990

90-
if flags.output_file:
91-
output_file = flags.output_file[0]
92-
else:
93-
output_file = flags.input_file[0] + ".h"
91+
if flags.output_file:
92+
output_file = flags.output_file[0]
93+
else:
94+
output_file = flags.input_file[0] + ".h"
9495

95-
output_filepath = os.path.join(flags.output_path[0], output_file)
96-
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
96+
output_filepath = os.path.join(flags.output_path[0], output_file)
97+
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
9798

98-
with open(output_filepath, "w") as output:
99-
output.write(header)
100-
return 0
99+
with open(output_filepath, "w") as output:
100+
output.write(header)
101+
return 0
101102

102103

103104
if __name__ == "__main__":
104-
sys.exit(main(sys.argv))
105+
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)