File: util.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (144 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download
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

class SectionReader:
    """
    Super class for the many necessary readers
    """

    def __init__(self, Instr, write_file, product_filename,
                 get_next_line, return_line):
        self.Instr = Instr
        self.write_file = write_file
        self.product_filename = product_filename
        self.instr_name = ""
        self.get_next_line = get_next_line
        self.return_line = return_line

    def set_instr_name(self, name):
        self.instr_name = name

    def _write_to_file(self, string_array):
        """
        In case a py file is being written, this function writes to the
        appropriate file.
        """

        if self.write_file:
            with open(self.product_filename, "a") as product_file:
                for string in string_array:
                    product_file.write(string)

    def _kw_to_string(self, kwargs):
        """
        Used when a dict containing keyword arguments need to be written
        to a string. This string can be used as argument in method call.
        """

        output_string = ""
        for kwarg in kwargs:
            output_string += ", "
            output_string += kwarg + "=" + str(kwargs[kwarg])

        return output_string

    def _split_func(self, *args):
        """
        Returns list of strings seperated by commas that are not
        within open parenthesis.
        """

        string = args[0]
        split_character = args[1]

        if len(args) == 3:
            limit = args[2]
        else:
            limit = -1

        split_positions = []
        parenthesis = 0
        for index in range(len(string)):
            character = string[index]
            if (character == split_character and parenthesis == 0
                    and limit != 0):
                split_positions.append(index)
                limit -= 1
            else:
                if character == "(":
                    parenthesis += 1
                if character == ")":
                    parenthesis -= 1

        split_positions.append(len(string)+1)  # virtual comma at the end

        result = []
        last_position = 0
        for position in split_positions:
            result.append(string[last_position:position])
            last_position = position + 1

        return result

    def _split_func_brack(self, *args):
        """
        Returns list of strings seperated by commas that are not
        within open parenthesis / brackets
        """

        string = args[0]
        split_character = args[1]

        if len(args) == 3:
            limit = args[2]
        else:
            limit = -1

        split_positions = []
        parenthesis = 0
        brackets = 0
        for index in range(len(string)):
            character = string[index]
            if (character == split_character and parenthesis == 0
                    and brackets == 0 and limit != 0):
                split_positions.append(index)
                limit -= 1
            else:
                if character == "(":
                    parenthesis += 1
                if character == ")":
                    parenthesis -= 1
                if character == "{":
                    brackets += 1
                if character == "}":
                    brackets -= 1

        split_positions.append(len(string)+1)  # virtual comma at the end

        result = []
        last_position = 0
        for position in split_positions:
            result.append(string[last_position:position])
            last_position = position + 1

        return result

    def _in_func(self, string, character):
        """
        Returns true of character is in string when excluding occurances
        within parenthesis.
        """

        if len(self._split_func(string, character, 1)) == 2:
            return True
        else:
            return False

    def _in_func_brack(self, string, character):
        """
        Returns true of character is in string when excluding occurances
        within parenthesis and brackets.
        """

        if len(self._split_func_brack(string, character, 1)) == 2:
            return True
        else:
            return False