🔧 Ruff format python files within utils folder (#12142)

Remove from `exclude` list
This commit is contained in:
Chris Sewell
2024-03-19 19:32:28 +01:00
committed by GitHub
parent 3bedde26a9
commit 392358d4de
4 changed files with 56 additions and 41 deletions

View File

@@ -486,5 +486,4 @@ exclude = [
"tests/test_versioning.py", "tests/test_versioning.py",
"tests/test_writers/**/*", "tests/test_writers/**/*",
"tests/utils.py", "tests/utils.py",
"utils/**/*",
] ]

View File

@@ -31,7 +31,7 @@ from babel.messages.pofile import read_po, write_po
from babel.util import pathmatch from babel.util import pathmatch
from jinja2.ext import babel_extract as extract_jinja2 from jinja2.ext import babel_extract as extract_jinja2
ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "..")) ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), '..', '..'))
TEX_DELIMITERS = { TEX_DELIMITERS = {
'variable_start_string': '<%=', 'variable_start_string': '<%=',
'variable_end_string': '%>', 'variable_end_string': '%>',
@@ -100,12 +100,15 @@ def run_extract() -> None:
options = opt_dict options = opt_dict
with open(os.path.join(root, filename), 'rb') as fileobj: with open(os.path.join(root, filename), 'rb') as fileobj:
for lineno, message, comments, context in extract( for lineno, message, comments, context in extract(
method, fileobj, KEYWORDS, options=options, method, fileobj, KEYWORDS, options=options
): ):
filepath = os.path.join(input_path, relative_name) filepath = os.path.join(input_path, relative_name)
catalogue.add( catalogue.add(
message, None, [(filepath, lineno)], message,
auto_comments=comments, context=context, None,
[(filepath, lineno)],
auto_comments=comments,
context=context,
) )
break break
@@ -137,7 +140,8 @@ def run_update() -> None:
catalog.update(template) catalog.update(template)
tmp_name = os.path.join( tmp_name = os.path.join(
os.path.dirname(filename), tempfile.gettempprefix() + os.path.basename(filename), os.path.dirname(filename),
tempfile.gettempprefix() + os.path.basename(filename),
) )
try: try:
with open(tmp_name, 'wb') as tmpfile: with open(tmp_name, 'wb') as tmpfile:
@@ -179,8 +183,13 @@ def run_compile() -> None:
for message, errors in catalog.check(): for message, errors in catalog.check():
for error in errors: for error in errors:
total_errors += 1 total_errors += 1
log.error('error: %s:%d: %s\nerror: in message string: %s', log.error(
po_file, message.lineno, error, message.string) 'error: %s:%d: %s\nerror: in message string: %s',
po_file,
message.lineno,
error,
message.string,
)
mo_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.mo') mo_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.mo')
log.info('compiling catalog %s to %s', po_file, mo_file) log.info('compiling catalog %s to %s', po_file, mo_file)
@@ -192,26 +201,30 @@ def run_compile() -> None:
js_catalogue = {} js_catalogue = {}
for message in catalog: for message in catalog:
if any( if any(
x[0].endswith(('.js', '.js.jinja', '.js_t', '.html')) x[0].endswith(('.js', '.js.jinja', '.js_t', '.html'))
for x in message.locations for x in message.locations
): ):
msgid = message.id msgid = message.id
if isinstance(msgid, (list, tuple)): if isinstance(msgid, (list, tuple)):
msgid = msgid[0] msgid = msgid[0]
js_catalogue[msgid] = message.string js_catalogue[msgid] = message.string
obj = json.dumps({ obj = json.dumps(
'messages': js_catalogue, {
'plural_expr': catalog.plural_expr, 'messages': js_catalogue,
'locale': str(catalog.locale), 'plural_expr': catalog.plural_expr,
}, sort_keys=True, indent=4) 'locale': str(catalog.locale),
},
sort_keys=True,
indent=4,
)
with open(js_file, 'wb') as outfile: with open(js_file, 'wb') as outfile:
# to ensure lines end with ``\n`` rather than ``\r\n``: # to ensure lines end with ``\n`` rather than ``\r\n``:
outfile.write(f'Documentation.addTranslations({obj});'.encode()) outfile.write(f'Documentation.addTranslations({obj});'.encode())
if total_errors > 0: if total_errors > 0:
log.error('%d errors encountered.', total_errors) log.error('%d errors encountered.', total_errors)
print("Compiling failed.", file=sys.stderr) print('Compiling failed.', file=sys.stderr)
raise SystemExit(2) raise SystemExit(2)
@@ -232,13 +245,13 @@ if __name__ == '__main__':
raise SystemExit(2) from None raise SystemExit(2) from None
os.chdir(ROOT) os.chdir(ROOT)
if action == "extract": if action == 'extract':
run_extract() run_extract()
elif action == "update": elif action == 'update':
run_update() run_update()
elif action == "compile": elif action == 'compile':
run_compile() run_compile()
elif action == "all": elif action == 'all':
run_extract() run_extract()
run_update() run_update()
run_compile() run_compile()

View File

@@ -25,22 +25,28 @@ SPHINX_VERSION_PREFIX = 'Sphinx=='
for file in DOCKERFILE_BASE, DOCKERFILE_LATEXPDF: for file in DOCKERFILE_BASE, DOCKERFILE_LATEXPDF:
content = file.read_text(encoding='utf-8') content = file.read_text(encoding='utf-8')
content = re.sub(rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"', content = re.sub(
rf'{OPENCONTAINERS_VERSION_PREFIX} = "{VERSION}"', rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"',
content) rf'{OPENCONTAINERS_VERSION_PREFIX} = "{VERSION}"',
content = re.sub(rf'{re.escape(SPHINX_VERSION_PREFIX)}{VERSION_PATTERN}', content,
rf'{SPHINX_VERSION_PREFIX}{VERSION}', )
content) content = re.sub(
rf'{re.escape(SPHINX_VERSION_PREFIX)}{VERSION_PATTERN}',
rf'{SPHINX_VERSION_PREFIX}{VERSION}',
content,
)
file.write_text(content, encoding='utf-8') file.write_text(content, encoding='utf-8')
def git(*args: str) -> None: def git(*args: str) -> None:
ret = subprocess.run(('git', *args), ret = subprocess.run(
capture_output=True, ('git', *args),
cwd=DOCKER_ROOT, capture_output=True,
check=True, cwd=DOCKER_ROOT,
text=True, check=True,
encoding='utf-8') text=True,
encoding='utf-8',
)
print(ret.stdout) print(ret.stdout)
print(ret.stderr, file=sys.stderr) print(ret.stderr, file=sys.stderr)

