py33 / locale / pavement.py

from __future__ import print_function

from paver.easy import *
from txclib.project import Project
import requests

@task
def update_pot():
    proj = Project()
    conf = proj.config

    base = path('pot')
    for pot in base.walkfiles('*.pot'):
        # 'pot/whatsnew/3.3.pot' => 'whatsnew-3_3'
        relpath = base.relpathto(pot)
        name = relpath.stripext().replace('/', '-').replace('.', '_')

        resource = "python-doc-ja.33-" + name
        print("Updating ", name)
        if not conf.has_section(resource):
            conf.add_section(resource)
        conf.set(resource, 'source_lang', 'en')
        conf.set(resource, 'source_file', pot)
        conf.set(resource, 'file_filter', '<lang>/LC_MESSAGES/' + relpath.stripext() + '.po')
        conf.set(resource, 'type', 'PO')

    proj.save()
    sh("tx push -s -len")


@task
@consume_args
def download(args):
    proj = Project()
    host = proj.config.get('main', 'host')
    username = proj.txrc.get(host, 'username')
    password = proj.txrc.get(host, 'password')
    auth = requests.auth.HTTPBasicAuth(username, password)
    url_ = 'https://www.transifex.com/api/2/project/python-doc-ja/resource/%s/translation/ja/?file'

    for section in proj.config.sections():
        if section == 'main':
            continue
        filename = proj.config.get(section, 'file_filter')
        filename = filename.replace('<lang>', 'ja')
        resource = section.split('.', 1)[1]
        url = url_ % resource
        response = requests.get(url, auth=auth)
        response.raise_for_status()
        print(filename, url)
        with open(filename, 'wb') as f:
            f.write(response.content)

@task
def compile():
    import polib
    base = path('ja')
    for po in base.walkfiles('*.po'):
        print("Compiling ", po)
        pofile = polib.pofile(po, encoding='utf-8')
        pofile.save_as_mofile(po.stripext() + '.mo')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.