__author__ = 'vg'
import io
import dircache
import sys, os


#default path
base_dir = os.path.dirname('C:\\c-list\\country\\icu\\')
s = dircache.listdir(base_dir)



#creating country code list
our_codes = io.open('our_codes.txt', mode='r',encoding='utf8').readlines()
code_list = []
for k in our_codes:
    code_list.append(k.rstrip('\n'))



#iso links
lang_codes = io.open('lang_code.txt', mode='r', encoding='utf8').readlines()
lang = {}
for l in lang_codes:
    lang[l[:2]] = l[3:5]
    #lang[l[3:5]] = l[:2]
print lang
print len(lang)



#lang_to_lang.txt
lang_to_lang = io.open('lang_to_lang.txt', mode='r', encoding='utf8').readlines()
lang_to = {}
lang_to1 = {}
for l in lang_to_lang:
    lang_to1[l[:2]] = l[3:5]
    lang_to[l[3:5]] = l[:2]
print lang_to
print lang_to1
print len(lang_to)
print len(lang_to1)




#parsing files
dir_folder = io.open('all_our_countries.txt', mode='w', encoding='utf8')
full_codes = io.open('full_list_lang_to_lang.txt', mode='w', encoding='utf8')
for i in s:
    folder = io.open(base_dir+"\\"+i+'\country.txt', mode='r', encoding='utf8')
    for f in folder:
        f1 = f.index('(')  #start index in line
        f2 = f.index(')')   #last index in line
        f3 = f[f1+1:f2]   # Code of county
        if len(i) == 2 and len(f3) == 2 and f3.lower() in code_list:
            f4 = f.replace(f[f1-1:f2+1], '')
            dir_folder.writelines(f3+'\t'+i+'\t'+f4)
            print f3
            if f3.lower() in lang.keys() and i in lang_to.values():
                full_codes.writelines(f3+'\t'+lang_to1[i]+'\t'+f4)
    folder.close()
dir_folder.close()