summaryrefslogtreecommitdiff
path: root/obsapi/repoflags.py
blob: c7badc70be36da35786224400c7ab174da183a70 (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
import sys
from lxml import etree
from collections import namedtuple


RepoFlag = namedtuple('RepoFlag', 'flag status repository arch')
valid_flag_types = ('build', 'publish', 'useforbuild', 'debuginfo')

if sys.version_info >= (3, 0):
    unicode = str


class RepoFlags():

    def __init__(self, xml):

        self.root = None

        if isinstance(xml, (str, unicode, bytes)):
            try:
                self.import_xml(xml)
            except Exception:
                raise Exception('Failed to import xml')
        elif isinstance(xml, etree._Element):
            self.root = xml

    def __str__(self):
        lines = []

        for flag_type in self.flag_types:
            lines.append('{}:'.format(flag_type))
            for flag in self.get_flag(flag_type):
                line = ''
                for attribute in [flag.status, flag.repository, flag.arch]:
                    if attribute is not None:
                        line = ' '.join([line, attribute])
                lines.append(line)

        return '\n'.join(lines)

    def import_xml(self, xml):
        try:
            self.root = etree.fromstring(xml)
        except Exception as e:
            raise e

    @property
    def flags(self):
        for element in self.root:
            for sub in element:
                if sub.tag in ['enable', 'disable']:
                    flag = element.tag
                    status = sub.tag
                    repo = sub.get('repository', None)
                    arch = sub.get('arch', None)
                    yield(RepoFlag(flag=flag, status=status, repository=repo, arch=arch))

    def get_flag(self, flag_type):
        return [f for f in self.flags if f.flag == flag_type]

    @property
    def flag_types(self):
        return set([f.flag for f in self.flags])