"""
Convert languages and language codes into ISO names

Copyright (C) 2014 Mikael Karlsson.
Licensed under AGPLv3.
"""


class iso_639_3(object):
    """
    This class is a close to drop-in replacement for pycountry.languages.
    But unlike pycountry.languages it also supports ISO 639-3.
    """
    def __init__(self):
        l, i = self._fabtabular()
        self.languages = [self.language(a, b, c, d, e, [x[2] for x in i if x[0] == a][0])
                          for a, b, c, d, _, _, e, _ in l]
        self.alpha3 = {x.alpha3: x for x in self.languages if x.alpha3}
        self.bibliographic = {x.bibliographic: x for x in self.languages if x.bibliographic}
        self.terminology = {x.terminology: x for x in self.languages if x.terminology}
        self.alpha2 = {x.alpha2: x for x in self.languages if x.alpha2}
        self.name = {x.name: x for x in self.languages if x.name}
        self.inverted = {x.inverted: x for x in self.languages if x.inverted}

    class language(object):
        def __init__(self, dash3, dash2b, dash2t, dash1, name, inverted):
            self.alpha3 = dash3
            self.bibliographic = dash2b
            self.terminology = dash2t
            self.alpha2 = dash1
            self.name = name
            self.inverted = inverted

    def get(self, **kwargs):
        if not len(kwargs) == 1:
            raise AttributeError("Only one keyword expected")
        key, value = kwargs.popitem()
        return getattr(self, key)[value]

    @staticmethod
    def _fabtabular():
        import csv
        import sys

        if sys.version_info[0] == 2:
            # from urllib2 import urlopen
            # u = urlopen('http://www-01.sil.org/iso639%2D3/iso-639-3.tab')
            u = open('iso-639-3.tab')
            # i = urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab')
            i = open('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab')
            return list(csv.reader(u, delimiter='\t'))[1:], list(csv.reader(i, delimiter='\t'))[1:]
        else:
            # from urllib.request import urlopen
            # import io
            # with io.StringIO(urlopen('http://www-01.sil.org/iso639%2D3/iso-639-3.tab').read().decode()) as u, \
            #         io.StringIO(urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab').read().decode()) as i:
            with open('iso-639-3.tab') as u, \
                    open('iso-639-3_Name_Index.tab') as i:
                return list(csv.reader(u, delimiter='\t'))[1:], list(csv.reader(i, delimiter='\t'))[1:]

@@ -115,16 +115,1224 @@
 .%0A%22%22%22%0A%0A%0A
+def _fabtabular():%0A    %22%22%22%0A    This function retrieves the ISO 639-3 and inverted names datasets as tsv files and returns them as two lists.%0A    %22%22%22%0A    import csv%0A    import sys%0A    from pkg_resources import resource_filename%0A%0A    data = resource_filename(__package__, 'iso-639-3.tab')%0A    inverted = resource_filename(__package__, 'iso-639-3_Name_Index.tab')%0A%0A    if sys.version_info%5B0%5D == 2:%0A        # from urllib2 import urlopen%0A        # u = urlopen('http://www-01.sil.org/iso639%252D3/iso-639-3.tab')%0A        u = open(data)%0A        # i = urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab')%0A        i = open(inverted)%0A        return list(csv.reader(u, delimiter='%5Ct'))%5B1:%5D, list(csv.reader(i, delimiter='%5Ct'))%5B1:%5D%0A    else:%0A        # from urllib.request import urlopen%0A        # import io%0A        # with io.StringIO(urlopen('http://www-01.sil.org/iso639%252D3/iso-639-3.tab').read().decode()) as u, %5C%0A        #         io.StringIO(urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab').read().decode()) as i:%0A        with open(data) as u, %5C%0A                open(inverted) as i:%0A            return list(csv.reader(u, delimiter='%5Ct'))%5B1:%5D, list(csv.reader(i, delimiter='%5Ct'))%5B1:%5D%0A%0A%0A
 class is
@@ -1529,42 +1529,8 @@
 f):%0A
-        l, i = self._fabtabular()%0A
     
@@ -1615,16 +1615,45 @@
  a%5D%5B0%5D)%0A
+        l, i = _fabtabular()%0A
         
@@ -2645,16 +2645,16 @@
 pitem()%0A
+
         
@@ -2690,1078 +2690,4 @@
 ue%5D%0A
-%0A    @staticmethod%0A    def _fabtabular():%0A        import csv%0A        import sys%0A%0A        if sys.version_info%5B0%5D == 2:%0A            # from urllib2 import urlopen%0A            # u = urlopen('http://www-01.sil.org/iso639%252D3/iso-639-3.tab')%0A            u = open('iso-639-3.tab')%0A            # i = urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab')%0A            i = open('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab')%0A            return list(csv.reader(u, delimiter='%5Ct'))%5B1:%5D, list(csv.reader(i, delimiter='%5Ct'))%5B1:%5D%0A        else:%0A            # from urllib.request import urlopen%0A            # import io%0A            # with io.StringIO(urlopen('http://www-01.sil.org/iso639%252D3/iso-639-3.tab').read().decode()) as u, %5C%0A            #         io.StringIO(urlopen('http://www-01.sil.org/iso639-3/iso-639-3_Name_Index.tab').read().decode()) as i:%0A            with open('iso-639-3.tab') as u, %5C%0A                    open('iso-639-3_Name_Index.tab') as i:%0A                return list(csv.reader(u, delimiter='%5Ct'))%5B1:%5D, list(csv.reader(i, delimiter='%5Ct'))%5B1:%5D%0A

Extract _fabtabular() out from iso_639_3()