Index: qPloneSkinDump/trunk/utils.py
===================================================================
--- qPloneSkinDump/trunk/utils.py	(revision 3713)
+++ qPloneSkinDump/trunk/utils.py	(revision 1)
@@ -1,4 +1,3 @@
-import os, re, string, sets, time
-from zope.app import zapi
+import os, re, string, sets
 from App.config import getConfiguration
 from Products.CMFCore.utils import getToolByName
@@ -6,18 +5,4 @@
 from config import *
 from write_utils import writeProps, writeFileContent, writeObjectsMeta
-
-from zope.interface import providedBy
-from zope.schema import getFields
-from zope.component import getMultiAdapter, getUtility, getSiteManager
-from zope.publisher.interfaces.browser import IBrowserRequest
-from five.customerize.interfaces import IViewTemplateContainer, ITTWViewTemplate
-from plone.portlets.interfaces import IPortletAssignmentMapping, IPortletManager, IPlacelessPortletManager
-from plone.portlets.interfaces import IPortletContext, IPortletDataProvider
-from plone.portlets.interfaces import ILocalPortletAssignmentManager
-from plone.app.customerize.registration import generateIdFromRegistration, interfaceName
-
-from Products.GenericSetup.utils import _getDottedName, _resolveDottedName
-from Products.GenericSetup.interfaces import IBody
-from Products.GenericSetup.context import BaseContext
 
 CSS_PATTERN = re.compile("^.+\.css$")
@@ -41,4 +26,16 @@
     return products
 
+def getFSSkinPath(fs_product_name, fs_skin_directory, skin_obj, subdir):
+    """ Return file system skin path for subdir."""
+    skinpath = "%s/%s/skins/%s" % (PRODUCTS_PATH \
+                                  ,fs_product_name \
+                                  ,fs_skin_directory)
+    # If in skin's subfolder - get its path
+    skinp, subp = [obj.getPhysicalPath() for obj in [skin_obj, subdir]]
+    if len(subp) != len(skinp):
+        # adapt skinpath for creating directory
+        skinpath += '/' + '/'.join( subp[len(skinp):] )
+    return skinpath
+
 def get_id(obj):
     """ Get real object's id."""
@@ -51,198 +48,22 @@
     return meta_type in ['Image', 'File'] and obj.manage_FTPget() or obj.document_src()
 
