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): """ 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 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: raise ImportStyleError('Can not create dom object') if os.path.isdir(absoluteTargetDir): style = Style(absoluteTargetDir) raise ImportStyleExistsError(style, absoluteTargetDir, u'Style directory already 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, u'The style name already exists') else: absoluteTargetDir.rmtree() raise ImportStyleError(u'Incorrect style format (does not include content.css') # If not error was thrown, style was successfully imported # Let the calling function inform the user as appropriate self.action = ""
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 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 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 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/3.0/', 'description': description, 'extra-head': extra_head, 'extra-body': extra_body, } 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 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 = ""