Repository: joke2k/faker
Pull request #199 (2015-01-31 21:57:12)

de-hardcoded `AVAILABLE_LOCALES`

coveralls: 
[![Coverage Status](https://coveralls.io/builds/1840987/badge)](https://coveralls.io/builds/1840987)

Coverage increased (+0.84%) to 86.58% when pulling **0b4419515e68f0a5421cac89944a9fe029c9fb33 on fcurella:feature/locales_autodiscovery** into **b28b909818c997144ca6905b85bd6fab59af2b58 on joke2k:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/1840992/badge)](https://coveralls.io/builds/1840992)

Coverage increased (+0.91%) to 86.64% when pulling **89d90ff7965c378b48f952c80b754254d990b321 on fcurella:feature/locales_autodiscovery** into **b28b909818c997144ca6905b85bd6fab59af2b58 on joke2k:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/1841004/badge)](https://coveralls.io/builds/1841004)

Coverage increased (+0.97%) to 86.71% when pulling **a9452239840cfc3abd2ab507dc8cef7accd62cd2 on fcurella:feature/locales_autodiscovery** into **b28b909818c997144ca6905b85bd6fab59af2b58 on joke2k:master**.


Patch:
diff --git a/faker/__init__.py b/faker/__init__.py
index bd041ca1..706c4b93 100644
--- a/faker/__init__.py
+++ b/faker/__init__.py
@@ -1,64 +1,5 @@
-# coding=utf-8
-
 VERSION = '0.4.2'
 
-
-AVAILABLE_LOCALES = [
-    'bg_BG',
-    'cs_CZ',
-    'de_AT',
-    'de_DE',
-    'dk_DK',
-    'el_GR',
-    'en_CA',
-    'en_GB',
-    'en_US',
-    'es_ES',
-    'es_MX',
-    'fa_IR',
-    'fi_FI',
-    'fr_FR',
-    'hi_IN',
-    'it_IT',
-    'ko_KR',
-    'lt_LT',
-    'lv_LV',
-    'ne_NP',
-    'nl_NL',
-    'pl_PL',
-    'pt_BR',
-    'pt_PT',
-    'ru_RU',
-    'sl_SI',
-    'tr_TR',
-    'uk_UA',
-    'zh_CN',
-    'zh_TW'
-]
-
-DEFAULT_LOCALE = 'en_US'
-
-DEFAULT_PROVIDERS = (
-    'address',
-    'barcode',
-    'color',
-    'company',
-    'credit_card',
-    'currency',
-    'date_time',
-    'file',
-    'internet',
-    'job',
-    'lorem',
-    'misc',
-    'person',
-    'profile',
-    'python',
-    'phone_number',
-    'ssn',
-    'user_agent',
-)
-
 from faker.generator import Generator
 from faker.factory import Factory
 
diff --git a/faker/cli.py b/faker/cli.py
index 626acb79..5c8a8a0f 100644
--- a/faker/cli.py
+++ b/faker/cli.py
@@ -8,7 +8,8 @@ import sys
 import argparse
 
 from faker import Faker, Factory, documentor
-from faker import VERSION, AVAILABLE_LOCALES, DEFAULT_LOCALE
+from faker import VERSION
+from faker.config import AVAILABLE_LOCALES, DEFAULT_LOCALE
 
 
 if sys.version < '3':
diff --git a/faker/config.py b/faker/config.py
new file mode 100644
index 00000000..085831ea
--- /dev/null
+++ b/faker/config.py
@@ -0,0 +1,27 @@
+# coding=utf-8
+from faker.utils.loading import find_available_locales
+
+DEFAULT_LOCALE = 'en_US'
+
+DEFAULT_PROVIDERS = (
+    'address',
+    'barcode',
+    'color',
+    'company',
+    'credit_card',
+    'currency',
+    'date_time',
+    'file',
+    'internet',
+    'job',
+    'lorem',
+    'misc',
+    'person',
+    'profile',
+    'python',
+    'phone_number',
+    'ssn',
+    'user_agent',
+)
+
+AVAILABLE_LOCALES = find_available_locales(DEFAULT_PROVIDERS)
diff --git a/faker/factory.py b/faker/factory.py
index c9d567ae..50d71852 100644
--- a/faker/factory.py
+++ b/faker/factory.py
@@ -6,9 +6,9 @@ from __future__ import absolute_import
 from importlib import import_module
 import locale as pylocale
 
-from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
 from faker import Generator
 from faker import providers as providers_mod
+from faker.config import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
 from faker.utils.loading import list_module
 
 
diff --git a/faker/utils/loading.py b/faker/utils/loading.py
index 63cbf22b..69d9b6a9 100644
--- a/faker/utils/loading.py
+++ b/faker/utils/loading.py
@@ -1,6 +1,26 @@
 import os
+from importlib import import_module
 
 
 def list_module(module):
     path = os.path.dirname(module.__file__)
-    return [i for i in os.listdir(path) if os.path.isdir(os.path.join(path, i)) and not i.startswith('_')]
\ No newline at end of file
+    return [i for i in os.listdir(path) if os.path.isdir(os.path.join(path, i)) and not i.startswith('_')]
+
+
+def find_available_locales(providers):
+    available_locales = set()
+    from faker import providers as providers_mod
+
+    for provider in providers:
+        providers_mod_name = providers_mod.__package__ or providers_mod.__name__
+ 
+        path = "{providers}.{provider}".format(
+            providers=providers_mod_name,
+            provider=provider
+        )
+
+        provider_module = import_module(path)
+        if getattr(provider_module, 'localized', False):
+            langs = list_module(provider_module)
+            available_locales.update(langs)
+    return available_locales


Test patch:
diff --git a/faker/tests/__init__.py b/faker/tests/__init__.py
index 8ca6177c..c6a84097 100644
--- a/faker/tests/__init__.py
+++ b/faker/tests/__init__.py
@@ -92,6 +92,13 @@ class UtilsTestCase(unittest.TestCase):
         self.assertEqual(result, {'a': 1, 'c': 3, 'b': 3, 'd': 4})
 
 
+    def test_find_available_locales(self):
+        from faker.utils.loading import find_available_locales
+        from faker.config import DEFAULT_PROVIDERS
+
+        result = find_available_locales(DEFAULT_PROVIDERS)
+        self.assertNotEqual(len(result), 0)
+
 class FactoryTestCase(unittest.TestCase):
 
     def setUp(self):