such.. this makes a new addObject that addContainer and addItem both call.. addObject is internal use only... push down the isdir and isfile stuff into defFS function... This means that we now have a chance to install custom directory handlers ala DVD VIDEO_TS handler! whoot! [git-p4: depot-paths = "//depot/": change = 803]v0.3
| @@ -39,16 +39,22 @@ class ContentDirectoryControl(UPnPPublisher, dict): | |||||
| self.nextID += 1 | self.nextID += 1 | ||||
| return ret | return ret | ||||
| def addContainer(self, parent, title, klass = Container, **kwargs): | |||||
| ret = self.addItem(parent, klass, title, **kwargs) | |||||
| def addContainer(self, parent, title, klass = Container, *args, **kwargs): | |||||
| ret = self.addObject(parent, klass, title, *args, **kwargs) | |||||
| self.children[ret] = self[ret] | self.children[ret] = self[ret] | ||||
| return ret | return ret | ||||
| def addItem(self, parent, klass, *args, **kwargs): | |||||
| '''Takes an optional arg, content, which is a twisted.web.resource.Resource that this item provides.''' | |||||
| def addItem(self, parent, klass, title, *args, **kwargs): | |||||
| if issubclass(klass, Container): | |||||
| return self.addContainer(parent, title, klass, *args, **kwargs) | |||||
| else: | |||||
| return self.addObject(parent, klass, title, *args, **kwargs) | |||||
| def addObject(self, parent, klass, title, *args, **kwargs): | |||||
| '''If the generated object (by klass) has an attribute content, it is installed into the web server.''' | |||||
| assert isinstance(self[parent], Container) | assert isinstance(self[parent], Container) | ||||
| nid = self.getnextID() | nid = self.getnextID() | ||||
| i = klass(self, nid, parent, *args, **kwargs) | |||||
| i = klass(self, nid, parent, title, *args, **kwargs) | |||||
| if hasattr(i, 'content'): | if hasattr(i, 'content'): | ||||
| self.webbase.putChild(nid, i.content) | self.webbase.putChild(nid, i.content) | ||||
| self.children[parent].append(i) | self.children[parent].append(i) | ||||
| @@ -96,6 +96,16 @@ mimetoklass = { | |||||
| } | } | ||||
| def defFS(path): | def defFS(path): | ||||
| if os.path.isdir(path): | |||||
| # new dir | |||||
| return FSDirectory, { 'path': path } | |||||
| elif os.path.isfile(path): | |||||
| # new file - fall through to below | |||||
| pass | |||||
| else: | |||||
| log.msg('skipping (not dir or reg): %s' % path) | |||||
| return None, None | |||||
| fn, ext = os.path.splitext(path) | fn, ext = os.path.splitext(path) | ||||
| ext = ext.lower() | ext = ext.lower() | ||||
| try: | try: | ||||
| @@ -129,7 +139,7 @@ def dofileadd(cd, parent, path, name): | |||||
| if klass is None: | if klass is None: | ||||
| return | return | ||||
| log.msg('matched:', os.path.join(path, name), `i`) | |||||
| log.msg('matched:', os.path.join(path, name), `i`, `klass`) | |||||
| return cd.addItem(parent, klass, name, **kwargs) | return cd.addItem(parent, klass, name, **kwargs) | ||||
| class FSDirectory(FSObject, StorageFolder): | class FSDirectory(FSObject, StorageFolder): | ||||
| @@ -153,20 +163,11 @@ class FSDirectory(FSObject, StorageFolder): | |||||
| del self.pathObjmap[i] | del self.pathObjmap[i] | ||||
| for i in children: | for i in children: | ||||
| fname = os.path.join(self.FSpath, i) | |||||
| if i in self.pathObjmap: | if i in self.pathObjmap: | ||||
| continue | continue | ||||
| # new object | # new object | ||||
| if os.path.isdir(fname): | |||||
| # new dir | |||||
| nf = self.cd.addContainer(self.id, i, klass = FSDirectory, path = fname) | |||||
| elif os.path.isfile(fname): | |||||
| # new file | |||||
| nf = dofileadd(self.cd, self.id, self.FSpath, i) | |||||
| else: | |||||
| nf = None | |||||
| log.msg('skipping: %s' % fname) | |||||
| nf = dofileadd(self.cd, self.id, self.FSpath, i) | |||||
| if nf is not None: | if nf is not None: | ||||
| self.pathObjmap[i] = nf | self.pathObjmap[i] = nf | ||||