# Path: pywrap/cython.py
# def make_cython_wrapper(filenames, sources, modulename=None, target=".",
#                         config=Config(), incdirs=(), compiler_flags=("-O3",),
#                         verbose=0):
#     """Make Cython wrapper for C++ files.
# 
#     Parameters
#     ----------
#     filenames : list of strings or string
#         C++ files
# 
#     sources : list of strings
#         C++ source files that have to be compiled
# 
#     modulename : string, optional (default: name of the only header)
#         Name of the module
# 
#     target : string, optional (default: ".")
#         Target directory
# 
#     config : Config, optional (default: defaultconfig.Config())
#         Configuration
# 
#     incdirs : list, optional (default: [])
#         Include directories
# 
#     compiler_flags : list, optional (default: ["-O3"])
#         Flags that will be passed directly to the compiler when building the
#         extension
# 
#     verbose : int, optional (default: 0)
#         Verbosity level
# 
#     Returns
#     -------
#     results : dict
#         Mapping from filename to generated file content
#     """
#     if isinstance(filenames, str):
#         filenames = [filenames]
#     if len(filenames) == 1 and modulename is None:
#         modulename = _derive_module_name_from(filenames[0])
#     if modulename is None:
#         raise ValueError("Please give a module name when there are multiple "
#                          "C++ files that you want to wrap.")
# 
#     for incdir in incdirs:
#         if not os.path.exists(incdir):
#             raise ValueError("Include directory '%s' does not exist." % incdir)
# 
#     for filename in filenames:
#         if file_ending(filename) not in config.cpp_header_endings:
#             raise ValueError("'%s' does not seem to be a header file which is "
#                              "required." % filename)
#         if not os.path.exists(filename):
#             raise ValueError("File '%s' does not exist" % filename)
# 
#     includes, type_info, asts = _parse_files(
#         filenames, config, incdirs, verbose)
# 
#     postprocess_asts(asts)
# 
#     results = dict(
#         [_make_extension(modulename, asts, includes, type_info, config),
#          _make_declarations(asts, includes, config),
#          _make_setup(sources, modulename, target, incdirs, compiler_flags,
#                      config)]
#     )
# 
#     if verbose >= 2:
#         for filename in sorted(results.keys()):
#             print(make_header("Exporting file '%s':" % filename))
#             print(results[filename])
# 
#     return results
# 
# def load_config(custom_config):
#     """Load configuration.
# 
#     Parameters
#     ----------
#     custom_config : str
#         Name of the configuration file, must have the file ending ".py"
#     """
#     if custom_config is None:
#         return Config()
# 
#     if not os.path.exists(custom_config):
#         raise ValueError("Configuration file '%s' does not exist."
#                          % custom_config)
# 
#     parts = custom_config.split(os.sep)
#     path = os.sep.join(parts[:-1])
#     filename = parts[-1]
#     module = _derive_module_name_from(filename)
# 
#     sys.path.insert(0, path)
#     imported_module = __import__(module)
#     sys.path.pop(0)
#     return imported_module.config
# 
# Path: pywrap/parser.py
# class TypeInfo:
#     def __init__(self, config=Config(), typedefs=None):
#         self.config = config
#         self.classes = []
#         self.typedefs = {}
#         if typedefs is not None:
#             self.typedefs.update(typedefs)
#         self.enums = []
#         self.spec = {}
# 
#     def attach_specialization(self, spec):
#         self.spec = spec
# 
#     def remove_specialization(self):
#         self.spec = {}
# 
#     def underlying_type(self, tname):
#         while tname in self.typedefs or tname in self.spec:
#             if tname in self.typedefs:
#                 tname = self.typedefs[tname]
#             else:
#                 tname = self.spec[tname]
#         return tname
# 
#     def get_specialization(self, tname):
#         return self.spec.get(tname, tname)


import os
from pywrap.cython import make_cython_wrapper, load_config
from pywrap.parser import TypeInfo
from nose.tools import (assert_raises_regexp, assert_false, assert_equal,
                        assert_is_not_none)
    assert_false(os.path.exists("missing.hpp.cc"))


def test_missing_modulename():
    assert_raises_regexp(ValueError, "give a module name", make_cython_wrapper,
                         ["test1.hpp", "test2.hpp"], [], None)


def test_load_config():
    configfile = "customconfigasd.py"
    with open(configfile, "w") as f:
        f.write("""
from pywrap.defaultconfig import Config

config = Config()
config.add_decleration("asd")
""")
    try:
        config = load_config(configfile)
        assert_equal(config.additional_declerations[0], "asd")
    finally:
        os.remove(configfile)


def test_load_default_config():
    config = load_config(None)
    assert_is_not_none(config)


def test_missing_config():

