This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
packtool.py
executable file
·50 lines (40 loc) · 1.65 KB
/
packtool.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
import json
import argparse
import string
import random
import base64
parser = argparse.ArgumentParser(description='nedb pack/unpack tool')
parser.add_argument('action', help='action')
parser.add_argument('file', help='pack to pack/unpack')
args = parser.parse_args()
random_ids = set()
def generate_random_id():
random_id = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
while random_id in random_ids:
random_id = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
return random_id
def img_to_base_64(image):
with open(image.replace("systems/D35E/",""), "rb") as img_file:
return base64.b64encode(img_file.read()).decode('utf-8')
if args.action == 'unpack' or args.action == 'repack':
data = []
with open('packs/'+args.file+'.db', 'r') as read_file:
db = read_file.read().splitlines()
for item in db:
data.append(json.loads(item))
with open('source/'+args.file+'.json', "w") as outfile:
outfile.write(json.dumps(data, indent=4))
if args.action == 'pack' or args.action == 'repack':
with open('source/'+args.file+'.json', "r") as read_file:
data = json.load(read_file)
with open('packs/'+args.file+'.db', 'w') as outfile:
for item in data:
if '_skip_' not in item['name']:
if item['_id'] == 'generate':
item['_id'] = generate_random_id()
# try:
# item['img'] = 'data:image/png;base64, ' + img_to_base_64(item['img'])
# except:
# pass
outfile.write(json.dumps(item))
outfile.write('\n')