def test_bad_role(self): self.assertEqual(parse_color_setting('unknown='), None) self.assertEqual(parse_color_setting('unknown=green'), None) self.assertEqual( parse_color_setting('unknown=green;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'}) )
def test_bad_role(self): self.assertIsNone(parse_color_setting('unknown=')) self.assertIsNone(parse_color_setting('unknown=green')) self.assertEqual( parse_color_setting('unknown=green;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'}) )
def test_color_case(self): self.assertEqual( parse_color_setting("error=GREEN"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=GREEN/BLUE"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "bg": "blue" }), ) self.assertEqual( parse_color_setting("error=gReEn"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=gReEn/bLuE"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "bg": "blue" }), )
def test_bad_option(self): self.assertEquals(parse_color_setting('error=green,unknown'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green'})) self.assertEquals(parse_color_setting('error=green,unknown,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'opts': ('blink',)}))
def test_fg_bg_opts(self): self.assertEquals(parse_color_setting('error=green/blue,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'bg':'blue', 'opts': ('blink',)})) self.assertEquals(parse_color_setting('error=green/blue,bold,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'bg':'blue', 'opts': ('blink','bold')}))
def test_role_case(self): self.assertEquals( parse_color_setting('ERROR=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEquals( parse_color_setting('eRrOr=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'}))
def test_bad_role(self): self.assertIsNone(parse_color_setting("unknown=")) self.assertIsNone(parse_color_setting("unknown=green")) self.assertEqual( parse_color_setting("unknown=green;sql_field=blue"), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), )
def test_role_case(self): self.assertEquals(parse_color_setting('ERROR=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green'})) self.assertEquals(parse_color_setting('eRrOr=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green'}))
def test_opts_case(self): self.assertEquals(parse_color_setting('error=green,BLINK'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'opts': ('blink',)})) self.assertEquals(parse_color_setting('error=green,bLiNk'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'opts': ('blink',)}))
def test_opts_case(self): self.assertEqual( parse_color_setting('error=green,BLINK'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'opts': ('blink',)}) ) self.assertEqual( parse_color_setting('error=green,bLiNk'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'opts': ('blink',)}) )
def test_empty_options(self): self.assertEqual(parse_color_setting('error=green,'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEqual(parse_color_setting('error=green,,,'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEqual(parse_color_setting('error=green,,blink,,'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'opts': ('blink',)}))
def test_bad_option(self): self.assertEqual( parse_color_setting('error=green,unknown'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'}) ) self.assertEqual( parse_color_setting('error=green,unknown,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'opts': ('blink',)}) )
def test_role_case(self): self.assertEqual( parse_color_setting("ERROR=green"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("eRrOr=green"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), )
def test_fg_bg_opts(self): self.assertEqual( parse_color_setting('error=green/blue,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue', 'opts': ('blink',)}) ) self.assertEqual( parse_color_setting('error=green/blue,bold,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue', 'opts': ('blink', 'bold')}) )
def test_color_case(self): self.assertEqual(parse_color_setting('error=GREEN'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEqual(parse_color_setting('error=GREEN/BLUE'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue'})) self.assertEqual(parse_color_setting('error=gReEn'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEqual(parse_color_setting('error=gReEn/bLuE'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue'}))
def test_custom_palette(self): custom_palettes = PALETTES.copy() custom_palettes[NOCOLOR_PALETTE].update({'WARNING': {}, 'NOTICE': {}}) custom_palettes[DARK_PALETTE].update({'WARNING': {'fg': 'yellow'}, 'NOTICE': {'fg': 'cyan'}}) custom_palettes[LIGHT_PALETTE].update({'WARNING': {'fg': 'black', 'bg': 'yellow'}, 'NOTICE': {'fg': 'blue'}}) # empty string. self.assertEquals(parse_color_setting('', custom_palettes), custom_palettes[DEFAULT_PALETTE]) # simple palette. self.assertEquals(parse_color_setting('light', custom_palettes), custom_palettes[LIGHT_PALETTE]) # override palette. self.assertEquals(parse_color_setting('light;error=green', custom_palettes), dict(custom_palettes[LIGHT_PALETTE], ERROR={'fg':'green'}))
def test_bad_option(self): self.assertEqual( parse_color_setting("error=green,unknown"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=green,unknown,blink"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", ) }), )
def make_style(config_string=''): """ Create a Style object from the given config_string. If config_string is empty django.utils.termcolors.DEFAULT_PALETTE is used. """ class Style(object): pass style = Style() color_settings = termcolors.parse_color_setting(config_string) # The nocolor palette has all available roles. # Use that palette as the basis for populating # the palette as defined in the environment. for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]: if color_settings: format = color_settings.get(role, {}) style_func = termcolors.make_style(**format) else: style_func = lambda x: x setattr(style, role, style_func) # For backwards compatibility, # set style for ERROR_OUTPUT == ERROR style.ERROR_OUTPUT = style.ERROR return style
def test_override_with_multiple_roles(self): self.assertEqual( parse_color_setting("light;error=green;sql_field=blue"), dict(PALETTES[LIGHT_PALETTE], ERROR={"fg": "green"}, SQL_FIELD={"fg": "blue"}), )
def test_empty_options(self): self.assertEqual( parse_color_setting("error=green,"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=green,,,"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=green,,blink,,"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", ) }), )
def test_opts_case(self): self.assertEqual( parse_color_setting("error=green,BLINK"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", ) }), ) self.assertEqual( parse_color_setting("error=green,bLiNk"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", ) }), )
def test_fg_bg(self): self.assertEquals( parse_color_setting('error=green/blue'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ 'fg': 'green', 'bg': 'blue' }))
def test_bad_color(self): self.assertEquals(parse_color_setting('error='), None) self.assertEquals( parse_color_setting('error=;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'})) self.assertEquals(parse_color_setting('error=unknown'), None) self.assertEquals( parse_color_setting('error=unknown;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'})) self.assertEquals( parse_color_setting('error=green/unknown'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEquals( parse_color_setting('error=green/blue/something'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ 'fg': 'green', 'bg': 'blue' })) self.assertEquals( parse_color_setting('error=green/blue/something,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ 'fg': 'green', 'bg': 'blue', 'opts': ('blink', ) }))
def test_bad_color(self): self.assertIsNone(parse_color_setting("error=")) self.assertEqual( parse_color_setting("error=;sql_field=blue"), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), ) self.assertIsNone(parse_color_setting("error=unknown")) self.assertEqual( parse_color_setting("error=unknown;sql_field=blue"), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), ) self.assertEqual( parse_color_setting("error=green/unknown"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), ) self.assertEqual( parse_color_setting("error=green/blue/something"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "bg": "blue" }), ) self.assertEqual( parse_color_setting("error=green/blue/something,blink"), dict( PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "bg": "blue", "opts": ("blink", ) }, ), )
def test_fg_bg(self): self.assertEqual( parse_color_setting("error=green/blue"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "bg": "blue" }), )
def test_multiple_roles(self): self.assertEqual( parse_color_setting("error=green;sql_field=blue"), dict( PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}, SQL_FIELD={"fg": "blue"}, ), )
def test_fg_opts(self): self.assertEqual( parse_color_setting("error=green,blink"), dict(PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", ) }), ) self.assertEqual( parse_color_setting("error=green,bold,blink"), dict( PALETTES[NOCOLOR_PALETTE], ERROR={ "fg": "green", "opts": ("blink", "bold") }, ), )
def test_bad_color(self): self.assertEqual(parse_color_setting('error='), None) self.assertEqual(parse_color_setting('error=;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'})) self.assertEqual(parse_color_setting('error=unknown'), None) self.assertEqual(parse_color_setting('error=unknown;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={'fg': 'blue'})) self.assertEqual(parse_color_setting('error=green/unknown'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'})) self.assertEqual(parse_color_setting('error=green/blue/something'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue'})) self.assertEqual(parse_color_setting('error=green/blue/something,blink'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green', 'bg': 'blue', 'opts': ('blink',)}))
def color_style(): """Returns a Style object with the Django color scheme.""" if not supports_color(): style = no_style() else: DJANGO_COLORS = os.environ.get('DJANGO_COLORS', '') color_settings = termcolors.parse_color_setting(DJANGO_COLORS) if color_settings: class dummy: pass style = dummy() # The nocolor palette has all available roles. # Use that pallete as the basis for populating # the palette as defined in the environment. for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]: format = color_settings.get(role,{}) setattr(style, role, termcolors.make_style(**format)) else: style = no_style() return style
def test_bad_palette(self): self.assertEquals(parse_color_setting('unknown'), None)
def test_simple_palette(self): self.assertEquals(parse_color_setting('light'), PALETTES[LIGHT_PALETTE]) self.assertEquals(parse_color_setting('dark'), PALETTES[DARK_PALETTE]) self.assertEquals(parse_color_setting('nocolor'), None)
def test_empty_string(self): self.assertEquals(parse_color_setting(''), PALETTES[DEFAULT_PALETTE])
def test_empty_definition(self): self.assertEquals(parse_color_setting(';'), None) self.assertEquals(parse_color_setting('light;'), PALETTES[LIGHT_PALETTE]) self.assertEquals(parse_color_setting(';;;'), None)
def test_multiple_roles(self): self.assertEquals(parse_color_setting('error=green;sql_field=blue'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green'}, SQL_FIELD={'fg':'blue'}))
def test_override_with_multiple_roles(self): self.assertEquals(parse_color_setting('light;error=green;sql_field=blue'), dict(PALETTES[LIGHT_PALETTE], ERROR={'fg':'green'}, SQL_FIELD={'fg':'blue'}))
def test_override_nocolor(self): self.assertEquals(parse_color_setting('nocolor;error=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'}))
def test_reverse_override(self): self.assertEquals(parse_color_setting('error=green;light'), PALETTES[LIGHT_PALETTE])
def test_override_palette(self): self.assertEquals(parse_color_setting('light;error=green'), dict(PALETTES[LIGHT_PALETTE], ERROR={'fg':'green'}))
def test_fg_bg(self): self.assertEquals(parse_color_setting('error=green/blue'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'bg':'blue'}))
def test_override_palette(self): self.assertEquals(parse_color_setting('light;error=green'), dict(PALETTES[LIGHT_PALETTE], ERROR={'fg': 'green'}))
def test_override_nocolor(self): self.assertEquals( parse_color_setting('nocolor;error=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg': 'green'}))
"""
def test_bad_palette(self): self.assertIsNone(parse_color_setting('unknown'))
def test_fg(self): self.assertEqual(parse_color_setting('error=green'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green'}))