plugins/c_ext/c_ext.py
changeset 31 33b38700d0db
child 45 00acf2162135
equal deleted inserted replaced
30:ea685658b388 31:33b38700d0db
       
     1 import wx
       
     2 import wx.stc as stc
       
     3 import os, sys, shutil
       
     4 from CppSTC import CppSTC
       
     5 from plugger import PlugTemplate
       
     6 
       
     7 class _Cfile:
       
     8     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
       
     9     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       
    10       <xsd:element name="C_File">
       
    11         <xsd:complexType>
       
    12           <xsd:attribute name="FileName" type="xsd:string" use="required" default="myfile.c"/>
       
    13         </xsd:complexType>
       
    14       </xsd:element>
       
    15     </xsd:schema>
       
    16     """
       
    17     def __init__(self):
       
    18         if not os.path.isfile(self.CFileName()):
       
    19             f = open(self.CFileName(), 'w')
       
    20             f.write("/*Empty*/")
       
    21 
       
    22     def CFileName(self):
       
    23         return os.path.join(self.PlugPath(),self.C_File.getFileName())
       
    24 
       
    25     def SetParamsAttribute(self, path, value, logger):
       
    26         oldname = self.CFileName()
       
    27         res = PlugTemplate.SetParamsAttribute(self, path, value, logger)
       
    28         if path == "C_File.FileName":
       
    29             shutil.move(oldname, self.CFileName())
       
    30             logger.write("\"%s\" renamed \"%s\"\n"%(oldname, self.CFileName()))
       
    31             return value, False
       
    32         return res
       
    33 
       
    34     _View = None
       
    35     def _OpenView(self, logger):
       
    36         if not self._View:
       
    37             def _onclose(evt):
       
    38                 self.OnPlugSave()
       
    39                 self._View = None
       
    40                 evt.Skip()
       
    41             self._View = wx.Frame(self.GetPlugRoot().AppFrame,-1)
       
    42             self._View.Bind(wx.EVT_CLOSE, _onclose)
       
    43             ed = CppSTC(self._View, wx.NewId())
       
    44             ed.SetText(open(self.CFileName()).read())
       
    45             ed.EmptyUndoBuffer()
       
    46             ed.Colourise(0, -1)
       
    47             ed.SetMarginType(1, stc.STC_MARGIN_NUMBER)
       
    48             ed.SetMarginWidth(1, 25)
       
    49             self._View.ed = ed
       
    50 
       
    51             self._View.Show()
       
    52 
       
    53     PluginMethods = [("Edit C File",_OpenView)]
       
    54 
       
    55     def OnPlugSave(self):
       
    56         if self._View:
       
    57             f = open(self.CFileName(),'w')
       
    58             f.write(self._View.ed.GetText())
       
    59         return True
       
    60 
       
    61     def PlugGenerate_C(self, buildpath, locations, logger):
       
    62         """
       
    63         Generate C code
       
    64         @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
       
    65         @param locations: List of complete variables locations \
       
    66             [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
       
    67             "NAME" : name of the variable (generally "__IW0_1_2" style)
       
    68             "DIR" : direction "Q","I" or "M"
       
    69             "SIZE" : size "X", "B", "W", "D", "L"
       
    70             "LOC" : tuple of interger for IEC location (0,1,2,...)
       
    71             }, ...]
       
    72         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
       
    73         """
       
    74         current_location = self.GetCurrentLocation()
       
    75         # define a unique name for the generated C file
       
    76         prefix = "_".join(map(lambda x:str(x), current_location))
       
    77         Gen_Cfile_path = os.path.join(buildpath, prefix + "_CFile.c" )
       
    78         f = open(Gen_Cfile_path,'w')
       
    79         f.write("/* Header generated by Beremiz c_ext plugin */\n")
       
    80         f.write("#include \"iec_std_lib.h\"\n")
       
    81         for loc in locations:
       
    82             f.write(loc["IEC_TYPE"]+" "+loc["NAME"]+";\n")
       
    83         f.write("/* End of header generated by Beremiz c_ext plugin */\n")
       
    84         
       
    85         return [(Gen_Cfile_path,"")],""
       
    86     
       
    87 class RootClass:
       
    88 
       
    89     PlugChildsTypes = [("C_File",_Cfile)]
       
    90     
       
    91     def PlugGenerate_C(self, buildpath, locations, logger):
       
    92         return [],""
       
    93 
       
    94