Skip to content

Commit 4865f98

Browse files
authored
Merge branch 'master' into master
2 parents 495bc7e + 134a64d commit 4865f98

File tree

17 files changed

+80
-59
lines changed

17 files changed

+80
-59
lines changed

.github/workflows/python-package-develop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
os: [ubuntu-latest]
2626
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
2727

28+
2829
steps:
2930
- uses: actions/checkout@v2
3031
- name: Set up Python ${{ matrix.python-version }}

.github/workflows/python-package.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
os: [ubuntu-latest, macos-11]
3131
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
3232

33+
3334
steps:
3435
- uses: actions/checkout@v2
3536
- name: Set up Python ${{ matrix.python-version }}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tags
55
dist/
66
build/
77
docs/build/
8-
8+
.coverage
99
/.venv
1010
/debug
1111
/.vimspector.json

c_formatter_42/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# #
1313
# ############################################################################ #
1414

15-
import sys
1615
import argparse
16+
import sys
1717

1818
from c_formatter_42.run import run_all
1919

@@ -48,15 +48,15 @@ def main():
4848
content = file.read()
4949
if args.confirm:
5050
result = input(
51-
"Are you sure you want to overwrite {}?[y/N]".format(filepath)
51+
f"Are you sure you want to overwrite {filepath}?[y/N]"
5252
)
5353
if result != "y":
5454
continue
55-
print("Writing to {}".format(filepath))
55+
print(f"Writing to {filepath}")
5656
with open(filepath, "w") as file:
5757
file.write(run_all(content))
5858
except OSError as e:
59-
print("Error: {}: {}".format(e.filename, e.strerror))
59+
print(f"Error: {e.filename}: {e.strerror}")
6060

6161

6262
if __name__ == "__main__":

c_formatter_42/formatters/clang_format.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
# #
1111
# ############################################################################ #
1212

13-
import os
14-
import sys
15-
import inspect
1613
import subprocess
14+
import sys
1715
from contextlib import contextmanager
16+
from pathlib import Path
1817

1918
import c_formatter_42.data
2019

21-
CONFIG_FILENAME = ".clang-format"
20+
CONFIG_FILENAME = Path(".clang-format")
2221

23-
DATA_DIR = os.path.dirname(inspect.getfile(c_formatter_42.data))
22+
DATA_DIR = Path(c_formatter_42.data.__file__).parent
2423

2524

