LibreOffice plugin to pipe whole Writer documents through Google Translate, that ought to keep most of the page formatting.

⌈⌋ ⎇ branch:  PageTranslate


Check-in [00e5bb6299]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:compact via fixture
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 00e5bb629912c2a4f7d4cfca40f23b36da04c6bb
User & Date: mario 2022-10-22 22:17:32
Context
2022-10-23
05:51
faux: move params= to .get check-in: 72453b0947 user: mario tags: trunk
2022-10-22
22:17
compact via fixture check-in: 00e5bb6299 user: mario tags: trunk
2022-10-21
20:42
omit # check-in: cc353ff2fc user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to test/argos.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# encoding: utf-8
import init, os
import translationbackends as tb

kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    "cmd": "",
}

def pons():
    t = tb.ArgosNmt(**kw)
    assert t.translate("Jedes Ende ist auch ein Anfang.") == "Each end is also a beginning."



<


<
<
<
<
<
<
<
<
<
<
<
|
<
|
|



1

2
3











4

5
6
7
8
9
# encoding: utf-8

import translationbackends as tb














def pons(params):
    t = tb.ArgosNmt(**params)
    assert t.translate("Jedes Ende ist auch ein Anfang.") == "Each end is also a beginning."


Changes to test/assign.py.

1
2
3
4

5
6
7
8
9
10
11
# encoding: utf-8
import init, os
import translationbackends as tb


kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",

<


>







1

2
3
4
5
6
7
8
9
10
11
# encoding: utf-8

import translationbackends as tb

# less static
kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
27
28
29
30
31
32
33














    assert isinstance(_ab("Argos"), tb.ArgosNmt)
    assert isinstance(_ab("Duck"), tb.DuckDuckGo)

def deep():
    for name in "DT: Google", "Pons Dict", "DT: Libre":
        assert isinstance(_ab(name), tb.DeepTranslator)
    assert isinstance(_ab("DTA: Any"), tb.DeepTransApi)





















>
>
>
>
>
>
>
>
>
>
>
>
>
>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    assert isinstance(_ab("Argos"), tb.ArgosNmt)
    assert isinstance(_ab("Duck"), tb.DuckDuckGo)

def deep():
    for name in "DT: Google", "Pons Dict", "DT: Libre":
        assert isinstance(_ab(name), tb.DeepTranslator)
    assert isinstance(_ab("DTA: Any"), tb.DeepTransApi)

def classes():
    assert len(tb.BackendUtils.subclasses()) >= 10

def dec_silcence():
    func = tb.BackendUtils.silence(lambda x: undef)
    func(2)

def dec_from_words():
    ls = []
    func = tb.BackendUtils.from_words(lambda text: ls.append(text))
    func("one two three")
    assert len(ls) == 3

Changes to test/cli.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import init
import translationbackends as tb

kw = {
    "from": "auto",
    "lang": "en",
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    #"cmd": "",
}

def tr():
    t = tb.CommandLine(cmd="echo -n -e {text}", **kw)
    assert t.translate("Abc") == "Abc"

<


<
<
<
<
<
<
<
<
<
<
<
<
|
|



1
2












3
4
5
6

import translationbackends as tb













def tr(params):
    t = tb.CommandLine(cmd="echo -n -e {text}", **params)
    assert t.translate("Abc") == "Abc"

Name change from test/init.py to test/conftest.py.



1
2
3
4
5
6
7

8

9
















10



import sys
sys.path.extend(["./pythonpath/", "../pythonpath/"])

import dotenv
dotenv.load_dotenv()

import pytest



import httprequests
















httprequests.update_headers("pytest/"+pytest.__version__, "2.x-dev")

>
>
|

<
<


|
>

>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
1
2
3
4


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import pytest
import dotenv
import sys, copy, os, re
sys.path.extend(["./pythonpath/", "../pythonpath/"])


dotenv.load_dotenv()

import httprequests
httprequests.update_headers("pytest/"+pytest.__version__, "2.x-dev")

@pytest.fixture(scope="module")
def params(request, **kw):
    test = re.sub("^\w+/|\.py$", "", request.node.name)
    p = copy.copy({
        "lang": "en",
        "from": "auto",
        "quick": 0,
        "quick": 0,
        "debug": 1,
        "mode": "page",
        "backend": "custom",
        "api_key": os.environ.get(test.upper()),
        #"email": "",
        #"cmd": "",
        "_test_name": request.node.name,
    })
    p.update(kw)
    return p

