Skip to content

Commit

Permalink
Update dependencies & project
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Dec 23, 2024
1 parent 0c043a5 commit 1cfd020
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/autodeps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Setup python
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
}}
steps:
- name: Checkout
with:
persist-credentials: false
uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v5
Expand Down
22 changes: 20 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autofix_prs: true
autoupdate_schedule: quarterly
submodules: false
skip: [badgie]
skip: [badgie, project-requirements]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand All @@ -23,7 +23,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.8.4
hooks:
- id: ruff
types: [file]
Expand All @@ -36,3 +36,21 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies:
- tomli
- repo: https://github.com/crate-ci/typos
rev: v1.28.4
hooks:
- id: typos
- repo: https://github.com/woodruffw/zizmor-pre-commit
rev: v0.10.0
hooks:
- id: zizmor
- repo: local
hooks:
- id: project-requirements
name: regenerate requirements.in
language: system
entry: python tools/project_requirements.py
pass_filenames: false
files: ^(test-requirements.in)|(pyproject.toml)$
13 changes: 11 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ keywords = [
]
dependencies = [
"trio~=0.27.0",
"httpx[http2]~=0.27.0",
"httpx[http2]~=0.28.1",
"beautifulsoup4~=4.12.3",
"lxml>=5.3.0"
]
Expand All @@ -49,15 +49,21 @@ version = {attr = "subtitle_translate.main.__version__"}
subtitle_translate = "subtitle_translate.main:cli_run"

[tool.setuptools.package-data]
subtitle_translate = ["py.typed", "data/*"]
subtitle_translate = ["py.typed"]

[tool.mypy]
files = ["src/subtitle_translate/",]
check_untyped_defs = true
disallow_any_decorated = true
disallow_any_generics = true
disallow_any_unimported = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
ignore_missing_imports = true
local_partial_types = true
no_implicit_optional = true
no_implicit_reexport = true
show_column_numbers = true
Expand All @@ -78,6 +84,9 @@ combine-as-imports = true
ignore-words-list = 'astroid,crasher,asend'
skip = "*/agents.py,*/languages.py,*.srt"

[tool.typos.default.extend-identifiers]
AmSavS = "AmSavS"

[tool.black]
line-length = 79

