Author: jmorliaguet Date: Thu Jun 22 14:49:55 2006 New Revision: 3489 Added: cpsskins/branches/paris-sprint-2006/setup/io/migration.py (contents, props changed)
Log: - prototype of CPSSkins v2 theme importer Added: cpsskins/branches/paris-sprint-2006/setup/io/migration.py ============================================================================== --- (empty file) +++ cpsskins/branches/paris-sprint-2006/setup/io/migration.py Thu Jun 22 14:49:55 2006 @@ -0,0 +1,93 @@ +############################################################################## +# +# Copyright (c) 2005-2006 Nuxeo and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" + +$Id$ +""" +__docformat__ = "reStructuredText" + +from xml.dom.minidom import parseString +from zope.component import getUtility + +from cpsskins.elements.theme import Theme +from cpsskins.setup.archives import TarArchive +from cpsskins.setup.interfaces import IResourceManager +from cpsskins.standard.fields.image import WebImage +from cpsskins.thememanager import ThemeManagementFolder + +class Migration(object): + """Migration utility for importing CPSSkins v2 themes + """ + def __init__(self, file): + self.file = file + + self.themes = ThemeManagementFolder() + self.themes.registerUtilities() + + self.archive = TarArchive(mode='r', data=file.read()) + self.resources = getUtility(IResourceManager) + + def __call__(self): + theme_names = self.getThemeNames() + for theme_name in theme_names: + self.importTheme(theme_name) + self.importImages(theme_name) + return self.themes + + def getThemeNames(self): + names = [] + for item in self.archive['themes/']: + if item.endswith(u'.xml'): + continue + names.append(item[len('themes/'):-1]) + return names + + def importTheme(self, name=u''): + path = 'themes/%s.xml' % name + doc = parseString(self.archive[path]) + + theme_node = doc.childNodes[0] + theme_name = theme_node.getAttribute('name') + props = self.getProperties(theme_node, 'title', 'author', 'copyright', + 'license') + theme = Theme(**props) + self.themes.addTheme(theme, name=theme_name) + + def importImages(self, name): + resources = self.resources + archive = self.archive + themes = self.themes + for item in archive['themes/%s/icons/' % name]: + image = WebImage(data=archive[item]) + name = self.getFileName(item) + resources.register(name=name, resource=image, context=themes) + + ### utilities + + def getProperties(self, node=None, *props): + info = {} + for property in node.getElementsByTagName('property'): + nodes = property.childNodes + name = property.getAttribute('name') + if name not in props: + continue + value = u'' + if nodes: + value = nodes[0].nodeValue + info[str(name)] = value + return info + + def getFileName(self, path): + return path.split('/')[-1] + -- http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins