Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Bertin committed Sep 27, 2013
0 parents commit 48ec95a
Show file tree
Hide file tree
Showing 31 changed files with 9,390 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[report]
exclude_lines =
def __repr__
raise NotImplementedError
if __name__ == .__main__.:
41 changes: 41 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .gitmodules
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
25 changes: 25 additions & 0 deletions .travis.yml
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
9 changes: 9 additions & 0 deletions AUTHORS
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!
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BabelFish Changelog
===================

0.1
---
*Release date: TBA*

* Initial version
25 changes: 25 additions & 0 deletions LICENSE
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.
16 changes: 16 additions & 0 deletions README.rst
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.
19 changes: 19 additions & 0 deletions babelfish/__init__.py
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()
39 changes: 39 additions & 0 deletions babelfish/converters/__init__.py
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
32 changes: 32 additions & 0 deletions babelfish/converters/alpha2.py
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)
32 changes: 32 additions & 0 deletions babelfish/converters/alpha3b.py
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)
32 changes: 32 additions & 0 deletions babelfish/converters/name.py
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)
26 changes: 26 additions & 0 deletions babelfish/converters/opensubtitles.py
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)
46 changes: 46 additions & 0 deletions babelfish/country.py
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)
Loading

0 comments on commit 48ec95a

Please sign in to comment.