1#
2#  Copyright (C) 2018 Codethink Limited
3#
4#  This program is free software; you can redistribute it and/or
5#  modify it under the terms of the GNU Lesser General Public
6#  License as published by the Free Software Foundation; either
7#  version 2 of the License, or (at your option) any later version.
8#
9#  This library is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12#  Lesser General Public License for more details.
13#
14#  You should have received a copy of the GNU Lesser General Public
15#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16#
17#  Authors:
18#        Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
19import re
20import copy
21import click
22
23
24# Profile()
25#
26# A class for formatting text with ansi color codes
27#
28# Kwargs:
29#    The same keyword arguments which can be used with click.style()
30#
31class Profile():
32    def __init__(self, **kwargs):
33        self._kwargs = dict(kwargs)
34
35    # fmt()
36    #
37    # Format some text with ansi color codes
38    #
39    # Args:
40    #    text (str): The text to format
41    #
42    # Kwargs:
43    #    Keyword arguments to apply on top of the base click.style()
44    #    arguments
45    #
46    def fmt(self, text, **kwargs):
47        kwargs = dict(kwargs)
48        fmtargs = copy.copy(self._kwargs)
49        fmtargs.update(kwargs)
50        return click.style(text, **fmtargs)
51
52    # fmt_subst()
53    #
54    # Substitute a variable of the %{varname} form, formatting
55    # only the substituted text with the given click.style() configurations
56    #
57    # Args:
58    #    text (str): The text to format, with possible variables
59    #    varname (str): The variable name to substitute
60    #    value (str): The value to substitute the variable with
61    #
62    # Kwargs:
63    #    Keyword arguments to apply on top of the base click.style()
64    #    arguments
65    #
66    def fmt_subst(self, text, varname, value, **kwargs):
67
68        def subst_callback(match):
69            # Extract and format the "{(varname)...}" portion of the match
70            inner_token = match.group(1)
71            formatted = inner_token.format(**{varname: value})
72
73            # Colorize after the pythonic format formatting, which may have padding
74            return self.fmt(formatted, **kwargs)
75
76        # Lazy regex, after our word, match anything that does not have '%'
77        return re.sub(r"%(\{(" + varname + r")[^%]*\})", subst_callback, text)
78