# Path: pywrap/cython.py
# def make_cython_wrapper(filenames, sources, modulename=None, target=".",
#                         config=Config(), incdirs=(), compiler_flags=("-O3",),
#                         verbose=0):
#     """Make Cython wrapper for C++ files.
# 
#     Parameters
#     ----------
#     filenames : list of strings or string
#         C++ files
# 
#     sources : list of strings
#         C++ source files that have to be compiled
# 
#     modulename : string, optional (default: name of the only header)
#         Name of the module
# 
#     target : string, optional (default: ".")
#         Target directory
# 
#     config : Config, optional (default: defaultconfig.Config())
#         Configuration
# 
#     incdirs : list, optional (default: [])
#         Include directories
# 
#     compiler_flags : list, optional (default: ["-O3"])
#         Flags that will be passed directly to the compiler when building the
#         extension
# 
#     verbose : int, optional (default: 0)
#         Verbosity level
# 
#     Returns
#     -------
#     results : dict
#         Mapping from filename to generated file content
#     """
#     if isinstance(filenames, str):
#         filenames = [filenames]
#     if len(filenames) == 1 and modulename is None:
#         modulename = _derive_module_name_from(filenames[0])
#     if modulename is None:
#         raise ValueError("Please give a module name when there are multiple "
#                          "C++ files that you want to wrap.")
# 
#     for incdir in incdirs:
#         if not os.path.exists(incdir):
#             raise ValueError("Include directory '%s' does not exist." % incdir)
# 
#     for filename in filenames:
#         if file_ending(filename) not in config.cpp_header_endings:
#             raise ValueError("'%s' does not seem to be a header file which is "
#                              "required." % filename)
#         if not os.path.exists(filename):
#             raise ValueError("File '%s' does not exist" % filename)
# 
#     includes, type_info, asts = _parse_files(
#         filenames, config, incdirs, verbose)
# 
#     postprocess_asts(asts)
# 
#     results = dict(
#         [_make_extension(modulename, asts, includes, type_info, config),
#          _make_declarations(asts, includes, config),
#          _make_setup(sources, modulename, target, incdirs, compiler_flags,
#                      config)]
#     )
# 
#     if verbose >= 2:
#         for filename in sorted(results.keys()):
#             print(make_header("Exporting file '%s':" % filename))
#             print(results[filename])
# 
#     return results
# 
# def load_config(custom_config):
#     """Load configuration.
# 
#     Parameters
#     ----------
#     custom_config : str
#         Name of the configuration file, must have the file ending ".py"
#     """
#     if custom_config is None:
#         return Config()
# 
#     if not os.path.exists(custom_config):
#         raise ValueError("Configuration file '%s' does not exist."
#                          % custom_config)
# 
#     parts = custom_config.split(os.sep)
#     path = os.sep.join(parts[:-1])
#     filename = parts[-1]
#     module = _derive_module_name_from(filename)
# 
#     sys.path.insert(0, path)
#     imported_module = __import__(module)
#     sys.path.pop(0)
#     return imported_module.config
# 
# Path: pywrap/parser.py
# class TypeInfo:
#     def __init__(self, config=Config(), typedefs=None):
#         self.config = config
#         self.classes = []
#         self.typedefs = {}
#         if typedefs is not None:
#             self.typedefs.update(typedefs)
#         self.enums = []
#         self.spec = {}
# 
#     def attach_specialization(self, spec):
#         self.spec = spec
# 
#     def remove_specialization(self):
#         self.spec = {}
# 
#     def underlying_type(self, tname):
#         while tname in self.typedefs or tname in self.spec:
#             if tname in self.typedefs:
#                 tname = self.typedefs[tname]
#             else:
#                 tname = self.spec[tname]
#         return tname
# 
#     def get_specialization(self, tname):
#         return self.spec.get(tname, tname)


# Path: pywrap/test/test_cython.py
import os
from pywrap.cython import make_cython_wrapper, load_config
from pywrap.parser import TypeInfo
from nose.tools import (assert_raises_regexp, assert_false, assert_equal,
                        assert_is_not_none)

    assert_false(os.path.exists("missing.hpp.cc"))


def test_missing_modulename():
    assert_raises_regexp(ValueError, "give a module name", make_cython_wrapper,
                         ["test1.hpp", "test2.hpp"], [], None)


def test_load_config():
    configfile = "customconfigasd.py"
    with open(configfile, "w") as f:
        f.write("""
from pywrap.defaultconfig import Config

config = Config()
config.add_decleration("asd")
""")
    try:
        config = load_config(configfile)
        assert_equal(config.additional_declerations[0], "asd")
    finally:
        os.remove(configfile)


def test_load_default_config():
    config = load_config(None)
    assert_is_not_none(config)


def test_missing_config():

    assert_raises_regexp(ValueError, "Configuration file", load_config,