2625
@contextmanager
@@ -29,31 +28,29 @@ def _config_context():
2928
If there already is a config in the current directory, it's backed up
3029
then put back in place after clang-format is done running
3130
"""
32-
config_path = os.path.join(DATA_DIR, CONFIG_FILENAME)
31+
config_path = DATA_DIR / CONFIG_FILENAME
3332
previous_config = None
3433
try:
35-
os.symlink(config_path, CONFIG_FILENAME)
34+
CONFIG_FILENAME.symlink_to(config_path)
3635
except FileExistsError:
37-
if not os.path.islink(CONFIG_FILENAME):
38-
with open(CONFIG_FILENAME) as f:
39-
previous_config = f.read()
40-
os.unlink(CONFIG_FILENAME)
41-
os.symlink(config_path, CONFIG_FILENAME)
36+
if not CONFIG_FILENAME.is_symlink():
37+
previous_config = CONFIG_FILENAME.read_text()
38+
CONFIG_FILENAME.unlink()
39+
CONFIG_FILENAME.symlink_to(config_path)
4240
try:
4341
yield
4442
finally:
45-
os.unlink(CONFIG_FILENAME)
43+
CONFIG_FILENAME.unlink()
4644
if previous_config is not None:
47-
with open(CONFIG_FILENAME, "w") as f:
48-
f.write(previous_config)
45+
CONFIG_FILENAME.write_text(previous_config)
4946

5047

5148
if sys.platform == "linux":
52-
CLANG_FORMAT_EXEC = os.path.join(DATA_DIR, "clang-format-linux")
49+
CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-linux"
5350
elif sys.platform == "darwin":
54-
CLANG_FORMAT_EXEC = os.path.join(DATA_DIR, "clang-format-darwin")
51+
CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-darwin"
5552
elif sys.platform == "win32":
56-
CLANG_FORMAT_EXEC = os.path.join(DATA_DIR, "clang-format-win32.exe")
53+
CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-win32.exe"
5754
else:
5855
raise NotImplementedError("Your platform is not supported")
5956

@@ -73,6 +70,6 @@ def clang_format(content: str) -> str:
7370
stderr=subprocess.PIPE,
7471
)
7572
out, err = process.communicate(input=content.encode())
76-
if process.returncode != 0:
73+
if process.returncode != 0: # pragma: no cover
7774
raise RuntimeError(f"clang-format error: {err.decode()}")
7875
return out.decode()

c_formatter_42/formatters/helper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import re
1414

15-
1615
# regex for a type
1716
REGEX_TYPE = r"(?!return)([a-z]+\s+)*[a-zA-Z_]\w*"
1817
# regex for a c variable/function name

c_formatter_42/formatters/hoist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def hoist(content: str) -> str:
5555
line,
5656
)
5757
if m is not None:
58-
lines.append("\t{}\t{};".format(m.group("type"), m.group("name")))
58+
lines.append(f"\t{m.group('type')}\t{m.group('name')};")
5959
lines.append(
6060
"{}{} = {};".format(
6161
m.group("indent"),

c_formatter_42/formatters/line_breaker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import re
2+
23
from c_formatter_42.formatters import helper
34

45

56
def line_breaker(content: str, column_limit: int = 80) -> str:
67
lines = content.split("\n")
78
lines = [insert_break(line, column_limit) for line in lines]
8-
99
return "\n".join(lines)
1010

1111

@@ -36,7 +36,6 @@ def insert_break(line: str, column_limit: int) -> str:
3636
return line
3737

3838

39-
#
4039
# additional indent level increases in proportion to corresponds paren depth
4140
#
4241
# (examples)

c_formatter_42/formatters/misc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def parenthesize_return(content: str) -> str:
1717
return re.sub(
1818
r"return\s+(?!;)(?!\(.*\);)(?P<value>\(?.*?)\s*;",
19-
lambda match: "return ({});".format(match.group("value").strip()),
19+
lambda match: f"return ({match.group('value').strip()});",
2020
content,
2121
re.DOTALL,
2222
)
@@ -33,14 +33,14 @@ def space_before_semi_colon(content: str) -> str:
3333
def remove_multiline_condition_space(content: str) -> str:
3434
return re.sub(
3535
r"(?P<tabs>\t+) {1,3}(?P<rest>.*)",
36-
lambda match: "{}\t{}".format(match.group("tabs"), match.group("rest")),
36+
lambda match: f"{match.group('tabs')}\t{match.group('rest')}",
3737
content,
3838
)
3939

4040

4141
def insert_void(content: str) -> str:
4242
return re.sub(
4343
r"(?P<funcdef>[0-9a-zA-Z_]+\t+\**[0-9a-zA-Z_]*\s*)\(\s*\)",
44-
lambda match: "{}({})".format(match.group("funcdef"), "void"),
44+
lambda match: f"{match.group('funcdef')}(void)",
4545
content,
4646
)

c_formatter_42/run.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
# #
1111
# ############################################################################ #
1212

13+
from c_formatter_42.formatters.align import align
1314
from c_formatter_42.formatters.clang_format import clang_format
1415
from c_formatter_42.formatters.hoist import hoist
15-
from c_formatter_42.formatters.align import align
16-
from c_formatter_42.formatters.preprocessor_directive import preprocessor_directive
17-
from c_formatter_42.formatters.return_type_single_tab import return_type_single_tab
16+
from c_formatter_42.formatters.line_breaker import line_breaker
1817
from c_formatter_42.formatters.misc import (
18+
insert_void,
1919
parenthesize_return,
20-
space_before_semi_colon,
2120
remove_multiline_condition_space,
22-
insert_void,
21+
space_before_semi_colon,
2322
)
24-
from c_formatter_42.formatters.line_breaker import line_breaker
23+
from c_formatter_42.formatters.preprocessor_directive import preprocessor_directive
24+
from c_formatter_42.formatters.return_type_single_tab import return_type_single_tab
2525

2626

2727
def run_all(content: str) -> str:

0 commit comments

Comments
 (0)