#       $Id: copyPropertySheet.py,v 1.1.1.1 2002/02/23 13:40:19 dieter Exp $
'''copy a PropertySheet.'''

from OFS.CopySupport import CopySource
from OFS.PropertySheets import PropertySheet
from types import ListType, StringType, TupleType

def copyPropertySheet(self,sheet, container=None, id= None):
  '''copy *sheet* to *container*; give new sheet *id*.'''
  if type(sheet) in (TupleType,StringType): sheet= self.restrictedTraverse(sheet)
  if container is None: container= sheet.aq_inner.aq_parent
  if type(container) in (TupleType,StringType): container= self.restrictedTraverse(container)
  # sanity checks
  if not isinstance(sheet, PropertySheet):
    raise ValueError, "sheet must be a PropertySheet"
  oc= sheet.__class__
  class C(oc,CopySource): pass
  try:
    sheet.__class__= C # make it a CopySource
    if id is None: id= sheet.getId()
    try: container.manage_clone(sheet, id)
    except ValueError:
      # it will happen, when the sheet is not empty
      pass
    ns= container._getOb(id)
    d= {}
    for (k,v) in sheet.propertyItems():
      if type(v) is ListType: v= list(v)
      d[k]= v
    apply(ns.manage_changeProperties,(),d)
  finally: sheet.__class__= oc
  return 'copied'


