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

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [core/] [configtree.py] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 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$
21
#
22
###############################################################################
23
import sys
24
import os
25
import os.path
26
import re
27
from common_radiohdl import listify
28
from configfile import ConfigFile, ConfigFileException
29
 
30
 
31
class ConfigTree(object):
32
 
33
    def __init__(self, rootdirs, filename, sections=None):
34
        """
35
        Collect the information of all configuration files that are present under the rootdirs.
36
        """
37
        # Save construction arguments
38
        self.rootdirs = listify(rootdirs)
39
        self.filename = filename
40
        self.sections = listify(sections)
41
 
42
        # Define result variables
43
        self._configfiles = {}
44
 
45
        # search and read the config files.
46
        self._read_all_configfiles()
47
        if len(self._configfiles) == 0:
48
            raise ConfigFileException("No '%s' files found in directory tree(s) '%s'." % (filename, rootdirs))
49
 
50
    def _read_all_configfiles(self):
51
        """
52
        Recursively search the rootdirs to find the configfiles and add the content to our admin.
53
        """
54
        ref_pattern = self.filename.replace("*", "(.+?)")
55
        name_mask = re.compile(ref_pattern+"$")
56
        for rootdir in self.rootdirs:
57
            for root, _, files in os.walk(rootdir):
58
                for some_filename in files:
59
                    if name_mask.search(some_filename):
60
                        cfgfile = self._factory_constructor("{}/{}".format(root, some_filename))
61
                        # check for duplicates
62
                        if cfgfile.ID in list(self._configfiles.keys()):
63
                            raise ConfigFileException("File with id '%s' found twice (at '%s' and '%s')"
64
                                                      % (cfgfile.ID, self._configfiles[cfgfile.ID].location, cfgfile.location))
65
                        self._configfiles[cfgfile.ID] = cfgfile
66
 
67
    def _factory_constructor(self, full_filename):
68
        """
69
        Function for returning the readin configfile. Derived classes *must* redefine this function.
70
        """
71
        return ConfigFile(full_filename, sections=self.sections)
72
 
73
    @property
74
    def configfiles(self):
75
        return self._configfiles
76
 
77
    def remove_files_from_tree(self, files_to_remove):
78
        """
79
        Remove the given list of configfiles from our configfile administration.
80
        :raise   KeyError when one of the files does not exist in our admin.
81
        """
82
        for cfgfileID in files_to_remove:
83
            self._configfiles.pop(cfgfileID)
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 is 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
    def get_configfiles(self, key, values=None, user_configfiles=None):
111
        """
112
        Get a list with all configfiles that contain the key with a value specified in values.
113
        If values==None then a list of all configfiles is returned that contain the key.
114
        The configfiles to search in may be restricted to the user_configfiles.
115
        """
116
        file_list = list(self._configfiles.values()) if not user_configfiles else user_configfiles
117
 
118
        result = []
119
        for cfgfile in file_list:
120
            if cfgfile not in result and key in cfgfile.content:
121
                if values is None or cfgfile.content[key] in values:
122
                    result.append(cfgfile)
123
        return result

powered by: WebSVN 2.1.0

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