OpenCores
URL https://opencores.org/ocsvn/radiohdl/radiohdl/trunk

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [base/] [configtree.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 danv
###############################################################################
2
#
3
# Copyright (C) 2014-2018
4
# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
5
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
#
20
# $Id: configtree.py 18891 2018-09-11 13:14:37Z overeem $
21
#
22
###############################################################################
23
import sys
24
import os
25
import os.path
26
import re
27
from common import listify
28
from configfile import ConfigFile, ConfigFileException
29
 
30
class ConfigTree(object):
31
 
32
    def __init__(self, rootdirs, filename, sections=None):
33
        """
34
        Collect the information of all configuration files that are present under the rootdirs.
35
        """
36
        # Save construction arguments
37
        self.rootdirs = listify(rootdirs)
38
        self.filename = filename
39
        self.sections = listify(sections)
40
 
41
        # Define result variables
42
        self._configfiles = {}
43
 
44
        # search and read the config files.
45
        self._read_all_configfiles()
46
        if len(self._configfiles) == 0:
47
            raise ConfigFileException("No '%s' files found in directory tree(s) '%s'." % (filename, rootdirs))
48
 
49
    def _read_all_configfiles(self):
50
        """
51
        Recursively search the rootdirs to find the configfiles and add the content to our admin.
52
        """
53
        ref_pattern = self.filename.replace("*", "(.+?)")
54
        name_mask = re.compile(ref_pattern+"$")
55
        for rootdir in self.rootdirs:
56
            for root, _, files in os.walk(rootdir):
57
                for some_filename in files:
58
                    if name_mask.search(some_filename):
59
                        cfgfile = self._factory_constructor("{}/{}".format(root, some_filename))
60
                        # check for duplicates
61
                        if cfgfile.ID in self._configfiles.keys():
62
                            raise ConfigFileException("File with id '%s' found twice (at '%s' and '%s')" %
63
                                  (cfgfile.ID, self._configfiles[cfgfile.ID].location, cfgfile.location))
64
                        self._configfiles[cfgfile.ID] = cfgfile
65
 
66
    def _factory_constructor(self, full_filename):
67
        """
68
        Function for returning the readin configfile. Derived classes *must* redefine this function.
69
        """
70
        return ConfigFile(full_filename, sections=self.sections)
71
 
72
    @property
73
    def configfiles(self):
74
        return self._configfiles
75
 
76
    def remove_files_from_tree(self, files_to_remove):
77
        """
78
        Remove the given list of configfiles from our configfile administration.
79
        :raise   KeyError when one of the files does not exist in our admin.
80
        """
81
        for cfgfileID in files_to_remove:
82
            self._configfiles.pop(cfgfileID)
83
 
84
 
85
    def limit_tree_to(self, files_to_keep):
86
        """
87
        Limit the configfile collection in our admin to the ones given in the files_to_keep argument.
88
        """
89
        for cfgfile in self._configfiles:
90
            if cfgfile.ID not in files_to_keep:
91
                self._configfiles.pop(cfgfile.ID)
92
 
93
    def get_key_values(self, key, configfiles=None, must_exist=False):
94
        """
95
        Get the value of a key in all configfiles. If the key does not exist in a configfile and
96
        the flag must_exist is False then None is added to the result list. When the flag must_exist
97
        is true and the key is not defined in a configfile then an exception is raised.
98
        The configfiles to search in may be limited to 'configfiles' otherwise the whole tree is used.
99
        :return   List of values
100
        :raises   ConfigFileException
101
        """
102
        if configfiles == None:
103
            configfiles = self._configfiles
104
 
105
        result = []
106
        for cfgfile in configfiles:
107
            result.append(cfgfile.get_value(key, must_exist))
108
        return result
109
 
110
 
111
 
112
    def get_configfiles(self, key, values=None, user_configfiles=None):
113
        """
114
        Get a list with all configfiles that contain the key with a value specified in values.
115
        If values==None then a list of all configfiles is returned that contain the key.
116
        The configfiles to search in may be restricted to the user_configfiles.
117
        """
118
        file_list = self._configfiles.values() if not user_configfiles else user_configfiles
119
 
120
        result = []
121
        for cfgfile in file_list:
122
            if cfgfile not in result and key in cfgfile.content:
123
                if values == None or cfgfile.content[key] in values:
124
                    result.append(cfgfile)
125
        return result

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.