Re: [Zope-dev] Recursive folders from Python
Try something like (taken from my own code, so it works for me, caveta emptor): def mkcontext(self,context=None): global default_dd if not context: return 'EH!' c=string.split(context,'/') newself=self for a in c: try: newself.manage_addFolder(a,createPublic=0,createUserF=0,REQUEST=getattr(self ,'REQUEST',None)) except: pass newself=getattr(newself,a) ## this is the relevant line newself.manage_addLocalRoles(c[-1],['Manager']) try: newself.manage_addDTMLMethod('index_html','Start Page',file=default_dd) except: pass This func takes a slash separated list of folders (actually a NDS context like 'dacs/media/stf/wmlph' and creates the folder hierarchy. It's probably overkill for what you want but the relevant line is the getattr one (as marked). hth Phil - Original Message - From: "Dieter Maurer" <[EMAIL PROTECTED]> To: "Jason Spisak" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Monday, October 30, 2000 6:43 PM Subject: Re: [Zope-dev] Recursive folders from Python > Jason Spisak writes: > > Does anyonw know why this code won't create a folder within a folder. > > It's tells me the id is already in use, so that means it's not > > descending into the newly created folder to make another folder. > > > > def create(self): > > for digit in range(0, 10): > > folder= self.manage_addFolder(str(digit), str(digit)) > > subfolder = self.folder.manage_addFolder(str(digit), str(digit)) > Apparently, the above line should add a new folder in (say) '00'. > What it does, however, is trying to add the new folder in the object named > 'folder' (accessible from 'self'). This may well go wrong. > > Keep in mind, that the local name space of the function 'create' > (where your variable 'folder' lives) is disjunct from the attribute > namespace of 'self' (where the attribute 'folder' lives). > > > Dieter > > ___ > Zope-Dev maillist - [EMAIL PROTECTED] > http://lists.zope.org/mailman/listinfo/zope-dev > ** No cross posts or HTML encoding! ** > (Related lists - > http://lists.zope.org/mailman/listinfo/zope-announce > http://lists.zope.org/mailman/listinfo/zope ) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Recursive folders from Python
Jason Spisak writes: > Does anyonw know why this code won't create a folder within a folder. > It's tells me the id is already in use, so that means it's not > descending into the newly created folder to make another folder. > > def create(self): > for digit in range(0, 10): > folder= self.manage_addFolder(str(digit), str(digit)) > subfolder = self.folder.manage_addFolder(str(digit), str(digit)) Apparently, the above line should add a new folder in (say) '00'. What it does, however, is trying to add the new folder in the object named 'folder' (accessible from 'self'). This may well go wrong. Keep in mind, that the local name space of the function 'create' (where your variable 'folder' lives) is disjunct from the attribute namespace of 'self' (where the attribute 'folder' lives). Dieter ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Recursive folders from Python, Thanks to all
Kapil and Others, Thanks for the enlightening alternatives. That's one thing about this list and Python, there's more than one way to skin an object. All my best, > not an answer to the original question... but an alternative approach > > > from OFS import Folder > > def create_folders(self, number): > ''' create a set of recursive folders with depth equal to number''' > > parent = self > for index in xrange(number): > folder = Folder() > folder.id = str(number) > parent._setObject(folder, str(number)) > parent = folder > > > > > > Andy McKay wrote: > > > > Try using getattr to get the object... heres a python script to do it a > > little more cleanly: > > > > import string > > > > def create_folders(self): > > path = '/a/b/c' # create a folder for each part of the string, nested. > > > > for p in string.split(path, '/'): > > if p != '': > >self = create_folder(self, p) > > > > return 'Done' > > > > def create_folder(folder, id): > > try: folder.manage_addFolder(id) > > except: pass > > > > new_folder = getattr(folder, id) > > return new_folder > > > > - Original Message - > > From: "Jason Spisak" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Sent: Monday, October 16, 2000 10:46 AM > > Subject: [Zope-dev] Recursive folders from Python > > > > > Zopists, > > > > > > Yesterday I was trying to create recursive folders from DTML. > > > I'd like to do the same from Python. > > > Does anyonw know why this code won't create a folder within a folder. > > > It's tells me the id is already in use, so that means it's not > > > descending into the newly created folder to make another folder. > > > > > > def create(self): > > > for digit in range(0, 10): > > > folder= self.manage_addFolder(str(digit), str(digit)) > > > subfolder = self.folder.manage_addFolder(str(digit), str(digit)) > > > Jason Spisak [EMAIL PROTECTED] ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Recursive folders from Python
not an answer to the original question... but an alternative approach from OFS import Folder def create_folders(self, number): ''' create a set of recursive folders with depth equal to number''' parent = self for index in xrange(number): folder = Folder() folder.id = str(number) parent._setObject(folder, str(number)) parent = folder Andy McKay wrote: > > Try using getattr to get the object... heres a python script to do it a > little more cleanly: > > import string > > def create_folders(self): > path = '/a/b/c' # create a folder for each part of the string, nested. > > for p in string.split(path, '/'): > if p != '': >self = create_folder(self, p) > > return 'Done' > > def create_folder(folder, id): > try: folder.manage_addFolder(id) > except: pass > > new_folder = getattr(folder, id) > return new_folder > > - Original Message - > From: "Jason Spisak" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Monday, October 16, 2000 10:46 AM > Subject: [Zope-dev] Recursive folders from Python > > > Zopists, > > > > Yesterday I was trying to create recursive folders from DTML. > > I'd like to do the same from Python. > > Does anyonw know why this code won't create a folder within a folder. > > It's tells me the id is already in use, so that means it's not > > descending into the newly created folder to make another folder. > > > > def create(self): > > for digit in range(0, 10): > > folder= self.manage_addFolder(str(digit), str(digit)) > > subfolder = self.folder.manage_addFolder(str(digit), str(digit)) > > > > All my best, > > > > Jason Spisak > > [EMAIL PROTECTED] > > > > ___ > > Zope-Dev maillist - [EMAIL PROTECTED] > > http://lists.zope.org/mailman/listinfo/zope-dev > > ** No cross posts or HTML encoding! ** > > (Related lists - > > http://lists.zope.org/mailman/listinfo/zope-announce > > http://lists.zope.org/mailman/listinfo/zope ) > > > > ___ > Zope-Dev maillist - [EMAIL PROTECTED] > http://lists.zope.org/mailman/listinfo/zope-dev > ** No cross posts or HTML encoding! ** > (Related lists - > http://lists.zope.org/mailman/listinfo/zope-announce > http://lists.zope.org/mailman/listinfo/zope ) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Recursive folders from Python
Try using getattr to get the object... heres a python script to do it a little more cleanly: import string def create_folders(self): path = '/a/b/c' # create a folder for each part of the string, nested. for p in string.split(path, '/'): if p != '': self = create_folder(self, p) return 'Done' def create_folder(folder, id): try: folder.manage_addFolder(id) except: pass new_folder = getattr(folder, id) return new_folder - Original Message - From: "Jason Spisak" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, October 16, 2000 10:46 AM Subject: [Zope-dev] Recursive folders from Python > Zopists, > > Yesterday I was trying to create recursive folders from DTML. > I'd like to do the same from Python. > Does anyonw know why this code won't create a folder within a folder. > It's tells me the id is already in use, so that means it's not > descending into the newly created folder to make another folder. > > def create(self): > for digit in range(0, 10): > folder= self.manage_addFolder(str(digit), str(digit)) > subfolder = self.folder.manage_addFolder(str(digit), str(digit)) > > All my best, > > Jason Spisak > [EMAIL PROTECTED] > > ___ > Zope-Dev maillist - [EMAIL PROTECTED] > http://lists.zope.org/mailman/listinfo/zope-dev > ** No cross posts or HTML encoding! ** > (Related lists - > http://lists.zope.org/mailman/listinfo/zope-announce > http://lists.zope.org/mailman/listinfo/zope ) > ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
[Zope-dev] Recursive folders from Python
Zopists, Yesterday I was trying to create recursive folders from DTML. I'd like to do the same from Python. Does anyonw know why this code won't create a folder within a folder. It's tells me the id is already in use, so that means it's not descending into the newly created folder to make another folder. def create(self): for digit in range(0, 10): folder= self.manage_addFolder(str(digit), str(digit)) subfolder = self.folder.manage_addFolder(str(digit), str(digit)) All my best, Jason Spisak [EMAIL PROTECTED] ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )