File: convert_script_path.py

package info (click to toggle)
aseba-plugin-blockly 20180211+git-2
  • links: PTS
  • area: non-free
  • in suites: bullseye, buster, sid
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (55 lines) | stat: -rwxr-xr-x 1,435 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
#!/usr/bin/python

from bs4 import BeautifulSoup
import sys
import re
from sets import Set

FILE_MAPPING = {
	r"^\.\./blockly/blocks/.+\.js"     : "js/blocks_compressed.js",
	r"^\.\./blockly/generators/([a-z]+)(?:/.+)?\.js" : r"js/\1_compressed.js",
	r"^\.\./blockly/overrides/.+\.js"  : "js/overrides_compressed.js",
	r"^\.\./blockly/blockly_uncompressed\.js" : "js/blockly_compressed.js",
	r"^\.\./blockly/msg/js/([a-z]{2}).js" : r"js/i18n/\1.js",
	r"^\.\./blockly/msg/messages.js" : r"js/i18n/en.js"
}

def replace_node_path(dom):
	found = Set()
	lst = dom.find_all("script", src=re.compile(r"^\.\./blockly"))
	for node in lst:
		for pattern, replacement in FILE_MAPPING.iteritems():
			pattern = re.compile(pattern)
			node_path = node.get("src")
			found.add(node_path)
			if re.match(pattern, node_path):
				print(node.get(node_path), replacement)
				res = re.sub(pattern, replacement, node_path)
				if res in found:
					node.decompose()
				else:
					node["src"] = res
					found.add(res)
				break


def main():
	try:
		file = sys.argv[1]
		with open(file, "r") as handle:
			dom = BeautifulSoup(handle, "lxml")
			replace_node_path(dom)
			html = ''.join([line.strip() for line in unicode(dom).split('\n')])
			with open(file, "w") as handle:
				handle.write(html.encode('UTF-8'))

		exit(0)
	except Exception as e:
		print(e)
		import traceback
		traceback.print_exc()
		exit(1)


if __name__ == "__main__":
    main()