~ubuntu-branches/ubuntu/raring/wxwidgets2.8/raring

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ed_ipc.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
   </arglist>
38
38
</edipc>
39
39
 
40
 
@example: SESSION_KEY;xml;MSGEND
41
40
@summary: Editra's IPC Library
42
41
 
43
42
"""
44
43
 
45
44
__author__ = "Cody Precord <cprecord@editra.org>"
46
 
__svnid__ = "$Id: ed_ipc.py 63657 2010-03-09 01:45:25Z CJP $"
47
 
__revision__ = "$Revision: 63657 $"
 
45
__svnid__ = "$Id: ed_ipc.py 67991 2011-06-20 23:48:01Z CJP $"
 
46
__revision__ = "$Revision: 67991 $"
48
47
 
49
48
#-----------------------------------------------------------------------------#
50
49
# Imports
56
55
#import select
57
56
 
58
57
# Editra Libs
59
 
import syntax
 
58
import util
 
59
import ed_xml
60
60
import ebmlib
61
61
 
62
62
#-----------------------------------------------------------------------------#
64
64
 
65
65
# Port choosing algorithm ;)
66
66
EDPORT = (10 * int('ed', 16) + sum(ord(x) for x in "itr") + int('a', 16)) * 10
67
 
MSGEND = u"*EDEND*"
 
67
MSGEND = "*EDEND*"
68
68
 
69
69
# Xml Implementation
70
 
EDXML_IPC       = u"edipc"
71
 
EDXML_FILELIST  = u"filelist"
72
 
EDXML_FILE      = u"file"
73
 
EDXML_ARGLIST   = u"arglist"
74
 
EDXML_ARG       = u"arg"
 
70
EDXML_IPC       = "edipc"
 
71
EDXML_FILELIST  = "filelist"
 
72
EDXML_FILE      = "file"
 
73
EDXML_ARGLIST   = "arglist"
 
74
EDXML_ARG       = "arg"
75
75
 
76
76
#-----------------------------------------------------------------------------#
77
77
 
115
115
    @todo: investigate possible security issues
116
116
 
117
117
    """
118
 
    def __init__(self, app, key):
 
118
    def __init__(self, app, key, port=EDPORT):
119
119
        """Create the server thread
120
120
        @param app: Application object the server belongs to
121
121
        @param key: Unique user authentication key (string)
 
122
        @keyword port: TCP port to attempt to connect to
122
123
 
123
124
        """
124
 
        threading.Thread.__init__(self)
 
125
        super(EdIpcServer, self).__init__()
125
126
 
126
127
        # Attributes
127
128
        self._exit = False
131
132
 
132
133
        # Setup
133
134
        ## Try new ports till we find one that we can use
134
 
        global EDPORT
135
135
        while True:
136
136
            try:
137
 
                self.socket.bind(('127.0.0.1', EDPORT))
 
137
                self.socket.bind(('127.0.0.1', port))
138
138
                break
139
139
            except:
140
 
                EDPORT += 1
 
140
                port += 1
141
141
 
 
142
        global EDPORT
 
143
        EDPORT = port
142
144
        self.socket.listen(5)
143
145
 
144
146
    def Shutdown(self):
172
174
                # If message key is correct and the message is ended, process
173
175
                # the input and dispatch to the app.
174
176
                if recieved.startswith(self.__key) and recieved.endswith(MSGEND):
175
 
                    recieved = recieved.replace(self.__key, u'', 1)
 
177
                    # Strip the key
 
178
                    recieved = recieved.replace(self.__key, '', 1)
 
179
                    # Strip the end token
 
180
                    xmlstr = recieved.rstrip(MSGEND).strip(";")
176
181
 
177
182
                    # Parse the xml
178
183
                    exml = IPCCommand()
179
184
                    try:
180
 
                        xmlstr = recieved.rstrip(MSGEND).strip(u";")
181
 
                        exml.LoadFromString(xmlstr)
 
