summaryrefslogtreecommitdiff
path: root/insanity/generator.py
blob: a32e904d4a40cf9204259e4e1a1a2447a8636c67 (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
# GStreamer QA system
#
#       generator.py
#
# Copyright (c) 2007, Edward Hervey <bilboed@bilboed.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.

"""
Generator classes

Generators expand some arguments into a dictionnary of arguments.
"""

# TODO
#  introspection
#
# We should be able to do something like:
# * chaining generators output
#   i.e. pass the output of FileSystemGenerator to PlaylistGenerator

class Generator(object):
    """
    Expands some arguments into a list of arguments.

    Base class, should not be used directly.
    """

    __args__ = {}
    __produces__ = None

    def __init__(self, *args, **kwargs):
        """
        Subclasses should call their parent __init__ will ALL arguments
        """
        self.args = args
        self.kwargs = kwargs
        self.generated = []
        self._length = None

    def copy(self):
        return self.__class__(*self.args, **self.kwargs)

    def generate(self):
        """
        Returns the full combination of results
        """
        if not self.generated:
            self.generated = self._generate()
        return self.generated

    def _generate(self):
        """
        Return the full list of results
        to be implemented by subclasses
        """
        raise NotImplementedError

    def __iter__(self):
        return iter(self.generate()[:])

    def __len__(self):
        if self._length == None:
            self._length = len(self.generate())
        return self._length

    def __getitem__(self, idx):
        return self.generate()[idx]