# Path: django_node/settings.py
# PATH_TO_NODE = setting_overrides.get(
#     'PATH_TO_NODE',
#     'node'
# )
# 
# PATH_TO_NPM = setting_overrides.get(
#     'PATH_TO_NPM',
#     'npm'
# )
# 
# NODE_VERSION_COMMAND = setting_overrides.get(
#     'NODE_VERSION_COMMAND',
#     '--version',
# )
# 
# NODE_VERSION_FILTER = setting_overrides.get(
#     'NODE_VERSION_FILTER',
#     lambda version: tuple(map(int, (version[1:] if version[0] == 'v' else version).split('.'))),
# )
# 
# NPM_VERSION_COMMAND = setting_overrides.get(
#     'NPM_VERSION_COMMAND',
#     '--version',
# )
# 
# NPM_VERSION_FILTER = setting_overrides.get(
#     'NPM_VERSION_FILTER',
#     lambda version: tuple(map(int, version.split('.'))),
# )
# 
# Path: django_node/exceptions.py
# class DynamicImportError(Exception):
#     pass
# 
# class ErrorInterrogatingEnvironment(Exception):
#     pass
# 
# class MalformedVersionInput(Exception):
#     pass
# 
# class MissingDependency(Exception):
#     pass
# 
# class OutdatedDependency(Exception):
#     pass
# 
# class ModuleDoesNotContainAnyServices(Exception):
#     pass


import sys
import subprocess
import tempfile
import importlib
import re
import inspect
from django.utils import six
from .settings import (
    PATH_TO_NODE, PATH_TO_NPM, NODE_VERSION_COMMAND, NODE_VERSION_FILTER, NPM_VERSION_COMMAND, NPM_VERSION_FILTER,
)
from .exceptions import (
    DynamicImportError, ErrorInterrogatingEnvironment, MalformedVersionInput, MissingDependency, OutdatedDependency,
    ModuleDoesNotContainAnyServices
)
    from . import node, npm  # Avoid a circular import
    from .base_service import BaseService  # Avoid a circular import
            (
                'The installed {application} version is outdated. Version {current_version} is installed, but version '
                '{required_version} is required. Please update {application}.'
            ).format(
                application=application,
                current_version=_format_version(current_version),
                required_version=_format_version(required_version),
            )
        )
    

def dynamic_import_module(import_path):
    try:
        return importlib.import_module(import_path)
    except ImportError as e:
        msg = 'Failed to import "{import_path}"'.format(
            import_path=import_path
        )
        six.reraise(DynamicImportError, DynamicImportError(msg, e.__class__.__name__, *e.args), sys.exc_info()[2])


def dynamic_import_attribute(import_path):
    module_import_path = '.'.join(import_path.split('.')[:-1])
    imported_module = dynamic_import_module(module_import_path)
    try:
        return getattr(imported_module, import_path.split('.')[-1])
    except AttributeError as e:
        msg = 'Failed to import "{import_path}"'.format(
            import_path=import_path
        )

# Path: django_node/settings.py
# PATH_TO_NODE = setting_overrides.get(
#     'PATH_TO_NODE',
#     'node'
# )
# 
# PATH_TO_NPM = setting_overrides.get(
#     'PATH_TO_NPM',
#     'npm'
# )
# 
# NODE_VERSION_COMMAND = setting_overrides.get(
#     'NODE_VERSION_COMMAND',
#     '--version',
# )
# 
# NODE_VERSION_FILTER = setting_overrides.get(
#     'NODE_VERSION_FILTER',
#     lambda version: tuple(map(int, (version[1:] if version[0] == 'v' else version).split('.'))),
# )
# 
# NPM_VERSION_COMMAND = setting_overrides.get(
#     'NPM_VERSION_COMMAND',
#     '--version',
# )
# 
# NPM_VERSION_FILTER = setting_overrides.get(
#     'NPM_VERSION_FILTER',
#     lambda version: tuple(map(int, version.split('.'))),
# )
# 
# Path: django_node/exceptions.py
# class DynamicImportError(Exception):
#     pass
# 
# class ErrorInterrogatingEnvironment(Exception):
#     pass
# 
# class MalformedVersionInput(Exception):
#     pass
# 
# class MissingDependency(Exception):
#     pass
# 
# class OutdatedDependency(Exception):
#     pass
# 
# class ModuleDoesNotContainAnyServices(Exception):
#     pass


# Path: django_node/utils.py
import sys
import subprocess
import tempfile
import importlib
import re
import inspect
from django.utils import six
from .settings import (
    PATH_TO_NODE, PATH_TO_NPM, NODE_VERSION_COMMAND, NODE_VERSION_FILTER, NPM_VERSION_COMMAND, NPM_VERSION_FILTER,
)
from .exceptions import (
    DynamicImportError, ErrorInterrogatingEnvironment, MalformedVersionInput, MissingDependency, OutdatedDependency,
    ModuleDoesNotContainAnyServices
)
    from . import node, npm  # Avoid a circular import
    from .base_service import BaseService  # Avoid a circular import

            (
                'The installed {application} version is outdated. Version {current_version} is installed, but version '
                '{required_version} is required. Please update {application}.'
            ).format(
                application=application,
                current_version=_format_version(current_version),
                required_version=_format_version(required_version),
            )
        )
    

def dynamic_import_module(import_path):
    try:
        return importlib.import_module(import_path)
    except ImportError as e:
        msg = 'Failed to import "{import_path}"'.format(
            import_path=import_path
        )
        six.reraise(DynamicImportError, DynamicImportError(msg, e.__class__.__name__, *e.args), sys.exc_info()[2])


def dynamic_import_attribute(import_path):
    module_import_path = '.'.join(import_path.split('.')[:-1])
    imported_module = dynamic_import_module(module_import_path)
    try:
        return getattr(imported_module, import_path.split('.')[-1])
    except AttributeError as e:
        msg = 'Failed to import "{import_path}"'.format(
            import_path=import_path
        )

        six.reraise(DynamicImportError, DynamicImportError(msg, e.__class__.__name__, *e.args), sys.exc_info()[2])