Skip to content

Commit

Permalink
Reformat all source with Black. (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
reventlov authored Sep 6, 2024
1 parent 0999d98 commit 495d3f2
Show file tree
Hide file tree
Showing 71 changed files with 22,856 additions and 18,249 deletions.
4 changes: 2 additions & 2 deletions .github/scripts/set_release_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import datetime

now_utc = datetime.datetime.now(datetime.timezone.utc)
version = now_utc.strftime('%Y.%m%d.%H%M%S')
print('::set-output name=version::v{}'.format(version))
version = now_utc.strftime("%Y.%m%d.%H%M%S")
print("::set-output name=version::v{}".format(version))
1 change: 0 additions & 1 deletion compiler/back_end/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

1 change: 0 additions & 1 deletion compiler/back_end/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

12 changes: 8 additions & 4 deletions compiler/back_end/cpp/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

class Attribute(str, Enum):
"""Attributes available in the C++ backend."""

NAMESPACE = "namespace"
ENUM_CASE = "enum_case"

Expand All @@ -37,6 +38,7 @@ class Scope(set, Enum):
Each entry is a set of (Attribute, default?) tuples, the first value being
the attribute itself, the second value being a boolean value indicating
whether the attribute is allowed to be defaulted in that scope."""

BITS = {
# Bits may contain an enum definition.
(Attribute.ENUM_CASE, True)
Expand All @@ -47,10 +49,12 @@ class Scope(set, Enum):
ENUM_VALUE = {
(Attribute.ENUM_CASE, False),
}
MODULE = {
(Attribute.NAMESPACE, False),
(Attribute.ENUM_CASE, True),
},
MODULE = (
{
(Attribute.NAMESPACE, False),
(Attribute.ENUM_CASE, True),
},
)
STRUCT = {
# Struct may contain an enum definition.
(Attribute.ENUM_CASE, True),
Expand Down
127 changes: 67 additions & 60 deletions compiler/back_end/cpp/emboss_codegen_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,79 @@


def _parse_command_line(argv):
"""Parses the given command-line arguments."""
parser = argparse.ArgumentParser(description="Emboss compiler C++ back end.",
prog=argv[0])
parser.add_argument("--input-file",
type=str,
help=".emb.ir file to compile.")
parser.add_argument("--output-file",
type=str,
help="Write header to file. If not specified, write " +
"header to stdout.")
parser.add_argument("--color-output",
default="if_tty",
choices=["always", "never", "if_tty", "auto"],
help="Print error messages using color. 'auto' is a "
"synonym for 'if_tty'.")
parser.add_argument("--cc-enum-traits",
action=argparse.BooleanOptionalAction,
default=True,
help="""Controls generation of EnumTraits by the C++
backend""")
return parser.parse_args(argv[1:])
"""Parses the given command-line arguments."""
parser = argparse.ArgumentParser(
description="Emboss compiler C++ back end.", prog=argv[0]
)
parser.add_argument("--input-file", type=str, help=".emb.ir file to compile.")
parser.add_argument(
"--output-file",
type=str,
help="Write header to file. If not specified, write " + "header to stdout.",
)
parser.add_argument(
"--color-output",
default="if_tty",
choices=["always", "never", "if_tty", "auto"],
help="Print error messages using color. 'auto' is a " "synonym for 'if_tty'.",
)
parser.add_argument(
"--cc-enum-traits",
action=argparse.BooleanOptionalAction,
default=True,
help="""Controls generation of EnumTraits by the C++
backend""",
)
return parser.parse_args(argv[1:])


def _show_errors(errors, ir, color_output):
"""Prints errors with source code snippets."""
source_codes = {}
for module in ir.module:
source_codes[module.source_file_name] = module.source_text
use_color = (color_output == "always" or
(color_output in ("auto", "if_tty") and
os.isatty(sys.stderr.fileno())))
print(error.format_errors(errors, source_codes, use_color), file=sys.stderr)
"""Prints errors with source code snippets."""
source_codes = {}
for module in ir.module:
source_codes[module.source_file_name] = module.source_text
use_color = color_output == "always" or (
color_output in ("auto", "if_tty") and os.isatty(sys.stderr.fileno())
)
print(error.format_errors(errors, source_codes, use_color), file=sys.stderr)


def generate_headers_and_log_errors(ir, color_output, config: header_generator.Config):
"""Generates a C++ header and logs any errors.
"""Generates a C++ header and logs any errors.
Arguments:
ir: EmbossIr of the module.
color_output: "always", "never", "if_tty", "auto"
config: Header generation configuration.
Arguments:
ir: EmbossIr of the module.
color_output: "always", "never", "if_tty", "auto"
config: Header generation configuration.
Returns:
A tuple of (header, errors)
"""
header, errors = header_generator.generate_header(ir, config)
if errors:
_show_errors(errors, ir, color_output)
return (header, errors)

Returns:
A tuple of (header, errors)
"""
header, errors = header_generator.generate_header(ir, config)
if errors:
_show_errors(errors, ir, color_output)
return (header, errors)

def main(flags):
if flags.input_file:
with open(flags.input_file) as f:
ir = ir_data_utils.IrDataSerializer.from_json(ir_data.EmbossIr, f.read())
else:
ir = ir_data_utils.IrDataSerializer.from_json(ir_data.EmbossIr, sys.stdin.read())
config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
header, errors = generate_headers_and_log_errors(ir, flags.color_output, config)
if errors:
return 1
if flags.output_file:
with open(flags.output_file, "w") as f:
f.write(header)
else:
print(header)
return 0


if __name__ == '__main__':
sys.exit(main(_parse_command_line(sys.argv)))
if flags.input_file:
with open(flags.input_file) as f:
ir = ir_data_utils.IrDataSerializer.from_json(ir_data.EmbossIr, f.read())
else:
ir = ir_data_utils.IrDataSerializer.from_json(
ir_data.EmbossIr, sys.stdin.read()
)
config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
header, errors = generate_headers_and_log_errors(ir, flags.color_output, config)
if errors:
return 1
if flags.output_file:
with open(flags.output_file, "w") as f:
f.write(header)
else:
print(header)
return 0


if __name__ == "__main__":
sys.exit(main(_parse_command_line(sys.argv)))
Loading

0 comments on commit 495d3f2

Please sign in to comment.