def do_update_translations(target=("t", ""), lang=("l", ""),
                           statistics=("s", False), i18n_dir=("i", ""),
                           all=("a", False)):
  """
  Update existing translations with updated pot files.
  """
  if not target and not all:
    print_status('Please specify target.')
    sys.exit(1)
  elif target == 'kay':
    print_status('Updating core strings')
    root = path.join(kay.KAY_DIR, 'i18n')
  elif all:
    targets = get_user_apps()
    for target in targets:
      do_update_translations(target=target, lang=lang, statistics=statistics,
                             i18n_dir=None, all=False)
    do_update_translations(target=kay.PROJECT_DIR, lang=lang,
                           statistics=statistics, i18n_dir=None, all=False)
    sys.exit(0)
  else:
    if i18n_dir:
      root = i18n_dir
    else:
      root = path.join(target, 'i18n')
    if not path.isdir(root):
      print_status('source folder missing')
      sys.exit(1)
    print_status('Updating %s' % root)

  for domain in domains:
    if lang:
      filepath = path.join(root, lang, 'LC_MESSAGES', domain+'.po')
      if not path.exists(filepath):
        print_status("%s not found, skipped." % filepath)
        continue
    try:
      f = file(path.join(root, domain+'.pot'))
    except __HOLE__:
      print_status('Can not open file: %s, skipped.' %
                   path.join(root, domain+'.pot'))
      continue
    try:
      template = read_po(f)
    finally:
      f.close()

    po_files = []
    for lang_dir in listdir(root):
      filename = path.join(root, lang_dir, 'LC_MESSAGES', domain+'.po')
      if lang and filename != \
                         path.join(root, lang, 'LC_MESSAGES', domain+'.po'):
        continue
      if path.exists(filename):
        print_status('Updating %s' % filename, nl=False)
        locale = Locale.parse(lang_dir)
        f = file(filename)
        try:
          catalog = read_po(f, locale=locale, domain=domain)
        finally:
          f.close()
        catalog.update(template)

        # XXX: this is kinda dangerous, but as we are using a
        # revision control system anyways that shouldn't make
        # too many problems
        f = file(filename, 'w')
        try:
          write_po(f, catalog, ignore_obsolete=True,
                   include_previous=False, width=79)
        finally:
          if statistics:
            translated = fuzzy = percentage = 0
            for message in list(catalog)[1:]:
              if message.string:
                translated +=1
              if 'fuzzy' in message.flags:
                fuzzy += 1
            if len(catalog):
              percentage = translated * 100 // len(catalog)
              print_status("-> %d of %d messages (%d%%) translated" % (
                translated, len(catalog), percentage), nl=False)
              if fuzzy:
                if fuzzy == 1:
                  print_status("%d of which is fuzzy" % fuzzy, nl=False)
                else:
                  print_status("%d of which are fuzzy" % fuzzy, nl=False)
              print_status()
          else:
            print_status()
          f.close()

  print_status('All done.')
dataset/ETHPy150Open IanLewis/kay/kay/management/update_translations.py/do_update_translations