File: parseVariableDescriptions.py

package info (click to toggle)
yuview 2.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,712 kB
  • sloc: cpp: 62,506; python: 788; xml: 54; makefile: 6
file content (164 lines) | stat: -rw-r--r-- 6,294 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

class VariableRestrictionRage:
    def __init__(self, min, max):
        self.min = min
        self.max = max

    def __str__(self):
        return f"Range({self.min}-{self.max})"


class VariableRestrictionGreaterThen:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"Greater({self.value})"


class VariableRestrictionLessThen:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"Less({self.value})"


class VariableRestrictionEqualTo:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"Equal({self.value})"


class VariableDescription():
    def __init__(self, name):
        self.name = name
        self.description = ""
        self.shallBe = None

    def __str__(self):
        if (self.shallBe):
            return f"{self.name} -- {self.shallBe}"
        else:
            return f"{self.name}"

    def finishReading(self):
        self.cleanDescriptionText()
        self.lookForShallBe()

    def cleanDescriptionText(self):
        self.description = self.description.replace(u'\xa0', u' ')

    def lookForShallBe(self):
        searchStrings = []
        searchStrings.append(self.name + " shall be ")
        searchStrings.append("the value of " + self.name + " shall be ")
        for searchString in searchStrings:
            posShallBe = self.description.lower().find(searchString)
            if (posShallBe != -1):
                posDot = self.description.find(".", posShallBe)
                if (posDot != -1):
                    self.parseRestriction(
                        self.description[posShallBe + len(searchString): posDot])
                    return

    def parseRestriction(self, restrictionText):
        conditionList = ["in the range of ", "greater than ",
                         "greater then ", "less than ", "less then ", "equal to "]
        for idx, condition in enumerate(conditionList):
            conditionPos = restrictionText.find(condition)
            conditionStart = conditionPos + len(condition)
            if (conditionPos != -1):
                conditionIndex = idx
                break
        if (conditionPos == -1):
            ignoreCases = ["the same for all pictures ",
                           "the same in all ",
                           "the same for all PPSs "]
            for c in ignoreCases:
                if (c in restrictionText):
                    return
            if (restrictionText == "0"):
                print("Warning: Restriction just says 'shall be 0'.")
                self.shallBe = VariableRestrictionEqualTo("0")
                return
            print("TODO: Add this restriction: " + restrictionText)
            return

        if (conditionIndex == 0):
            posMinEnd = restrictionText.find(" to ", conditionStart)
            posMaxStart = posMinEnd + len(" to ")
            posMaxEnd = restrictionText.find(", inclusive", posMaxStart)
            if (posMinEnd == -1 or posMaxStart == -1 or posMaxEnd == -1):
                print("Error parsing range")
                return
            self.shallBe = VariableRestrictionRage(
                restrictionText[conditionStart: posMinEnd], restrictionText[posMaxStart: posMaxEnd])
        elif (conditionIndex in [1, 2]):
            self.shallBe = VariableRestrictionGreaterThen(
                restrictionText[conditionStart:])
        elif (conditionIndex in [3, 4]):
            self.shallBe = VariableRestrictionLessThen(
                restrictionText[conditionStart:])
        elif (conditionIndex == 5):
            self.shallBe = VariableRestrictionEqualTo(
                restrictionText[conditionStart:])
        else:
            assert(False)

        if (conditionIndex in [2, 4]):
            print("Warning: Using then in a comparison.")


def parseDocForVariableDescriptions(document):
    firstLastEntry = ["NAL unit header semantics", "Slice data semantics"]
    firstEntryFound = False
    variableDescriptions = dict()
    currentDescription = None
    for paragraph in document.paragraphs:
        if (firstLastEntry[0] in paragraph.text):
            firstEntryFound = True
        if (firstLastEntry[1] in paragraph.text):
            break
        if (firstEntryFound):
            if (len(paragraph.runs) > 1):
                runText = paragraph.runs[0].text
                isBold = paragraph.runs[0].font.bold
                if (isBold):
                    if (currentDescription != None):
                        currentDescription.finishReading()
                        variableDescriptions[currentDescription.name] = currentDescription
                    # It can happen that the variable description is split over multiple
                    # runs. It will all be bold without spaces and can just be concatenated.
                    # Please don't ask me why.
                    runIndex = 1
                    for i in range(1, len(paragraph.runs)):
                        isBold = paragraph.runs[i].font.bold
                        text = paragraph.runs[i].text
                        containsSpaceAtEnd = (text[-1] == " ")
                        if (containsSpaceAtEnd):
                            text = text[:-1]
                        containesSpaces = (text.find(" ") != -1)
                        if (isBold and not containesSpaces):
                            runText += text
                        else:
                            break
                        if (containsSpaceAtEnd):
                            break
                        runIndex += 1
                    currentDescription = VariableDescription(runText)
                    # Get all the text after the variable name
                    for i in range(runIndex, len(paragraph.runs)):
                        runText = paragraph.runs[i].text
                        currentDescription.description += runText
                    continue
            if (currentDescription != None):
                currentDescription.description += paragraph.text

    if (currentDescription != None):
        currentDescription.finishReading()
        variableDescriptions[currentDescription.name] = currentDescription

    return variableDescriptions