def _process_css(self, css): stylesheet = css._localFile path = get_archived_file(stylesheet, self.archive_dirs)[1] if path is None: self.print_error('%[red!]CSS file not found on disk; skipping it') return with open(path, 'rb') as f: stylesheet_content = convert_to_unicode(f.read()) self.event.stylesheet_metadata = { 'size': len(stylesheet_content), 'hash': crc32(stylesheet_content), 'filename': secure_filename(convert_to_unicode(stylesheet.fileName), 'stylesheet.css'), 'content_type': 'text/css' } self.event.stylesheet = stylesheet_content if not self.quiet: self.print_success('- %[cyan][CSS] {}'.format(stylesheet.fileName))
def migrate(self): for obj, minutes, special_prot in self._iter_minutes(): if special_prot: self.print_warning('%[yellow!]{} minutes have special permissions; skipping them'.format(obj)) continue path = get_archived_file(minutes, self.archive_dirs)[1] if path is None: self.print_error('%[red!]{} minutes not found on disk; skipping them'.format(obj)) continue with open(path, 'r') as f: data = convert_to_unicode(f.read()).strip() if not data: self.print_warning('%[yellow]{} minutes are empty; skipping them'.format(obj), always=False) continue note = EventNote(object=obj) note.create_revision(RenderMode.html, data, self.system_user) if not self.quiet: self.print_success('%[cyan]{}'.format(obj))
def _process_icon(self, cat, icon): path = get_archived_file(icon, self.archive_dirs)[1] if path is None: self.print_error('%[red!]Icon not found on disk; skipping it', event_id=cat.id) return try: icon_image = Image.open(path) except IOError as e: self.print_warning("Cannot open {}: {}".format(path, e), event_id=cat.id) return if icon_image.mode == 'CMYK': self.print_warning("Icon is a CMYK {}; converting to RGB".format( icon_image.format), always=False, event_id=cat.id) # this may result in wrong colors, but there's not much we can do... icon_image = icon_image.convert('RGB') if icon_image.size != (16, 16): self.print_warning( "Icon is {}x{}; resizing to 16x16".format(*icon_image.size), always=False, event_id=cat.id) icon_image = icon_image.resize((16, 16), Image.ANTIALIAS) icon_bytes = BytesIO() icon_image.save(icon_bytes, 'PNG') icon_bytes.seek(0) icon_content = icon_bytes.read() icon_filename = secure_filename(convert_to_unicode(icon.fileName), 'icon') icon_filename = os.path.splitext(icon_filename)[0] + '.png' cat.icon_metadata = { 'size': len(icon_content), 'hash': crc32(icon_content), 'filename': icon_filename, 'content_type': 'image/png' } cat.icon = icon_content
def _process_logo(self, logo): path = get_archived_file(logo, self.archive_dirs)[1] if path is None: self.print_error('%[red!]Logo not found on disk; skipping it') return try: logo_image = Image.open(path) except IOError as e: self.print_warning("Cannot open {}: {}".format(path, e)) return if logo_image.mode == 'CMYK': self.print_warning("Logo is a CMYK {}; converting to RGB".format( logo_image.format)) # this may result in wrong colors, but there's not much we can do... logo_image = logo_image.convert('RGB') logo_bytes = BytesIO() try: logo_image.save(logo_bytes, 'PNG') except Exception as e: self.print_warning("Cannot write PNG logo: {}".format(path, e)) return logo_bytes.seek(0) logo_content = logo_bytes.read() logo_filename = secure_filename(convert_to_unicode(logo.fileName), 'logo') logo_filename = os.path.splitext(logo_filename)[0] + '.png' self.event.logo_metadata = { 'size': len(logo_content), 'hash': crc32(logo_content), 'filename': logo_filename, 'content_type': 'image/png' } self.event.logo = logo_content if not self.quiet: self.print_success('- %[cyan][Logo] {}'.format(logo.fileName))