[Zope-dev] Zope2 OFS/CopySupport's _get_id()

Alexandre Boeglin alex at nexedi.com
Mon Nov 22 05:47:31 EST 2004


Hello,

While I was reading the _get_id function, I felt it was really strange :

####
    def _get_id(self, id):
        # Allow containers to override the generation of
        # object copy id by attempting to call its _get_id
        # method, if it exists.
        n=0
        if (len(id) > 8) and (id[8:]=='copy_of_'):
            n=1
        orig_id=id
        while 1:
            if self._getOb(id, None) is None:
                return id
            id='copy%s_of_%s' % (n and n+1 or '', orig_id)
            n=n+1
####

currently, when copying and pasting an object, 'copy[0-9]*_of_' will be 
prepended to the id (if the id is already in use in the current 
folder). Thus, copying 'x' will create 'copy_of_x', 'copy2_of_x', etc. 
and copying 'copy_of_x' will create 'copy_of_copy_of_x', 
'copy2_of_copy_of_x', etc.

What I don't get, is what the two lines between 'n=0' and 'orig_id=id' 
are supposed to do. What they do is this : if an object id is 16 chars 
long, and it ends with 'copy_of_' (like 'XXXXXXXXcopy_of_'), its first 
copy will be called 'copy2_of_XXXXXXXXcopy_of_' instead of 
'copy_of_XXXXXXXXcopy_of_'...

What I think the author intended to do, is that, when you copy an object 
called 'copy_of_x', the copy will be named 'copy2_of_x' instead of 
'copy_of_copy_of_x'.

Thus, here's a function that does the job :

####
  import re
  copy_re=re.compile('^copy[0-9]*_of_')

  def _get_id(self, id):
      # Allow containers to override the generation of
      # object copy id by attempting to call its _get_id
      # method, if it exists.
      copy_match=self.copy_re.match(id)
      if (copy_match) and (copy_match.end() < len(id)):
          n=1
          orig_id=self.copy_re.sub('', id)
      else:
          n=0
          orig_id=id
      while 1:
          if self._getOb(id, None) is None:
              return id
          id='copy%s_of_%s' % (n and n+1 or '', orig_id)
          n=n+1
####

Then, I don't know what is the preferred naming for new objects, but I 
fear that 'copy_of_copy_of_copy_of_copy_of_copy_of_copy_of_object' will 
quickly be too long, if objects are copied by an automated process that 
doesn't show the Ids to the user or don't allow their modification.

What are your feelings about this ?

Regards,
Alexandre Boeglin


More information about the Zope-Dev mailing list