Expand Down
8 changes: 4 additions & 4 deletions src/subtitle_translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def translate_texts(
return {k: tuple(v) for k, v in new_texts.items()}


async def translate_subtiles(
async def translate_subtitles(
source_file: str,
dest_file: str,
source_language: str = "auto",
Expand All @@ -77,8 +77,8 @@ async def translate_subtiles(
print("Translating...")
new_texts = await translate_texts(texts, source_language, dest_language)

sentance_count = sum(map(len, texts.values()))
print(f"Translated {sentance_count} sentences.")
sentence_count = sum(map(len, texts.values()))
print(f"Translated {sentence_count} sentences.")

print("Updating subtitle texts...")
subs = subtitle_parser.modify_subtitles(subs, new_texts)
Expand Down Expand Up @@ -141,7 +141,7 @@ async def run_async() -> None:
name, ext = args.source_file.rsplit(".", 1)
args.dest_file = f"{name}.{args.dest_lang}.{ext}"

await translate_subtiles(
await translate_subtitles(
args.source_file,
args.dest_file,
args.source_lang,
Expand Down
Empty file added src/subtitle_translate/py.typed
Empty file.
8 changes: 5 additions & 3 deletions test-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ typing-extensions
# Types
types-beautifulsoup4

# Checker's own dependencies
trio~=0.27.0
httpx[http2]~=0.27.0
# Subtitle-Translate's own dependencies
#<TOML_DEPENDENCIES>
beautifulsoup4~=4.12.3
httpx[http2]~=0.28.1
lxml>=5.3.0
trio~=0.27.0
#</TOML_DEPENDENCIES>
31 changes: 15 additions & 16 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# This file was autogenerated by uv via the following command:
# uv pip compile --universal --python-version=3.10 test-requirements.in -o test-requirements.txt
anyio==4.6.2.post1
anyio==4.7.0
# via httpx
attrs==24.2.0
attrs==24.3.0
# via
# outcome
# trio
beautifulsoup4==4.12.3
# via -r test-requirements.in
black==24.10.0 ; implementation_name == 'cpython'
# via -r test-requirements.in
certifi==2024.8.30
certifi==2024.12.14
# via
# httpcore
# httpx
cffi==1.17.1 ; implementation_name != 'pypy' and os_name == 'nt'
# via trio
click==8.1.7 ; implementation_name == 'cpython'
click==8.1.8 ; implementation_name == 'cpython'
# via black
codespell==2.3.0
# via -r test-requirements.in
colorama==0.4.6 ; (implementation_name != 'cpython' and sys_platform == 'win32') or (platform_system != 'Windows' and sys_platform == 'win32') or (implementation_name == 'cpython' and platform_system == 'Windows')
colorama==0.4.6 ; sys_platform == 'win32'
# via
# click
# pytest
coverage==7.6.4
coverage==7.6.9
# via
# -r test-requirements.in
# pytest-cov
Expand All @@ -39,9 +39,9 @@ h2==4.1.0
# via httpx
hpack==4.0.0
# via h2
httpcore==1.0.6
httpcore==1.0.7
# via httpx
httpx==0.27.2
httpx==0.28.1
# via -r test-requirements.in
hyperframe==6.0.1
# via h2
Expand All @@ -54,20 +54,20 @@ iniconfig==2.0.0
# via pytest
lxml==5.3.0
# via -r test-requirements.in
mypy==1.13.0
mypy==1.14.0
# via -r test-requirements.in
mypy-extensions==1.0.0
# via
# -r test-requirements.in
# black
# mypy
orjson==3.10.11 ; implementation_name == 'cpython'
orjson==3.10.12 ; implementation_name == 'cpython'
# via -r test-requirements.in
outcome==1.3.0.post0
# via
# pytest-trio
# trio
packaging==24.1
packaging==24.2
# via
# black
# pytest
Expand All @@ -79,7 +79,7 @@ pluggy==1.5.0
# via pytest
pycparser==2.22 ; implementation_name != 'pypy' and os_name == 'nt'
# via cffi
pytest==8.3.3
pytest==8.3.4
# via
# -r test-requirements.in
# pytest-cov
Expand All @@ -88,18 +88,17 @@ pytest-cov==6.0.0
# via -r test-requirements.in
pytest-trio==0.8.0
# via -r test-requirements.in
ruff==0.7.2
ruff==0.8.4
# via -r test-requirements.in
sniffio==1.3.1
# via
# anyio
# httpx
# trio
sortedcontainers==2.4.0
# via trio
soupsieve==2.6
# via beautifulsoup4
tomli==2.0.2 ; python_full_version <= '3.11'
tomli==2.2.1 ; python_full_version <= '3.11'
# via
# black
# coverage
Expand All @@ -119,5 +118,5 @@ typing-extensions==4.12.2
# anyio
# black
# mypy
uv==0.4.29
uv==0.5.11
# via -r test-requirements.in
106 changes: 106 additions & 0 deletions tools/project_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3

"""Project Requirements - Write test-requirements.in based on pyproject.toml."""

# Programmed by CoolCat467

from __future__ import annotations

# Project Requirements - Write test-requirements.in based on pyproject.toml.
# Copyright (C) 2024 CoolCat467
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__title__ = "Project Requirements"
__author__ = "CoolCat467"
__version__ = "0.0.0"
__license__ = "GNU General Public License Version 3"

import sys
from pathlib import Path
from typing import Final

import tomllib

# Key to start replacing inside of contents
KEY: Final = "TOML_DEPENDENCIES"


def run() -> None:
"""Run program."""
# Find root folder
this = Path(__file__).absolute()
tools = this.parent
root = tools.parent
# Make sure it's right
assert (root / "LICENSE").exists(), "Not in correct directory!"

# Read pyproject.toml
pyproject = root / "pyproject.toml"
with pyproject.open("rb") as fp:
data = tomllib.load(fp)

# Get dependencies list
assert isinstance(data, dict)
project = data["project"]
assert isinstance(project, dict)
dependencies = project["dependencies"]
assert isinstance(dependencies, list)

# Read requirements file
requirements_list = root / "test-requirements.in"
assert requirements_list.exists(), f"{requirements_list} does not exist!"
requirements_data = requirements_list.read_text("utf-8")

# Find out what start and end should be based on key.
key_start = f"<{KEY}>"
key_end = f"</{KEY}>"

# Try to find start and end triggers in requirements data
start_char = requirements_data.find(key_start)
end_char = requirements_data.find(key_end)
if -1 in {start_char, end_char}:
raise ValueError(
f"{key_start!r} or {key_end!r} not found in {requirements_list}",
)

# Create overwrite text
dependencies_text = "\n".join(sorted(dependencies))
overwrite_text = "\n".join(
(
key_start,
dependencies_text,
f"#{key_end}",
),
)
# Create new file contents
end = end_char + len(key_end)
new_text = (
requirements_data[:start_char]
+ overwrite_text
+ requirements_data[end:]
)

# If new text differs, overwrite and alert
if new_text != requirements_data:
print("Requirements file is outdated...")
requirements_list.write_text(new_text, "utf-8")
print("Requirements file updated successfully.")
return 1
print("Requirements file is up to date.")
return 0


if __name__ == "__main__":
sys.exit(run())

0 comments on commit 1cfd020

Please sign in to comment.