View File

@@ -23,9 +23,7 @@ RELEASE_TYPE = {'a': 'alpha', 'b': 'beta'}
VersionInfo: TypeAlias = tuple[int, int, int, str, int] VersionInfo: TypeAlias = tuple[int, int, int, str, int]
def stringify_version( def stringify_version(version_info: VersionInfo, in_develop: bool = True) -> str:
version_info: VersionInfo, in_develop: bool = True,
) -> str:
version = '.'.join(str(v) for v in version_info[:3]) version = '.'.join(str(v) for v in version_info[:3])
if not in_develop and version_info[3] != 'final': if not in_develop and version_info[3] != 'final':
version += version_info[3][0] + str(version_info[4]) version += version_info[3][0] + str(version_info[4])
@@ -33,9 +31,7 @@ def stringify_version(
return version return version
def bump_version( def bump_version(path: Path, version_info: VersionInfo, in_develop: bool = True) -> None:
path: Path, version_info: VersionInfo, in_develop: bool = True,
) -> None:
version = stringify_version(version_info, in_develop) version = stringify_version(version_info, in_develop)
with open(path, encoding='utf-8') as f: with open(path, encoding='utf-8') as f:
@@ -177,9 +173,10 @@ def parse_options(argv: Sequence[str]) -> argparse.Namespace:
def main() -> None: def main() -> None:
options = parse_options(sys.argv[1:]) options = parse_options(sys.argv[1:])
with processing("Rewriting sphinx/__init__.py"): with processing('Rewriting sphinx/__init__.py'):
bump_version(package_dir / 'sphinx' / '__init__.py', bump_version(
options.version, options.in_develop) package_dir / 'sphinx' / '__init__.py', options.version, options.in_develop
)
with processing('Rewriting CHANGES'): with processing('Rewriting CHANGES'):
changes = Changes(package_dir / 'CHANGES.rst') changes = Changes(package_dir / 'CHANGES.rst')