├── README.md ├── icon.png ├── changelog.txt ├── lib ├── AUTHORS ├── README.rst ├── babelfish │ ├── converters │ │ ├── name.py │ │ ├── alpha2.py │ │ ├── alpha3b.py │ │ ├── alpha3t.py │ │ ├── scope.py │ │ ├── type.py │ │ ├── countryname.py │ │ ├── opensubtitles.py │ │ └── __init__.py │ ├── __init__.py │ ├── data │ │ ├── get_files.py │ │ ├── iso-3166-1.txt │ │ ├── iso15924-utf8-20131012.txt │ │ └── opensubtitles_languages.txt │ ├── script.py │ ├── exceptions.py │ ├── country.py │ ├── language.py │ └── tests.py ├── LICENSE └── HISTORY.rst ├── addon.xml ├── LICENSE └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # script.module.babelfish 2 | Python babelfish library packed for Kodi. 3 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramiro/script.module.babelfish/master/icon.png -------------------------------------------------------------------------------- /changelog.txt: -------------------------------------------------------------------------------- 1 | 0.5.5 2 | - Pack babelfish 0.5.5 for Kodi 17.x Krypton (API 2.25.0) and later (Python >= 2.7) 3 | -------------------------------------------------------------------------------- /lib/AUTHORS: -------------------------------------------------------------------------------- 1 | BabelFish is written and maintained by: 2 | * Antoine Bertin 3 | * Nicolas Wack 4 | * Rémi Alvergnat 5 | 6 | Other contributors, listed alphabetically, are: 7 | 8 | * Your name here! -- what you did 9 | 10 | Many thanks for all contributions! 11 | -------------------------------------------------------------------------------- /lib/README.rst: -------------------------------------------------------------------------------- 1 | BabelFish 2 | ========= 3 | 4 | BabelFish is a Python library to work with countries and languages. 5 | 6 | .. image:: https://travis-ci.org/Diaoul/babelfish.png?branch=master 7 | :target: https://travis-ci.org/Diaoul/babelfish 8 | 9 | .. image:: https://coveralls.io/repos/Diaoul/babelfish/badge.png 10 | :target: https://coveralls.io/r/Diaoul/babelfish 11 | 12 | License 13 | ------- 14 | 15 | BabelFish is licensed under the `3-clause BSD license `_. 16 | Copyright (c) 2013, the BabelFish authors and contributors. 17 | -------------------------------------------------------------------------------- /lib/babelfish/converters/name.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageEquivalenceConverter 9 | from ..language import LANGUAGE_MATRIX 10 | 11 | 12 | class NameConverter(LanguageEquivalenceConverter): 13 | CASE_SENSITIVE = False 14 | SYMBOLS = {} 15 | for iso_language in LANGUAGE_MATRIX: 16 | if iso_language.name: 17 | SYMBOLS[iso_language.alpha3] = iso_language.name 18 | -------------------------------------------------------------------------------- /lib/babelfish/converters/alpha2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageEquivalenceConverter 9 | from ..language import LANGUAGE_MATRIX 10 | 11 | 12 | class Alpha2Converter(LanguageEquivalenceConverter): 13 | CASE_SENSITIVE = True 14 | SYMBOLS = {} 15 | for iso_language in LANGUAGE_MATRIX: 16 | if iso_language.alpha2: 17 | SYMBOLS[iso_language.alpha3] = iso_language.alpha2 18 | -------------------------------------------------------------------------------- /lib/babelfish/converters/alpha3b.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageEquivalenceConverter 9 | from ..language import LANGUAGE_MATRIX 10 | 11 | 12 | class Alpha3BConverter(LanguageEquivalenceConverter): 13 | CASE_SENSITIVE = True 14 | SYMBOLS = {} 15 | for iso_language in LANGUAGE_MATRIX: 16 | if iso_language.alpha3b: 17 | SYMBOLS[iso_language.alpha3] = iso_language.alpha3b 18 | -------------------------------------------------------------------------------- /lib/babelfish/converters/alpha3t.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageEquivalenceConverter 9 | from ..language import LANGUAGE_MATRIX 10 | 11 | 12 | class Alpha3TConverter(LanguageEquivalenceConverter): 13 | CASE_SENSITIVE = True 14 | SYMBOLS = {} 15 | for iso_language in LANGUAGE_MATRIX: 16 | if iso_language.alpha3t: 17 | SYMBOLS[iso_language.alpha3] = iso_language.alpha3t 18 | -------------------------------------------------------------------------------- /addon.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 11 | 12 | babelfish - Python library to work with countries and languages 13 | Packed for Kodi from https://github.com/Diaoul/babelfish 14 | all 15 | 16 | Revised BSD License 17 | 18 | http://babelfish.readthedocs.io/en/latest/ 19 | https://github.com/ramiro/script.module.babelfish 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /lib/babelfish/converters/scope.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageConverter 9 | from ..exceptions import LanguageConvertError 10 | from ..language import LANGUAGE_MATRIX 11 | 12 | 13 | class ScopeConverter(LanguageConverter): 14 | FULLNAME = {'I': 'individual', 'M': 'macrolanguage', 'S': 'special'} 15 | SYMBOLS = {} 16 | for iso_language in LANGUAGE_MATRIX: 17 | SYMBOLS[iso_language.alpha3] = iso_language.scope 18 | codes = set(SYMBOLS.values()) 19 | 20 | def convert(self, alpha3, country=None, script=None): 21 | if self.SYMBOLS[alpha3] in self.FULLNAME: 22 | return self.FULLNAME[self.SYMBOLS[alpha3]] 23 | raise LanguageConvertError(alpha3, country, script) 24 | -------------------------------------------------------------------------------- /lib/babelfish/converters/type.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageConverter 9 | from ..exceptions import LanguageConvertError 10 | from ..language import LANGUAGE_MATRIX 11 | 12 | 13 | class LanguageTypeConverter(LanguageConverter): 14 | FULLNAME = {'A': 'ancient', 'C': 'constructed', 'E': 'extinct', 'H': 'historical', 'L': 'living', 'S': 'special'} 15 | SYMBOLS = {} 16 | for iso_language in LANGUAGE_MATRIX: 17 | SYMBOLS[iso_language.alpha3] = iso_language.type 18 | codes = set(SYMBOLS.values()) 19 | 20 | def convert(self, alpha3, country=None, script=None): 21 | if self.SYMBOLS[alpha3] in self.FULLNAME: 22 | return self.FULLNAME[self.SYMBOLS[alpha3]] 23 | raise LanguageConvertError(alpha3, country, script) 24 | -------------------------------------------------------------------------------- /lib/babelfish/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | __title__ = 'babelfish' 8 | __version__ = '0.5.5-dev' 9 | __author__ = 'Antoine Bertin' 10 | __license__ = 'BSD' 11 | __copyright__ = 'Copyright 2015 the BabelFish authors' 12 | 13 | import sys 14 | 15 | if sys.version_info[0] >= 3: 16 | basestr = str 17 | else: 18 | basestr = basestring 19 | 20 | from .converters import (LanguageConverter, LanguageReverseConverter, LanguageEquivalenceConverter, CountryConverter, 21 | CountryReverseConverter) 22 | from .country import country_converters, COUNTRIES, COUNTRY_MATRIX, Country 23 | from .exceptions import Error, LanguageConvertError, LanguageReverseError, CountryConvertError, CountryReverseError 24 | from .language import language_converters, LANGUAGES, LANGUAGE_MATRIX, Language 25 | from .script import SCRIPTS, SCRIPT_MATRIX, Script 26 | -------------------------------------------------------------------------------- /lib/babelfish/converters/countryname.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import CountryReverseConverter, CaseInsensitiveDict 9 | from ..country import COUNTRY_MATRIX 10 | from ..exceptions import CountryConvertError, CountryReverseError 11 | 12 | 13 | class CountryNameConverter(CountryReverseConverter): 14 | def __init__(self): 15 | self.codes = set() 16 | self.to_name = {} 17 | self.from_name = CaseInsensitiveDict() 18 | for country in COUNTRY_MATRIX: 19 | self.codes.add(country.name) 20 | self.to_name[country.alpha2] = country.name 21 | self.from_name[country.name] = country.alpha2 22 | 23 | def convert(self, alpha2): 24 | if alpha2 not in self.to_name: 25 | raise CountryConvertError(alpha2) 26 | return self.to_name[alpha2] 27 | 28 | def reverse(self, name): 29 | if name not in self.from_name: 30 | raise CountryReverseError(name) 31 | return self.from_name[name] 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, by the respective authors (see lib/AUTHORS file). 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | * Neither the name of the BabelFish authors nor the names of its contributors 13 | may be used to endorse or promote products derived from this software without 14 | specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 23 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /lib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, by the respective authors (see AUTHORS file). 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | * Neither the name of the BabelFish authors nor the names of its contributors 13 | may be used to endorse or promote products derived from this software without 14 | specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 23 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | upstream/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /lib/babelfish/data/get_files.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 5 | # Use of this source code is governed by the 3-clause BSD license 6 | # that can be found in the LICENSE file. 7 | # 8 | from __future__ import unicode_literals 9 | import os.path 10 | import tempfile 11 | import zipfile 12 | import requests 13 | 14 | 15 | DATA_DIR = os.path.dirname(__file__) 16 | 17 | # iso-3166-1.txt 18 | print('Downloading ISO-3166-1 standard (ISO country codes)...') 19 | with open(os.path.join(DATA_DIR, 'iso-3166-1.txt'), 'w') as f: 20 | r = requests.get('http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt.htm') 21 | f.write(r.content.strip()) 22 | 23 | # iso-639-3.tab 24 | print('Downloading ISO-639-3 standard (ISO language codes)...') 25 | with tempfile.TemporaryFile() as f: 26 | r = requests.get('http://www-01.sil.org/iso639-3/iso-639-3_Code_Tables_20130531.zip') 27 | f.write(r.content) 28 | with zipfile.ZipFile(f) as z: 29 | z.extract('iso-639-3.tab', DATA_DIR) 30 | 31 | # iso-15924 32 | print('Downloading ISO-15924 standard (ISO script codes)...') 33 | with tempfile.TemporaryFile() as f: 34 | r = requests.get('http://www.unicode.org/iso15924/iso15924.txt.zip') 35 | f.write(r.content) 36 | with zipfile.ZipFile(f) as z: 37 | z.extract('iso15924-utf8-20131012.txt', DATA_DIR) 38 | 39 | # opensubtitles supported languages 40 | print('Downloading OpenSubtitles supported languages...') 41 | with open(os.path.join(DATA_DIR, 'opensubtitles_languages.txt'), 'w') as f: 42 | r = requests.get('http://www.opensubtitles.org/addons/export_languages.php') 43 | f.write(r.content) 44 | 45 | print('Done!') 46 | -------------------------------------------------------------------------------- /lib/babelfish/converters/opensubtitles.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from . import LanguageReverseConverter, CaseInsensitiveDict 9 | from ..exceptions import LanguageReverseError 10 | from ..language import language_converters 11 | 12 | 13 | class OpenSubtitlesConverter(LanguageReverseConverter): 14 | def __init__(self): 15 | self.alpha3b_converter = language_converters['alpha3b'] 16 | self.alpha2_converter = language_converters['alpha2'] 17 | self.to_opensubtitles = {('por', 'BR'): 'pob', ('gre', None): 'ell', ('srp', None): 'scc', ('srp', 'ME'): 'mne'} 18 | self.from_opensubtitles = CaseInsensitiveDict({'pob': ('por', 'BR'), 'pb': ('por', 'BR'), 'ell': ('ell', None), 19 | 'scc': ('srp', None), 'mne': ('srp', 'ME')}) 20 | self.codes = (self.alpha2_converter.codes | self.alpha3b_converter.codes | set(['pob', 'pb', 'scc', 'mne'])) 21 | 22 | def convert(self, alpha3, country=None, script=None): 23 | alpha3b = self.alpha3b_converter.convert(alpha3, country, script) 24 | if (alpha3b, country) in self.to_opensubtitles: 25 | return self.to_opensubtitles[(alpha3b, country)] 26 | return alpha3b 27 | 28 | def reverse(self, opensubtitles): 29 | if opensubtitles in self.from_opensubtitles: 30 | return self.from_opensubtitles[opensubtitles] 31 | for conv in [self.alpha3b_converter, self.alpha2_converter]: 32 | try: 33 | return conv.reverse(opensubtitles) 34 | except LanguageReverseError: 35 | pass 36 | raise LanguageReverseError(opensubtitles) 37 | -------------------------------------------------------------------------------- /lib/babelfish/script.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Copyright (c) 2013 the BabelFish authors. All rights reserved. 4 | # Use of this source code is governed by the 3-clause BSD license 5 | # that can be found in the LICENSE file. 6 | # 7 | from __future__ import unicode_literals 8 | from collections import namedtuple 9 | from pkg_resources import resource_stream # @UnresolvedImport 10 | from . import basestr 11 | 12 | #: Script code to script name mapping 13 | SCRIPTS = {} 14 | 15 | #: List of countries in the ISO-15924 as namedtuple of code, number, name, french_name, pva and date 16 | SCRIPT_MATRIX = [] 17 | 18 | #: The namedtuple used in the :data:`SCRIPT_MATRIX` 19 | IsoScript = namedtuple('IsoScript', ['code', 'number', 'name', 'french_name', 'pva', 'date']) 20 | 21 | f = resource_stream('babelfish', 'data/iso15924-utf8-20131012.txt') 22 | f.readline() 23 | for l in f: 24 | l = l.decode('utf-8').strip() 25 | if not l or l.startswith('#'): 26 | continue 27 | script = IsoScript._make(l.split(';')) 28 | SCRIPT_MATRIX.append(script) 29 | SCRIPTS[script.code] = script.name 30 | f.close() 31 | 32 | 33 | class Script(object): 34 | """A human writing system 35 | 36 | A script is represented by a 4-letter code from the ISO-15924 standard 37 | 38 | :param string script: 4-letter ISO-15924 script code 39 | 40 | """ 41 | def __init__(self, script): 42 | if script not in SCRIPTS: 43 | raise ValueError('%r is not a valid script' % script) 44 | 45 | #: ISO-15924 4-letter script code 46 | self.code = script 47 | 48 | @property 49 | def name(self): 50 | """English name of the script""" 51 | return SCRIPTS[self.code] 52 | 53 | def __getstate__(self): 54 | return self.code 55 | 56 | def __setstate__(self, state): 57 | self.code = state 58 | 59 | def __hash__(self): 60 | return hash(self.code) 61 | 62 | def __eq__(self, other): 63 | if isinstance(other, basestr): 64 | return self.code == other 65 | if not isinstance(other, Script): 66 | return False 67 | return self.code == other.code 68 | 69 | def __ne__(self, other): 70 | return not self == other 71 | 72 | def __repr__(self): 73 | return '