-
Notifications
You must be signed in to change notification settings - Fork 2
/
aimlViewer.py
executable file
·85 lines (65 loc) · 2.6 KB
/
aimlViewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# aimlViewer.py
# a basic aiml file viewer
from AIMLParser import AIMLParser
import argparse
import wx
import wx.dataview as dv
from wx.lib.mixins.listctrl import ColumnSorterMixin
import sys
import pprint
import os
FRAME_WIDTH = 640
FRAME_HEIGHT = 480
class Viewer(wx.Frame):
def __init__(self, parent, id, title, data):
wx.Frame.__init__(self, parent, id, title, size=(FRAME_WIDTH, FRAME_HEIGHT))
panel = wx.Panel(self, -1)
# create the listctrl
self.dvlc = dvlc = self.createDataView(panel, data)
# Set the layout so the listctrl fills the panel
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(dvlc, proportion=1, flag=wx.EXPAND | wx.ALL, border=0)
panel.SetSizer(hbox)
# Bind Events
self.Bind(wx.EVT_SIZE, self.OnSize)
# Show the frame
self.Centre()
self.Show(True)
def createDataView(self, parent, data):
dvlc = dv.DataViewListCtrl(parent,style=wx.BORDER_THEME
| dv.DV_ROW_LINES # nice alternating bg colors
| dv.DV_VERT_RULES
| dv.DV_MULTIPLE)
# Give it some columns.
dvlc.AppendTextColumn('Pattern', width=self.computeColumnWidth(), mode=dv.DATAVIEW_CELL_EDITABLE)
dvlc.AppendTextColumn('Response', width=self.computeColumnWidth(), mode=dv.DATAVIEW_CELL_EDITABLE)
# Load the data. Each item (row) is added as a sequence of values
# whose order matches the columns
for itemvalues in data:
dvlc.AppendItem(itemvalues)
return dvlc
def computeColumnWidth(self):
size = self.GetSize()
return round(size.x*0.5) - 5
def OnSize(self, event):
for i in range(0,2):
col = self.dvlc.GetColumn(i)
col.SetWidth(self.computeColumnWidth())
event.Skip()
if __name__ == '__main__':
# Grab file name from the command line
parser = argparse.ArgumentParser(description="""A simple AIML file viewer""")
parser.add_argument("file",help="path to AIML file (or directory of files) to load")
args = parser.parse_args()
# Load AIML file and print categoryList
print "Loading "+args.file+"..."
AP = AIMLParser(args.file)
rulesTuples = AP.toCategoryList()
rules = [list(rule) for rule in rulesTuples] # convert to nested lists for dataview
print str(len(rules)) + " rules loaded:"
pprint.pprint(rules)
app = wx.App()
title = "aimlViewer: %s" % os.path.abspath(args.file)
Viewer(None, -1, title, data=rules)
app.MainLoop()