185
                        # Well formed xml must be utf-8 string not unicode
 
186
                        xmlstr = xmlstr.encode('utf-8')
 
187
                        exml = IPCCommand.parse(xmlstr)
182
188
                    except Exception, msg:
183
 
                        # ignore parsing errors
 
189
                        # Log and ignore parsing errors
 
190
                        logmsg = "[ed_ipc][err] Parsing failed: %s\n" % msg
 
191
                        xmlstr = xmlstr.replace('\n', '').strip()
 
192
                        logmsg += "Bad xml was: %s" % repr(xmlstr)
 
193
                        util.Log(logmsg)
184
194
                        continue
185
195
 
186
196
                    evt = IpcServerEvent(edEVT_COMMAND_RECV, wx.ID_ANY, exml)
206
216
    @return: bool
207
217
 
208
218
    """
209
 
    assert isinstance(xmlobj, syntax.EditraXml), "SendCommands expects an xml object"
 
219
    assert isinstance(xmlobj, ed_xml.EdXml), "SendCommands expects an xml object"
210
220
 
211
221
    # Build the edipc protocol msg
212
222
    cmds = list()
220
230
        client.connect(('127.0.0.1', EDPORT))
221
231
 
222
232
        # Server expects commands delimited by ;
223
 
        message = u";".join(cmds)
 
233
        message = ";".join(cmds)
224
234
        client.send(message)
225
235
        client.shutdown(socket.SHUT_RDWR)
226
236
        client.close()
232
242
#-----------------------------------------------------------------------------#
233
243
# Command Serialization
234
244
 
235
 
class IPCCommand(syntax.EditraXml):
236
 
    """Xml packet used for sending data to remote process through ipc"""
237
 
    def __init__(self):
238
 
        syntax.EditraXml.__init__(self)
239
 
 
240
 
        # Attributes
241
 
        self._files = IPCFileList()
242
 
        self._args = IPCArgList()
243
 
 
244
 
        # Setup
245
 
        self.SetName(EDXML_IPC)
246
 
        self.RegisterHandler(self._files)
247
 
        self.RegisterHandler(self._args)
248
 
 
249
 
    #---- Public Api ----#
250
 
 
251
 
    def GetArgs(self):
252
 
        """Get the list of paths
253
 
        @return: list of tuples
254
 
 
255
 
        """
256
 
        return self._args.GetArgs()
257
 
 
258
 
    def SetArgs(self, args):
259
 
        """Set the files
260
 
        @param flist: list of strings
261
 
 
262
 
        """
263
 
        self._args.SetArgs(args)
264
 
 
265
 
    def GetFiles(self):
266
 
        """Get the list of paths
267
 
        @return: list of strings
268
 
 
269
 
        """
270
 
        return self._files.GetFiles()
271
 
 
272
 
    def SetFiles(self, flist):
273
 
        """Set the files
274
 
        @param flist: list of strings
275
 
 
276
 
        """
277
 
        self._files.SetFiles(flist)
278
 
 
279
 
class IPCFileList(syntax.EditraXml):
 
245
class IPCFile(ed_xml.EdXml):
280
246
    """Xml object for holding the list of files
281
 
 
282
 
    <filelist>
283
 
       <file value="/path/to/file"/>
284
 
    </filelist>
 
247
    <file value="/path/to/file"/>
285
248
 
