def discover(self, type=None, source=None): """ Discover any available add-ons in the known search paths. If ``type`` is specified, only discover add-ons of that type or types. Returns a list of discovered add-ons. This is probably the most important single function of the Add-on Manager, as it must be called before you can access your add-ons. """ if isinstance(type, (tuple, list)): types = type else: types = [type] if type else self._types.keys() output = [] # Iterate over the type list, and walk for each type. for type in types: log.info('Discovering add-ons of type %r.' % type) if not type in self._addons: self._addons[type] = {} info_class, info_regex, search_paths = self._types[type][:3] for spath in search_paths: log.debug('Searching path: %s' % spath) for root, dirs, files in path.walk(spath, source=source): for file in files: filepath = path.join(root, file) match = info_regex.search(filepath) if not match: continue # If we've already got an add-on with this name, just # continue. name = match.group('name') if name in self._addons[type]: continue # We've got an add-on. Build the info instance. try: addon = info_class(name, filepath, match.groupdict(), source=source) except (IOError, ValueError): log.exception( 'Problem loading add-on information for the ' 'add-on %r (%r).' % (name, filepath)) continue # Store it! log.info('Found %s: %s' % (type, addon.data['name'])) self._addons[type][name] = addon output.append(addon) log.info('Discovery finished.') return output
def data_url(url): # Extract the useful information from the URL's path. format, sep, data = url.path().partition(',') if not sep and not data: data = format format = '' mimetype, _, format = format.partition(';') if not mimetype: ext = 'txt' else: _, _, ext = mimetype.rpartition('/') if not format: format = 'charset=US-ASCII' # Build the filename. filename = path.join(path.cache(), 'data-uris', '%s.%s' % (hashlib.md5(data).hexdigest(), ext)) # Ensure the path exists and write the file. try: if not os.path.exists(filename): os.makedirs(filename) with open(filename, 'wb') as f: if format == 'base64': f.write(base64.b64decode(data)) elif format.startswith('charset='): data = urllib.unquote(data).encode('latin1') cs = format[8:] if cs and cs.lower() not in ('utf-8', 'utf8'): data = data.decode(cs).encode('utf-8') f.write(data) else: return except (ValueError, OSError, IOError, TypeError): return return path.normpath(filename)
def icon(name, extension=None, style=None, use_inheritance=True, allow_theme=True, _always_return=True): """ Find an icon with the given ``name`` and ``extension`` and return a :class:`PySide.QtGui.QIcon` for that icon. ================ =========== ============ Argument Default Description ================ =========== ============ name The name of the icon to load. extension The desired filename extension of the icon to load. If this isn't set, a list of supported formats will be used. style The style to load the icon from. If this isn't set, the current style will be assumed. use_inheritance ``True`` Whether or not to search the parent style if the given style doesn't contain an icon. allow_theme ``True`` Whether or not to fall back on Qt icon themes if an icon cannot be found. ================ =========== ============ """ if style: if isinstance(style, basestring): style = addons.get('style', style) elif not isinstance(style, StyleInfo): raise TypeError("Can only activate StyleInfo instances!") else: style = _current_style # If we don't have a style, return a null icon now. if not style: return QIcon() # Right, time to find the icon. if isinstance(extension, (tuple, list)): extensions = extension elif extension: extensions = [extension] else: extensions = (str(ext) for ext in QImageReader.supportedImageFormats()) # Iteration powers, activate! for ext in extensions: filename = '%s.%s' % (name, ext) icon_path = path.join('images', filename) if style.path.exists(icon_path): # We've got it, but what is it? if (not isinstance(style.path_source, basestring) or style.path_source.startswith('py:')): # pkg_resource! Do things the fun and interesting way. with style.path.open(icon_path) as f: pixmap = QPixmap() pixmap.loadFromData(f.read()) return QIcon(pixmap) # Just a regular file. Open normally. return QIcon(style.path.abspath(icon_path)) # Still here? We didn't find our icon then. If we're inheriting, then call # icon again for the style we inherit from. if use_inheritance and style.inherits: for parent in style.inherits: result = icon(name, extension, parent, True, False, False) if result: return result # For one last try, see if we can use the theme icons. if allow_theme and QIcon.hasThemeIcon(name): return QIcon.fromTheme(name) # We don't have an icon. Return a null QIcon. if _always_return: return QIcon()