plugins/c_ext/c_ext.py
author etisserant
Tue, 26 Feb 2008 17:14:06 +0100
changeset 115 9ffdac9cf609
parent 106 9810689febb0
child 137 187a4e2412e5
permissions -rw-r--r--
Fixed empty build exception.
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     1
import wx
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     2
import wx.stc as stc
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     3
import os, sys, shutil
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     4
from CppSTC import CppSTC
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     5
from plugger import PlugTemplate
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
     6
import tempfile
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     7
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     8
class _Cfile:
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
     9
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    10
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    11
      <xsd:element name="C_Extension">
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    12
        <xsd:complexType>
86
f0a9d74e3b26 Adding support for the new version of xmlclass
lbessard
parents: 65
diff changeset
    13
          <xsd:attribute name="C_Files" type="xsd:string" use="optional" default="myfile.c"/>
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    14
          <xsd:attribute name="CFLAGS" type="xsd:string" use="required"/>
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    15
          <xsd:attribute name="LDFLAGS" type="xsd:string" use="required"/>
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    16
        </xsd:complexType>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    17
      </xsd:element>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    18
    </xsd:schema>
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    19
    """
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    20
    def __init__(self):
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    21
        self.CheckCFilesExist()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    22
        
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    23
    def CheckCFilesExist(self):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    24
        for CFile in self.CFileNames():
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    25
            if not os.path.isfile(CFile):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    26
                f = open(CFile, 'w')
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    27
                f.write("/*Empty*/")
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    28
                f.close()
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    29
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    30
    def CFileBaseNames(self):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    31
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    32
        Returns list of C files base names, out of C_Extension.C_Files, coma separated list
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    33
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    34
        return map(str.strip,str(self.C_Extension.getC_Files()).split(','))
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    35
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    36
    def CFileName(self, fn):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    37
        return os.path.join(self.PlugPath(),fn)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    38
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    39
    def CFileNames(self):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    40
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    41
        Returns list of full C files paths, out of C_Extension.C_Files, coma separated list
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    42
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    43
        return map(self.CFileName, self.CFileBaseNames())
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    44
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    45
    def SetParamsAttribute(self, path, value, logger):
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    46
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    47
        Take actions if C_Files changed
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    48
        """
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    49
        # Get a C files list before changes
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    50
        oldnames = self.CFileNames()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    51
        # Apply changes
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    52
        res = PlugTemplate.SetParamsAttribute(self, path, value, logger)
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    53
        # If changes was about C files,
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    54
        if path == "C_Extension.C_Files":
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    55
            # Create files if did not exist
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    56
            self.CheckCFilesExist()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    57
            # Get new list
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    58
            newnames = self.CFileNames()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    59
            # Move unused files into trash (temporary directory)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    60
            for oldfile in oldnames:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    61
                if oldfile not in newnames:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    62
                    # define new "trash" name
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    63
                    trashname = os.path.join(tempfile.gettempdir(),os.path.basename(oldfile))
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    64
                    # move the file
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    65
                    shutil.move(oldfile, trashname)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    66
                    # warn user
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    67
                    logger.write_warning("\"%s\" moved to \"%s\"\n"%(oldfile, trashname))
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    68
            return value, False
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    69
        return res
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    70
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    71
    _Views = {}
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    72
    def _OpenView(self, logger):
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    73
        lst = self.CFileBaseNames()
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    74
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    75
        dlg = wx.MultiChoiceDialog( self.GetPlugRoot().AppFrame, 
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 45
diff changeset
    76
                                   "Choose C files to Edit :",
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 45
diff changeset
    77
                                   "Edit", lst)
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    78
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    79
        if (dlg.ShowModal() == wx.ID_OK):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    80
            selections = dlg.GetSelections()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    81
            for selected in [lst[x] for x in selections]:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    82
                if selected not in self._Views:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    83
                    # keep track of selected name as static for later close
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    84
                    def _onclose(evt, sel = selected):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    85
                        self.SaveCView(sel)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    86
                        self._Views.pop(sel)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    87
                        evt.Skip()
