[Pythonmac-SIG] design questions: new hierarchy/tree class?

Henning Hraban Ramm hraban at fiee.net
Sun Sep 16 19:25:47 CEST 2007


Dear gang,
I don't want to re-invent the wheel, but I couldn't find what I intend
to write...

Everyone needs classes for hierarchical data - e.g. for directory
trees but also for GUI menu hierarchies or GUI TreeControls - and
sometimes you've to generate some GUI tree from a dir tree, which
itself can be local or remote (e.g. FTP). Further I need to derive
trees from data in some SQL database.
Thus I look for a general solution for hierarchical objects.
Please discuss and hint if there are ready solutions that I could adapt.

[Use case #1: CD cover generator for backups]
The cover of a backup CD (for internal archive or for a customer)
should depict a tree of some of the contents
- read in the file system of a CD/DVD
- display it in a wx tree view (the user decides which directories to expand)
- generate graphical output format (e.g. TeX, EPS, SVG)

[Use case #2: menu for a website]
The menu should be changable online for the site admin.
- read data from MySQL via a django model
- present it as HTML

[Use case #3: virtual directory on a website]
Users should get a look at some FTP directories that they can't access
directly, the view depends from their permissions.
- read a FTP tree
- cache it in a database
- present it filtered as HTML

[TREE ITEM CLASS]

My approach would be to put most logic in the tree item like this:

class TreeItem(object):
	parent = None # reference or None for root
	children = [] # list defines order
	id = None # simply integer ids?

Is it enough to define the neighbours of objects through their order
in their parent's children?

I'd prefer if the item only knows its parent and its children, but
doesn't store its absolute path (position in the hierarchy) to
simplify reordering - if you move a directory, you would only change
the parent reference and the parents' childrens list, not every single
entry.

I don't care about arbitrary objects in the tree; I'd simply derive
several specialized types for my use cases from a base class. Or do
you think that a mixin or decorator approach would be better? (Never
did that yet.)


[TREE CLASS]

For initialization I'd like to hand some URI to the tree like those:

MUST:
file:///Users/user/Documents/workspace --> directory tree
ftp://user:password@ftp.domain.tld/mypath --> ftpTree directory tree
mysql://user:password@localhost/database/table?id=123 --> tree from
linked database rows
sqlite:///path/to/database/file.ext/table?name=this --> like mysql
MORE IDEAS:
wxglade://myapp.xml/anything --> get GUI objects hierarchy
wxmenu://application/ --> grab menu tree from app somehow?
http://www.domain.tld/index.html --> tree of linked html documents (?)
(In the case of HTML documents we'd need to specify if we want the
inner structure or a sitemap, but that's not my first concern ;-)

The tree object would decide which Item type to choose depending on
the service declaration - so one could simply plug in another service
handler for a new item type.

For those URIs I need a general URI parser; urlparse can't do that; see also
http://mail.python.org/pipermail/python-dev/2005-November/058359.html
(Mike Brown said he implemented that in 4Suite's Ft.Lib.Uri; didn't
look at that yet.)

It should be easy to convert such a tree to XML (pickle everything
that doesn't fit else...) and then you could use XQuery on it - so I
need not to implement an own query syntax. Conversion to nested dicts
and JSON should be as easy.


I'd be glad about some useful hints!

Greetlings,
Hraban


More information about the Pythonmac-SIG mailing list