import translationbackends as tb

Changes to test/deepl.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

27
28
29
30
31

import init, os, pytest, requests.exceptions, re
import translationbackends as tb

kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": os.environ.get("DEEPL"),
    "email": "",
    "cmd": "",
}

def hello():
    t = tb.DeeplApi(**kw)
    assert t.translate("Hilfe") == "Help"

def cake():
    t = tb.DeeplApi(**kw)
    assert t.translate("Der Kuchen wird kalt.") == "The cake is getting cold."

@pytest.mark.xfail(reason="429")
def deepl_web():

    t = tb.DeeplWeb(**kw)
    try:
        assert t.translate("Hallo Welt.") == "Hello, world."
    except requests.exceptions.HTTPError as e:
        assert re.match("429 Client Error", str(e))

|


<
<
<
<
<
<
<
<
<
<
<
|
<
|
|


|
|


<
|
>
|


|
|
>
1
2
3











4

5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
import pytest, requests.exceptions, re, os
import translationbackends as tb














def hello(params):
    t = tb.DeeplApi(**params)
    assert t.translate("Hilfe") == "Help"

def cake(params):
    t = tb.DeeplApi(**params)
    assert t.translate("Der Kuchen wird kalt.") == "The cake is getting cold."


def deepl_web(params):
    del params["api_key"]
    t = tb.DeeplWeb(**params)
    try:
        assert t.translate("Hallo Welt.") == "Hello, world."
    except requests.exceptions.HTTPError as exc:
        if not re.match('429 Client Error', str(exc)):
            raise exc

Changes to test/duck.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import init, os
import translationbackends as tb

kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    "cmd": "",
}

def frog():
    t = tb.DuckDuckGo(**kw)
    assert t.translate("Frosch tanzt") == "Frog dances"

<


<
<
<
<
<
<
<
<
<
<
<
<
<
|
|



1
2













3
4
5
6

import translationbackends as tb














def frog(params):
    t = tb.DuckDuckGo(**params)
    assert t.translate("Frosch tanzt") == "Frog dances"

Changes to test/google.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import init, os
import translationbackends as tb

kw = {
    "lang": "en",
    "from": "auto",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    "cmd": "",
}

def web():
    t = tb.GoogleWeb(**kw)
    assert t.translate("kuchen") == "cake"

def ajax():
    t = tb.GoogleAjax(**kw)
    assert t.translate("kartoffel") == "potato"

<


<
<
<
<
<
<
<
<
<
<
<
<
<
|
|


|
|



1
2













3
4
5
6
7
8
9
10

import translationbackends as tb














def web(params):
    t = tb.GoogleWeb(**params)
    assert t.translate("kuchen") == "cake"

def ajax(params):
    t = tb.GoogleAjax(**params)
    assert t.translate("kartoffel") == "potato"

Changes to test/httpfaux.py.

1
2
3


4
5
6
7
8
9
10
11
12
13
14
15
16
17
import init, os
from httprequests import FauxRequests



http = FauxRequests()

def get():
    resp = http.get("https://example.org/")
    assert resp.status_code == 200
    assert resp.headers

def post():
    resp = http.post("https://httpbin.org/post", params={"a":"b", "c":"d"})
    assert resp.json()["form"]["a"] == "b"

def json():
    resp = http.get("https://httpbin.org/json")
    assert resp.json()
|


>
>
|

|




|



|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pytest
from httprequests import FauxRequests

@pytest.fixture(scope="module")
def http():
    return FauxRequests()

def get(http):
    resp = http.get("https://example.org/")
    assert resp.status_code == 200
    assert resp.headers

def post(http):
    resp = http.post("https://httpbin.org/post", params={"a":"b", "c":"d"})
    assert resp.json()["form"]["a"] == "b"

def json(http):
    resp = http.get("https://httpbin.org/json")
    assert resp.json()

Changes to test/libre.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
import init, os
import translationbackends as tb

kw = {
    "lang": "en",
    "from": "de",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    "cmd": "",
}

def run():

    t = tb.LibreTranslate(**kw)
    assert t.translate("rennen") == "run"

<


<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
|



1
2













3
4
5
6
7

import translationbackends as tb














