Skip to content

Commit fad9295

Browse files
authored
fix: modernize python code (#668)
1 parent 711f08a commit fad9295

9 files changed

+392
-407
lines changed

.github/workflows/lint_and_format_check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
- uses: chartboost/ruff-action@e18ae971ccee1b2d7bbef113930f00c670b78da4 # v1.0.0
3636
name: Lint with Ruff
3737
with:
38-
version: 0.4.2
38+
version: 0.4.4

pyproject.toml

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
'lint.select' = [
1+
[project]
2+
name = "ada-url"
3+
requires-python = ">=3.12"
4+
5+
[tool.ruff]
6+
line-length = 120
7+
target-version = "py312"
8+
9+
[tool.ruff.format]
10+
quote-style = "single"
11+
indent-style = "space"
12+
docstring-code-format = true
13+
14+
[tool.ruff.lint]
15+
select = [
216
"C90", # McCabe cyclomatic complexity
317
"E", # pycodestyle
418
"F", # Pyflakes
@@ -17,12 +31,8 @@
1731
"YTT", # flake8-2020
1832
"ANN" # flake8-annotations
1933
]
20-
exclude = [
21-
"docs",
22-
"tests",
23-
]
24-
'lint.ignore' = [
25-
"E722" # Do not use bare `except`
34+
ignore = [
35+
"E722", # Do not use bare `except`
36+
"ANN101", # Missing type annotation for self in method
37+
"TID252", # Prefer absolute imports over relative imports from parent modules
2638
]
27-
line-length = 120
28-
target-version = "py312"

singleheader/amalgamate.py

+48-43
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,40 @@
1111
import re
1212
import shutil
1313
import datetime
14+
1415
if sys.version_info[0] < 3:
15-
sys.stdout.write("Sorry, requires Python 3.x or better\n")
16+
sys.stdout.write('Sorry, requires Python 3.x or better\n')
1617
sys.exit(1)
1718

1819
SCRIPTPATH = os.path.dirname(os.path.abspath(sys.argv[0]))
1920
PROJECTPATH = os.path.dirname(SCRIPTPATH)
20-
print(f"SCRIPTPATH={SCRIPTPATH} PROJECTPATH={PROJECTPATH}")
21+
print(f'SCRIPTPATH={SCRIPTPATH} PROJECTPATH={PROJECTPATH}')
2122

22-
if "AMALGAMATE_SOURCE_PATH" not in os.environ:
23-
AMALGAMATE_SOURCE_PATH = os.path.join(PROJECTPATH, "src")
23+
if 'AMALGAMATE_SOURCE_PATH' not in os.environ:
24+
AMALGAMATE_SOURCE_PATH = os.path.join(PROJECTPATH, 'src')
2425
else:
25-
AMALGAMATE_SOURCE_PATH = os.environ["AMALGAMATE_SOURCE_PATH"]
26-
if "AMALGAMATE_INCLUDE_PATH" not in os.environ:
27-
AMALGAMATE_INCLUDE_PATH = os.path.join(PROJECTPATH, "include")
26+
AMALGAMATE_SOURCE_PATH = os.environ['AMALGAMATE_SOURCE_PATH']
27+
if 'AMALGAMATE_INCLUDE_PATH' not in os.environ:
28+
AMALGAMATE_INCLUDE_PATH = os.path.join(PROJECTPATH, 'include')
2829
else:
29-
AMALGAMATE_INCLUDE_PATH = os.environ["AMALGAMATE_INCLUDE_PATH"]
30-
if "AMALGAMATE_OUTPUT_PATH" not in os.environ:
30+
AMALGAMATE_INCLUDE_PATH = os.environ['AMALGAMATE_INCLUDE_PATH']
31+
if 'AMALGAMATE_OUTPUT_PATH' not in os.environ:
3132
AMALGAMATE_OUTPUT_PATH = os.path.join(SCRIPTPATH)
3233
else:
33-
AMALGAMATE_OUTPUT_PATH = os.environ["AMALGAMATE_OUTPUT_PATH"]
34+
AMALGAMATE_OUTPUT_PATH = os.environ['AMALGAMATE_OUTPUT_PATH']
3435

3536
# this list excludes the "src/generic headers"
36-
ALLCFILES = ["ada.cpp"]
37+
ALLCFILES = ['ada.cpp']
3738

3839
# order matters
39-
ALLCHEADERS = ["ada.h"]
40+
ALLCHEADERS = ['ada.h']
4041

4142
found_includes = []
4243

43-
current_implementation=''
44+
current_implementation = ''
4445

45-
def doinclude(fid: str, file: str, line: str, origin: str) -> None:
4646

47+
def doinclude(fid: str, file: str, line: str, origin: str) -> None:
4748
p = os.path.join(AMALGAMATE_INCLUDE_PATH, file)
4849
pi = os.path.join(AMALGAMATE_SOURCE_PATH, file)
4950

@@ -61,22 +62,23 @@ def doinclude(fid: str, file: str, line: str, origin: str) -> None:
6162
pass
6263
else:
6364
# If we don't recognize it, just emit the #include
64-
print("unrecognized:", file, " from ", line, " in ", origin)
65+
print('unrecognized:', file, ' from ', line, ' in ', origin)
6566
print(line, file=fid)
6667

68+
6769
def dofile(fid: str, prepath: str, filename: str) -> None:
6870
file = os.path.join(prepath, filename)
6971
RELFILE = os.path.relpath(file, PROJECTPATH)
7072
# Last lines are always ignored. Files should end by an empty lines.
71-
print(f"/* begin file {RELFILE} */", file=fid)
73+
print(f'/* begin file {RELFILE} */', file=fid)
7274
includepattern = re.compile('\\s*#\\s*include "(.*)"')
7375
with open(file, 'r') as fid2:
7476
for line in fid2:
7577
line = line.rstrip('\n')
7678
s = includepattern.search(line)
7779
if s:
7880
includedfile = s.group(1)
79-
if includedfile == "ada.h" and filename == "ada.cpp":
81+
if includedfile == 'ada.h' and filename == 'ada.cpp':
8082
print(line, file=fid)
8183
continue
8284

@@ -86,7 +88,7 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
8688
doinclude(fid, includedfile, line, filename)
8789
else:
8890
print(line, file=fid)
89-
print(f"/* end file {RELFILE} */", file=fid)
91+
print(f'/* end file {RELFILE} */', file=fid)
9092

9193

9294
# Get the generation date from git, so the output is reproducible.
@@ -95,51 +97,54 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
9597
# Forcing it to be UTC is difficult, because it needs to be portable
9698
# between gnu date and busybox date.
9799
try:
98-
timestamp = subprocess.run(['git', 'show', '-s', '--format=%ci', 'HEAD'],
99-
stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
100+
timestamp = (
101+
subprocess.run(['git', 'show', '-s', '--format=%ci', 'HEAD'], stdout=subprocess.PIPE)
102+
.stdout.decode('utf-8')
103+
.strip()
104+
)
100105
except Exception:
101-
print("git not found, timestamp based on current time")
106+
print('git not found, timestamp based on current time')
102107
timestamp = str(datetime.datetime.now())
103-
print(f"timestamp is {timestamp}")
108+
print(f'timestamp is {timestamp}')
104109

105110
os.makedirs(AMALGAMATE_OUTPUT_PATH, exist_ok=True)
106-
AMAL_H = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada.h")
107-
AMAL_C = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada.cpp")
108-
DEMOCPP = os.path.join(AMALGAMATE_OUTPUT_PATH, "cpp")
109-
README = os.path.join(AMALGAMATE_OUTPUT_PATH, "README.md")
111+
AMAL_H = os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.h')
112+
AMAL_C = os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.cpp')
113+
DEMOCPP = os.path.join(AMALGAMATE_OUTPUT_PATH, 'cpp')
114+
README = os.path.join(AMALGAMATE_OUTPUT_PATH, 'README.md')
110115

111-
print(f"Creating {AMAL_H}")
116+
print(f'Creating {AMAL_H}')
112117
amal_h = open(AMAL_H, 'w')
113-
print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_h)
118+
print(f'/* auto-generated on {timestamp}. Do not edit! */', file=amal_h)
114119
for h in ALLCHEADERS:
115-
doinclude(amal_h, h, f"ERROR {h} not found", h)
120+
doinclude(amal_h, h, f'ERROR {h} not found', h)
116121

117122
amal_h.close()
118123
print()
119124
print()
120-
print(f"Creating {AMAL_C}")
125+
print(f'Creating {AMAL_C}')
121126
amal_c = open(AMAL_C, 'w')
122-
print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_c)
127+
print(f'/* auto-generated on {timestamp}. Do not edit! */', file=amal_c)
123128
for c in ALLCFILES:
124-
doinclude(amal_c, c, f"ERROR {c} not found", c)
129+
doinclude(amal_c, c, f'ERROR {c} not found', c)
125130

126131
amal_c.close()
127132

128133
# copy the README and DEMOCPP
129134
if SCRIPTPATH != AMALGAMATE_OUTPUT_PATH:
130-
shutil.copy2(os.path.join(SCRIPTPATH,"demo.cpp"),AMALGAMATE_OUTPUT_PATH)
131-
shutil.copy2(os.path.join(SCRIPTPATH,"demo.c"),AMALGAMATE_OUTPUT_PATH)
132-
shutil.copy2(os.path.join(SCRIPTPATH,"README.md"),AMALGAMATE_OUTPUT_PATH)
135+
shutil.copy2(os.path.join(SCRIPTPATH, 'demo.cpp'), AMALGAMATE_OUTPUT_PATH)
136+
shutil.copy2(os.path.join(SCRIPTPATH, 'demo.c'), AMALGAMATE_OUTPUT_PATH)
137+
shutil.copy2(os.path.join(SCRIPTPATH, 'README.md'), AMALGAMATE_OUTPUT_PATH)
133138

134-
shutil.copy2(os.path.join(AMALGAMATE_INCLUDE_PATH,"ada_c.h"),AMALGAMATE_OUTPUT_PATH)
139+
shutil.copy2(os.path.join(AMALGAMATE_INCLUDE_PATH, 'ada_c.h'), AMALGAMATE_OUTPUT_PATH)
135140

136-
zf = zipfile.ZipFile(os.path.join(AMALGAMATE_OUTPUT_PATH,'singleheader.zip'), 'w', zipfile.ZIP_DEFLATED)
137-
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada.cpp"), "ada.cpp")
138-
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada.h"), "ada.h")
139-
zf.write(os.path.join(AMALGAMATE_INCLUDE_PATH,"ada_c.h"), "ada_c.h")
141+
zf = zipfile.ZipFile(os.path.join(AMALGAMATE_OUTPUT_PATH, 'singleheader.zip'), 'w', zipfile.ZIP_DEFLATED)
142+
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.cpp'), 'ada.cpp')
143+
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.h'), 'ada.h')
144+
zf.write(os.path.join(AMALGAMATE_INCLUDE_PATH, 'ada_c.h'), 'ada_c.h')
140145

141146

142-
print("Done with all files generation.")
147+
print('Done with all files generation.')
143148

144-
print(f"Files have been written to directory: {AMALGAMATE_OUTPUT_PATH}/")
145-
print("Done with all files generation.")
149+
print(f'Files have been written to directory: {AMALGAMATE_OUTPUT_PATH}/')
150+
print('Done with all files generation.')

tools/release/create_release.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
from github import Github
55
import lib.release as release
66

7-
WORK_DIR = os.path.dirname(os.path.abspath(__file__)).replace("/tools/release", "")
7+
WORK_DIR = os.path.dirname(os.path.abspath(__file__)).replace('/tools/release', '')
88

9-
NEXT_TAG = os.environ["NEXT_RELEASE_TAG"]
10-
REPO_NAME = os.environ["GITHUB_REPOSITORY"]
11-
TOKEN = os.environ["GITHUB_TOKEN"]
9+
NEXT_TAG = os.environ['NEXT_RELEASE_TAG']
10+
REPO_NAME = os.environ['GITHUB_REPOSITORY']
11+
TOKEN = os.environ['GITHUB_TOKEN']
1212
if not NEXT_TAG or not REPO_NAME or not TOKEN:
13-
raise Exception(
14-
"Bad environment variables. Invalid GITHUB_REPOSITORY, GITHUB_TOKEN or NEXT_RELEASE_TAG"
15-
)
13+
raise Exception('Bad environment variables. Invalid GITHUB_REPOSITORY, GITHUB_TOKEN or NEXT_RELEASE_TAG')
1614

1715
g = Github(TOKEN)
1816
repo = g.get_repo(REPO_NAME)
@@ -22,7 +20,7 @@
2220
release.create_release(repo, NEXT_TAG, release_notes)
2321

2422
release = repo.get_release(NEXT_TAG)
25-
release.upload_asset("singleheader/ada.cpp")
26-
release.upload_asset("singleheader/ada.h")
27-
release.upload_asset("singleheader/ada_c.h")
28-
release.upload_asset("singleheader/singleheader.zip")
23+
release.upload_asset('singleheader/ada.cpp')
24+
release.upload_asset('singleheader/ada.h')
25+
release.upload_asset('singleheader/ada_c.h')
26+
release.upload_asset('singleheader/singleheader.zip')

0 commit comments

Comments
 (0)