'Subcommand for compiling a message catalog to a MO file.
:param argv: the command arguments
:since: version 0.9'

def compile(self, argv):

    parser = OptionParser(usage=(self.usage % ('compile', '')), description=self.commands['compile'])
    parser.add_option('--domain', '-D', dest='domain', help="domain    of    MO    and    PO    files    (default    '%default')")
    parser.add_option('--directory', '-d', dest='directory', metavar='DIR', help='base    directory    of    catalog    files')
    parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', help='locale    of    the    catalog')
    parser.add_option('--input-file', '-i', dest='input_file', metavar='FILE', help='name    of    the    input    file')
    parser.add_option('--output-file', '-o', dest='output_file', metavar='FILE', help="name    of    the    output    file    (default    '<output_dir>/<locale>/LC_MESSAGES/<domain>.mo')")
    parser.add_option('--use-fuzzy', '-f', dest='use_fuzzy', action='store_true', help='also    include    fuzzy    translations    (default    %default)')
    parser.add_option('--statistics', dest='statistics', action='store_true', help='print    statistics    about    translations')
    parser.set_defaults(domain='messages', use_fuzzy=False, compile_all=False, statistics=False)
    (options, args) = parser.parse_args(argv)
    po_files = []
    mo_files = []
    if (not options.input_file):
        if (not options.directory):
            parser.error('you    must    specify    either    the    input    file    or    the    base    directory')
        if options.locale:
            po_files.append((options.locale, os.path.join(options.directory, options.locale, 'LC_MESSAGES', (options.domain + '.po'))))
            mo_files.append(os.path.join(options.directory, options.locale, 'LC_MESSAGES', (options.domain + '.mo')))
        else:
            for locale in os.listdir(options.directory):
                po_file = os.path.join(options.directory, locale, 'LC_MESSAGES', (options.domain + '.po'))
                if os.path.exists(po_file):
                    po_files.append((locale, po_file))
                    mo_files.append(os.path.join(options.directory, locale, 'LC_MESSAGES', (options.domain + '.mo')))
    else:
        po_files.append((options.locale, options.input_file))
        if options.output_file:
            mo_files.append(options.output_file)
        else:
            if (not options.directory):
                parser.error('you    must    specify    either    the    input    file    or    the    base    directory')
            mo_files.append(os.path.join(options.directory, options.locale, 'LC_MESSAGES', (options.domain + '.mo')))
    if (not po_files):
        parser.error('no    message    catalogs    found')
    for (idx, (locale, po_file)) in enumerate(po_files):
        mo_file = mo_files[idx]
        infile = open(po_file, 'r')
        try:
            catalog = read_po(infile, locale)
        finally:
            infile.close()
        if options.statistics:
            translated = 0
            for message in list(catalog)[1:]:
                if message.string:
                    translated += 1
            percentage = 0
            if len(catalog):
                percentage = ((translated * 100) // len(catalog))
            self.log.info('%d    of    %d    messages    (%d%%)    translated    in    %r', translated, len(catalog), percentage, po_file)
        if (catalog.fuzzy and (not options.use_fuzzy)):
            self.log.warn('catalog    %r    is    marked    as    fuzzy,    skipping', po_file)
            continue
        for (message, errors) in catalog.check():
            for error in errors:
                self.log.error('error:    %s:%d:    %s', po_file, message.lineno, error)
        self.log.info('compiling    catalog    %r    to    %r', po_file, mo_file)
        outfile = open(mo_file, 'wb')
        try:
            write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy)
        finally:
            outfile.close()
