-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Bertin
committed
Sep 27, 2013
0 parents
commit 48ec95a
Showing
31 changed files
with
9,390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[report] | ||
exclude_lines = | ||
def __repr__ | ||
raise NotImplementedError | ||
if __name__ == .__main__.: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
*.py[cod] | ||
|
||
# Packages | ||
*.egg | ||
*.egg-info | ||
dist | ||
build | ||
eggs | ||
parts | ||
bin | ||
var | ||
sdist | ||
develop-eggs | ||
.installed.cfg | ||
lib | ||
lib64 | ||
|
||
# Installer logs | ||
pip-log.txt | ||
|
||
# Unit test / coverage reports | ||
.coverage | ||
.tox | ||
nosetests.xml | ||
|
||
# Translations | ||
*.mo | ||
|
||
# Mr Developer | ||
.mr.developer.cfg | ||
|
||
# Pydev | ||
.project | ||
.pydevproject | ||
.settings | ||
|
||
# Rope | ||
.ropeproject | ||
|
||
# Sphinx | ||
docs/_build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "docs/_themes"] | ||
path = docs/_themes | ||
url = git://github.com/Diaoul/diaoul-sphinx-themes.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
language: python | ||
|
||
python: | ||
- "2.7" | ||
- "3.3" | ||
- "pypy" | ||
|
||
install: | ||
- pip install coveralls --use-mirrors | ||
|
||
script: | ||
- coverage run --source=babelfish setup.py test | ||
|
||
after_success: | ||
- coveralls | ||
|
||
notifications: | ||
email: false | ||
irc: | ||
channels: | ||
- "irc.freenode.org#babelfish" | ||
on_success: change | ||
on_failure: always | ||
use_notice: true | ||
skip_join: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
BabelFish is written and maintained by: | ||
* Antoine Bertin <[email protected]> | ||
* Nicolas Wack <[email protected]> | ||
|
||
Other contributors, listed alphabetically, are: | ||
|
||
* Your name here! -- what you did | ||
|
||
Many thanks for all contributions! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
BabelFish Changelog | ||
=================== | ||
|
||
0.1 | ||
--- | ||
*Release date: TBA* | ||
|
||
* Initial version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) 2013, by the respective authors (see AUTHORS file). | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
* Neither the name of the BabelFish authors nor the names of its contributors | ||
may be used to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
BabelFish | ||
========= | ||
|
||
BabelFish is a Python library to work with countries and languages. | ||
|
||
.. image:: https://travis-ci.org/Diaoul/babelfish.png?branch=master | ||
:target: https://travis-ci.org/Diaoul/babelfish | ||
|
||
.. image:: https://coveralls.io/repos/Diaoul/babelfish/badge.png | ||
:target: https://coveralls.io/r/Diaoul/babelfish | ||
|
||
License | ||
------- | ||
|
||
BabelFish is licensed under the `3-clause BSD license <http://opensource.org/licenses/BSD-3-Clause>`_. | ||
Copyright (c) 2013, the BabelFish authors and contributors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
__title__ = 'babelfish' | ||
__version__ = '0.1' | ||
__author__ = 'Antoine Bertin' | ||
__license__ = 'BSD' | ||
__copyright__ = 'Copyright 2013 the BabelFish authors' | ||
|
||
from .language import CONVERTERS, LANGUAGES, Language, register_converter, unregister_converter, load_converters, clear_converters | ||
from .country import COUNTRIES, Country | ||
from .converters import Converter, ReverseConverter | ||
from .exceptions import * | ||
|
||
|
||
load_converters() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
|
||
|
||
class Converter(object): | ||
"""A :class:`Converter` supports converting an alpha3 language code with an | ||
alpha2 country code into a custom code | ||
""" | ||
def convert(self, alpha3, country=None): | ||
"""Convert an alpha3 language code with an alpha2 country code | ||
into a custom code | ||
:param string alpha3: ISO-639-3 language code | ||
:param country: ISO-3166 country code, if any | ||
:type country: string or None | ||
:return: the corresponding custom code | ||
:rtype: string | ||
""" | ||
raise NotImplementedError | ||
|
||
|
||
class ReverseConverter(Converter): | ||
"""A :class:`Converter` able to reverse a custom code into a alpha3 | ||
ISO-639-3 language code and alpha2 ISO-3166-1 country code | ||
""" | ||
def reverse(self, code): | ||
"""Reverse a custom code into alpha3 and country code | ||
:param string code: custom code to reverse | ||
:return: the corresponding alpha3 ISO-639-3 language code and alpha2 ISO-3166-1 country code | ||
:rtype: tuple | ||
""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
from __future__ import unicode_literals | ||
from pkg_resources import resource_stream # @UnresolvedImport | ||
from . import ReverseConverter | ||
from ..exceptions import NoConversionError | ||
|
||
|
||
class Alpha2Converter(ReverseConverter): | ||
def __init__(self): | ||
self.to_alpha2 = {} | ||
self.from_alpha2 = {} | ||
with resource_stream('babelfish', 'data/iso-639-3.tab') as f: | ||
f.readline() | ||
for l in f: | ||
(alpha3, _, _, alpha2, _, _, _, _) = l.decode('utf-8').split('\t') | ||
self.to_alpha2[alpha3] = alpha2 | ||
self.from_alpha2[alpha2] = alpha3 | ||
|
||
def convert(self, alpha3, country=None): | ||
if alpha3 not in self.to_alpha2: | ||
raise NoConversionError | ||
return self.to_alpha2[alpha3] | ||
|
||
def reverse(self, alpha2): | ||
if alpha2 not in self.from_alpha2: | ||
raise NoConversionError | ||
return (self.from_alpha2[alpha2], None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
from __future__ import unicode_literals | ||
from pkg_resources import resource_stream # @UnresolvedImport | ||
from . import ReverseConverter | ||
from ..exceptions import NoConversionError | ||
|
||
|
||
class Alpha3BConverter(ReverseConverter): | ||
def __init__(self): | ||
self.to_alpha3b = {} | ||
self.from_alpha3b = {} | ||
with resource_stream('babelfish', 'data/iso-639-3.tab') as f: | ||
f.readline() | ||
for l in f: | ||
(alpha3, alpha3b, _, _, _, _, _, _) = l.decode('utf-8').split('\t') | ||
self.to_alpha3b[alpha3] = alpha3b | ||
self.from_alpha3b[alpha3b] = alpha3 | ||
|
||
def convert(self, alpha3, country=None): | ||
if alpha3 not in self.to_alpha3b: | ||
raise NoConversionError | ||
return self.to_alpha3b[alpha3] | ||
|
||
def reverse(self, alpha3b): | ||
if alpha3b not in self.from_alpha3b: | ||
raise NoConversionError | ||
return (self.from_alpha3b[alpha3b], None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
from __future__ import unicode_literals | ||
from pkg_resources import resource_stream # @UnresolvedImport | ||
from . import ReverseConverter | ||
from ..exceptions import NoConversionError | ||
|
||
|
||
class NameConverter(ReverseConverter): | ||
def __init__(self): | ||
self.to_name = {} | ||
self.from_name = {} | ||
with resource_stream('babelfish', 'data/iso-639-3.tab') as f: | ||
f.readline() | ||
for l in f: | ||
(alpha3, _, _, _, _, _, name, _) = l.decode('utf-8').split('\t') | ||
self.to_name[alpha3] = name | ||
self.from_name[name] = alpha3 | ||
|
||
def convert(self, alpha3, country=None): | ||
if alpha3 not in self.to_name: | ||
raise NoConversionError | ||
return self.to_name[alpha3] | ||
|
||
def reverse(self, name): | ||
if name not in self.from_name: | ||
raise NoConversionError | ||
return (self.from_name[name], None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
from __future__ import unicode_literals | ||
from .alpha3b import Alpha3BConverter | ||
|
||
|
||
class OpenSubtitlesConverter(Alpha3BConverter): | ||
def __init__(self): | ||
super(OpenSubtitlesConverter, self).__init__() | ||
self.to_opensubtitles = {('por', 'BR'): 'pob'} | ||
self.from_opensubtitles = {'pob': ('por', 'BR')} | ||
|
||
def convert(self, alpha3, country=None): | ||
alpha3b = super(OpenSubtitlesConverter, self).convert(alpha3, country) | ||
if (alpha3b, country) in self.to_opensubtitles: | ||
return self.to_opensubtitles[(alpha3b, country)] | ||
return alpha3b | ||
|
||
def reverse(self, opensubtitles): | ||
if opensubtitles in self.from_opensubtitles: | ||
return self.from_opensubtitles[opensubtitles] | ||
return super(OpenSubtitlesConverter, self).reverse(opensubtitles) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2013 the BabelFish authors. All rights reserved. | ||
# Use of this source code is governed by the 3-clause BSD license | ||
# that can be found in the LICENSE file. | ||
# | ||
from __future__ import unicode_literals | ||
from pkg_resources import resource_stream # @UnresolvedImport | ||
|
||
|
||
COUNTRIES = {} | ||
with resource_stream('babelfish', 'data/iso-3166-1.txt') as f: | ||
f.readline() | ||
for l in f: | ||
(name, alpha2) = l.decode('utf-8').strip().split(';') | ||
COUNTRIES[alpha2] = name | ||
|
||
|
||
class Country(object): | ||
"""A country on Earth | ||
A country is represented by a two-letters code from the ISO-3166 standard | ||
:param string country: two-letters ISO-3166 country code | ||
""" | ||
def __init__(self, country): | ||
if country not in COUNTRIES: | ||
raise ValueError('{} is not a valid country'.format(country)) | ||
self.alpha2 = country | ||
|
||
@property | ||
def name(self): | ||
return COUNTRIES[self.alpha2] | ||
|
||
def __hash__(self): | ||
return hash(self.alpha2) | ||
|
||
def __eq__(self, other): | ||
return self.alpha2 == other.alpha2 | ||
|
||
def __ne__(self, other): | ||
return not self == other | ||
|
||
def __repr__(self): | ||
return '<Country {}>'.format(self.name) |
Oops, something went wrong.