MSWSP: parse_CColumnGroupArray() etc.
[wireshark-wip.git] / epan / wspython / register-dissector.py
blobc67ae6f7e71498d9d4216d8a6c5b2ec929a83853
1 # register-dissector.py
3 # $Id$
5 # Wireshark Protocol Python Binding
7 # Copyright (c) 2009 by Sebastien Tandel <sebastien [AT] tandel [dot] be>
8 # Copyright (c) 2001 by Gerald Combs <gerald@wireshark.org>
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 import sys
24 import re
25 import os
26 import imp
29 # Build a list of files belonging to a directory and matching a regexp (f.e.
30 # '(?P<plugin>.*)\.py$' )
32 def get_plugin_list(dir, regexp):
33 lDir = os.listdir(dir)
35 lPlugins=[]
36 for sDir in lDir:
37 MatchedObject = re.match(regexp, sDir)
38 if (MatchedObject != None):
39 lPlugins.append(MatchedObject.group("plugin"))
40 return lPlugins
42 #Import the module "name"
43 def plugin_import(name):
44 #if the module was already loaded
45 try:
46 return sys.modules[name]
47 except KeyError:
48 pass
50 r = __import__(name)
51 return r
53 def register_dissectors(wspython_dir, plugins_pers_dir=None):
54 #append dir to be able to import py_lib
55 sys.path.append(wspython_dir)
56 from wspy_libws import get_libws_handle
57 libws = get_libws_handle()
59 dissectors_dirs = [
60 os.path.join(wspython_dir, 'wspy_dissectors'),
61 plugins_pers_dir
64 registered_protocols = []
65 for dissectors_dir in dissectors_dirs:
66 #Check if we have the dissectors directory
67 if not os.path.isdir(dissectors_dir):
68 continue
70 #append dir to be able to import python dissectors
71 sys.path.append(dissectors_dir)
73 #Read all python dissectors
74 dissectors = get_plugin_list(dissectors_dir, "(?P<plugin>.*)\.py$")
76 #For each dissector, register it and put it in the list of registered
77 #protocols
78 for dissector in dissectors:
79 try:
80 d = plugin_import(dissector)
81 registered_protocol = d.register_protocol()
82 if registered_protocol:
83 registered_protocols.append(registered_protocol)
84 except Exception as e:
85 print('register dissector %s exception %s' % (dissector, e))
86 return registered_protocols
88 if False:
89 import linecache
91 # Start tracing when import has finished
92 def tracer(frame, event, arg):
93 if event == "line":
94 lineno = frame.f_lineno
95 filename = frame.f_globals["__file__"]
96 if (filename.endswith(".pyc") or
97 filename.endswith(".pyo")):
98 filename = filename[:-1]
99 name = frame.f_globals["__name__"]
100 line = linecache.getline(filename, lineno)
101 print("%s:%s: %s" % (name, lineno, line.rstrip()))
102 if event == "exception":
103 print("exception", arg)
104 return tracer
106 sys.settrace(tracer)