aboutsummaryrefslogtreecommitdiff
path: root/src/utils/parse.py
blob: d2198e5fde46eab6fe5d52466fc5ffce170d23fa (about) (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import decimal, io, typing
from . import datetime, errors

COMMENT_TYPES = ["#", "//"]
def hashflags(filename: str
        ) -> typing.List[typing.Tuple[str, typing.Optional[str]]]:
    hashflags = [] # type: typing.List[typing.Tuple[str, typing.Optional[str]]]
    with io.open(filename, mode="r", encoding="utf8") as f:
        for line in f:
            line = line.strip("\n")
            found = False
            for comment_type in COMMENT_TYPES:
                if line.startswith(comment_type):
                    line = line.replace(comment_type, "", 1).lstrip()
                    found = True
                    break

            if not found:
                break
            elif line.startswith("--"):
                hashflag, sep, value = line[2:].partition(" ")
                hashflags.append((hashflag, (value if sep else None)))
    return hashflags

class Docstring(object):
    def __init__(self, description: str, items: typing.Dict[str, str],
            var_items: typing.Dict[str, typing.List[str]]):
        self.description = description
        self.items = items
        self.var_items = var_items

def docstring(s: str) -> Docstring:
    description = ""
    last_item = None
    last_item_no_space = False
    items = {} # type: typing.Dict[str, str]
    var_items = {} # type: typing.Dict[str, typing.List[str]]
    if s:
        for line in s.split("\n"):
            line = line.strip()

            if line:
                if line[0] == ":":
                    key, _, value = line[1:].partition(": ")
                    last_item = key.lstrip("-")
                    last_item_no_space = key.startswith("-")

                    if key in var_items:
                        var_items[last_item].append(value)
                    elif key in items:
                        var_items[last_item] = [items.pop(last_item), value]
                    else:
                        items[last_item] = value
                else:
                    if last_item:
                        if last_item_no_space:
                            items[last_item] += line
                        else:
                            items[last_item] += " %s" % line
                    else:
                        if description:
                            description += " "
                        description += line
    return Docstring(description, items, var_items)

def keyvalue(s: str, delimiter: str=" "
        ) -> typing.Dict[str, typing.Optional[str]]:
    items = {} # type: typing.Dict[str, typing.Optional[str]]
    pairs = s.split(delimiter)
    for pair in filter(None, pairs):
        key, sep, value = pair.partition("=")
        if sep:
            items[key] = value
        else:
            items[key] = None
    return items

def try_int(s: str) -> typing.Optional[int]:
    try:
        return int(s)
    except ValueError:
        return None

def line_normalise(s: str) -> str:
    lines = list(filter(None, [line.strip() for line in s.split("\n")]))
    return "  ".join(line.replace("  ", " ") for line in lines)

def parse_number(s: str) -> str:
    try:
        decimal.Decimal(s)
        return s
    except:
        pass

    unit = s[-1].lower()
    number_str = s[:-1]
    try:
        number = decimal.Decimal(number_str)
    except:
        raise ValueError("Invalid format '%s' passed to parse_number" %
            number_str)

    if unit == "k":
        number *= decimal.Decimal("1_000")
    elif unit == "m":
        number *= decimal.Decimal("1_000_000")
    elif unit == "b":
        number *= decimal.Decimal("1_000_000_000")
    else:
        raise ValueError("Unknown unit '%s' given to parse_number" % unit)
    return str(number)

def timed_args(args, min_args: int=0):
    if args and args[0][0] == "+":
        if len(args[1:]) < min_args:
            raise errors.EventError("Not enough arguments")
        time = datetime.from_pretty_time(args[0][1:])
        if time == None:
            raise errors.EventError("Invalid timeframe")
        return time, args[1:]
    return None, args

def format_tokens(s: str, names: typing.List[str], sigil: str="$"
        ) -> typing.List[typing.Tuple[int, str]]:
    names = names.copy()
    names.sort()
    names.reverse()

    i = 0
    max = len(s)-1
    sigil_found = False
    tokens: typing.List[typing.Tuple[int, str]] = []

    while i < max:
        if s[i] == sigil:
            i += 1
            if not s[i] == sigil:
                for name in names:
                    if len(name) <= (len(s)-i) and s[i:i+len(name)] == name:
                        tokens.append((i-1, "%s%s" % (sigil, name)))
                        i += len(name)
                        break
            else:
                tokens.append((i-1, "$$"))
        i += 1
    return tokens

def format_token_replace(s: str, vars: typing.Dict[str, str],
        sigil: str="$") -> str:
    vars = vars.copy()
    vars.update({sigil: sigil})
    tokens = format_tokens(s, list(vars.keys()), sigil)
    tokens.sort(key=lambda x: x[0])
    tokens.reverse()
    for i, token in tokens:
        s = s[:i] + vars[token.replace(sigil, "", 1)] + s[i+len(token):]
    return s

class SpecArgumentType(object):
    def __init__(self, type_name: str, name: typing.Optional[str], exported: str):
        self.type = type_name
        self._name = name
        self.exported = exported

    def name(self) -> typing.Optional[str]:
        return self._name
    def simple(self, args: typing.List[str]) -> typing.Tuple[typing.Any, int]:
        return None, -1
    def error(self) -> typing.Optional[str]:
        return None

class SpecArgumentTypeWord(SpecArgumentType):
    def simple(self, args: typing.List[str]) -> typing.Tuple[typing.Any, int]:
        if args:
            return args[0], 1
        return None, 1
class SpecArgumentTypeWordLower(SpecArgumentTypeWord):
    def simple(self, args: typing.List[str]) -> typing.Tuple[typing.Any, int]:
        out = SpecArgumentTypeWord.simple(self, args)
        if out[0]:
            return out[0].lower(), out[1]
        return out

class SpecArgumentTypeString(SpecArgumentType):
    def name(self):
        return "%s ..." % SpecArgumentType.name(self)
    def simple(self, args: typing.List[str]) -> typing.Tuple[typing.Any, int]:
        return " ".join(args), len(args)
class SpecArgumentTypeTime(SpecArgumentType):
    def name(self):
        return "+%s" % (SpecArgumentType.name(self) or "time")
    def simple(self, args: typing.List[str]) -> typing.Tuple[typing.Any, int]:
        time, _ = timed_args(args)
        return time, 1
    def error(self) -> typing.Optional[str]:
        return "Invalid timeframe"

SPEC_ARGUMENT_TYPES = {
    "word": SpecArgumentTypeWord,
    "wordlower": SpecArgumentTypeWordLower,
    "string": SpecArgumentTypeString,
    "time": SpecArgumentTypeTime
}

class SpecArgument(object):
    def __init__(self, optional: bool, types: typing.List[SpecArgumentType]):
        self.optional = optional
        self.types = types

def argument_spec(spec: str) -> typing.List[SpecArgument]:
    out: typing.List[SpecArgument] = []
    for spec_argument in spec.split(" "):
        optional = spec_argument[0] == "?"

        argument_types: typing.List[SpecArgumentType] = []
        for argument_type in spec_argument[1:].split("|"):
            exported = ""
            if "~" in argument_type:
                exported = argument_type.split("~", 1)[1]
                argument_type = argument_type.replace("~", "", 1)

            argument_type_name: typing.Optional[str] = None
            name_end = argument_type.find(">")
            if argument_type.startswith("<") and name_end > 0:
                argument_type_name = argument_type[1:name_end]
                argument_type = argument_type[name_end+1:]

            argument_type_class = SpecArgumentType
            if argument_type in SPEC_ARGUMENT_TYPES:
                argument_type_class = SPEC_ARGUMENT_TYPES[argument_type]
            argument_types.append(argument_type_class(argument_type,
                argument_type_name, exported))
        out.append(SpecArgument(optional, argument_types))
    return out

def argument_spec_human(spec: typing.List[SpecArgument]) -> str:
    out: typing.List[str] = []
    for spec_argument in spec:
        names = [t.name() or t.type for t in spec_argument.types]
        names = list(filter(None, names))

        if spec_argument.optional:
            format = "[%s]"
        else:
            format = "<%s>"
        out.append(format % "|".join(names))
    return " ".join(out)