-def dumpPortalViewCustomization(context):
-    result = []
-
-    components = getSiteManager(context)
-    localregs = [reg for reg in components.registeredAdapters() if (len(reg.required) in (2, 4, 5) and
-                                                                     reg.required[1].isOrExtends(IBrowserRequest) and
-                                                                     ITTWViewTemplate.providedBy(reg.factory))]
-    container = getUtility(IViewTemplateContainer)
-#    import pdb;pdb.set_trace()
-    for lreg in localregs:
-#        ttw_id = generateIdFromRegistration(lreg)
-        ttw_id = lreg.factory.__name__
-                            
-        if ttw_id not in container.objectIds():
-            continue
-        ttw = getattr(container, ttw_id)
-        ttw_info = {'for_name'  : interfaceName(lreg.required[0]),
-                    'type_name' : interfaceName(lreg.required[-1]),
-                    'view_name' : lreg.name,
-                    'kwargs'    : {'text' : ttw._text,
-                                   'content_type' : ttw.content_type,
-                                   'encoding' : ttw.output_encoding,
-                                   }}
-        result.append(ttw_info)
-
-    return result
-
-def extractInfoFromAssignment(name, assignment):
-    klass = assignment.__class__
-    a = {'name' : name, 'class' : '%s' % _getDottedName(klass)}
-    data = assignment.data
-    kwargs = {}
-    for i in list(providedBy(data)):
-        if i.isOrExtends(IPortletDataProvider):
-            for field_name, field in getFields(i).items():
-                kwargs[field_name] = field.get(assignment)
-    a['kwargs'] = kwargs
-    return a
-
-def extractSiteWidePortlets(context, managers):
-    """ Extract site-wide portlets 
-        Data structure:
-            '__site-wide-portlets__', [(<manager1_name>, <manager1_info>),
-                                       (<manager2_name>, <manager2_info>),
-                                       (<manager3_name>, <manager3_info>)])
-            <manager_info>:
-                {'category1' : <catmapping1>,
-                 'category2' : <catmapping2>}
-            <catmapping>:
-                {'key1' : <mapping1>,
-                 'key2' : <mapping2>}
-            <mapping>:
-                {'assignment_name1' : <assignment1>,
-                 'assignment_name2' : <assignment2>}
-            <assignment>:
-                {'name'   : 'Assignment',
-                 'class'  : 'dotted.path.to.assignment.class',
-                 'kwargs' : {'parameter1' : 'value1',
-                             'parameter2' : 'value2'}
-    """
-    info = []
-    for manager_name, manager in managers:
-        manager_info = {}
-        for category, catmapping in manager.items():
-            catmapping_info = {}
-            for key, mapping in catmapping.items():
-                mapping_info = {}
-                for name, assignment in mapping.items():
-                    mapping_info[name] = extractInfoFromAssignment(name, assignment)
-                catmapping_info[key] = mapping_info
-            manager_info[category] = catmapping_info
-        info.append((manager_name, manager_info))
-    return info
-
-def extractContextPortletsFromManager(context, manager):
-    """ Extract all contextual portlets from given object and portlet manager, and portlets blacklists
-        Data structure:
-        <manager_info> =
-            {'blacklists'  : [(GROUP_CATEGORY, True),
-                              (CONTENT_TYPE_CATEGORY, False),
-                              (CONTEXT_CATEGORY, None)],
-             'assignments' : [{'name'   : 'Assignment-2',
-                               'class'  : 'dotted.path.to.assignment.class',
-                               'kwargs' : {'parameter1' : 'value1',
-                                          'parameter2' : 'value2'}},
-                              {'name'   : 'Assignment',
-                               'class'  : 'dotted.path.to.assignment.class',
-                               'kwargs' : {'parameter1' : 'value1',
-                                            'parameter2' : 'value2'}]}
-    """
-
-    info = {}
-    info['assignments'] = assignments = []
-    info['blacklists'] = blacklists = []
-
-    # Extract contextual portlets
-    mapping = getMultiAdapter((context, manager), IPortletAssignmentMapping, context=context)
-    for name, assignment in mapping.items():
-        assignments.append(extractInfoFromAssignment(name, assignment))
-
-    # Extract blacklists for given object and manager
-    localassignmentmanager = getMultiAdapter((context, manager), ILocalPortletAssignmentManager)
-    blacklist = localassignmentmanager._getBlacklist()
-    if blacklist is not None:
-        for category, key in blacklist.items():
-            blacklists.append((category, key))
-
-    return info
-
-def extractPortletsFromContext(context, slot_structure, typesToShow, managers):
-    """ Extract portlets for given object assigned through all portlet managers.
-        Data structure:
-            ('unique/path/to/context', [(<manager1_name>, <manager1_info>),
-                                        (<manager2_name>, <manager2_info>),
-                                        (<manager3_name>, <manager3_info>)])
-    """
-
-
-    info = []
-    key = '/'.join(context.getPhysicalPath()[2:])
-
-    for name, manager in managers:
-        info.append((name, extractContextPortletsFromManager(context, manager)))
-
-    slot_structure.append((key, info))
-
-    return slot_structure
-
-def dumpAllPortlets(context, slot_structure, typesToShow, managers):
-    extractPortletsFromContext(context, slot_structure, typesToShow, managers)
-    if getattr(context.aq_base, 'isPrincipiaFolderish', 0):
-        for id, obj in context.contentItems():
-            if obj.portal_type in typesToShow:
-                dumpAllPortlets(obj, slot_structure, typesToShow, managers)
-
-    return slot_structure
-
-def dumpPortlets(context, dump_policy, dump_portlets_selection):
-    """ Extract portlets from given set of objects and site-wide portlets too.
-        Data structure:
-            SLOT_STRUCTURE =
-                [(), (), ()]
-    """
-
-    portal = getToolByName(context, 'portal_url').getPortalObject()
-    portal_state = getMultiAdapter((portal, context.REQUEST), name=u'plone_portal_state')
-    typesToShow = portal_state.friendly_types()
-
-    components = getSiteManager(context)
-    managers = [r for r in components.registeredUtilities() if r.provided.isOrExtends(IPortletManager)]
-    context_managers = [(m.name, getUtility(IPortletManager, name=m.name, context=context)) for m in managers
-                                                                               if not IPlacelessPortletManager.providedBy(m.component)]
-    managers = [(m.name, getUtility(IPortletManager, name=m.name, context=context)) for m in managers]
-
-    slot_structure = []
-    if dump_policy == 'root':
-        extractPortletsFromContext(portal, slot_structure, typesToShow, context_managers)
-    elif dump_policy == 'all':
-        dumpAllPortlets(portal, slot_structure, typesToShow, context_managers)
-    elif dump_policy == 'selection':
-        for ppath in dump_portlets_selection:
-            obj = portal.restrictedTraverse(ppath)
-            extractPortletsFromContext(obj, slot_structure, typesToShow, context_managers)
-
-    slot_structure.append(('__site-wide-portlets__', extractSiteWidePortlets(portal, managers)))
-
-    return slot_structure
-
-def buildSkinLayers(context, zmi_base_skin_name):
-    pskins = getToolByName(context,'portal_skins')
-    layers = (pskins.getSkinPath(zmi_base_skin_name) or '').split(',')
-    return "\n".join(['   <layer name="%s"/>' % l for l in layers])
-
-def getFSSkinPath(folder, fs_dest_directory, fs_product_name):
-    """ Return file system skin path for subdir."""
-
-    folder_path = '/'.join(folder.getPhysicalPath()[list(folder.getPhysicalPath()).index('portal_skins')+1:])
-    skinpath = "%s/%s/skins/%s" % (fs_dest_directory, fs_product_name, folder_path)
-
-    # If in skin's subfolder - get its path
-    #skinp, subp = [obj.getPhysicalPath() for obj in [skin_obj, subdir]]
-    #if len(subp) != len(skinp):
-        ## adapt skinpath for creating directory
-        #skinpath += '/' + '/'.join( subp[len(skinp):] )
-    return skinpath
-
-def dumpFolder(folder, fs_dest_directory, fs_product_name):
-    skinpath = getFSSkinPath(folder, fs_dest_directory, fs_product_name)
+def dumpSkin(context, \
+             skin_name='custom', \
+             subdir=None, \
+             fs_skin_directory='custom', \
+             fs_product_name='QSkinTemplate', \
+             erase_from_skin=0):
+    """Dump custom information to file."""
+    # In case of recursable call go into subdir in skin folder.
+    skin_obj = getToolByName( context, 'portal_skins' )[skin_name]
+    subdir = subdir or skin_obj
+    skinpath = getFSSkinPath(fs_product_name, fs_skin_directory, skin_obj, subdir)
     # Create directory in FS if not yet exist
