8 colorize.make('foo').bold
9 colorize.make('foo').green
10 colorize.make('foo').yellow
11 colorize.make('foo').red
12 colorize.make('foo').blue
14 Otherwise you could go the long way (for example if you are
17 string = colorize('foo')
18 string._set_attributes()
23 def __init__(self, string):
24 self.stdout = sys.__stdout__
27 self.isatty = self.stdout.isatty()
29 def _set_attributes(self):
31 Sets the attributes here because the str class does not
32 allow to pass in anything other than a string to the constructor
33 so we can't really mess with the other attributes.
35 for k, v in self.__colors__.items():
36 setattr(self, k, self.make_color(v))
38 def make_color(self, color):
41 return color + self + '\033[0m' + self.appends
55 def make(cls, string):
57 A helper method to return itself and workaround the fact that
58 the str object doesn't allow extra arguments passed in to the
66 # Common string manipulations
68 yellow = lambda x: colorize.make(x).yellow # noqa
69 blue = lambda x: colorize.make(x).blue # noqa
70 green = lambda x: colorize.make(x).green # noqa
71 red = lambda x: colorize.make(x).red # noqa
72 bold = lambda x: colorize.make(x).bold # noqa
73 red_arrow = red('--> ')
74 blue_arrow = blue('--> ')
75 green_arrow = green('--> ')
76 yellow_arrow = yellow('--> ')
81 def __init__(self, _writer=None, prefix='', suffix='', flush=False):
82 self._writer = _writer or sys.stdout
87 def bold(self, string):
88 self.write(bold(string))
90 def raw(self, string):
91 if not string.endswith('\n'):
92 string = '%s\n' % string
95 def write(self, line):
96 self._writer.write(self.prefix + line + self.suffix)
102 return _Write(prefix=blue(' stdout: ')).raw(msg)
106 return _Write(prefix=yellow(' stderr: ')).raw(msg)
110 return _Write().raw(msg)
114 return _Write(prefix=red_arrow).raw(msg)
118 return _Write(prefix=yellow_arrow).raw(msg)
122 return _Write(prefix=green_arrow).raw(msg)
125 def dispatch(mapper, argv=None):
126 argv = argv or sys.argv
127 for count, arg in enumerate(argv, 1):
128 if arg in mapper.keys():
129 instance = mapper.get(arg)(argv[count:])
130 if hasattr(instance, 'main'):
137 Look at every value of every key in the mapper and will output any
138 ``class.help`` possible to return it as a string that will be sent to
142 for key, value in mapper.items():
144 help_text = value.help
145 except AttributeError:
147 help_text_lines.append("%-24s %s" % (key, help_text))
150 return "Available subcommands:\n\n%s" % '\n'.join(help_text_lines)