def run(params):
    params["from"] = "de"
    t = tb.LibreTranslate(**params)
    assert t.translate("rennen") == "run"

Changes to test/linebreakwise.py.

1
2
3
4
5
6
7
8
import init, os
import translationbackends as tb

def run():
    t = tb.BackendUtils(lang="none")
    t.translate = lambda text: "line"
    assert t.linebreakwise("first\n\nsecond\npara") == "line\n\nline"

<








1
2
3
4
5
6
7

import translationbackends as tb

def run():
    t = tb.BackendUtils(lang="none")
    t.translate = lambda text: "line"
    assert t.linebreakwise("first\n\nsecond\npara") == "line\n\nline"

Changes to test/mymemory.py.

1
2
3
4

5
6
7
8
9
10
11
12
13
14
15
16

17
18

19
20
21
22
# encoding: utf-8
import init, os, pytest
import translationbackends as tb


kw = {
    "lang": "fr",
    "from": "de",
    "quick": 2,
    "debug": 2,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": os.environ.get("EMAIL"),
    "cmd": "",
}


@pytest.mark.often_yields_none
def mymem():

    t = tb.MyMemory(**kw)
    assert t.translate("apfelkuchen") == "Tarte aux pommes"



|


>



<
<
<
<
<

<


>

|
>
|



1
2
3
4
5
6
7
8





9

10
11
12
13
14
15
16
17
18
19
# encoding: utf-8
import pytest, os
import translationbackends as tb

# semi fixture
kw = {
    "lang": "fr",
    "from": "de",





    "email": os.environ.get("EMAIL"),

}


@pytest.mark.often_yields_none
def mymem(params):
    params.update(kw)
    t = tb.MyMemory(**params)
    assert t.translate("apfelkuchen") == "Tarte aux pommes"


Changes to test/pons.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
21
import init, os
import translationbackends as tb

kw = {
    "lang": "it",
    "from": "de",
    "quick": 0,
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": "",
    "email": "",
    "cmd": "",
}

def pons():

    t = tb.PonsWeb(**kw)
    assert t.translate("umlauf") == "circolazione"


<


<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
|




1
2













3
4
5
6
7
8

import translationbackends as tb














def pons(params):
    params["lang"] = "it"
    t = tb.PonsWeb(**params)
    assert t.translate("umlauf") == "circolazione"


Changes to test/spliterate.py.

1
2
3
4
5
6
7
8
import init, os
import translationbackends as tb

# should become 5 segments for 100 chars each
text = """
Aaaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbbb Ccccccccccccccccccc Ddddddddddddddddddd Eeeeeeeeeeeeeeeeeexxx.
Aaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbb Cccccccccccccccccc Dddddddddddddddddd Eeeeeeeeeeeeeeeee.
Aaaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbbb Ccccccccccccccccccc Ddddddddddddddddddd Eeeeeeeeeeeeeee.
<








1
2
3
4
5
6
7

import translationbackends as tb

# should become 5 segments for 100 chars each
text = """
Aaaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbbb Ccccccccccccccccccc Ddddddddddddddddddd Eeeeeeeeeeeeeeeeeexxx.
Aaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbb Cccccccccccccccccc Dddddddddddddddddd Eeeeeeeeeeeeeeeee.
Aaaaaaaaaaaaaaaaaaaa Bbbbbbbbbbbbbbbbbbb Ccccccccccccccccccc Ddddddddddddddddddd Eeeeeeeeeeeeeee.

Changes to test/systran.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# encoding: utf-8

import init
import translationbackends as tb
import os

kw = {
    "from": "auto",
    "lang": "en",
    "quick": 0,
    "debug": 1,
    "mode": "page",
    "backend": "custom",
    "api_key": os.environ.get("SYSTRAN"),
    "email": "",
    "cmd": "",
}


def st():
    t = tb.SysTran(**kw)
    assert t.translate("Die Kuh schläft. Der Frosch tanzt.") == "The cow is asleep. The frog is dancing."


<
<
|
|

<
<
<
<
<
<
<
<
<
<
<
|
|
<
|


1


2
3
4











5
6

7
8
9
# encoding: utf-8


import translationbackends as tb, os
import pytest












def systran(params):
    print(params)

    t = tb.SysTran(**params)
    assert t.translate("Die Kuh schläft. Der Frosch tanzt.") == "The cow is asleep. The frog is dancing."