'Download a file from Pootle
@ToDo: Allow selection between different variants of language files'

def download(self, lang_code):

    import requests
    import zipfile
    try:
        from cStringIO import StringIO
    except:
        from StringIO import StringIO
    from subprocess import call
    from tempfile import NamedTemporaryFile
    code = lang_code
    if (len(lang_code) > 2):
        code = ('%s_%s' % (lang_code[:2], lang_code[(-2):].upper()))
    pootle_url = current.deployment_settings.get_L10n_pootle_url()
    link = ('%s%s/eden/export/zip' % (pootle_url, code))
    try:
        r = requests.get(link)
    except:
        current.log.error('Connection    Error')
        return False
    zipf = zipfile.ZipFile(StringIO.StringIO(r.content))
    zipf.extractall()
    file_name_po = ('%s.po' % lang_code)
    file_name_py = ('%s.py' % lang_code)
    f = NamedTemporaryFile(delete=False)
    w2pfilename = ('%s.py' % f.name)
    call(['po2web2py', '-i', file_name_po, '-o', w2pfilename])
    S = Strings()
    path = os.path.join(current.request.folder, 'languages', file_name_py)
    pystrings = S.read_w2p(path)
    pystrings.sort(key=(lambda tup: tup[0]))
    postrings = S.read_w2p(w2pfilename)
    postrings = [tup for tup in postrings if (tup[0] != tup[1])]
    postrings.sort(key=(lambda tup: tup[0]))
    os.unlink(file_name_po)
    os.unlink(w2pfilename)
    return (postrings, pystrings)
