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: absoluteTargetDir.rmtree() # content.css is missing raise ImportStyleError( _('File %s does not exist or is not readable.') % 'content.css') # If not error was thrown, style was successfully imported # Let the calling function inform the user as appropriate self.action = ""
def doDeleteStyle(self, style): try: styleDir = self.config.stylesDir styleDelete = Style(styleDir / style) self.__deleteStyle(styleDelete) self.alert(_(u'Correct'), _(u'Style deleted correctly')) self.reloadPanel('doList') except: self.alert(_(u'Error'), _(u'An unexpected error has occurred')) self.action = ""
def overwriteLocalStyle(self, style_dir, filename): """ Overwrites an already installed style with a new version from file """ log.debug(u"Overwriting style %s with style from %s" % (style_dir, filename)) try: # Delete local style style_path = Path(style_dir) styleDelete = Style(style_path) self.__deleteStyle(styleDelete) # Import downloaded style self.doImportStyle(filename) self.client.sendScript("Ext.MessageBox.alert('%s', '%s')" % (_('Correct'), _('Style updated correctly'))) except: self.client.sendScript("Ext.MessageBox.alert('%s', '%s')" % (_('Error'), _('An unexpected error has occurred'))) finally: Path(filename).remove()
def __load(self): """ Carga los estilos desde el directorio de estilos definido en config (loads the styles from the directory defined in config) """ styleDir = self._config.stylesDir log.debug("loadStyles from %s" % styleDir) for subDir in styleDir.dirs(): style = Style(subDir) if style.isValid(): log.debug(" loading style %s" % style.get_name()) self.addStyle(style) #print style else: log.debug(" style %s is not valid")
def doImportStyle(self, filename): """ Importa un estilo desde un fichero ZIP Comprueba si es un estilo valido (contiene content.css), si el directorio no existe (para no machacar) y si tiene config.xml que el nombre no exista ya. """ 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: self.alert(_(u'Error'), _(u'File %s does not exist or is not readable.') % filename) return None if os.path.isdir(absoluteTargetDir): self.alert(_(u'Error'), _(u'Style directory already exists: %s') % targetDir) 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() self.alert(_(u'Error'), _(u'The style name already exists: %s') % style.get_name()) else: self.alert(_(u'Success'), _(u'Successfully imported style: %s') % style.get_name()) else: absoluteTargetDir.rmtree() self.alert(_(u'Error'), _(u'Incorrect style format (does not include content.css)')) self.action = ""
def saveStyle(self, style_dirname, style_data): """ Updates the style with data given from Styles Designer """ styleDir = self.config.stylesDir / style_dirname # Check that the target dir already exists and update files if not os.path.isdir(styleDir): raise StyleDesignerError( _('Error saving style, style directory does not exist')) else: try: style = Style(styleDir) # Save all uploaded files to style dir self.saveUploadedFiles(styleDir, style_data) # Overwrite content.css, nav.css and config.xml files with the data # from the style designer contentcss = style_data['contentcss'][0] navcss = style_data['navcss'][0] author = 'exeLearning.net' if 'author' in style_data: author = cgi.escape(style_data['author'][0], True) author_url = 'http://exelearning.net' if 'author_url' in style_data: author_url = cgi.escape(style_data['author_url'][0], True) description = '' if 'description' in style_data: description = cgi.escape(style_data['description'][0], True) new_version = style.get_version() if 'version' in style_data: new_version = style_data['version'][0] # If user has chosen a new version, use it # otherwise autoincrement minor version if new_version != style.get_version(): next_version = new_version else: current_version = tuple( map(int, style.get_version().split('.'))) next_version = (current_version[0], current_version[1] + 1) next_version = '.'.join(map(str, next_version)) # extra-head and extra-body attributes can contain user defined scripts or headers # ('base' style contains scripts and parameters needed for responsiveness). # The UI has no fields to modify these attributes, so they will never be in # 'style_data', but since user can edit 'config.xml' any time, the values # present in there must be kept config_org = ET.parse(styleDir / 'config.xml') extra_head = config_org.find('extra-head').text extra_body = config_org.find('extra-body').text if config_org.find('edition-extra-head'): edition_extra_head = config_org.find( 'edition-extra-head').text else: # To review # edition-extra-head was not in the previous version of StyleDesigner # edition_extra_head was not found in the Style copy_from = 'base' baseStyleDir = self.config.stylesDir / copy_from config_base = ET.parse(baseStyleDir / 'config.xml') config_extra_head = config_base.find('extra-head').text if config_extra_head == extra_head: # The user did not change the original 'extra-head', so _style_js.js exists # We just use Base's 'edition-extra-head' edition_extra_head = config_base.find( 'edition-extra-head').text else: # The user changed the original 'extra-head', so _style_js.js might not exist # edition_extra_head edition_extra_head = '' configxml = { 'name': style_data['style_name'][-1], 'version': next_version, 'compatibility': version.version, 'author': author, 'author-url': author_url, 'license': 'Creative Commons by-sa', 'license-url': 'http://creativecommons.org/licenses/by-sa/4.0/', 'description': description, 'extra-head': extra_head, 'extra-body': extra_body, 'edition-extra-head': edition_extra_head } newStyleDir = self.updateStyle(styleDir, contentcss, navcss, configxml) newStyle = Style(newStyleDir) self.config.styleStore.delStyle(style) self.config.styleStore.addStyle(newStyle) return newStyle except Exception, e: raise StyleDesignerError(e.message)
def createStyle(self, style_dirname, style_data): """ Creates a new style with the name and data given """ # Check that the target dir does not already exists and create copy_from = 'base' if 'copy_from' in style_data: copy_from = style_data['copy_from'][0] styleDir = self.config.stylesDir / style_dirname if os.path.isdir(styleDir): raise CreateStyleExistsError( styleDir, _(u'Style directory %s already exists') % (style_dirname)) else: try: os.mkdir(styleDir) # Copy ALL files from the base style baseStyleDir = self.config.stylesDir / copy_from base_files = os.listdir(baseStyleDir) for file_name in base_files: full_file_name = os.path.join(baseStyleDir, file_name) if (os.path.isfile(full_file_name)): shutil.copy(full_file_name, styleDir) # Save all uploaded files to style dir self.saveUploadedFiles(styleDir, style_data) # Overwrite content.css, nav.css and config.xml files with the data # from the style designer contentcss = style_data['contentcss'][0] navcss = style_data['navcss'][0] author = 'exeLearning.net' if 'author' in style_data: author = cgi.escape(style_data['author'][0], True) author_url = 'http://exelearning.net' if 'author_url' in style_data: author_url = cgi.escape(style_data['author_url'][0], True) description = '' if 'description' in style_data: description = cgi.escape(style_data['description'][0], True) # extra-head and extra-body attributes can contain user defined scripts or headers # ('base' style contains scripts and parameters needed for responsiveness). # The UI has no fields to modify these attributes, so they will never be in # 'style_data', but since user can edit 'config.xml' any time, the values # present in there must be kept config_base = ET.parse(baseStyleDir / 'config.xml') extra_head = config_base.find('extra-head').text extra_body = config_base.find('extra-body').text edition_extra_head = config_base.find( 'edition-extra-head').text configxml = { 'name': style_data['style_name'][-1], 'version': '1.0', 'compatibility': version.version, 'author': author, 'author-url': author_url, 'license': 'Creative Commons by-sa', 'license-url': 'http://creativecommons.org/licenses/by-sa/4.0/', 'description': description, 'extra-head': extra_head, 'extra-body': extra_body, 'edition-extra-head': edition_extra_head } self.updateStyle(styleDir, contentcss, navcss, configxml) # New style dir has been created, add style to eXe Styles store style = Style(styleDir) if style.isValid(): if not self.config.styleStore.addStyle(style): styleDir.rmtree() raise CreateStyleExistsError( styleDir, _('The style name already exists')) return style except Exception, e: if os.path.isdir(styleDir): styleDir.rmtree() raise CreateStyleError(e.message)
def doPreExportStyle(self, style): styleDir = self.config.stylesDir styleProperties = Style(styleDir / style) self.properties = styleProperties.renderPropertiesJSON() self.action = 'PreExport' self.style = style
def doPropertiesStyle(self, style): styleDir = self.config.stylesDir styleProperties = Style(styleDir / style) self.properties = styleProperties.renderPropertiesJSON() self.action = 'Properties' self.style = styleProperties.get_name()
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 = ""
def doDeleteStyle(self, style): styleDir = self.config.stylesDir styleDelete = Style(styleDir/style) self.__deleteStyle(styleDelete)