Example #1
0
    def declare(self, name, vartype, local):
        '''Creates an instance of new variable. It loads values from the OS if the variable is not local.'''
        if self.asWriter:
            self._writeVarToXML(name, 'declare', '', vartype, local)

        if not isinstance(local, bool):
            if str(local).lower() == 'true':
                local = True
            else:
                local = False

        if name in self.variables.keys():
            if self.variables[name].local != local:
                raise Variable.EnvError(name, 'redeclaration')
            else:
                if vartype.lower() == "list":
                    if not isinstance(self.variables[name],Variable.List):
                        raise Variable.EnvError(name, 'redeclaration')
                else:
                    if not isinstance(self.variables[name],Variable.Scalar):
                        raise Variable.EnvError(name, 'redeclaration')

        if vartype.lower() == "list":
            a = Variable.List(name, local)
        else:
            a = Variable.Scalar(name, local)

        if self.loadFromSystem and not local and name in os.environ:
            a.expandVars = False # disable var expansion when importing from the environment
            a.set(os.environ[name], os.pathsep, environment=self.variables)
            a.expandVars = True

        self.variables[name] = a
Example #2
0
    def __init__(self, loadFromSystem=True, useAsWriter=False, searchPath=None):
        '''Initial variables to be pushed and setup

        append switch between append and prepend for initial variables.
        loadFromSystem causes variable`s system value to be loaded on first encounter.
        If useAsWriter == True than every change to variables is recorded to XML file.
        reportLevel sets the level of messaging.
        '''
        self.log = logging.getLogger('Environment')

        self.separator = ':'

        # Prepeare the internal search path for xml files (used by 'include' elements)
        if searchPath is None:
            self.searchPath = []
        else:
            self.searchPath = list(searchPath)

        def addToSearchPath(n, _1, _2):
            '''
            Add entries to the search path expanding variables inside.
            '''
            entries = Variable.List('_SEARCH_PATH')
            entries.set(n, os.pathsep, environment=self.variables)
            self.searchPath.extend(entries)

        self.actions = {}
        self.actions['include'] = lambda n, c, h: self.loadXML(self._locate(n, c, h))
        self.actions['append'] = lambda n, v, _: self.append(n, v)
        self.actions['prepend'] = lambda n, v, _: self.prepend(n, v)
        self.actions['set'] = lambda n, v, _: self.set(n, v)
        self.actions['unset'] = lambda n, v, _: self.unset(n, v)
        self.actions['default'] = lambda n, v, _: self.default(n, v)
        self.actions['remove'] = lambda n, v, _: self.remove(n, v)
        self.actions['remove-regexp'] = lambda n, v, _: self.remove_regexp(n, v)
        self.actions['declare'] = self.declare
        self.actions['search_path'] = addToSearchPath

        self.variables = {}

        self.loadFromSystem = loadFromSystem
        self.asWriter = useAsWriter
        if useAsWriter:
            self.writer = xmlModule.XMLFile()
            self.startXMLinput()

        self.loadedFiles = set()

        # Prepare the stack for the directory of the loaded file(s)
        self._fileDirStack = []
        # Note: cannot use self.declare() because we do not want to write out
        #       the changes to ${.}
        dot = Variable.Scalar('.', local=True)
        dot.expandVars = False
        dot.set('')
        self.variables['.'] = dot
Example #3
0
 def default(self, name, value):
     '''Sets a single variable only if it is not already set!'''
     name = str(name)
     if self.asWriter:
         self._writeVarToXML(name, 'default', value)
     else:
         # Here it is different from the other actions because after a 'declare'
         # we cannot tell if the variable was already set or not.
         # FIXME: improve declare() to allow for a default.
         if name not in self.variables:
             if self._guessType(name) == 'list':
                 v = Variable.List(name, False)
             else:
                 v = Variable.Scalar(name, False)
             if self.loadFromSystem and name in os.environ:
                 v.set(os.environ[name], os.pathsep, environment=self.variables)
             else:
                 v.set(value, self.separator, environment=self.variables)
             self.variables[name] = v
         else:
             v = self.variables[name]
             if not v.val:
                 v.set(value, self.separator, environment=self.variables)