[Twisted-Python] distrib.UserDirectory improvements
Paul Boehm
typo at soniq.net
Thu Jun 12 15:43:15 MDT 2003
hi,
moshez suggested me to extend distrib.UserDirectory for my needs.
attached is what i am using right now.
i hope to integrate this into distrib.py for cvs, later.
please give me feedback!
paul
-------------- next part --------------
from twisted.web import widgets, static
from twisted.web.distrib import *
import os
if (os.sys.platform != 'win32') and (os.name != 'java'):
import pwd
# Lines below this, can be merged into distrib.py
def createUserPath(homedir, username, dirname, subdir=''):
if dirname[0:1] == '~':
userpath = os.path.join(homedir, dirname[1:])
else:
userpath = os.path.join(dirname, username, subdir)
return userpath
class UserDirectory(widgets.Gadget, widgets.StreamWidget):
def __init__(self, userDirName='~/public_html',
userSocketName='~/.twistd-web-pb', pbextension='.twistd'):
widgets.Gadget.__init__(self)
self.userDirName = userDirName
self.userSocketName = userSocketName
self.pbextension = pbextension
def stream(self, write, request):
write('<UL>\n')
for user in pwd.getpwall():
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell \
= user
realname = string.split(pw_gecos,',')[0]
if not realname:
realname = pw_name
fmtStr = '<LI><A HREF="%s/">%s (%s)</a>\n'
userdirpath = createUserPath(pw_dir, pw_name, self.userDirName)
if os.path.exists(userdirpath):
write(fmtStr% (pw_name,realname,'file'))
twistdsock = createUserPath(pw_dir, pw_name,
self.userSocketName, "twistd-web-pb")
if os.path.exists(twistdsock):
linknm = '%s%s' % (pw_name, self.pbextension)
write(fmtStr % (linknm, realname, self.pbextension))
write('</UL>\n')
def getWidget(self, chnam, request):
print "x"
if chnam[-len(self.pbextension):] == self.pbextension:
username = chnam[:-len(self.pbextension)]
sub = 1
else:
username = chnam
sub = 0
try:
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell \
= pwd.getpwnam(username)
except KeyError:
return error.NoResource()
if sub:
userpath = createUserPath(pw_dir, username, self.userSocketName,
"twistd-web-pb")
rs = ResourceSubscription('unix', userpath)
self.putChild(chnam, rs)
return rs
else:
userpath = createUserPath(pw_dir, username, self.userDirName)
return static.File(userpath)
More information about the Twisted-Python
mailing list