def doExportStyle(self, stylename, filename, cfgxml): if filename != '': styleDir = self.config.stylesDir style = Style(styleDir / stylename) log.debug("dir %s" % style.get_style_dir()) self.__exportStyle(style.get_style_dir(), unicode(filename), cfgxml)
def doImportStyle(self, filename): """ Imports an style from a ZIP file Checks that it is a valid style file (has a content.css), that the directory does not exist (prevent overwriting), and if config.xml file exists, it checks that the style name does not exist. """ styleDir = self.config.stylesDir log.debug("Import style from %s" % filename) filename = filename.decode('utf-8') BaseFile = os.path.basename(filename) targetDir = BaseFile[0:-4:] absoluteTargetDir = styleDir / targetDir try: sourceZip = ZipFile(filename, 'r') except IOError: # Can not create DOM object raise ImportStyleError(_('Could not retrieve data (Core error)')) if os.path.isdir(absoluteTargetDir): style = Style(absoluteTargetDir) raise ImportStyleExistsError(style, absoluteTargetDir, _('Directory exists')) else: os.mkdir(absoluteTargetDir) for name in sourceZip.namelist(): sourceZip.extract(name, absoluteTargetDir) sourceZip.close() style = Style(absoluteTargetDir) if style.isValid(): if not self.config.styleStore.addStyle(style): absoluteTargetDir.rmtree() raise ImportStyleExistsError( style, absoluteTargetDir, _('The style name already exists')) else: # Check missing files cssFile = style.get_style_dir() / 'content.css' files_to_check = ['content.css'] missing_files = [] for f in files_to_check: if not (style.get_style_dir() / f).exists(): missing_files.append(f) if missing_files: # Missing files error missing_files_text = ', '.join(missing_files) if len(missing_files) > 1: style_error = ImportStyleError( _('Files %s does not exist or are not readable.') % missing_files_text) else: style_error = ImportStyleError( _('File %s does not exist or is not readable.') % missing_files_text) else: configStyle = style.get_style_dir() / 'config.xml' if configStyle.exists(): # We consider that if no file is missing the error is due to the format of config.xml style_error = ImportStyleError( _('Wrong config.xml file format.')) else: # Generic error style_error = ImportStyleError( _('An unknown error occurred while importing the style.' )) # Remove the style absoluteTargetDir.rmtree() # Raise error raise style_error # If not error was thrown, style was successfully imported # Let the calling function inform the user as appropriate self.action = ""