-    if not os.path.exists(skinpath):
-        os.makedirs(skinpath)
+    if not os.path.exists( skinpath ):
+        os.makedirs( skinpath )
     # Loop of copying content from ZMIskin-folder to FSskin-folder 
+    deletelist = []
     obj_meta = {}
-    for o in folder.objectValues():
+    for o in subdir.objectValues():
         meta_type = o.meta_type
         id = get_id(o)
@@ -253,12 +74,18 @@
         if meta_type == 'Folder':
             # very plone specific
-            if id in ['stylesheet_properties', 'base_properties'] or id.startswith('base_properties'):
-                writeProps(o, skinpath, extension = '.props')
+            if id in ['stylesheet_properties', 'base_properties'] \
+               or id.startswith('base_properties'):
+                writeProps( o, skinpath, extension = '.props' )
             else:
-                dumpFolder(o, fs_product_name, fs_product_name)
+                dumpSkin( context, skin_name, subdir = o,\
+                          fs_skin_directory=fs_skin_directory,\
+                          fs_product_name=fs_product_name,\
+                          erase_from_skin = erase_from_skin )
+            deletelist.append( o.getId() )
         elif meta_type in _write_custom_meta_type_list:
             #writeProps( o, skinpath )      # write object's properties
             # extract content from object(depend on metatype) and write it to the file
-            writeFileContent(o, skinpath, getData(o, meta_type))
+            writeFileContent( o, skinpath, getData(o, meta_type) )
+            deletelist.append( o.getId() )
         else:
             print 'method ignoring ', meta_type
@@ -266,16 +93,7 @@
     if obj_meta :
         writeObjectsMeta(obj_meta, skinpath)
-
-def dumpSkin(context, skin_names=['custom',], fs_dest_directory=PRODUCTS_PATH,
-             fs_product_name='QSkinTemplate', erase_from_skin=0):
-    """Dump custom information to file."""
-    if type(skin_names) not in (type([]), type(())):
-        skin_names = [skin_names,]
-    for skin_name in list(skin_names):
-        folder = getToolByName(context, 'portal_skins')[skin_name]
-        dumpFolder(folder, fs_dest_directory, fs_product_name)
-        # delete objects from the skin, if request
-        if erase_from_skin:
-            folder.manage_delObjects(ids = folder.objectIds())
+    # delete objects from the skin, if request
+    if erase_from_skin:
+        subdir.manage_delObjects( ids = deletelist )
 
 def fillinFileTemplate(f_path_read, f_path_write=None, dict={}):
@@ -287,8 +105,5 @@
     f_tmpl.close()
     f_tmpl = open(f_path_write, 'w')
-    try:
-       f_tmpl.write(tmpl % dict)
-    except:
-        raise str(tmpl)
+    f_tmpl.write(tmpl % dict)
     f_tmpl.close()
 
@@ -342,94 +157,34 @@
             f_dst.write(data)
             f_dst.close()
-        elif os.path.isdir(src_path) \
-             and not ".svn" in src_path:
+        elif os.path.isdir(src_path):
             if not os.path.exists(dst_path):
                 os.mkdir(dst_path)
             copyDir(src_path, dst_path, productName)
 
-def fsDirectoryViewsXML(folder_names, product_name, remove=False):
-    pattern = """ <object name="%(folder_name)s" meta_type="Filesystem Directory View"
-       directory="Products.%(product_name)s:skins/%(folder_name)s" %(remove)s/>\n"""
-    xml = ''
-    if type(folder_names) not in (type([]), type(())):
-        folder_names = [folder_names,]
-    for name in folder_names:
-       xml += pattern % {'product_name' : product_name, \
-                         'folder_name'  : name, \
-                         'remove'       : remove and 'remove="True"' or '', \
-                        }
-    return xml
-
-def makeNewProduct(context, destinationDir, productName, productSkinName, \
-                   zmi_skin_names, zmi_base_skin_name, subdir,\
+def makeNewProduct(context, productName, productSkinName, \
+                   zmi_skin_name, zmi_base_skin_name, subdir,\
                    doesCustomizeSlots, left_slots, right_slots, slot_forming, main_column, \
-                   doesExportObjects, import_policy, dump_CSS, dump_JS, \
-                   dump_portlets, dump_policy, dump_portlets_selection, dump_custom_views):
+                   doesExportObjects, import_policy, dump_CSS, dump_JS):
     """Create new skin-product's directory and 
-       copy skin-product template with little modification"""
-    products_path = destinationDir
+       copy skin-product template with little modification""" 
+    products_path = PRODUCTS_PATH
     productPath = ospJoin(products_path, productName)
-    if not (productName in os.listdir(products_path)):
+    if not ( productName in os.listdir(products_path) ):
         os.mkdir(productPath)
-    files_to_remove = []
-    files_to_add    = []
     # Form CSS and JS importing list and regestry data (looking in subdir too) for Plone 2.1.0+ 
-    stylesheets_xml = ''
-    javascripts_xml = ''
-    #subdir = subdir or getToolByName(context, 'portal_skins')[zmi_skin_name]
-    portal_setup = getToolByName(context, 'portal_setup')
+    subdir = subdir or getToolByName( context, 'portal_skins' )[zmi_skin_name]
     result_css_list = skin_css_regdata = result_js_list = skin_js_regdata = []
-    base_context = BaseContext(portal_setup, portal_setup.getEncoding())
     if dump_CSS:
-        res_reg = getToolByName(context, 'portal_css', None)
-        exporter = zapi.queryMultiAdapter((res_reg, base_context), IBody)
-        if exporter is not None:
-            stylesheets_xml = exporter.body
-        #result_css_list, skin_css_regdata = getResourceListRegdata(context, subdir,
-                                            #CSS_PATTERN, 'portal_css', CSS_REG_PROPS)
-    if stylesheets_xml == '':
-        files_to_remove.append(ospJoin('profiles', 'default', 'cssregistry.xml'))
+        result_css_list, skin_css_regdata = getResourceListRegdata(context, subdir,
+                                            CSS_PATTERN, 'portal_css', CSS_REG_PROPS)
     if dump_JS:
-        res_reg = getToolByName(context, 'portal_javascripts', None)
-        exporter = zapi.queryMultiAdapter((res_reg, base_context), IBody)
-        if exporter is not None:
-            javascripts_xml = exporter.body
-        #result_js_list, skin_js_regdata = getResourceListRegdata(context, subdir,
-                                            #JS_PATTERN, 'portal_javascripts', JS_REG_PROPS)
-    if javascripts_xml == '':
-        files_to_remove.append(ospJoin('profiles', 'default', 'jsregistry.xml'))
-
-    slots = ()
-    if dump_portlets:
-        slots = dumpPortlets(context, dump_policy, dump_portlets_selection)
-
+        result_js_list, skin_js_regdata = getResourceListRegdata(context, subdir,
+                                            JS_PATTERN, 'portal_javascripts', JS_REG_PROPS)
     # Get Slots customization information
     if not doesCustomizeSlots:
         left_slots = right_slots = None
         slot_forming = main_column = None
-
-    # Prepare XML strings for add to skins.xml
-    skin_layers = buildSkinLayers(context, zmi_base_skin_name)
-
-    # Prepare profiles
-    default_marker, afterinstall_marker, uninstall_marker = {}, {}, {}
-    profiles = ['default', 'afterinstall', 'uninstall']
-    profiles_path = ospJoin(products_path, productName, 'profiles')
-    for profile in profiles:
-        varname = "%s_marker" % profile
-        file_name = "%s_%s.txt" % (productName.lower(), profile)
-        locals()[varname].update({'fname' : file_name, \
-                                  'fpath' : ospJoin(profiles_path, profile, file_name), \
-                                  'fdata' : "# Marker file for %s profile of %s skin" % \
-                                            (profile, productName) })
-        files_to_add.append(locals()[varname])
-
-    # dump customized objects from portal_view_customization
-    custom_views = []
-    if dump_custom_views:
-        custom_views = dumpPortalViewCustomization(context)
-
     # Copy skin_template to SKIN_PRODUCT directory
-    templatePath = ospJoin(PRODUCTS_PATH, PROJECTNAME, TEMPLATE_PATH)
+    templatePath = ospJoin(products_path, PROJECTNAME, TEMPLATE_PATH)
     copyDir(templatePath, productPath, productName)
     # Form data dictionary and form Skin Product's files
@@ -449,53 +204,9 @@
                 ,"SLOT_FORMING" : slot_forming \
                 ,"MAIN_COLUMN" : main_column \
-                ,"product_name" : productName \
-                ,"skin_name" : productSkinName \
-                ,"skin_name_lowercase" : productSkinName.lower() \
-                ,"skin_name_capital" : '%s%s' % (productSkinName[0].upper(), productSkinName[1:]) \
-                ,"product_name_lowercase" : productName.lower() \
-                ,"viewlets_zcml" : '' \
-                ,"stylesheets_xml" : stylesheets_xml \
-                ,"javascripts_xml" : javascripts_xml \
-                ,"version" : time.strftime('%Y%m%d') \
-                ,"slot_structure" : str(slots) \
-                ,"skin_layers" : skin_layers \
-                ,"custom_views" : str(custom_views) \
-                ,"directory_views_xml" : fsDirectoryViewsXML(zmi_skin_names, productName) \
-                ,"remove_directory_views_xml" : fsDirectoryViewsXML(zmi_skin_names, productName, remove=True) \
-                ,"creation_date" : time.strftime('%d/%m/%Y') \
-                ,"install_profile_marker" : default_marker['fname'] \
-                ,"afterinstall_profile_marker" : afterinstall_marker['fname'] \
-                ,"uninstall_profile_marker" : uninstall_marker['fname'] \
-    }
+                }
     sp_updated_files = ['config.py' \
                        ,'README.txt' \
-                       ,'HISTORY.txt' \
-                       ,'setuphandlers.py' \
-                       ,'uninstallhandlers.py' \
-                       ,'profiles.zcml' \
-                       ,'utils.py' \
-                       ,'configure.zcml' \
-                       ,'skins.zcml' \
-                       ,ospJoin('Extensions', 'Install.py')\
-                       ,ospJoin('browser', 'interfaces.py')\
-                       ,ospJoin('browser', 'viewlets.zcml')\
-                       ,ospJoin('browser', 'configure.zcml')\
-                       ,ospJoin('profiles', 'default', 'skins.xml')\
-                       ,ospJoin('profiles', 'default', 'cssregistry.xml')\
-                       ,ospJoin('profiles', 'default', 'jsregistry.xml')\
-                       ,ospJoin('profiles', 'default', 'propertiestool.xml')\
-                       ,ospJoin('profiles', 'afterinstall', 'import_steps.xml')\
-                       ,ospJoin('profiles', 'uninstall', 'import_steps.xml')\
-                       ,ospJoin('profiles', 'uninstall', 'skins.xml')\
-                       ,ospJoin('browser', 'configure.zcml') \
-                       #,ospJoin('profiles', 'uninstall', 'cssregistry.xml')\
-                       #,ospJoin('profiles', 'uninstall', 'jsregistry.xml')\
-                       ]
+                       ,ospJoin('Extensions', 'utils.py')\
+                       ,ospJoin('Extensions', 'Install.py')]
     for fp in sp_updated_files:
         fillinFileTemplate(ospJoin(productPath, fp), dict=conf_dict)
-    for fp in files_to_remove:
-        os.remove(ospJoin(productPath,fp))
-    for data in files_to_add:
-        f = file(data['fpath'],'w')
-        f.write(data['fdata'])
-        f.close()
