GithubHelp home page GithubHelp logo

isabella232 / transifex-python-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from transifex/transifex-python-library

0.0 0.0 0.0 145 KB

Python wrapper for the Transifex API.

License: GNU Lesser General Public License v3.0

Python 99.00% Makefile 0.36% Dockerfile 0.64%

transifex-python-library's Introduction

Transifex Python Library

This library is a wrapper around the Transifex API, built to provide a simpler interface to users of the API.

At this moment, it supports only a subset of the endpoints of the API. In particular, it allows certain operations on Projects, Resources and Translations.

Usage

Initialization

Before making any requests, you need to setup the HTTP handler, which will be used for all subsequent requests.

Authentication works both with username/password or with an API token.

from txlib.http.auth import BasicAuth
from txlib.http.http_requests import HttpRequest
from txlib.registry import registry

credentials = BasicAuth(username='api', password='')

host = 'https:/www.transifex.com/'
conn = HttpRequest(host, auth=credentials)
registry.setup({'http_handler': conn})

Projects

TODO

Resources

Get resource

from txlib.api.resources import Resource
from txlib.http.exceptions import NotFoundError, ServerError

try:
    r = Resource.get(project_slug='project_slug', slug='resource_slug')
    print(r.slug) # 'resource_slug'
except NotFoundError:
    print('Resource not found')
except ServerError as e:
    print('Exception while retrieving resource: {}'.format(e))

Create/update resource

from txlib.api.resources import Resource
from txlib.http.exceptions import NotFoundError, ServerError

try:
    r = Resource.get(project_slug='project_slug', slug='resource_slug')
    # Resource exists, update it
    # Not all fields are allowed here. For example, if i18n_type
    # is provided, the request will fail with a 400 error
    try:
        r.save(content='{"key1": "text1"}')
    except ServerError as e:
        print('Exception while updating resource: {}'.format(e))

except NotFoundError:
    # Resource does not exist, create one now
    try:
        r = Resource(project_slug='project_slug', slug='resource_slug')
        r.save(name='R1', i18n_type='KEYVALUEJSON', content='{"key1": "text1"}')
    except ServerError as e:
        print('Exception while creating resource: {}'.format(e))

except ServerError as e:
    print('Exception while retrieving resource: {}'.format(e))

Alternatively, instead of passing all parameters to save(), you can do the following:

r = Resource.get(...)
r.name = '...'
r.i18n_type = '...'
r.content = '...'
r.save()

Translations

Get translation

from txlib.api.translations import Translation
from txlib.http.exceptions import NotFoundError, ServerError

try:
    t = Translation.get(project_slug='project_slug', slug='resource_slug', lang='translation_language')
    print(t.lang) # 'translation_language'
except NotFoundError:
    print('Translation not found')
except ServerError as e:
    print('Exception while retrieving translation: {}'.format(e))

Create/update translation

from txlib.api.translations import Translation
from txlib.http.exceptions import NotFoundError, ServerError

try:
    t = Translation(
        project_slug=project_slug, slug=resource_slug, lang=language_code
    )
    t.save(content=content)
except ServerError as e:
    print('Exception while retrieving translation: {}'.format(e))

transifex-python-library's People

Contributors

dbendilas avatar deathbird avatar diontsoumas avatar kouk avatar mpessas avatar nyergler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.