Django

Code

Changeset 2056

Show
Ignore:
Timestamp:
01/18/06 18:54:15 (3 years ago)
Author:
adrian
Message:

Changed unique-messages.py, compile-messages.py and make-messages.py to use 'if name == main' so they can be imported and won't mess up utilities such as pychecker

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/bin/compile-messages.py

    r1814 r2056  
    55import getopt 
    66 
    7 basedir = None 
     7def compile_messages(): 
     8    basedir = None 
    89 
    9 if os.path.isdir(os.path.join('conf', 'locale')): 
    10     basedir = os.path.abspath(os.path.join('conf', 'locale')) 
    11 elif os.path.isdir('locale'): 
    12     basedir = os.path.abspath('locale') 
    13 else: 
    14     print "this script should be run from the django svn tree or your project or app tree" 
    15     sys.exit(1) 
     10    if os.path.isdir(os.path.join('conf', 'locale')): 
     11        basedir = os.path.abspath(os.path.join('conf', 'locale')) 
     12    elif os.path.isdir('locale'): 
     13        basedir = os.path.abspath('locale') 
     14    else: 
     15        print "this script should be run from the django svn tree or your project or app tree" 
     16        sys.exit(1) 
    1617 
    17 for (dirpath, dirnames, filenames) in os.walk(basedir): 
    18     for file in filenames: 
    19         if file.endswith('.po'): 
    20             sys.stderr.write('processing file %s in %s\n' % (file, dirpath)) 
    21             pf = os.path.splitext(os.path.join(dirpath, file))[0] 
    22             cmd = 'msgfmt -o "%s.mo" "%s.po"' % (pf, pf) 
    23             os.system(cmd) 
     18    for (dirpath, dirnames, filenames) in os.walk(basedir): 
     19        for file in filenames: 
     20            if file.endswith('.po'): 
     21                sys.stderr.write('processing file %s in %s\n' % (file, dirpath)) 
     22                pf = os.path.splitext(os.path.join(dirpath, file))[0] 
     23                cmd = 'msgfmt -o "%s.mo" "%s.po"' % (pf, pf) 
     24                os.system(cmd) 
    2425 
     26if __name__ == "__main__": 
     27    compile_messages() 
  • django/trunk/django/bin/make-messages.py

    r1814 r2056  
    11#!/usr/bin/python 
    22 
     3from django.utils.translation import templateize 
    34import re 
    45import os 
     
    67import getopt 
    78 
    8 from django.utils.translation import templateize 
    9  
    109pythonize_re = re.compile(r'\n\s*//') 
    1110 
    12 localedir = None 
     11def make_messages(): 
     12    localedir = None 
    1313 
    14 if os.path.isdir(os.path.join('conf', 'locale')): 
    15     localedir = os.path.abspath(os.path.join('conf', 'locale')) 
    16 elif os.path.isdir('locale'): 
    17     localedir = os.path.abspath('locale') 
    18 else: 
    19     print "This script should be run from the django svn tree or your project or app tree." 
    20     print "If you did indeed run it from the svn checkout or your project or application," 
    21     print "maybe you are just missing the conf/locale (in the django tree) or locale (for project" 
    22     print "and application) directory?" 
    23     print "make-messages.py doesn't create it automatically, you have to create it by hand if" 
    24     print "you want to enable i18n for your project or application." 
    25     sys.exit(1) 
     14    if os.path.isdir(os.path.join('conf', 'locale')): 
     15        localedir = os.path.abspath(os.path.join('conf', 'locale')) 
     16    elif os.path.isdir('locale'): 
     17        localedir = os.path.abspath('locale') 
     18    else: 
     19        print "This script should be run from the django svn tree or your project or app tree." 
     20        print "If you did indeed run it from the svn checkout or your project or application," 
     21        print "maybe you are just missing the conf/locale (in the django tree) or locale (for project" 
     22        print "and application) directory?" 
     23        print "make-messages.py doesn't create it automatically, you have to create it by hand if" 
     24        print "you want to enable i18n for your project or application." 
     25        sys.exit(1) 
    2626 
    27 (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va') 
     27    (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va') 
    2828 
    29 lang = None 
    30 domain = 'django' 
    31 verbose = False 
    32 all = False 
     29    lang = None 
     30    domain = 'django' 
     31    verbose = False 
     32    all = False 
    3333 
    34 for o, v in opts: 
    35     if o == '-l': 
    36         lang = v 
    37     elif o == '-d': 
    38         domain = v 
    39     elif o == '-v': 
    40         verbose = True 
    41     elif o == '-a': 
    42         all = True 
     34    for o, v in opts: 
     35        if o == '-l': 
     36            lang = v 
     37        elif o == '-d': 
     38            domain = v 
     39        elif o == '-v': 
     40            verbose = True 
     41        elif o == '-a': 
     42            all = True 
    4343 
    44 if domain not in ('django', 'djangojs'): 
    45     print "currently make-messages.py only supports domains 'django' and 'djangojs'" 
    46     sys.exit(1) 
    47 if (lang is None and not all) or domain is None: 
    48     print "usage: make-messages.py -l <language>" 
    49     print "   or: make-messages.py -a" 
    50     sys.exit(1) 
     44    if domain not in ('django', 'djangojs'): 
     45        print "currently make-messages.py only supports domains 'django' and 'djangojs'" 
     46        sys.exit(1) 
     47    if (lang is None and not all) or domain is None: 
     48        print "usage: make-messages.py -l <language>" 
     49        print "   or: make-messages.py -a" 
     50        sys.exit(1) 
    5151 
    52 languages = [] 
     52    languages = [] 
    5353 
    54 if lang is not None: 
    55     languages.append(lang) 
    56 elif all: 
    57     languages = [el for el in os.listdir(localedir) if not el.startswith('.')] 
     54    if lang is not None: 
     55        languages.append(lang) 
     56    elif all: 
     57        languages = [el for el in os.listdir(localedir) if not el.startswith('.')] 
    5858 
    59 for lang in languages: 
     59    for lang in languages: 
    6060 
    61     print "processing language", lang 
    62     basedir = os.path.join(localedir, lang, 'LC_MESSAGES') 
    63     if not os.path.isdir(basedir): 
    64         os.makedirs(basedir) 
     61        print "processing language", lang 
     62        basedir = os.path.join(localedir, lang, 'LC_MESSAGES') 
     63        if not os.path.isdir(basedir): 
     64            os.makedirs(basedir) 
    6565 
    66     pofile = os.path.join(basedir, '%s.po' % domain) 
    67     potfile = os.path.join(basedir, '%s.pot' % domain) 
     66        pofile = os.path.join(basedir, '%s.po' % domain) 
     67        potfile = os.path.join(basedir, '%s.pot' % domain) 
    6868 
    69     if os.path.exists(potfile): 
    70         os.unlink(potfile) 
     69        if os.path.exists(potfile): 
     70            os.unlink(potfile) 
    7171 
    72     for (dirpath, dirnames, filenames) in os.walk("."): 
    73         for file in filenames: 
    74             if domain == 'djangojs' and file.endswith('.js'): 
    75                 if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
    76                 src = open(os.path.join(dirpath, file), "rb").read() 
    77                 src = pythonize_re.sub('\n#', src) 
    78                 open(os.path.join(dirpath, '%s.py' % file), "wb").write(src) 
    79                 thefile = '%s.py' % file 
    80                 cmd = 'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
    81                     os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
    82                 (stdin, stdout, stderr) = os.popen3(cmd, 'b') 
    83                 msgs = stdout.read() 
    84                 errors = stderr.read() 
    85                 if errors:  
    86                     print "errors happened while running xgettext on %s" % file 
    87                     print errors 
    88                     sys.exit(8) 
    89                 old = '#: '+os.path.join(dirpath, thefile)[2:] 
    90                 new = '#: '+os.path.join(dirpath, file)[2:] 
    91                 msgs = msgs.replace(old, new) 
    92                 if msgs: 
    93                     open(potfile, 'ab').write(msgs) 
    94                 os.unlink(os.path.join(dirpath, thefile)) 
    95             elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')): 
    96                 thefile = file 
    97                 if file.endswith('.html'): 
     72        for (dirpath, dirnames, filenames) in os.walk("."): 
     73            for file in filenames: 
     74                if domain == 'djangojs' and file.endswith('.js'): 
     75                    if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
    9876                    src = open(os.path.join(dirpath, file), "rb").read() 
    99                     open(os.path.join(dirpath, '%s.py' % file), "wb").write(templateize(src)) 
     77                    src = pythonize_re.sub('\n#', src) 
     78                    open(os.path.join(dirpath, '%s.py' % file), "wb").write(src) 
    10079                    thefile = '%s.py' % file 
    101                 if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
    102                 cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
    103                     os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
    104                 (stdin, stdout, stderr) = os.popen3(cmd, 'b') 
    105                 msgs = stdout.read() 
    106                 errors = stderr.read() 
    107                 if errors:  
    108                     print "errors happened while running xgettext on %s" % file 
    109                     print errors 
    110                     sys.exit(8) 
    111                 if thefile != file: 
     80                    cmd = 'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
     81                        os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
     82                    (stdin, stdout, stderr) = os.popen3(cmd, 'b') 
     83                    msgs = stdout.read() 
     84                    errors = stderr.read() 
     85                    if errors: 
     86                        print "errors happened while running xgettext on %s" % file 
     87                        print errors 
     88                        sys.exit(8) 
    11289                    old = '#: '+os.path.join(dirpath, thefile)[2:] 
    11390                    new = '#: '+os.path.join(dirpath, file)[2:] 
    11491                    msgs = msgs.replace(old, new) 
    115                 if msgs: 
    116                     open(potfile, 'ab').write(msgs) 
    117                 if thefile != file: 
     92                    if msgs: 
     93                        open(potfile, 'ab').write(msgs) 
    11894                    os.unlink(os.path.join(dirpath, thefile)) 
     95                elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')): 
     96                    thefile = file 
     97                    if file.endswith('.html'): 
     98                        src = open(os.path.join(dirpath, file), "rb").read() 
     99                        open(os.path.join(dirpath, '%s.py' % file), "wb").write(templateize(src)) 
     100                        thefile = '%s.py' % file 
     101                    if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) 
     102                    cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy -o - "%s"' % ( 
     103                        os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile)) 
     104                    (stdin, stdout, stderr) = os.popen3(cmd, 'b') 
     105                    msgs = stdout.read() 
     106                    errors = stderr.read() 
     107                    if errors: 
     108                        print "errors happened while running xgettext on %s" % file 
     109                        print errors 
     110                        sys.exit(8) 
     111                    if thefile != file: 
     112                        old = '#: '+os.path.join(dirpath, thefile)[2:] 
     113                        new = '#: '+os.path.join(dirpath, file)[2:] 
     114                        msgs = msgs.replace(old, new) 
     115                    if msgs: 
     116                        open(potfile, 'ab').write(msgs) 
     117                    if thefile != file: 
     118                        os.unlink(os.path.join(dirpath, thefile)) 
    119119 
    120     if os.path.exists(potfile): 
    121         (stdin, stdout, stderr) = os.popen3('msguniq "%s"' % potfile, 'b') 
    122         msgs = stdout.read() 
    123         errors = stderr.read() 
    124         if errors: 
    125             print "errors happened while running msguniq" 
    126             print errors 
    127             sys.exit(8) 
    128         open(potfile, 'w').write(msgs) 
    129         if os.path.exists(pofile): 
    130             (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b') 
     120        if os.path.exists(potfile): 
     121            (stdin, stdout, stderr) = os.popen3('msguniq "%s"' % potfile, 'b') 
    131122            msgs = stdout.read() 
    132123            errors = stderr.read() 
    133124            if errors: 
    134                 print "errors happened while running msgmerge
     125                print "errors happened while running msguniq
    135126                print errors 
    136127                sys.exit(8) 
    137         open(pofile, 'wb').write(msgs) 
    138         os.unlink(potfile) 
     128            open(potfile, 'w').write(msgs) 
     129            if os.path.exists(pofile): 
     130                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b') 
     131                msgs = stdout.read() 
     132                errors = stderr.read() 
     133                if errors: 
     134                    print "errors happened while running msgmerge" 
     135                    print errors 
     136                    sys.exit(8) 
     137            open(pofile, 'wb').write(msgs) 
     138            os.unlink(potfile) 
    139139 
     140if __name__ == "__main__": 
     141    make_messages() 
  • django/trunk/django/bin/unique-messages.py

    r1821 r2056  
    55import getopt 
    66 
    7 basedir = None 
     7def unique_messages(): 
     8    basedir = None 
    89 
    9 if os.path.isdir(os.path.join('conf', 'locale')): 
    10     basedir = os.path.abspath(os.path.join('conf', 'locale')) 
    11 elif os.path.isdir('locale'): 
    12     basedir = os.path.abspath('locale') 
    13 else: 
    14     print "this script should be run from the django svn tree or your project or app tree" 
    15     sys.exit(1) 
     10    if os.path.isdir(os.path.join('conf', 'locale')): 
     11        basedir = os.path.abspath(os.path.join('conf', 'locale')) 
     12    elif os.path.isdir('locale'): 
     13        basedir = os.path.abspath('locale') 
     14    else: 
     15        print "this script should be run from the django svn tree or your project or app tree" 
     16        sys.exit(1) 
    1617 
    17 for (dirpath, dirnames, filenames) in os.walk(basedir): 
    18     for file in filenames: 
    19         if file.endswith('.po'): 
    20             sys.stderr.write('processing file %s in %s\n' % (file, dirpath)) 
    21             pf = os.path.splitext(os.path.join(dirpath, file))[0] 
    22             cmd = 'msguniq "%s.po"' % pf 
    23             stdout = os.popen(cmd) 
    24             msg = stdout.read() 
    25             open('%s.po' % pf, 'w').write(msg) 
     18    for (dirpath, dirnames, filenames) in os.walk(basedir): 
     19        for file in filenames: 
     20            if file.endswith('.po'): 
     21                sys.stderr.write('processing file %s in %s\n' % (file, dirpath)) 
     22                pf = os.path.splitext(os.path.join(dirpath, file))[0] 
     23                cmd = 'msguniq "%s.po"' % pf 
     24                stdout = os.popen(cmd) 
     25                msg = stdout.read() 
     26                open('%s.po' % pf, 'w').write(msg) 
    2627 
     28if __name__ == "__main__": 
     29    unique_messages()