import imp
import os

from labonneboite.conf.common import settings_common

# Settings
# --------

# Default settings of the application are defined in `labonneboite/conf/common/settings_common.py`.
# A specific environment (staging, production...) can define its custom settings by:
# - creating a specific `settings` file, e.g. `lbb_staging_settings.py`
# - defining an environment variable containing the path to this specific `settings` file
#
# Specific and default settings will be merged, and values found in specific settings will take precedence.
# When no specific settings are found, `labonneboite/conf/local_settings.py` is used.

# Dynamically import LBB_SETTINGS environment variable as the `settings`
# module, or import `local_settings.py` as the `settings` module if it does not
# exist.
settings = settings_common
if settings_common.get_current_env() != settings_common.ENV_TEST:
    # Don't override settings in tests
    settings_module = os.path.join(os.path.dirname(__file__), 'local_settings.py')
    settings_module = os.environ.get('LBB_SETTINGS', settings_module)
    settings = imp.load_source('settings', settings_module)

    # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
    # imported `settings` module if they don't already exist.
    for setting in dir(settings_common):
        if not hasattr(settings, setting):
            setattr(settings, setting, getattr(settings_common, setting))

import imp
import os

from labonneboite.conf.common import settings_common

# Settings
# --------

# Default settings of the application are defined in `labonneboite/conf/common/settings_common.py`.
# A specific environment (staging, production...) can define its custom settings by:
# - creating a specific `settings` file, e.g. `lbb_staging_settings.py`
# - defining an environment variable containing the path to this specific `settings` file
#
# Specific and default settings will be merged, and values found in specific settings will take precedence.
# When no specific settings are found, `labonneboite/conf/local_settings.py` is used.

# Dynamically import LBB_SETTINGS environment variable as the `settings`
# module, or import `local_settings.py` as the `settings` module if it does not
# exist.
settings = settings_common
if settings_common.get_current_env() != settings_common.ENV_TEST:
    # Don't override settings in tests
    settings_module = os.path.join(os.path.dirname(__file__), 'local_settings.py')
    settings_module = os.environ.get('LBB_SETTINGS', settings_module)
    try:
        settings = imp.load_source('settings', settings_module)
    except FileNotFoundError:
        pass
    else:
        # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
        # imported `settings` module if they don't already exist.
        for setting in dir(settings_common):
            if not hasattr(settings, setting):
                setattr(settings, setting, getattr(settings_common, setting))

Fix FileNotFoundError on missing local_settings.py
Fix FileNotFoundError on missing local_settings.py

This has been broken for a long time... When running LBB without a
local_settings.py and without an LBB_ENV environment variable, importing
local_settings.py was resulting in a FileNotFoundError.

  import imp
  import os
  
  from labonneboite.conf.common import settings_common
  
  # Settings
  # --------
  
  # Default settings of the application are defined in `labonneboite/conf/common/settings_common.py`.
  # A specific environment (staging, production...) can define its custom settings by:
  # - creating a specific `settings` file, e.g. `lbb_staging_settings.py`
  # - defining an environment variable containing the path to this specific `settings` file
  #
  # Specific and default settings will be merged, and values found in specific settings will take precedence.
  # When no specific settings are found, `labonneboite/conf/local_settings.py` is used.
  
  # Dynamically import LBB_SETTINGS environment variable as the `settings`
  # module, or import `local_settings.py` as the `settings` module if it does not
  # exist.
  settings = settings_common
  if settings_common.get_current_env() != settings_common.ENV_TEST:
      # Don't override settings in tests
      settings_module = os.path.join(os.path.dirname(__file__), 'local_settings.py')
      settings_module = os.environ.get('LBB_SETTINGS', settings_module)
+     try:
-     settings = imp.load_source('settings', settings_module)
+         settings = imp.load_source('settings', settings_module)
+     except FileNotFoundError:
+         pass
+     else:
+         # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
+         # imported `settings` module if they don't already exist.
+         for setting in dir(settings_common):
+             if not hasattr(settings, setting):
+                 setattr(settings, setting, getattr(settings_common, setting))
  
-     # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
-     # imported `settings` module if they don't already exist.
-     for setting in dir(settings_common):
-         if not hasattr(settings, setting):
-             setattr(settings, setting, getattr(settings_common, setting))
- 
Fix FileNotFoundError on missing local_settings.py
## Code Before:
import imp
import os

from labonneboite.conf.common import settings_common

# Settings
# --------

# Default settings of the application are defined in `labonneboite/conf/common/settings_common.py`.
# A specific environment (staging, production...) can define its custom settings by:
# - creating a specific `settings` file, e.g. `lbb_staging_settings.py`
# - defining an environment variable containing the path to this specific `settings` file
#
# Specific and default settings will be merged, and values found in specific settings will take precedence.
# When no specific settings are found, `labonneboite/conf/local_settings.py` is used.

# Dynamically import LBB_SETTINGS environment variable as the `settings`
# module, or import `local_settings.py` as the `settings` module if it does not
# exist.
settings = settings_common
if settings_common.get_current_env() != settings_common.ENV_TEST:
    # Don't override settings in tests
    settings_module = os.path.join(os.path.dirname(__file__), 'local_settings.py')
    settings_module = os.environ.get('LBB_SETTINGS', settings_module)
    settings = imp.load_source('settings', settings_module)

    # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
    # imported `settings` module if they don't already exist.
    for setting in dir(settings_common):
        if not hasattr(settings, setting):
            setattr(settings, setting, getattr(settings_common, setting))

## Instruction:
Fix FileNotFoundError on missing local_settings.py
## Code After:
import imp
import os

from labonneboite.conf.common import settings_common

# Settings
# --------

# Default settings of the application are defined in `labonneboite/conf/common/settings_common.py`.
# A specific environment (staging, production...) can define its custom settings by:
# - creating a specific `settings` file, e.g. `lbb_staging_settings.py`
# - defining an environment variable containing the path to this specific `settings` file
#
# Specific and default settings will be merged, and values found in specific settings will take precedence.
# When no specific settings are found, `labonneboite/conf/local_settings.py` is used.

# Dynamically import LBB_SETTINGS environment variable as the `settings`
# module, or import `local_settings.py` as the `settings` module if it does not
# exist.
settings = settings_common
if settings_common.get_current_env() != settings_common.ENV_TEST:
    # Don't override settings in tests
    settings_module = os.path.join(os.path.dirname(__file__), 'local_settings.py')
    settings_module = os.environ.get('LBB_SETTINGS', settings_module)
    try:
        settings = imp.load_source('settings', settings_module)
    except FileNotFoundError:
        pass
    else:
        # Iterate over each setting defined in the `settings_common` module and add them to the dynamically
        # imported `settings` module if they don't already exist.
        for setting in dir(settings_common):
            if not hasattr(settings, setting):
                setattr(settings, setting, getattr(settings_common, setting))
