1# Copyright 2008-2017 Free Software Foundation, Inc.
2# This file is part of GNU Radio
3#
4# GNU Radio Companion is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# GNU Radio Companion 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
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
17
18from __future__ import absolute_import
19
20
21class TemplateArg(str):
22    """
23    A cheetah template argument created from a param.
24    The str of this class evaluates to the param's to code method.
25    The use of this class as a dictionary (enum only) will reveal the enum opts.
26    The __call__ or () method can return the param evaluated to a raw python data type.
27    """
28
29    def __new__(cls, param):
30        value = param.to_code()
31        instance = str.__new__(cls, value)
32        setattr(instance, '_param', param)
33        return instance
34
35    def __getitem__(self, item):
36        return str(self._param.get_opt(item)) if self._param.is_enum() else NotImplemented
37
38    def __getattr__(self, item):
39        if not self._param.is_enum():
40            raise AttributeError()
41        try:
42            return str(self._param.get_opt(item))
43        except KeyError:
44            raise AttributeError()
45
46    def __str__(self):
47        return str(self._param.to_code())
48
49    def __call__(self):
50        return self._param.get_evaluated()
51