here's something I did recently as a 'proof of concept', it reads a
directory containing a number of config files, each file has a set of
variables in a tree-like format eg:
-------config sample -------------------
\Grib\CharsPerLine=120
\Grib\FixedPositions=1
\Grib\WindGridThinning=18
\Grib\TextGridThinning=18
\Grib\GribName\1=BrcWfs
\Grib\GribName\2=KWBC2124
\Grib\GribName\3=KWBC2526
\Grib\GribName\4=KWBC50
\Grib\GribName\5=KWBCWafs
\Grib\GribDescription\BrcWfs=Bracknell WAFC
\Grib\GribDescription\KWBCWfs=Washington WAFC
\Grib\GribDescription\KWBC2124=Washington FOS1
\Grib\GribDescription\KWBC2526=Washington FOS2
\Grib\GribDescription\KWBC50=Washington FOS3
\Grib\GribDescription\KWBCWafs=Washington WAFC
---------end config sample --------------------
It uses recursive functions to both build the data structure and
populate the tree. It was a hour or so's work, so be careful out there.
It should give a few pointers though.
#####################################################3
#!/usr/bin/env python2.3
"""
parses spec files and pack into a tree
"""
import os
import wx
class ConfRegistry:
"""
Contains a hierarchy of parsed conf files and their values.
"""
def __init__(self):
self.confDict = {}
def makeConfDict(self, src='./config/'):
"""
makes and returns the configuration dict
"""
flist = self.getConfFiles(src)
self.parseFiles(flist, src)
return self.confDict
def getConfDict(self):
"""
returns the current configuration dict
"""
return self.confDict
def parseFiles(self, flist, src):
"""
parse the files for variable keys
"""
for filename in flist: # pack each file's config variables in
his own dict
if not self.confDict.has_key(filename):
self.confDict[filename] = {}
f = open(src + filename + '.cfg', 'r')
lines = f.readlines()
for line in lines:
line = line.strip()
if len(line) > 0:
try:
if line[0] == '\\' and line.rfind('=') > 0:
vars = line.split('=')
keys = vars[0].split('\\')
keys.remove('')
value = vars[1]
last =
self.packDict(self.confDict[filename], keys[:-1])
last[keys[-1]] =
value
except IndexError:
continue
def packDict(self, dict, keys):
"""
packs the given keys into the configuration, if a dictionary is
found with
the given key, return it.
"""
if keys == :
return dict
for key in keys:
if not dict.has_key(key):
dict[key] = {}
return self.packDict(dict[key], keys[1:])
def getConfFiles(self, src):
"""
returs a list of spec files
"""
flist =
names = os.listdir(src)
for name in names:
try:
nm, tp = name.split('.')
if tp == 'cfg':
flist.append(nm)
except ValueError:
pass
flist.sort()
return flist
class TreeFrame(wx.Frame):
"""
controlls the viewpanel
"""
def __init__(self, parent):
wx.Frame.__init__(self,
parent,
-1,
"APX Registry Editor",
wx.DefaultPosition,
style=wx.DEFAULT_FRAME_STYLE)
self.rootLabel = 'APX'
registry = ConfRegistry()
self.regDict = registry.makeConfDict()
self.tree = wx.TreeCtrl(self,
-1,
style=wx.TR_HAS_BUTTONS|wx.TR_EDIT_LABELS|
wx.TR_HAS_VARIABLE_ROW_HEIGHT|wx.SUNKEN_BORDER)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.tree, 1, wx.GROW|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
self.buildTree(self.regDict)
def buildTree(self, data):
"""
Build the value registry tree
"""
root = self.tree.AddRoot(self.rootLabel)
#self.tree.SetPyData(root, (self.rootLabel, None))
self.branchBuilder(root, data)
self.tree.Expand(root)
def branchBuilder(self, root, data):
"""
adds sub branches to the tree nodes
"""
if type(data) == type({}):
for key in data.keys():
if type(data[key]) == type({}):
r = self.tree.AppendItem(root, key)
self.branchBuilder(r, data[key])
elif type(data[key]) == type(""):
r = self.tree.AppendItem(root, key)
self.tree.AppendItem(r, data[key])
class ConfTree(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
panel = TreeFrame(None)
panel.Show()
return 1
app = ConfTree(0)
app.MainLoop()
################################################################3
regards
ยทยทยท
On Fri, 2003-12-05 at 16:13, Lukas Meyer wrote:
Hello,
I want to build with wxTreeCtrl a tree that shows the entries of an ldap
tree. I wrote a funktion that reads ldap data in a dict. So I want to
display that dict with wxTreeCtrl recursively. The dict is constructed
like. L[dc=us], L[dc=us][dc=domain] and so on.
Does anyone can give me a hint on programming anything as described
above? or do i have to change my function so a list or anything else is
beiing constructed?
best regards
Lukas
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
--
Thys Meintjes
BEng Electronic (UP), MEng Bio (UP)
Intrepid Investigator of Interesting Things
+27 82 3764 602