-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_services.py
More file actions
1185 lines (983 loc) · 41 KB
/
document_services.py
File metadata and controls
1185 lines (983 loc) · 41 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import re
import sys
from os import path, listdir
from abc import ABC, abstractmethod
import traceback as tb
from subprocess import Popen, PIPE
from typing import Any, Iterable
WARNCOUNT: dict[str, int] = {}
CURRENT_SERVICE: str | None = None
def warn(msg: str, *context: Any):
"""Prints a warning message to the console and increments the global WARNCOUNT variable.
:param str msg: The warning message to print.
:param Any *context: Any contextual info to be passed along.
"""
WARN = "\033[0m\033[43m\033[30mWARN:\033[0m "
WARN_TAB = "\033[0m \033[43m\033[33m|\033[0m "
WARN_TAB_LAST = "\033[0m \033[43m\033[33m\033[58;5;0m\033[4m|\033[0m "
WARNCOUNT[msg] = (WARNCOUNT.get(msg) or 0) + 1
stack = tb.extract_stack()
stack_str = " -> ".join([f"\033[34m{frame.name}\033[0m" for frame in stack if frame != stack[-1]])
service_msg: str
if CURRENT_SERVICE is None:
service_msg = ""
else:
service_msg = f"in service \033[36m{CURRENT_SERVICE}\033[0m "
context = tuple(f"{c}".strip() for c in context)
context_formatted = [f"\n{c}\033[0m".replace('\n', f"\n\033[0m{WARN_TAB} \033[2m") for c in context]
if len(context_formatted) > 0:
context_formatted[-1] = context_formatted[-1].replace(WARN_TAB, WARN_TAB_LAST)
print(
f"{WARN}\033[31m{msg}\033[0m {service_msg}({stack_str})",
*context_formatted,
file=sys.stderr,
sep=""
)
def convert_php_type_to_normal_type(php_type: str) -> tuple[str, bool]:
CONVERSIONS = {
"int": "int",
"string": "String",
"bool": "bool",
}
if php_type[0] == '?':
nullable = True
php_type = php_type[1:]
else:
nullable = False
if php_type in CONVERSIONS.keys():
return CONVERSIONS[php_type], nullable
else:
warn("unrecognized php datatype", php_type)
return php_type, nullable
def convert_moodle_type_to_normal_type(param_type: str) -> str:
CONVERSIONS = {
"PARAM_INT": "int",
"PARAM_TEXT": "String",
"PARAM_URL": "String",
"PARAM_BOOL": "bool",
}
return CONVERSIONS.get(param_type, param_type)
def explain_php_value(val: str) -> tuple[None | str | int | bool, str]:
SPECIAL_VALUES = {
"$USER->id": ("derived from token", "int"), # this exact phrase is checked for in the web UI
"null": (None, "")
}
if val in SPECIAL_VALUES.keys():
return SPECIAL_VALUES[val]
elif val.isnumeric():
return (int(val), "int")
elif val.lower() in ("true", "false"):
return (val.lower() == "true", "bool")
elif val[0] + val[-1] in ("''", '""'):
# see if the same kind of quote is within string
if val[1:-1].count(val[0]) > 0:
warn("found potentially non-literal string", val)
return (val[1:-1], "String")
else:
warn("found unknown value", val)
return (f"unknown: {val}", "unknown")
def parse_isrequired(inpot: str) -> bool | None:
if inpot in ('VALUE_REQUIRED', ''):
return True
elif inpot == 'VALUE_DEFAULT':
return False
else:
warn("found unparseable value for isrequired", inpot)
return None
def parse_nullable(inpot: str) -> bool | None:
if inpot in ('', 'NULL_NOT_ALLOWED'):
return False
elif inpot == 'NULL_ALLOWED':
return True
else:
warn("found weird value for nullable", inpot)
return None
class PHPNameResolution:
__slots__ = ('namespace', 'imports')
namespace: str | None
imports: list[str]
def __init__(self, namespace: str | None, imports: list[str]):
self.namespace = namespace
self.imports = imports
def __str__(self) -> str:
statements = []
if self.namespace is not None:
statements.append(f"namespace local_lbplanner\\{self.namespace};")
for use in self.imports:
statements.append(f"use local_lbplanner\\{use}")
return " ".join(statements)
class PHPExpression(ABC):
@abstractmethod
def __str__(self) -> str:
raise NotImplementedError()
class PHPString(PHPExpression, ABC):
@abstractmethod
def get_value(self) -> str:
raise NotImplementedError()
class PHPStringLiteral(PHPString):
__slots__ = ('value')
value: str
def __init__(self, val: str):
self.value = val
def __str__(self) -> str:
return f"'{self.value.replace('\'', '\\\'')}'"
def get_value(self) -> str:
return self.value
class PHPConcat(PHPString):
__slots__ = ('left', 'right')
left: PHPString
right: PHPString
def __init__(self, left: PHPString, right: PHPString):
self.left = left
self.right = right
def __str__(self) -> str:
return f"{self.left}.{self.right}"
def get_value(self) -> str:
return self.left.get_value() + self.right.get_value()
class PHPUserID(PHPExpression):
def __str__(self) -> str:
return "$USER->id"
class PHPArray(PHPExpression):
__slots__ = ('keys', 'values')
keys: list[PHPString] | None
values: list[PHPExpression]
def __init__(self, vals: list[PHPExpression], keys: list[PHPString] | None = None):
self.keys = keys
self.values = vals
def __str__(self) -> str:
inner: Iterable[str]
if self.keys is not None:
inner = (f"{k} => {v}" for k, v in zip(self.keys, self.values))
else:
inner = (str(v) for v in self.values)
return '[' + ", ".join(inner) + ']'
class PHPClassMemberFunction(PHPExpression):
__slots__ = ('classname', 'funcname', 'fp')
classname: str
funcname: str
fp: str | None
def __init__(self, classname: str, funcname: str, fp: str | None):
self.classname = classname
self.funcname = funcname
self.fp = fp
def resolve(self) -> PHPExpression:
meth_pattern = rf"public static function {self.funcname}\(\)(?: ?: ?\w+)? ?{{(?P<body>.*?)}}"
if self.fp is None:
# already warned in parse_imports, we don't need to warn again
return PHPConstant('null')
with open(self.fp, "r") as f:
new_file_content = f.read()
meth_matches: list[str] = re.findall(meth_pattern, new_file_content, re.DOTALL)
if len(meth_matches) == 0:
warn("Missing class member", f"couldn't find {self} inside {self.fp}")
return PHPConstant('null')
elif len(meth_matches) > 1:
raise Exception(f"Found multiple definitions for {self} inside {self.fp}")
else:
imports = extract_imports(new_file_content)
result = parse_code(meth_matches[0], imports)
return result
def __str__(self) -> str:
return f"{self.classname}::{self.funcname}()"
class PHPEnum():
@classmethod
def getcases(cls, classname: str) -> dict[str, str]:
# https://regex101.com/r/p5FzCh
casepattern = r"const (\w+) = (\d+|true|false|(['\"]).*?\3)"
fullbody_pattern = f"class {classname} extends (Enum|\\w+) {{(.*?)}}"
cases = {}
fp = f"lbplanner/classes/enums/{classname}.php"
if not path.exists(fp):
warn("Couldn't find enum file", fp)
return {}
with open(fp, "r") as f:
matches: list[list[str]] = re.findall(fullbody_pattern, f.read(), re.DOTALL)
if len(matches) == 1:
if matches[0][0] != 'Enum':
cases = cls.getcases(matches[0][0])
body = matches[0][1]
else:
warn("couldn't parse enum", f"name: {classname}", matches)
matches2: list[str] = re.findall(casepattern, body)
for match in matches2:
val = match[1].replace("'", '"')
cases[match[0]] = val
return cases
class PHPEnumCase(PHPEnum, PHPString):
__slots__ = ('classname', 'casename', 'fp')
classname: str
casename: str
fp: str
def __init__(self, classname: str, casename: str, fp: str):
self.classname = classname
self.casename = casename
self.fp = fp
def resolve(self) -> PHPString:
cases = self.getcases(self.classname)
if self.casename not in cases.keys():
warn("enum member not found", f"{self.classname}::{self.casename}", cases)
return PHPStringLiteral("?")
val = cases[self.casename]
if val.startswith('"') and val.endswith('"'):
val = val[1:-1]
return PHPStringLiteral(val)
def get_value(self) -> str:
return self.resolve().get_value()
def __str__(self) -> str:
return f"{self.classname}::{self.casename}"
class PHPEnumFormat(PHPEnum, PHPClassMemberFunction, PHPString):
def resolve(self) -> PHPString:
cases = self.getcases(self.classname)
# capitalizing first letter of each key
cases = {"".join([name[0].upper(), name[1:].lower()]): case for name, case in cases.items()}
return PHPStringLiteral("{ " + ", ".join([f"{name} = {value}" for name, value in cases.items()]) + " }")
def get_value(self) -> str:
return self.resolve().get_value()
class PHPConstructor(PHPExpression):
__slots__ = ('name', 'parameters')
name: str
parameters: list[PHPExpression]
def __init__(self, name: str, params: list[PHPExpression]):
self.name = name
self.parameters = params
def __str__(self) -> str:
return f"new {self.name}(" + ", ".join(str(p) for p in self.parameters) + ")"
def toIR(self) -> 'IRElement':
match self.name:
case 'external_function_parameters' | 'external_single_structure':
assert isinstance(self.parameters[0], PHPArray)
arr = self.parameters[0]
fields = {}
if len(arr.values) != 0:
assert arr.keys is not None
for k, v in zip(arr.keys, arr.values):
assert isinstance(v, PHPConstructor)
fields[k.get_value()] = v.toIR()
desc = ""
if len(self.parameters) >= 2:
assert isinstance(self.parameters[1], PHPString)
desc = self.parameters[1].get_value()
required = True
if len(self.parameters) >= 3:
assert isinstance(self.parameters[2], PHPConstant)
_required = parse_isrequired(self.parameters[2].name)
if _required is not None:
required = _required
return IRObject(fields, description=desc, required=required)
case 'external_multiple_structure':
assert isinstance(self.parameters[0], PHPConstructor)
con = self.parameters[0]
desc = ""
if len(self.parameters) >= 2:
assert isinstance(self.parameters[1], PHPString)
desc = self.parameters[1].get_value()
required = True
if len(self.parameters) >= 3:
assert isinstance(self.parameters[2], PHPConstant)
_required = parse_isrequired(self.parameters[2].name)
if _required is not None:
required = _required
return IRArray(con.toIR(), description=desc, required=required)
case 'external_value':
if len(self.parameters) < 2:
warn("found external_value with not enough parameters", self.parameters)
return IRValue(None, None, nullable=True, description="", required=True)
assert isinstance(self.parameters[0], PHPConstant)
assert isinstance(self.parameters[1], PHPString)
typ = convert_moodle_type_to_normal_type(self.parameters[0].name)
desc = self.parameters[1].get_value()
required = True
if len(self.parameters) >= 3:
assert isinstance(self.parameters[2], PHPConstant)
_required = parse_isrequired(self.parameters[2].name)
if _required is not None:
required = _required
default: None | bool | str = None
if len(self.parameters) >= 4:
if isinstance(self.parameters[3], PHPConstant):
match self.parameters[3].name:
case 'null':
default = None
case 'false':
default = False
case 'true':
default = True
case _:
warn("unknown PHPConstant as default", self.parameters[3])
default = None
elif isinstance(self.parameters[3], PHPUserID):
default = "derived from token"
nullable = False
if len(self.parameters) >= 5:
assert isinstance(self.parameters[4], PHPConstant)
_nullable = parse_nullable(self.parameters[4].name)
if _nullable is not None:
nullable = _nullable
return IRValue(typ, default_value=default, nullable=nullable, description=desc, required=required)
case _:
warn("unkown constructor name", self.name)
return IRValue(None, None, nullable=True)
class PHPConstant(PHPExpression):
__slots__ = ('name')
name: str
def __init__(self, name: str):
self.name = name
def __str__(self) -> str:
return self.name
class SlotsDict:
@property
def __dict__(self):
return {name: self.__getattribute__(name) for name in self.get_slots()}
def get_slots(self):
slots = tuple()
for cls in self.__class__.__mro__:
if cls != SlotsDict and issubclass(cls, SlotsDict):
slots = cls.__slots__ + slots
return slots
class FunctionInfo(SlotsDict):
__slots__ = ('name', 'group', 'capabilities', 'description', 'path')
def __init__(self, name: str, group: str, capabilities: list[str], description: str, path: str):
self.name = name
self.group = group
self.capabilities = capabilities
self.description = description
self.path = path
class FunctionInfoEx(FunctionInfo):
__slots__ = ('parameters', 'returns')
def __init__(self,
parent: FunctionInfo,
parameters: 'IRElement | None',
returns: 'IRElement | None'):
super().__init__(**parent.__dict__)
self.parameters = parameters
self.returns = returns
class DocString_TypeDescPair(SlotsDict):
__slots__ = ('typ', 'description')
typ: str
description: str
def __init__(self, typ: str, description: str):
self.typ = typ
self.description = description
class DocString(SlotsDict):
__slots__ = ('description', 'params', 'returns', 'subpackage', 'copyright')
description: str
params: dict[str, DocString_TypeDescPair]
returns: DocString_TypeDescPair | None
subpackage: str | None
copyright: tuple[int, str] | None
def __init__(
self,
desc: str,
params: dict[str, DocString_TypeDescPair],
returns: DocString_TypeDescPair | None,
subpackage: str | None,
copyright: tuple[int, str] | None,
):
self.description = desc
self.params = params
self.returns = returns
self.subpackage = subpackage
self.copyright = copyright
class ExtractedAPIFunction(SlotsDict):
__slots__ = ('docstring', 'name', 'params', 'returns', 'body')
docstring: DocString
name: str
params: dict[str, str]
returns: str
body: str
def __init__(self, docstring: DocString, name: str, params: dict[str, str], returns: str, body: str):
self.docstring = docstring
self.name = name
self.params = params
self.returns = returns
self.body = body
class IRElement(SlotsDict, ABC):
__slots__ = ('description', 'required', 'type')
def __init__(self, description: str, required: bool):
self.description = description
self.required = required
class IRValue(IRElement):
__slots__ = ('default_value', 'type', 'nullable')
def __init__(self, type, default_value, nullable: bool, **kwargs):
self.type = type
self.default_value = default_value
self.nullable = nullable
super().__init__(**kwargs)
class IRObject(IRElement):
__slots__ = ('fields',)
fields: dict[str, IRElement]
def __init__(self, fields: dict[str, IRElement], **kwargs):
self.fields = fields
self.type = 'ObjectValue'
super().__init__(**kwargs)
class IRArray(IRElement):
__slots__ = ('value',)
value: IRElement
def __init__(self, value: IRElement, **kwargs):
self.value = value
self.type = 'ArrayValue'
super().__init__(**kwargs)
def parse_code(code: str, nr: PHPNameResolution) -> PHPExpression:
code = code.strip()
while True:
i, expr = parse_statement(code, nr)
if expr is not None:
return expr
code = code[i:].strip()
def parse_statement(code: str, nr: PHPNameResolution) -> tuple[int, PHPExpression | None]:
buf = []
i = 0
while True:
c = code[i]
if c.isalpha() or c == '_':
buf.append(c)
i += 1
elif c.isspace():
i += 1
if len(buf) == 0:
continue
word = "".join(buf)
buf = []
if word == 'global':
# just skip this statement; we're not interested in globals
return i + code[i:].index(';') + 1, None
elif word == 'return':
iplus, expr = parse_expression(code[i:], nr)
i += iplus
return i + 1, expr
else:
raise ValueError(f"unknown keyword: {word}")
elif c == ';':
return i + 1, None
elif code[i:i + 2] == '//':
i += code[i:].index('\n')
else:
raise ValueError(f"unknown char: {c}")
def parse_expression(code: str, nr: PHPNameResolution) -> tuple[int, PHPExpression | None]:
expr: PHPExpression | None = None
buf: list[str] = []
i = 0
while True:
if len(buf) == 0 and code[i:].startswith('$USER->id'):
assert expr is None
i += len('$USER->id')
expr = PHPUserID()
c = code[i]
if c.isalpha() or c == '_':
assert expr is None
buf.append(c)
i += 1
elif c.isspace():
i += 1
if len(buf) == 0:
continue
word = "".join(buf)
buf = []
if word == 'new':
iplus, expr = parse_constructor(code[i:], nr)
i += iplus
else:
# just assume this is a constant
assert expr is None
expr = PHPConstant(word)
elif c == '[':
assert expr is None
i += 1
if len(buf) > 0:
raise NotImplementedError("map access not implemented")
iplus, expr = parse_array(code[i:], nr)
i += iplus
elif c in '\'"':
assert len(buf) == 0
assert expr is None
iplus, expr = parse_string(code[i:])
i += iplus
elif c == '.':
assert isinstance(expr, PHPString)
i += 1
iplus, after = parse_expression(code[i:], nr)
i += iplus
assert isinstance(after, PHPString)
expr = PHPConcat(expr, after)
elif code[i:i + 2] == '::':
# remote value
assert len(buf) > 0
assert expr is None
i += 2
iplus = 1
while 95 <= ord(code[i + iplus].lower()) <= 122: # until it hits non-word character
iplus += 1
is_func = code[i + iplus] == '('
membername = code[i:i + iplus]
classname = "".join(buf)
i += iplus
if is_func:
assert code[i:i + 2] == '()'
i += 2
C: type[PHPClassMemberFunction]
fp_import: str | None
if membername == 'format':
C = PHPEnumFormat
fp_import = path.join(path.dirname(__file__), "lbplanner", "enums", f"{classname}.php")
else:
C = PHPClassMemberFunction
fp_import = find_import(nr, classname)
expr = C(classname, membername, fp_import).resolve()
buf = []
else:
fp_import = path.join(path.dirname(__file__), "lbplanner", "enums", f"{classname}.php")
expr = PHPEnumCase(classname, membername, fp_import).resolve()
buf = []
else:
# unkown character? simply bail
if len(buf) > 0:
# assume we have a constant on our hands
word = "".join(buf)
assert expr is None
expr = PHPConstant(word)
return i, expr
def parse_constructor(code: str, nr: PHPNameResolution) -> tuple[int, PHPConstructor]:
paramlist: list[PHPExpression] = []
fnname, parenth, params = code.partition('(')
assert fnname.replace('_', '').isalpha()
assert parenth == '(' # if parenthesis not found, parenth is an empty string
offset = len(fnname) + 1
i = 0
while True:
iplus, expr = parse_expression(params[i:], nr)
i += iplus
if expr is not None:
paramlist.append(expr)
if params[i] == ',':
i += 1
elif params[i] == ')':
return i + offset + 1, PHPConstructor(fnname, paramlist)
else:
raise ValueError(f"unknown char: {params[i]}")
def parse_array(code: str, nr: PHPNameResolution) -> tuple[int, PHPArray]:
associative: bool | None = None
keys: list[PHPString] = []
vals: list[PHPExpression] = []
i = 0
while True:
iplus, expr = parse_expression(code[i:], nr)
i += iplus
if code[i] == ',':
i += 1
assert expr is not None
vals.append(expr)
if associative is None:
associative = False
elif code[i:i + 2] == '=>':
i += 2
assert isinstance(expr, PHPString)
keys.append(expr)
associative = True
elif code[i] == ']':
if expr is not None:
vals.append(expr)
if associative is True:
assert len(keys) == len(vals)
return i + 1, PHPArray(vals, keys if associative else None)
else:
raise ValueError(f"unknown char: {code[i]}")
def parse_string(code: str) -> tuple[int, PHPStringLiteral]:
quotetype = code[0]
assert quotetype in '\'"'
simple = quotetype == '\''
if not simple:
raise NotImplementedError() # TODO
result: list[str] = []
i = 1
while True:
c = code[i]
i += 1
if c == quotetype:
return i, PHPStringLiteral("".join(result))
elif c == '\\':
if code[i] == quotetype:
result.append(quotetype)
elif code[i] == '\\':
result.append('\\')
elif simple:
result.append('\\')
result.append(code[i])
elif code[i] == 'n':
result.append('\n')
elif code[i] == 'r':
result.append('\r')
else:
raise NotImplementedError(f"can't escape \"{code[i]}\" in double-quoted string")
i += 1
else:
if simple:
result.append(c)
def extract_function_info(file_content: str) -> list[FunctionInfo]:
function_infos = []
# Removing line comments, PHP tags, and definitions
clean_content = re.sub(r"//.*|<\?php|defined\(.*\)\s*\|\|\s*die\(\);", "", file_content)
# Splitting the content based on function definition blocks
# https://regex101.com/r/qyzYks
functions = re.findall(r"'(local_lbplanner_(\w+?)_(\w+))' => \[(.*?)\],", clean_content, re.DOTALL)
# to make sure we never accidentally duplicate descriptions
existing_descriptions = []
for function in functions:
func_dict = {}
# Extracting function name and group
func_dict["name"] = function[2]
func_dict["group"] = function[1]
# Extracting and adjusting capabilities
capabilities = re.search(r"'capabilities' => '(.*?:.*?)'", function[3])
if capabilities is None:
# check if call needs no capabilities
capabilities = re.search(r"'capabilities' => ''", function[3])
func_dict["capabilities"] = [] if capabilities else None
else:
func_dict["capabilities"] = [cap.strip() for cap in capabilities.group(1).split(',') if len(cap) > 0]
# Extracting description
description = re.search(r"'description' => '([^\n]*)'", function[3])
func_dict["description"] = description.group(1).replace('\\\'', '\'') if description else None
# Extracting and adjusting path
classpath = re.search(r"'classpath' => 'local/(.*?)'", function[3])
func_dict["path"] = classpath.group(1) if classpath else None
# Only adding to the list if all information is present
if all(value is not None for value in func_dict.values()):
finfo = FunctionInfo(**func_dict)
function_infos.append(finfo)
if finfo.description in existing_descriptions:
warn("duplicated API function description", finfo.description)
else:
existing_descriptions.append(finfo.description)
else:
warn("Could not gather all info for API function", func_dict["name"], func_dict)
if len(function_infos) == 0:
warn("Couldn't find any functions!")
# double-checking using the services list below
services_function_block = re.search(r"\$services = \[.*?'functions' => \[(['a-z_,\s]+)\]", clean_content, re.DOTALL)
if services_function_block is None:
warn("Couldn't find $services")
else:
services_functions = re.findall(r"'local_lbplanner_([a-z]+)_([a-z_]+)'", services_function_block[1])
function_infos_copy = function_infos.copy()
for function in services_functions:
# Extracting function name and group
func_name = function[1]
func_group = function[0]
found = False
for functioninfo in function_infos_copy:
if functioninfo.name == func_name and functioninfo.group == func_group:
function_infos_copy.remove(functioninfo)
found = True
break
if not found:
warn("Couldn't find service function in $functions", f"{func_group}_{func_name}")
for functioninfo in function_infos_copy:
# The ones left here are not in services_function.
warn("Couldn't find service function in $services", f"{functioninfo.group}_{functioninfo.name}")
# double-checking using existing files
function_infos_copy = function_infos.copy()
searchdir = './lbplanner/services'
for subdir in listdir(searchdir):
dirpath = path.join(searchdir, subdir)
if not path.isdir(dirpath):
warn('found file in services folder', subdir)
continue
for filename in listdir('lbplanner/services/' + subdir):
if path.isdir(filename):
warn('found directory in folder', filename, dirpath)
continue
if not filename.endswith('.php'):
warn('found non-php file in folder', filename, dirpath)
continue
for functioninfo in function_infos_copy:
if functioninfo.name == filename[:-4] and functioninfo.group == subdir:
function_infos_copy.remove(functioninfo)
found = True
break
if not found:
warn("Couldn't find service function in $function", f"{func_group}_{func_name}")
for functioninfo in function_infos_copy:
# The ones left here are not in the dirs.
warn("Couldn't find file in services folder", f"{functioninfo.group}/{functioninfo.name}.php")
return function_infos
def extract_api_functions(php_code: str, name: str) -> tuple[ExtractedAPIFunction | None, ExtractedAPIFunction | None, ExtractedAPIFunction | None]:
# Regular expression to match the function names and bodies
# https://regex101.com/r/9GtIMA
pattern = re.compile(
r"(?P<docstring> /\*\*\n(?: \*[^\n]*\n)*? \*/)\s*public static function (?P<name>\w+(?:_returns|_parameters|))\(\s*(?P<params>(?:\??(?:int|string|bool) \$[a-z]*(?:,\s+)?)*)\)(?:: (?P<returns>[a-z_]+))? (?P<body>{.+?^ }$)",
re.DOTALL | re.MULTILINE
)
# Find all matches in the PHP code
matches: list[tuple[str, str]] = pattern.findall(php_code)
parameters_function = None
returns_function = None
main_function = None
for match in matches:
# Extract function name
if len(match) == 4:
func_docstring, func_name, func_params, func_body = match
func_returns = None
elif len(match) == 5:
func_docstring, func_name, func_params, func_returns, func_body = match
else:
raise Exception("unreachable")
function_packed = ExtractedAPIFunction(
parse_docstring(func_docstring),
func_name,
parse_php_function_parameters(func_params),
func_returns,
func_body
)
if func_name.endswith("_parameters"):
parameters_function = function_packed
elif func_name.endswith("_returns"):
returns_function = function_packed
else:
main_function = function_packed
if parameters_function is None:
warn("Couldn't find parameters function", name, php_code)
if returns_function is None:
warn("Couldn't find returns function", name, php_code)
if main_function is None:
warn("Couldn't find main function", name, php_code)
return parameters_function, main_function, returns_function
def extract_main_api_docstring(inpot: str) -> DocString:
return parse_docstring(inpot[inpot.find('/**'):inpot.find('*/')])
def parse_docstring(inpot: str) -> DocString:
desc_a = []
params: dict[str, DocString_TypeDescPair] = {}
returns: DocString_TypeDescPair | None = None
subpackage: str | None = None
copyright: tuple[int, str] | None = None
isdesc = True
for line in inpot.splitlines():
strippedline = line[line.find('*') + 1:].strip()
if strippedline in ('*', ' ', ''):
continue # empty line, ignore
elif strippedline == '/':
break # last line, ignore
elif strippedline.startswith('NOTE:'):
isdesc = False
continue # internal notes, ignore
elif strippedline.startswith('@'):
isdesc = False
splitline = strippedline.split(' ')
match splitline[0]:
case '@param':
if splitline[2] in params:
warn("specified @param twice in docstring", splitline[2])
params[splitline[2]] = DocString_TypeDescPair(splitline[1], " ".join(splitline[3:]))
case '@return':
if returns is not None:
warn("specified @returns twice in docstring")
returns = DocString_TypeDescPair(splitline[1], " ".join(splitline[2:]))
case '@package':
if splitline[1] != "local_lbplanner":
warn("found @package with invalid value instead of local_lbplanner", splitline[1])
case '@subpackage':
subpackage = " ".join(splitline[1:])
case '@copyright':
copyright = int(splitline[1]), " ".join(splitline[2:])
case '@throws' | '@see' | '@link' | '@license':
pass
case unknown:
warn("unknown @-rule", unknown, line)
elif isdesc:
desc_a.append(strippedline)
desc = " ".join(desc_a)
# remove trailing period
if desc.endswith('.'):
desc = desc[:-1]
return DocString(desc, params, returns, subpackage, copyright)
def parse_php_function_parameters(inpot: str) -> dict[str, str]:
""" "int $a, string $b" → {"a": "int", "b": "string"} """
# https://regex101.com/r/zfWGKi
matches = re.findall(r"(\??[a-z]+)\s+\$([a-z_]+)", inpot)
out = {}
for match in matches:
out[match[1]] = match[0]
return out
def find_import(nr: PHPNameResolution, symbol: str) -> str | None:
def makepath(p: str, symbol: str):
return path.join(path.dirname(__file__), "lbplanner", p, f"{symbol}.php")
namespaces = { # it's technically possible to import from outside /classes/
"helpers": "classes/helpers",
"enums": "classes/enums",
"polyfill": "classes/polyfill",
"model": "classes/model",
}
fp_l: list[str] = []
for use in nr.imports:
im_symbol = use.split('\\')[-1].replace(';', '')
found = False
if im_symbol.startswith('{'):
for subsymbol in im_symbol[1:-1].split(','):
if subsymbol.strip() == symbol:
found = True
break
else:
found = symbol == im_symbol
if not found:
continue
for namespace, p in namespaces.items():
if use.startswith(namespace):
fp_l.append(makepath(p, symbol))
if len(fp_l) == 0 and nr.namespace is not None:
fallback = makepath(namespaces[nr.namespace], symbol)
if path.exists(fallback):
fp_l.append(fallback)
if len(fp_l) > 1:
warn("found potential import collision", f"{symbol} in [{nr}]")
return None