transifex / transifex-client

The Transifex command-line tool.
https://www.transifex.com/
GNU General Public License v2.0
135 stars 75 forks source link

Python API instead of CLI #267

Closed FranklinYu closed 4 years ago

FranklinYu commented 4 years ago

Currently we seems to only provide the CLI tool tx. Would be useful if Python API is provided. Example:

import transifex

transifex.push(source=True, translations=True, language='en-US')
nelefth commented 4 years ago

Hi @FranklinYu ,

How about using something this:

import requests
import io
import json

AUTH = ('username', 'password')
URL = 'https://www.transifex.com/api/2/project/project_slug/resource/resource_slug/translation/lang_code/'
content = io.open('translation_filename.extension', 'r', encoding='utf-8').read()
data = {
        'content': content
    }
headers = {'content-type': 'application/json'}
response = requests.put(URL, data = json.dumps(data), headers=headers, auth=AUTH)
print response;
FranklinYu commented 4 years ago

@nelefth It’s much easier to expose Python API of existing working library than writing a new one from scratch.

diegobz commented 4 years ago

@FranklinYu There are 2 ways you can do it with Python.

1. Looking under the hood of the CLI code and leveraging its power

After installing transifex-client and being in the repo directory where you have your .tx folder with your config file, you can do:

In [1]: from txclib.project import Project

In [2]: tx = Project()

The push and pull methods will be available to you.

In [3]: tx.push?
Signature: tx.push(source=False, translations=False, force=False, resources=None, languages=None, skip=False, no_interactive=False, xliff=False, branch=None, parallel=False)
Docstring: Push all the resources
File:      ~/.virtualenvs/cli-13/lib/python3.8/site-packages/txclib/project.py
Type:      instancemethod

In [4]: tx.pull?
Signature: tx.pull(languages=None, resources=None, overwrite=True, fetchall=False, fetchsource=False, force=False, skip=False, minimum_perc=0, mode=None, pseudo=False, xliff=False, branch=None, parallel=False, no_interactive=False)
Docstring: Pull all translations file from Transifex server.
File:      ~/.virtualenvs/cli-13/lib/python3.8/site-packages/txclib/project.py
Type:      instancemethod

As it's underline code, it has the danger of breaking in the future though.

2. Using the Python library we have

This library will require more code than you described, as it doesn't work based on a config file like the CLI.

https://github.com/transifex/transifex-python-library

I hope it helps.

FranklinYu commented 4 years ago

The library is awesome! I should have found it earlier. Or, has it ever been mentioned on https://docs.transifex.com?

In addition, does this depend on the Transifex CLI in any way?

diegobz commented 4 years ago

It's totally independent of the CLI.

We don't promote it at the moment in the docs.