Project

General

Profile

Download (2.3 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / gorilla.py @ b1444d9c

1
#!/usr/bin/env python3
2
#
3
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
4
# Licensing information is collated in the `copyright` file
5

    
6
"""
7
A partial re-implementation of Hydrilla in Python, for testing purposes.
8

    
9
This will eventually be replaced with a build of the actual thing.
10
"""
11

    
12
import json, os, sys
13

    
14
def load_db(path):
15
	DB = {'script': {}, 'bag': {}, 'page': {}}
16
	if path[-1] != os.sep:
17
		path += os.sep
18
	
19
	DB['path'] = path
20
	for file in os.listdir(path):
21
		subdir = path + file
22
		
23
		if (os.st.S_IFMT(os.stat(subdir).st_mode) & os.st.S_IFDIR) == 0:
24
			continue
25
		
26
		with open(subdir + os.sep + 'index.json') as f:
27
			data = json.load(f)
28
		
29
		type_, data['file'] = data['type'], file
30
		name_key = 'pattern' if type_ == 'page' else 'name'
31
		
32
		DB[type_][data[name_key]] = data
33
		del data['type'], data[name_key]
34
		if type_ == 'script':
35
			with open(path + file + os.sep + data['location']) as f:
36
				data['text'] = f.read()
37
	
38
	return DB
39

    
40

    
41
def mkhachette(patterns):
42
	patterns = set(patterns)
43
	if '*' in patterns:
44
		patterns.remove('*')
45
		patterns.update(DB['page'].keys())
46
	
47
	scripts, bags, pages, new_bags = {}, {}, {}, []
48
	for pattern in patterns:
49
		pages[pattern] = data = DB['page'][pattern]
50
		type_, name = data['payload']
51
		if type_ == 'script':
52
			scripts[name] = DB['script'][name]
53
		else:
54
			new_bags.append(name)
55
	
56
	while new_bags:
57
		name = new_bags.pop(0)
58
		if name in bags:
59
			continue
60
		
61
		bags[name] = data = DB['bag'][name]['components']
62
		for type_, name in data:
63
			if type_ == 'script':
64
				scripts[name] = DB['script'][name]
65
			else:
66
				new_bags.append(name)
67
	
68
	items, path = [], DB['path']
69
	for script, data in scripts.items():
70
		item = {'hash': data['sha256']}
71
		with open(path + data['file'] + os.sep + data['location']) as f:
72
			item['text'] = f.read()
73
		
74
		items.append({'s' + script: item})
75
	
76
	for bag, data in bags.items():
77
		items.append({'b' + bag: [[type_[0], name] for type_, name in data]})
78
	
79
	for page, data in pages.items():
80
		type_, name = data['payload']
81
		items.append({'p' + page: {'components': [type_[0], name]}})
82
	
83
	return items
84

    
85

    
86
if __name__ == '__main__':
87
	if len(sys.argv) < 3 or not os.path.isdir(sys.argv[1]):
88
		sys.stderr.write('Usage: %s [hydrilla content path] (page pattern (page pattern (...)))' % sys.argv[0])
89
		sys.exit(1)
90
	
91
	DB = load_db(sys.argv[1])
92
	print(json.dumps(mkhachette(sys.argv[2:])))
(1-1/6)