# -*- coding: utf-8 -*-

import traceback
import importlib
import logging
import pkgutil
import sys
from os import linesep

log = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s: %(message)s')


def handle_error(error_message):
    log.error(error_message)
    sys.exit(1)


def add_tax_benefit_system_arguments(parser):
    parser.add_argument('-c', '--country-package', action = 'store', help = u'country package to use. If not provided, an automatic detection will be attempted by scanning the python packages installed in your environment which name contains the word "openfisca".')
    parser.add_argument('-e', '--extensions', action = 'store', help = u'extensions to load', nargs = '*')
    parser.add_argument('-r', '--reforms', action = 'store', help = u'reforms to apply to the country package', nargs = '*')

    return parser


def build_tax_benefit_system(country_package_name, extensions, reforms):
    if country_package_name is None:
        country_package_name = detect_country_package()
    try:
        country_package = importlib.import_module(country_package_name)
    except ImportError:
        message = linesep.join([traceback.format_exc(),
                                u'Could not import module `{}`.'.format(country_package_name),
                                u'Are you sure it is installed in your environment? If so, look at the stack trace above to determine the origin of this error.',
                                u'See more at <https://github.com/openfisca/country-template#installing>.'])

        handle_error(message)
    if not hasattr(country_package, 'CountryTaxBenefitSystem'):
        handle_error(u'`{}` does not seem to be a valid Openfisca country package.'.format(country_package_name))

    country_package = importlib.import_module(country_package_name)
    tax_benefit_system = country_package.CountryTaxBenefitSystem()

    if extensions:
        for extension in extensions:
            tax_benefit_system.load_extension(extension)

    if reforms:
        for reform_path in reforms:
            tax_benefit_system = tax_benefit_system.apply_reform(reform_path)

    return tax_benefit_system


# For retro-compatibility:
build_tax_benefit_sytem = build_tax_benefit_system


def detect_country_package():
    installed_country_packages = []
    for module_description in pkgutil.iter_modules():
        module_name = module_description[1]
        if 'openfisca' in module_name.lower():
            try:
                module = importlib.import_module(module_name)
            except ImportError:
                message = linesep.join([traceback.format_exc(),
                                        u'Could not import module `{}`.'.format(module_name),
                                        u'Look at the stack trace above to determine the error that stopped installed modules detection.'])
                handle_error(message)
            if hasattr(module, 'CountryTaxBenefitSystem'):
                installed_country_packages.append(module_name)

    if len(installed_country_packages) == 0:
        handle_error(u'No country package has been detected on your environment. If your country package is installed but not detected, please use the --country-package option.')
    if len(installed_country_packages) > 1:
        log.warning(u'Several country packages detected : `{}`. Using `{}` by default. To use another package, please use the --country-package option.'.format(', '.join(installed_country_packages), installed_country_packages[0]))
    return installed_country_packages[0]

@@ -86,19 +86,8 @@
 til%0A
-import sys%0A
 from
@@ -203,88 +203,8 @@
 )%0A%0A%0A
-def handle_error(error_message):%0A    log.error(error_message)%0A    sys.exit(1)%0A%0A%0A
 def 
@@ -1462,32 +1462,37 @@
 %5D)%0A%0A        
-handle_e
+raise ImportE
 rror(message
@@ -1561,32 +1561,37 @@
 '):%0A        
-handle_e
+raise ImportE
 rror(u'%60%7B%7D%60 
@@ -2802,32 +2802,37 @@
             
-handle_e
+raise ImportE
 rror(message
@@ -3013,16 +3013,21 @@
     
-handle_e
+raise ImportE
 rror

Improve error handling in tbs creation