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

View File

@@ -25,22 +25,28 @@ SPHINX_VERSION_PREFIX = 'Sphinx=='
for file in DOCKERFILE_BASE, DOCKERFILE_LATEXPDF:
content = file.read_text(encoding='utf-8')
content = re.sub(rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"',
rf'{OPENCONTAINERS_VERSION_PREFIX} = "{VERSION}"',
content)
content = re.sub(rf'{re.escape(SPHINX_VERSION_PREFIX)}{VERSION_PATTERN}',
rf'{SPHINX_VERSION_PREFIX}{VERSION}',
content)
content = re.sub(
rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"',
rf'{OPENCONTAINERS_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')
def git(*args: str) -> None:
ret = subprocess.run(('git', *args),
capture_output=True,
cwd=DOCKER_ROOT,
check=True,
text=True,
encoding='utf-8')
ret = subprocess.run(
('git', *args),
capture_output=True,
cwd=DOCKER_ROOT,
check=True,
text=True,
encoding='utf-8',
)
print(ret.stdout)
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]
def stringify_version(
version_info: VersionInfo, in_develop: bool = True,
) -> str:
def stringify_version(version_info: VersionInfo, in_develop: bool = True) -> str:
version = '.'.join(str(v) for v in version_info[:3])
if not in_develop and version_info[3] != 'final':
version += version_info[3][0] + str(version_info[4])
@@ -33,9 +31,7 @@ def stringify_version(
return version
def bump_version(
path: Path, version_info: VersionInfo, in_develop: bool = True,
) -> None:
def bump_version(path: Path, version_info: VersionInfo, in_develop: bool = True) -> None:
version = stringify_version(version_info, in_develop)
with open(path, encoding='utf-8') as f:
@@ -177,9 +173,10 @@ def parse_options(argv: Sequence[str]) -> argparse.Namespace:
def main() -> None:
options = parse_options(sys.argv[1:])
with processing("Rewriting sphinx/__init__.py"):
bump_version(package_dir / 'sphinx' / '__init__.py',
options.version, options.in_develop)
with processing('Rewriting sphinx/__init__.py'):
bump_version(
package_dir / 'sphinx' / '__init__.py', options.version, options.in_develop
)
with processing('Rewriting CHANGES'):
changes = Changes(package_dir / 'CHANGES.rst')