def register_pygments_style(pygment_styles): """ Registers Pygments style classes. Pygments currently has plugin support at pkg_resources level which is not always straightforword. This method allows to register pygments styles easier and on the fly. Expects a dict with alias keys and classes/paths to classes values. Default pygments styles are always registered and you cannot override them (would raise ImproperlyConfigured exception). After style is registered, it may be received with ``richtemplates.pygstyles.get_style`` method which expects alias parameter. """ try: from pygments.styles import STYLE_MAP, get_style_by_name from pygments.style import Style except ImportError: raise ImproperlyConfigured("pygments are required to use richtemplates") registry = {} for alias, style_path in pygment_styles.items(): if alias in STYLE_MAP: raise ImproperlyConfigured("Cannot override builtin %s pygments " "styles" % alias) if isinstance(style_path, Style): klass = style_path else: try: splitted = style_path.split('.') mod_path, class_name = '.'.join(splitted[:-1]), splitted[-1] mod = __import__(mod_path, (), (), [class_name], -1) klass = getattr(mod, class_name) except (ImportError, AttributeError), err: raise ImproperlyConfigured("Cannot register style %s: %s.\n" "Original error was: %s" % (alias, style_path, err)) registry[alias] = klass logger.debug("Registered %s as pygments style with alias %s" % (klass.__name__, alias))
def register_rst_directives(directives_items): """ Registers restructuredText directives given as dictionary with keys being names and paths to directive function. If directive_path is None then we would turn directive off. """ for name, directive_path in directives_items: if directive_path is None: directives.register_directive(name, None) msg = "Turned off '%s' directive" % name logger.debug(msg) continue try: splitted = directive_path.split('.') mod_path, method_name = '.'.join(splitted[:-1]), splitted[-1] mod = __import__(mod_path, (), (), [method_name], -1) directive = getattr(mod, method_name) directives.register_directive(name, directive) msg = "Registered restructuredText directive: %s" % method_name logger.debug(msg) except ImportError, err: msg = "Couldn't register restructuredText directive. Original "\ "exception was: %s" % err logger.warn(msg)
for alias, style_path in pygment_styles.items(): if alias in STYLE_MAP: raise ImproperlyConfigured("Cannot override builtin %s pygments " "styles" % alias) if isinstance(style_path, Style): klass = style_path else: try: splitted = style_path.split('.') mod_path, class_name = '.'.join(splitted[:-1]), splitted[-1] mod = __import__(mod_path, (), (), [class_name], -1) klass = getattr(mod, class_name) except (ImportError, AttributeError), err: raise ImproperlyConfigured("Cannot register style %s: %s.\n" "Original error was: %s" % (alias, style_path, err)) registry[alias] = klass logger.debug("Registered %s as pygments style with alias %s" % (klass.__name__, alias)) for alias in STYLE_MAP: klass = get_style_by_name(alias) registry[alias] = klass logger.debug("Registered %s as pygments style with alias %s" % (klass.__name__, alias)) return registry PYGMENTS_STYLES = getattr(settings, 'RICHTEMPLATES_PYGMENTS_STYLES', {}) REGISTERED_PYGMENTS_STYLES = register_pygments_style(PYGMENTS_STYLES)