# This file is part of the dune-gdt project:
#   https://github.com/dune-community/dune-gdt
# Copyright 2010-2017 dune-gdt developers and contributors. All rights reserved.
# License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
#      or  GPL-2.0+ (http://opensource.org/licenses/gpl-license)
#          with "runtime exception" (http://www.dune-project.org/license.html)
# Authors:
#   Felix Schindler (2017)
#   Rene Milk       (2016)

from importlib import import_module

import dune.xt.common # inits MPI via mpi4py

_init_logger_methods = list()
_init_mpi_methods = list()
_other_modules = ('xt.common', 'xt.grid', 'xt.functions', 'xt.la')

# the following ordering is not arbitrary
_gdt_modules = ['spaces', # is required by all others (aka: needs to be loaded first)
                'assembler', # requires spaces and is required by others
                'discretefunction',
                'projections',
                'functionals_l2',
                'operators_elliptic',
                'operators_elliptic_ipdg']

for module_name in _gdt_modules:
    mod = import_module('.__{}'.format(module_name), 'dune.gdt')
    to_import = [name for name in mod.__dict__ if not name.startswith('_')]
    globals().update({name: mod.__dict__[name] for name in to_import})
    _init_logger_methods.append(mod.__dict__['_init_logger'])
    _init_mpi_methods.append(mod.__dict__['_init_mpi'])

del _gdt_modules


def init_logger(max_info_level=-1,
                max_debug_level=-1,
                enable_warnings=True,
                enable_colors=True,
                info_color='blue',
                debug_color='darkgray',
                warning_color='red'):
    init_logger_methods = _init_logger_methods.copy()
    for module_name in _other_modules:
        try:
            mm = import_module('dune.{}'.format(module_name))
            for init_logger_method in mm._init_logger_methods:
                init_logger_methods.append(init_logger_method)
        except ModuleNotFoundError:
            pass
    for init_logger_method in init_logger_methods:
        init_logger_method(max_info_level, max_debug_level, enable_warnings, enable_colors, info_color, debug_color,
                           warning_color)

def init_mpi(args=list()):
    init_mpi_methods = _init_mpi_methods.copy()
    for module_name in _other_modules:
        try:
            mm = import_module('dune.{}'.format(module_name))
            for init_mpi_method in mm._init_mpi_methods:
                init_mpi_methods.append(init_mpi_method)
        except ModuleNotFoundError:
            pass
    for init_mpi_method in init_mpi_methods:
        init_mpi_method(args)


HAVE_DUNE_FEM = 'Cg2dCubeYaspgridLeafTo1x1FemSpace' in globals()
HAVE_DUNE_PDELAB = 'Cg2dCubeYaspgridLeafTo1x1PdelabSpace' in globals()


@@ -508,22 +508,20 @@
 module%0A%0A
-import
+from
  dune.xt
@@ -528,16 +528,29 @@
 .common 
+import DEBUG 
 # inits 
@@ -2300,16 +2300,95 @@
 ist()):%0A
+    if DEBUG:%0A        init_mpi_methods = %5B_init_mpi_methods%5B0%5D,%5D%0A    else:%0A    
     init
@@ -2423,32 +2423,36 @@
 hods.copy()%0A    
+  
+  
 for module_name 
@@ -2470,37 +2470,45 @@
 odules:%0A        
+    
 try:%0A
+    
             mm =
@@ -2557,32 +2557,36 @@
 e))%0A            
+   
+ 
 for init_mpi_met
@@ -2622,32 +2622,36 @@
                 
+    
 init_mpi_methods
@@ -2675,32 +2675,36 @@
 method)%0A        
+  
+  
 except ModuleNot
@@ -2707,32 +2707,36 @@
 eNotFoundError:%0A
+    
             pass

fix mpi_init in DEBUG builds