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('\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)