286
249
    """
287
 
    def __init__(self):
288
 
        syntax.EditraXml.__init__(self)
289
 
 
290
 
        # Attributes
291
 
        self._files = list()
292
 
 
293
 
        # Setup
294
 
        self.SetName(EDXML_FILELIST)
295
 
 
296
 
    #---- Xml Implementation ----#
297
 
 
298
 
    def startElement(self, name, attrs):
299
 
        """Collect all the file elements"""
300
 
        if name == EDXML_FILE:
301
 
            fname = attrs.get(syntax.EXML_VALUE, None)
302
 
            if fname is not None:
303
 
                self._files.append(fname)
304
 
 
305
 
    def GetSubElements(self):
306
 
        """Get the objects subelements"""
307
 
        xmlstr = u""
308
 
        tmp = u"<%s %s=\"" % (EDXML_FILE, syntax.EXML_VALUE)
309
 
        tmp += u"%s\"/>"
310
 
        for fname in self._files:
311
 
            if not ebmlib.IsUnicode(fname):
312
 
                fname = fname.decode(sys.getfilesystemencoding())
313
 
            xmlstr += tmp % fname
314
 
        return xmlstr
315
 
 
316
 
    #--- public api ----#
317
 
 
318
 
    def GetFiles(self):
319
 
        """Get the list of paths
320
 
        @return: list of strings
321
 
 
322
 
        """
323
 
        return self._files
324
 
 
325
 
    def SetFiles(self, flist):
326
 
        """Set the list of files
327
 
        @param flist: list of strings
328
 
 
329
 
        """
330
 
        self._files = flist
331
 
 
332
 
class IPCArgList(syntax.EditraXml):
 
250
    class meta:
 
251
        tagname = EDXML_FILE
 
252
    value = ed_xml.String(required=True)
 
253
 
 
254
class IPCArg(ed_xml.EdXml):
333
255
    """Xml object for holding the list of args
334
 
 
335
 
    <arglist>
336
256
       <arg name="test" value="x"/>
337
 
    </arglist>
338
257
 
339
258
    """
340
 
    def __init__(self):
341
 
        syntax.EditraXml.__init__(self)
342
 
 
343
 
        # Attributes
344
 
        self._args = list()
345
 
 
346
 
        # Setup
347
 
        self.SetName(EDXML_ARGLIST)
348
 
 
349
 
    #---- Xml Implementation ----#
350
 
 
351
 
    def startElement(self, name, attrs):
352
 
        """Collect all the file elements"""
353
 
        if name == EDXML_ARG:
354
 
            arg = attrs.get(syntax.EXML_NAME, None)
355
 
            val = attrs.get(syntax.EXML_VALUE, u'')
356
 
            if not val.isdigit():
357
 
                val = -1
358
 
            else:
359
 
                val = int(val)
360
 
            if arg is not None:
361
 
                self._args.append((arg, val))
362
 
 
363
 
    def GetSubElements(self):
364
 
        """Get the objects sub-elements"""
365
 
        xmlstr = u""
366
 
        tmp = u"<%s %s=\"" % (EDXML_ARG, syntax.EXML_NAME)
367
 
        tmp += u"%s\" "
368
 
        tmp += "%s=\"" % syntax.EXML_VALUE
369
 
        tmp += "%s\"/>"
370
 
        for argval in self._args:
371
 
            xmlstr += tmp % argval
372
 
        return xmlstr
373
 
 
374
 
    #--- public api ----#
375
 
 
376
 
    def GetArgs(self):
377
 
        """Get the list of arguments (command line switches)
378
 
        @return: list of tuples
379
 
 
380
 
        """
381
 
        return self._args
382
 
 
383
 
    def SetArgs(self, args):
384
 
        """Set the list of files
385
 
        @param flist: list of tuples
386
 
 
387
 
        """
388
 
        self._args = args
389
 
    
 
 
b'\\ No newline at end of file'
 
259
    class meta:
 
260
        tagname = EDXML_ARG
 
261
    name = ed_xml.String(required=True)
 
262
    value = ed_xml.String(required=True)
 
263
 
 
264
class IPCCommand(ed_xml.EdXml):
 
265
    """IPC XML Command"""
 
266
    class meta:
 
267
        tagname = EDXML_IPC
 
268
    filelist = ed_xml.List(ed_xml.Model(IPCFile))
 
269
    arglist = ed_xml.List(ed_xml.Model(IPCArg))