forked from chinjun/clash-config-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·50 lines (39 loc) · 1.18 KB
/
main.py
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
#!/usr/bin/env python3
import re
import sys
import yaml
from collections import OrderedDict
import utils
import v1
_HELP_TEXT = """\
Usage: python main.py <source-path> [save-path]
<source-path>: path to preprocessor config
[save-path]: path to clash config
See also:
https://github.com/Kr328/clash-config-preprocessor
"""
def main():
utils.setup_order_yaml()
if len(sys.argv) < 2:
print("Argument source required")
print(_HELP_TEXT)
return
print("开始执行: " + sys.argv[1] + " ...")
with open(sys.argv[1], "r") as f:
data: OrderedDict = yaml.load(f, Loader=yaml.Loader)
if data["preprocessor"]["version"] == 1:
result = v1.handle_v1(data)
else:
print("Unsupported version")
return
result = yaml.dump(result, default_flow_style=False, allow_unicode=True)
regex = re.compile(r'\b(password:\s*)\b[\"\']?(.*)[\'\"]?\b', re.VERBOSE)
result = regex.sub(r'\1"\2"', result)
if len(sys.argv) > 2:
with open(sys.argv[2], "w") as f:
f.write(result)
print("生成Clash配置文件: " + sys.argv[2])
else:
print(result)
if __name__ == "__main__":
main()