plugins/c_ext/c_ext.py
author etisserant
Tue, 11 Sep 2007 16:11:15 +0200
changeset 31 33b38700d0db
child 45 00acf2162135
permissions -rw-r--r--
added basic C Code extention plugin
import wx
import wx.stc as stc
import os, sys, shutil
from CppSTC import CppSTC
from plugger import PlugTemplate

class _Cfile:
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="C_File">
        <xsd:complexType>
          <xsd:attribute name="FileName" type="xsd:string" use="required" default="myfile.c"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    """
    def __init__(self):
        if not os.path.isfile(self.CFileName()):
            f = open(self.CFileName(), 'w')
            f.write("/*Empty*/")

    def CFileName(self):
        return os.path.join(self.PlugPath(),self.C_File.getFileName())

    def SetParamsAttribute(self, path, value, logger):
        oldname = self.CFileName()
        res = PlugTemplate.SetParamsAttribute(self, path, value, logger)
        if path == "C_File.FileName":
            shutil.move(oldname, self.CFileName())
            logger.write("\"%s\" renamed \"%s\"\n"%(oldname, self.CFileName()))
            return value, False
        return res

    _View = None
    def _OpenView(self, logger):
        if not self._View:
            def _onclose(evt):
                self.OnPlugSave()
                self._View = None
                evt.Skip()
            self._View = wx.Frame(self.GetPlugRoot().AppFrame,-1)
            self._View.Bind(wx.EVT_CLOSE, _onclose)
            ed = CppSTC(self._View, wx.NewId())
            ed.SetText(open(self.CFileName()).read())
            ed.EmptyUndoBuffer()
            ed.Colourise(0, -1)
            ed.SetMarginType(1, stc.STC_MARGIN_NUMBER)
            ed.SetMarginWidth(1, 25)
            self._View.ed = ed

            self._View.Show()

    PluginMethods = [("Edit C File",_OpenView)]

    def OnPlugSave(self):
        if self._View:
            f = open(self.CFileName(),'w')
            f.write(self._View.ed.GetText())
        return True

    def PlugGenerate_C(self, buildpath, locations, logger):
        """
        Generate C code
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
        @param locations: List of complete variables locations \
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
            "NAME" : name of the variable (generally "__IW0_1_2" style)
            "DIR" : direction "Q","I" or "M"
            "SIZE" : size "X", "B", "W", "D", "L"
            "LOC" : tuple of interger for IEC location (0,1,2,...)
            }, ...]
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
        """
        current_location = self.GetCurrentLocation()
        # define a unique name for the generated C file
        prefix = "_".join(map(lambda x:str(x), current_location))
        Gen_Cfile_path = os.path.join(buildpath, prefix + "_CFile.c" )
        f = open(Gen_Cfile_path,'w')
        f.write("/* Header generated by Beremiz c_ext plugin */\n")
        f.write("#include \"iec_std_lib.h\"\n")
        for loc in locations:
            f.write(loc["IEC_TYPE"]+" "+loc["NAME"]+";\n")
        f.write("/* End of header generated by Beremiz c_ext plugin */\n")
        
        return [(Gen_Cfile_path,"")],""
    
class RootClass:

    PlugChildsTypes = [("C_File",_Cfile)]
    
    def PlugGenerate_C(self, buildpath, locations, logger):
        return [],""