# Path: kotori/daq/intercom/pyclibrary_ext/backend_ctypes.py
# def monkeypatch_pyclibrary_ctypes_struct():
#     pyclibrary.backends.ctypes.CTypesCLibrary._get_struct = _get_struct
# 
# Path: kotori/util/common.py
# def slm(message):
#     """sanitize log message"""
#     return str(message).replace('{', '{{').replace('}', '}}')


import os
import re
from collections import OrderedDict
from cornice.util import to_list
from pprint import pprint
from binascii import hexlify
from tabulate import tabulate
from appdirs import user_cache_dir
from ctypes import c_uint8, c_uint16, c_uint32, c_int8, c_int16, c_int32
from pyclibrary.c_parser import CParser
from pyclibrary import CLibrary, auto_init
from pyclibrary.utils import add_header_locations, add_library_locations
from sympy.core.sympify import sympify
from twisted.logger import Logger
from kotori.daq.intercom.pyclibrary_ext.c_parser import CParserEnhanced
from kotori.daq.intercom.pyclibrary_ext.backend_ctypes import monkeypatch_pyclibrary_ctypes_struct
from kotori.util.common import slm

    @classmethod
    def from_header(cls, include_path=None, header_files=None):
        cache_dir = user_cache_dir('lst', 'kotori')
        if not os.path.isdir(cache_dir): os.makedirs(cache_dir)
        library = LibraryAdapter(
            header_files, cls.compile(include_path, header_files),
            include_path=include_path, library_path=include_path,
            cache_path=cache_dir)
        return library

    @classmethod
    def compile(cls, include_path, header_files):

        # compute list of source files (.h) with absolute paths
        source_files = [os.path.join(include_path, header_file) for header_file in header_files]
        source_files = cls.augment_sources(source_files)
        source_files = ' '.join(source_files)

        # compute absolute path to library (.so) file
        library_file = header_files[0].rstrip('.h') + '.so'
        library_file = os.path.join(include_path, library_file)

        # assemble compiler command
        compiler = cls.find_compiler()
        if not compiler:
            raise ValueError('Could not find compiler')
        command = '{compiler} -std=c++11 -shared -fPIC -lm -o {library_file} {source_files}'.format(**locals())

        # run compile command

# Path: kotori/daq/intercom/pyclibrary_ext/backend_ctypes.py
# def monkeypatch_pyclibrary_ctypes_struct():
#     pyclibrary.backends.ctypes.CTypesCLibrary._get_struct = _get_struct
# 
# Path: kotori/util/common.py
# def slm(message):
#     """sanitize log message"""
#     return str(message).replace('{', '{{').replace('}', '}}')


# Path: kotori/daq/intercom/c.py
import os
import re
from collections import OrderedDict
from cornice.util import to_list
from pprint import pprint
from binascii import hexlify
from tabulate import tabulate
from appdirs import user_cache_dir
from ctypes import c_uint8, c_uint16, c_uint32, c_int8, c_int16, c_int32
from pyclibrary.c_parser import CParser
from pyclibrary import CLibrary, auto_init
from pyclibrary.utils import add_header_locations, add_library_locations
from sympy.core.sympify import sympify
from twisted.logger import Logger
from kotori.daq.intercom.pyclibrary_ext.c_parser import CParserEnhanced
from kotori.daq.intercom.pyclibrary_ext.backend_ctypes import monkeypatch_pyclibrary_ctypes_struct
from kotori.util.common import slm


    @classmethod
    def from_header(cls, include_path=None, header_files=None):
        cache_dir = user_cache_dir('lst', 'kotori')
        if not os.path.isdir(cache_dir): os.makedirs(cache_dir)
        library = LibraryAdapter(
            header_files, cls.compile(include_path, header_files),
            include_path=include_path, library_path=include_path,
            cache_path=cache_dir)
        return library

    @classmethod
    def compile(cls, include_path, header_files):

        # compute list of source files (.h) with absolute paths
        source_files = [os.path.join(include_path, header_file) for header_file in header_files]
        source_files = cls.augment_sources(source_files)
        source_files = ' '.join(source_files)

        # compute absolute path to library (.so) file
        library_file = header_files[0].rstrip('.h') + '.so'
        library_file = os.path.join(include_path, library_file)

        # assemble compiler command
        compiler = cls.find_compiler()
        if not compiler:
            raise ValueError('Could not find compiler')
        command = '{compiler} -std=c++11 -shared -fPIC -lm -o {library_file} {source_files}'.format(**locals())

        # run compile command

        logger.info(slm('Compiling: {}'.format(command)))