Skip to content

Commit

Permalink
#105 Merge pull request from astropenguin/astropenguin/issue104
Browse files Browse the repository at this point in the history
Fix deepl module
  • Loading branch information
astropenguin committed May 7, 2023
2 parents 1a93a5e + 9457012 commit 658b214
Show file tree
Hide file tree
Showing 8 changed files with 953 additions and 897 deletions.
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM ubuntu:focal
ENV DEBIAN_FRONTEND=noninteractive
ENV PATH=$PATH:/root/.local/bin
ENV POETRY_VIRTUALENVS_CREATE=false
ENV POETRY_VERSION=1.4.2

RUN apt-get update \
&& apt-get install -y curl git python3 python3-pip \
Expand Down
37 changes: 20 additions & 17 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
"dockerfile": "Dockerfile"
},
"postCreateCommand": ".devcontainer/postCreateCommand",
"extensions": [
"github.vscode-pull-request-github",
"mhutchie.git-graph",
"ms-python.python",
"streetsidesoftware.code-spell-checker",
"tamasfe.even-better-toml"
],
"settings": {
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"python.analysis.typeCheckingMode": "basic",
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"[python]": {
"editor.formatOnSave": true,
"editor.insertSpaces": true,
"editor.tabSize": 4
"customizations": {
"vscode": {
"extensions": [
"github.vscode-pull-request-github",
"mhutchie.git-graph",
"ms-python.python",
"streetsidesoftware.code-spell-checker",
"tamasfe.even-better-toml"
],
"settings": {
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"python.formatting.provider": "black",
"python.languageServer": "Pylance",
"[python]": {
"editor.formatOnSave": true,
"editor.insertSpaces": true,
"editor.tabSize": 4
}
}
}
}
}
17 changes: 7 additions & 10 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
release:
types:
- created
- edited

jobs:
job:
Expand All @@ -13,19 +12,17 @@ jobs:
env:
POETRY_VIRTUALENVS_CREATE: false
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
- name: Install dependencies
run: |
pip install poetry
poetry install
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install project dependencies
run: pip install poetry && poetry install
- name: Build docs
run: docs/build
- name: Deploy docs
uses: peaceiris/actions-gh-pages@v3
with:
force_orphan: true
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/_build
publish_branch: gh-pages
force_orphan: true
6 changes: 4 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jobs:
env:
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Publish package to PyPI
run: pip install poetry && poetry publish --build
22 changes: 11 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ on:

jobs:
job:
name: Test (Python ${{ matrix.python-version }})
name: Test (Python ${{ matrix.python }})
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
env:
POETRY_VIRTUALENVS_CREATE: false
TARGET_DIRS: arxiv_post docs tests
strategy:
fail-fast: false
matrix:
python: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
python-version: ${{ matrix.python }}
- name: Install project dependencies
run: |
pip install poetry
poetry install
playwright install chromium
- name: Test code's formatting (Black)
run: black --check ${TARGET_DIRS}
run: black --check arxiv_post docs tests
- name: Test code's execution (pytest)
run: pytest -v tests
- name: Test docs' build (Sphinx)
- name: Test docs' building (Sphinx)
run: docs/build
26 changes: 17 additions & 9 deletions arxiv_post/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


# standard library
import re
from asyncio import gather, sleep, run
from logging import getLogger
from textwrap import shorten
Expand All @@ -26,8 +27,10 @@


# constants
DEEPL_INPUT = "textarea.lmt__source_textarea"
DEEPL_OUTPUT = "#target-dummydiv"
DEEPL_SOURCE_DIV = "data-testid=translator-source-input"
DEEPL_SOURCE_BOX = "role=textbox"
DEEPL_TARGET_DIV = "data-testid=translator-target-input"
DEEPL_TARGET_BOX = "role=textbox"
DEEPL_TRANSLATOR = "https://deepl.com/translator"


Expand Down Expand Up @@ -161,27 +164,32 @@ async def inner(translatables: Iterable[TL]) -> List[TL]:

async def _translate(translatable: TL, page: Page, timeout: float) -> TL:
"""Translate an object in a page of a browser."""
if not (original := str(translatable)):
if not (source := str(translatable)):
return translatable

await page.fill(DEEPL_INPUT, "")
await page.fill(DEEPL_INPUT, original)
source_box = page.locator(DEEPL_SOURCE_DIV).locator(DEEPL_SOURCE_BOX)
target_box = page.locator(DEEPL_TARGET_DIV).locator(DEEPL_TARGET_BOX)

await source_box.fill("")
await source_box.fill(source)

for _ in range(int(timeout / 0.5)):
await sleep(0.5)

if (content := await page.text_content(DEEPL_OUTPUT)) is None:
if (target := await target_box.inner_text()) is None:
continue

if not (content := content.strip()):
if not (target := target.strip()):
continue

target = re.sub("\n+", "\n", target)

try:
return translatable.replace(original, content)
return translatable.replace(source, target)
except ValueError:
break

logger.warn(f"Failed to translate: {shorten(original, 50)!r}")
logger.warn(f"Failed to translate: {shorten(source, 50)!r}")
return translatable


Expand Down
Loading

0 comments on commit 658b214

Please sign in to comment.