aboutsummaryrefslogtreecommitdiffstats
path: root/debian/lib/python/debian_linux/utils.py
blob: 5f73837f0bf7e551861461e97ca3a82b737b11b5 (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
126
127
128
129
130
import debian, re, textwrap

class sorted_dict(dict):
    __slots__ = '_list',

    def __init__(self, entries = None):
        super(sorted_dict, self).__init__()
        self._list = []
        if entries is not None:
            for key, value in entries:
                self[key] = value

    def __delitem__(self, key):
        super(sorted_dict, self).__delitem__(key)
        self._list.remove(key)

    def __setitem__(self, key, value):
        super(sorted_dict, self).__setitem__(key, value)
        if key not in self._list:
            self._list.append(key)

    def iterkeys(self):
        for i in iter(self._list):
            yield i

    def iteritems(self):
        for i in iter(self._list):
            yield (i, self[i])

    def itervalues(self):
        for i in iter(self._list):
            yield self[i]

class field_list(list):
    TYPE_WHITESPACE = object()
    TYPE_COMMATA = object()

    def __init__(self, value = None, type = TYPE_WHITESPACE):
        self.type = type
        if isinstance(value, field_list):
            self.type = value.type
            self.extend(value)
        elif isinstance(value, (list, tuple)):
            self.extend(value)
        else:
            self._extend(value)

    def __str__(self):
        if self.type is self.TYPE_WHITESPACE:
            type = ' '
        elif self.type is self.TYPE_COMMATA:
            type = ', '
        return type.join(self)

    def _extend(self, value):
        if self.type is self.TYPE_WHITESPACE:
            type = '\s'
        elif self.type is self.TYPE_COMMATA:
            type = ','
        if value is not None:
            self.extend([j.strip() for j in re.split(type, value.strip())])

    def extend(self, value):
        if isinstance(value, str):
            self._extend(value)
        else:
            super(field_list, self).extend(value)

class field_list_commata(field_list):
    def __init__(self, value = None):
        super(field_list_commata, self).__init__(value, field_list.TYPE_COMMATA)

class field_string(str):
    def __str__(self):
        return '\n '.join(self.split('\n'))

class templates(dict):
    def __init__(self, dir = None):
        if dir is None:
            self.dir = "debian/templates"
        else:
            self.dir = dir

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError: pass
        ret = self._read(key)
        dict.__setitem__(self, key, ret)
        return ret

    def __setitem__(self, key, value):
        raise NotImplemented()

    def _read(self, filename):
        entries = []

        f = file("%s/%s.in" % (self.dir, filename))

        while True:
            e = debian.package()
            while True:
                line = f.readline()
                if not line:
                    break
                line = line.strip('\n')
                if not line:
                    break
                if line[0] in ' \t':
                    if not last:
                        raise ValueError('Continuation line seen before first header')
                    e[last] += '\n' + line.lstrip()
                    continue
                i = line.find(':')
                if i < 0:
                    raise ValueError("Not a header, not a continuation: ``%s''" % line)
                last = line[:i]
                e[last] = line[i+1:].lstrip()
            if not e:
                break

            entries.append(e)

        return entries

class wrap(textwrap.TextWrapper):
    wordsep_re = re.compile(
        r'(\s+|'                                  # any whitespace
        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash