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

⌈⌋ branch:  PageTranslate

Artifact [bbd0da8ca7]

Artifact bbd0da8ca784851dce74befec836cf61b66b797a:

  • File pythonpath/ — part of check-in [72453b0947] at 2022-10-23 05:51:10 on branch trunk — faux: move params= to .get (user: mario size: 3191)

# encoding: utf-8
# api: python
##type: classes
# category: http
# title: request/fallback
# description: loads requests, or similuates API via urllib
# version: 0.7
# state: beta
# license: CC-0
# depends: python:requests (>= 2.5)
# config: -
# pylint: disable=invalid-name, missing-function-docstring, missing-module-docstring
# Wraps requests or fakes a http.get() implementation.

__all__ = ["http", "urllib", "urlencode", "quote", "quote_plus", "update_headers"]

# http preparations
import logging as log
import urllib
from json import loads, dumps
    from urllib.parse import urlencode, quote, quote_plus
    from urllib.request import urlopen, Request
except ImportError:
    from urllib import urlencode, quote, quote_plus
    from urllib2 import urlopen, Request
#    from six.moves.urllib import urlencode, quote, quote_plus
#    from six.moves.urllib.request import urlopen, Request

class FauxResponse:
    """ shallow copy """

    def __init__(self, response):
        self.content =
        self.status_code = response.code
        #print(response,, dir(response))

    def json(self):
        """ decode """
        return loads(self.content)

    def raise_for_status(self):
        """ exc """
        if self.status_code >= 400:
            raise Exception("HTTP failure code", self.status_code)

    def text(self):
        return self.content.decode("utf-8")

class FauxRequests:
    """ rudimentary shim """

    ssl_args = {}
    headers = {}

    def __init__(self):
        """ optional ssl """"using FauxRequests() for now")
        import ssl
        if not hasattr(ssl, "create_default_context"):
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        self.ssl_args["context"] = context

    def get(self, url, params={}, **kwargs):
        """ urlopen """
        kwargs["headers"] = dict(
            list(self.headers.items()) + list(kwargs.get("headers", {}).items())
        if params:
            url = url + "?&"[url.find("?")>0] + urlencode(params)
        return FauxResponse(
                Request(url, **kwargs), **self.ssl_args

    def post(self, url, data=None, json=None, **kwargs):
        """ POST just adds data= """
        if json:
            data = dumps(json).encode("utf-8")
        elif data and isinstance(data, dict):
            data = urlencode(data).encode("utf-8")
        return self.get(url, data=data, **kwargs)

    import requests
    http = requests.Session()
except ImportError:
        "Missing library: `pip install requests` (system-wide, or into libreoffice program/ dir)"
    http = FauxRequests()

# headers
def update_headers(office_version="LibreOffice/7.x", pt_version="2.0"):
        "User-Agent": "Mozilla/5.0 (X11; Linux; "+office_version+"), PageTranslate/"+pt_version,
        "Accept-Language": "*; q=1.0",
        "Accept-Encoding": "utf-8"