"""Generate Cython interface for C code for the fast gradient method."""

import os

from muaompc._ldt.codegen.solver.fgm.codegen import CCodeGenerator as CCG

class CythonCodeGenerator(CCG, object):

    """Generate Cython code to interface with C code of fast gradient method.

    This class overrides the following methods:
    generate_code: generates cython specific code on its own directory
    """

    def __init__(self, ccg):
        """Assign to self prefix, path attributes from the given C code.

        This depends on C-code already being generated by C-code
        generation class. This creates the interface to that specific code.

        :param ccg: an instance of the C code generator class for which
        a Cython interface is to be created
        :type ccg: ompc._ldt.codegen.solver.fgm.codegen.CCodeGenerator
        """
        self.prefix = ccg.prefix
        self.path = ccg.path

    def generate_code(self):
        self._make_destination_dir_tree()
        self._generate_cython_code()

    def _make_destination_dir_tree(self):
        try:
            os.mkdir(os.path.join(self.path['dest'], 'cython'))
        except OSError:
            pass

    def _generate_cython_code(self):
        self._replace_prefix('fgmsetup.py', srcdir='cython')
        self._replace_prefix('fgm.pyx', srcdir='cython')
        self._replace_prefix('Cfgm.pxd', srcdir='cython')

