Revision 9e280d45
Added by jahoti about 2 years ago
test/gorilla.py | ||
---|---|---|
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 |
with open(path + file + os.sep + 'index.json') as f: |
|
22 |
data = json.load(f) |
|
23 |
|
|
24 |
type_, data['file'] = data['type'], file |
|
25 |
name_key = 'pattern' if type_ == 'page' else 'name' |
|
26 |
|
|
27 |
DB[type_][data[name_key]] = data |
|
28 |
del data['type'], data[name_key] |
|
29 |
if type_ == 'script': |
|
30 |
with open(path + file + os.sep + data['location']) as f: |
|
31 |
data['text'] = f.read() |
|
32 |
|
|
33 |
return DB |
|
34 |
|
|
35 |
|
|
36 |
def mkhachette(patterns): |
|
37 |
scripts, bags, pages, new_bags = {}, {}, {}, [] |
|
38 |
for pattern in patterns: |
|
39 |
pages[pattern] = data = DB['page'][pattern] |
|
40 |
type_, name = data['payload'] |
|
41 |
if type_ == 'script': |
|
42 |
scripts[name] = DB['script'][name] |
|
43 |
else: |
|
44 |
new_bags.append(name) |
|
45 |
|
|
46 |
while new_bags: |
|
47 |
name = new_bags.pop(0) |
|
48 |
if name in bags: |
|
49 |
continue |
|
50 |
|
|
51 |
bags[name] = data = DB['bag'][name]['components'] |
|
52 |
for type_, name in data: |
|
53 |
if type_ == 'script': |
|
54 |
scripts[name] = DB['script'][name] |
|
55 |
else: |
|
56 |
new_bags.append(name) |
|
57 |
|
|
58 |
items, path = [], DB['path'] |
|
59 |
for script, data in scripts.items(): |
|
60 |
item = {'hash': data['sha256']} |
|
61 |
with open(path + data['file'] + os.sep + data['location']) as f: |
|
62 |
item['text'] = f.read() |
|
63 |
|
|
64 |
items.append({'s' + script: item}) |
|
65 |
|
|
66 |
for bag, data in bags.items(): |
|
67 |
items.append({'b' + bag: [[type_[0], name] for type_, name in data]}) |
|
68 |
|
|
69 |
for page, data in pages.items(): |
|
70 |
type_, name = data['payload'] |
|
71 |
items.append({'p' + page: {'components': [type_[0], name]}}) |
|
72 |
|
|
73 |
return items |
|
74 |
|
|
75 |
|
|
76 |
if __name__ == '__main__': |
|
77 |
if len(sys.argv) < 3 or not os.path.isdir(sys.argv[1]): |
|
78 |
sys.stderr.write('Usage: %s [hydrilla content path] (page pattern (page pattern (...)))' % sys.argv[0]) |
|
79 |
sys.exit(1) |
|
80 |
|
|
81 |
DB = load_db(sys.argv[1]) |
|
82 |
print(json.dumps(mkhachette(sys.argv[2:]))) |
Also available in: Unified diff
Begin work on a Hydrilla-compatible virtual website for testing
The file test/gorilla.py will help with testing respositories.
It also provides a CLI Hydrilla > Hachette fix converter.