From 45769b29075d0b858a18c13face39503cf401298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Mon, 21 Jan 2013 10:31:16 +0100 Subject: [PATCH] Allow overriding the renderer when instantiating Powerline This is useful to allow e.g. the prompt script to use a common shell theme while having different renderers for different shells. --- powerline/core.py | 10 ++++++---- powerline/lib/__init__.py | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/powerline/core.py b/powerline/core.py index f6b6c446..8b2ce088 100644 --- a/powerline/core.py +++ b/powerline/core.py @@ -7,10 +7,11 @@ import sys from colorscheme import Colorscheme from matcher import Matcher +from powerline.lib import underscore_to_camelcase class Powerline(object): - def __init__(self, ext): + def __init__(self, ext, renderer_module=None): config_home = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) config_path = os.path.join(config_home, 'powerline') plugin_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'config_files') @@ -41,9 +42,10 @@ class Powerline(object): local_themes[key] = {'config': self._load_theme_config(ext, local_theme_name)} # Load and initialize extension renderer - renderer_module_name = 'powerline.renderers.{0}'.format(ext) - renderer_class_name = '{0}Renderer'.format(ext.capitalize()) - Renderer = getattr(importlib.import_module(renderer_module_name), renderer_class_name) + renderer_module_name = renderer_module or ext + renderer_module_import = 'powerline.renderers.{0}'.format(renderer_module_name) + renderer_class_name = '{0}Renderer'.format(underscore_to_camelcase(renderer_module_name)) + Renderer = getattr(importlib.import_module(renderer_module_import), renderer_class_name) self.renderer = Renderer(theme_config, local_themes, theme_kwargs) def add_local_theme(self, key, config): diff --git a/powerline/lib/__init__.py b/powerline/lib/__init__.py index 8d2de86f..f2f97e2b 100644 --- a/powerline/lib/__init__.py +++ b/powerline/lib/__init__.py @@ -1,2 +1,7 @@ -from humanize_bytes import humanize_bytes -from memoize import memoize +from memoize import memoize # NOQA +from humanize_bytes import humanize_bytes # NOQA + + +def underscore_to_camelcase(string): + '''Return a underscore_separated_string as CamelCase.''' + return ''.join(word.capitalize() or '_' for word in string.split('_'))