-
Notifications
You must be signed in to change notification settings - Fork 36
/
require-files.py
64 lines (55 loc) · 2.01 KB
/
require-files.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
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
import argparse
import re
import os
import urllib.request
def get_file_content(path, requires):
if path.startswith('yaofang:'):
filename = os.path.join(requires['yaofang'], 'extension', path[len('yaofang://'):])
with open(filename, 'r', encoding='utf-8') as file:
return file.read()
if path.startswith('https:'):
with urllib.request.urlopen(path) as req:
return req.read().decode('utf-8')
return ''
def inline_required_files(yawf, configs):
with open(yawf, 'r', encoding='utf-8') as source_file:
source = source_file.readlines()
result = []
region_start, region_end = re.compile(r'^//#region @require (.*)'), re.compile(r'^//#endregion')
in_require = False
for line in source:
if region_end.match(line):
in_require = False
match = region_start.match(line)
if not in_require:
result.append(line)
if match:
uri = str(match.group(1))
if uri.startswith('https:') and not configs['remote']:
continue
in_require = True
content = get_file_content(uri, configs)
result.append(content.rstrip() + '\n')
output = ''.join(result)
with open(yawf, 'w', newline='\n', encoding='utf-8') as output_file:
output_file.write(output)
parser = argparse.ArgumentParser(description='A helper tool that inline required files')
parser.add_argument(
'yawf',
nargs='?',
default='./Yet_Another_Weibo_Filter.user.js',
help='path to yawf.user.js (default: "./Yet_Another_Weibo_Filter.user.js")'
)
parser.add_argument(
'--yaofang',
default='../yaofang',
help='path to yaofang repo, a extension folder should be there (default: "../yaofang")'
)
parser.add_argument(
'--remote',
default=False,
help='enable script fetching and updating remote resources'
)
args = vars(parser.parse_args())
inline_required_files(args['yawf'], { 'yaofang': args['yaofang'], 'remote': args['remote'] })