aboutsummaryrefslogtreecommitdiff
blob: af996ab13072b3c213cc4ee7db0b7f4becf08357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# config.py -- Portage Config
# Copyright 2007-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

__all__ = [
    "ConfigLoaderKlass",
    "GenericFile",
    "PackageKeywordsFile",
    "PackageUseFile",
    "PackageMaskFile",
    "PortageModulesFile",
]

from portage.cache.mappings import UserDict
from portage.env.loaders import (
    KeyListFileLoader,
    KeyValuePairFileLoader,
    ItemFileLoader,
)


class ConfigLoaderKlass(UserDict):
    """
    A base class stub for things to inherit from.
    Users may want a non-file backend.
    """

    def __init__(self, loader):
        """
        @param loader: A class that has a load() that returns two dicts
                the first being a data dict, the second being a dict of errors.
        """
        UserDict.__init__(self)
        self._loader = loader

    def load(self):
        """
        Load the data from the loader.

        @throws LoaderError:
        """

        self.data, self.errors = self._loader.load()


class GenericFile(UserDict):
    """
    Inherits from ConfigLoaderKlass, attempts to use all known loaders
    until it gets <something> in data.  This is probably really slow but is
    helpful when you really have no idea what you are loading (hint hint the file
    should perhaps declare  what type it is? ;)
    """

    loaders = [KeyListFileLoader, KeyValuePairFileLoader, ItemFileLoader]

    def __init__(self, filename):
        UserDict.__init__(self)
        self.filename = filename

    def load(self):
        for loader in self.loaders:
            l = loader(self.filename, None)
            data, errors = l.load()
            if len(data) and not len(errors):
                (self.data, self.errors) = (data, errors)
                return


class PackageKeywordsFile(ConfigLoaderKlass):
    """
    Inherits from ConfigLoaderKlass; implements a file-based backend.
    """

    default_loader = KeyListFileLoader

    def __init__(self, filename):
        super(PackageKeywordsFile, self).__init__(
            self.default_loader(filename, validator=None)
        )


class PackageUseFile(ConfigLoaderKlass):
    """
    Inherits from PackageUse; implements a file-based backend.  Doesn't handle recursion yet.
    """

    default_loader = KeyListFileLoader

    def __init__(self, filename):
        super(PackageUseFile, self).__init__(
            self.default_loader(filename, validator=None)
        )


class PackageMaskFile(ConfigLoaderKlass):
    """
    A class that implements a file-based package.mask

    Entires in package.mask are of the form:
    atom1
    atom2
    or optionally
    -atom3
    to revert a previous mask; this only works when masking files are stacked
    """

    default_loader = ItemFileLoader

    def __init__(self, filename):
        super(PackageMaskFile, self).__init__(
            self.default_loader(filename, validator=None)
        )


class PortageModulesFile(ConfigLoaderKlass):
    """
    File Class for /etc/portage/modules
    """

    default_loader = KeyValuePairFileLoader

    def __init__(self, filename):
        super(PortageModulesFile, self).__init__(
            self.default_loader(filename, validator=None)
        )