47
fd45c291fed0 PLC and plugins compilation with gcc now starts (and fail).
etisserant
parents: 45
diff changeset
    88
                    New_View = wx.Frame(self.GetPlugRoot().AppFrame,-1,selected)
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    89
                    New_View.Bind(wx.EVT_CLOSE, _onclose)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    90
                    ed = CppSTC(New_View, wx.NewId())
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    91
                    ed.SetText(open(self.CFileName(selected)).read())
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    92
                    ed.EmptyUndoBuffer()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    93
                    ed.Colourise(0, -1)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    94
                    ed.SetMarginType(1, stc.STC_MARGIN_NUMBER)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    95
                    ed.SetMarginWidth(1, 25)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    96
                    New_View.ed = ed
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    97
                    New_View.Show()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
    98
                    self._Views[selected] = New_View
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
    99
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   100
        dlg.Destroy()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   101
        
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   102
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   103
    PluginMethods = [
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   104
        {"name" : "Edit C File", 
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   105
         "tooltip" : "Edit C File",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 86
diff changeset
   106
         "method" : "_OpenView"},
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   107
        {"name" : "Import C File", 
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   108
         "tooltip" : "Import C File",
105
434aed8dc58d Added ability to override plugin methods with arbitrary python code (methods.py) when loading plugins
etisserant
parents: 86
diff changeset
   109
         "method" : "_OpenView"}
65
e55d6faee9d1 Adding icons in Beremiz GUI
lbessard
parents: 55
diff changeset
   110
    ]
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   111
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   112
    def SaveCView(self, name):
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   113
        f = open(self.CFileName(name),'w')
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   114
        f.write(self._Views[name].ed.GetText())
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   115
        f.close()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   116
        
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   117
    def OnPlugSave(self):
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   118
        for name in self._Views:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   119
            self.SaveCView(name)
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   120
        return True
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   121
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   122
    def PlugGenerate_C(self, buildpath, locations, logger):
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   123
        """
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   124
        Generate C code
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   125
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   126
        @param locations: List of complete variables locations \
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   127
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   128
            "NAME" : name of the variable (generally "__IW0_1_2" style)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   129
            "DIR" : direction "Q","I" or "M"
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   130
            "SIZE" : size "X", "B", "W", "D", "L"
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   131
            "LOC" : tuple of interger for IEC location (0,1,2,...)
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   132
            }, ...]
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   133
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   134
        """
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   135
        current_location = self.GetCurrentLocation()
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   136
        # define a unique name for the generated C file
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   137
        location_str = "_".join(map(lambda x:str(x), current_location))
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   138
        res = []
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   139
        for CFile in self.CFileBaseNames():
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents: 47
diff changeset
   140
            Gen_Cfile_path = os.path.join(buildpath, "CFile_%s_%s.c"%(location_str, os.path.splitext(CFile)[0]))
45
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   141
            f = open(Gen_Cfile_path,'w')
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   142
            f.write("/* Header generated by Beremiz c_ext plugin */\n")
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   143
            f.write("#include \"iec_std_lib.h\"\n")
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   144
            f.write("#define EXT_C_INIT __init_%s\n"%location_str)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   145
            f.write("#define EXT_C_CLEANUP __init_%s\n"%location_str)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   146
            f.write("#define EXT_C_PUBLISH __init_%s\n"%location_str)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   147
            f.write("#define EXT_C_RETRIVE __init_%s\n"%location_str)
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   148
            for loc in locations:
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   149
                f.write(loc["IEC_TYPE"]+" "+loc["NAME"]+";\n")
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   150
            f.write("/* End of header generated by Beremiz c_ext plugin */\n\n")
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   151
            src_file = open(self.CFileName(CFile),'r')
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   152
            f.write(src_file.read())
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   153
            src_file.close()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   154
            f.close()
00acf2162135 Now, C extension plugin do handle multiple files
etisserant
parents: 31
diff changeset
   155
            res.append((Gen_Cfile_path,str(self.C_Extension.getCFLAGS())))
55
9c26e67c041a Updated plugins PluGenerate_C to conform to plugger.py
etisserant
parents: 49
diff changeset
   156
        return res,str(self.C_Extension.getLDFLAGS()),True
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   157
    
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   158
class RootClass:
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   159
106
9810689febb0 Added plugins creation helpstrings, changed GUI layout (more compact), solved staticbitmap issues on win32, re-designed some icons...
etisserant
parents: 105
diff changeset
   160
    PlugChildsTypes = [("C_File",_Cfile, "C file")]
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   161
    
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   162
    def PlugGenerate_C(self, buildpath, locations, logger):
55
9c26e67c041a Updated plugins PluGenerate_C to conform to plugger.py
etisserant
parents: 49
diff changeset
   163
        return [],"",False
31
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   164
33b38700d0db added basic C Code extention plugin
etisserant
parents:
diff changeset
   165