1# MIT License
2#
3# Copyright The SCons Foundation
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24import re
25
26_is_valid_var = re.compile(r'[_a-zA-Z]\w*$')
27
28_rm = re.compile(r'\$[()]')
29_remove = re.compile(r'\$\([^$]*(\$[^)][^$]*)*\$\)')
30
31# Regular expressions for splitting strings and handling substitutions,
32# for use by the scons_subst() and scons_subst_list() functions:
33#
34# The first expression compiled matches all of the $-introduced tokens
35# that we need to process in some way, and is used for substitutions.
36# The expressions it matches are:
37#
38#       "$$"
39#       "$("
40#       "$)"
41#       "$variable"             [must begin with alphabetic or underscore]
42#       "${any stuff}"
43#
44# The second expression compiled is used for splitting strings into tokens
45# to be processed, and it matches all of the tokens listed above, plus
46# the following that affect how arguments do or don't get joined together:
47#
48#       "   "                   [white space]
49#       "non-white-space"       [without any dollar signs]
50#       "$"                     [single dollar sign]
51#
52_dollar_exps_str = r'\$[\$\(\)]|\$[_a-zA-Z][\.\w]*|\${[^}]*}'
53_dollar_exps = re.compile(r'(%s)' % _dollar_exps_str)
54_separate_args = re.compile(r'(%s|\s+|[^\s$]+|\$)' % _dollar_exps_str)
55
56# This regular expression is used to replace strings of multiple white
57# space characters in the string result from the scons_subst() function.
58_space_sep = re.compile(r'[\t ]+(?![^{]*})')
59
60class ValueTypes:
61    """
62    Enum to store what type of value the variable holds.
63    """
64    UNKNOWN = 0
65    STRING = 1
66    CALLABLE = 2
67    VARIABLE = 3
68
69
70class EnvironmentValue:
71    """
72    Hold a single value. We're going to cache parsed version of the file
73    We're going to keep track of variables which feed into this values evaluation
74    """
75    def __init__(self, value):
76        self.value = value
77        self.var_type = ValueTypes.UNKNOWN
78
79        if callable(self.value):
80            self.var_type = ValueTypes.CALLABLE
81        else:
82            self.parse_value()
83
84
85    def parse_value(self):
86        """
87        Scan the string and break into component values
88        """
89
90        try:
91            if '$' not in self.value:
92                self._parsed = self.value
93                self.var_type = ValueTypes.STRING
94            else:
95                # Now we need to parse the specified string
96                result = _dollar_exps.sub(sub_match, args)
97                print(result)
98        except TypeError:
99            # likely callable? either way we don't parse
100            self._parsed = self.value
101
102    def parse_trial(self):
103        """
104        Try alternate parsing methods.
105        :return:
106        """
107        parts = []
108        for c in self.value:
109            pass
110
111
112class EnvironmentValues:
113    """
114    A class to hold all the environment variables
115    """
116    def __init__(self, **kw):
117        self._dict = {}
118        for k in kw:
119            self._dict[k] = EnvironmentValue(kw[k])
120