fileserver/migrate.py

81 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-08-31 19:20:21 +00:00
import json
import pathlib
import lxml.etree
import lxml.builder
from typing import Any
base = pathlib.Path('data')
base.mkdir(exist_ok=True)
data: dict[str, Any]
with open('data.json', 'r') as f:
data = json.load(f)
E = lxml.builder.ElementMaker()
DATA = E.data
USER = E.user
NODE = E.node
def parse_node(nodes: dict[int, dict[str, object]], nid: int):
n = nodes[nid]
flags = 0
if n['file']: flags = flags | 1
if n['preview']: flags = flags | 2
xn = NODE({
'id': str(n['id']),
'name': str(n['name']),
'flags': str(flags)
})
if not n['file']:
for c in n['children']:
xn.append(parse_node(nodes, c))
else:
xn.attrib['size'] = str(n['size'])
return xn
doc = DATA({
'version': '1',
'next-user': str(data['next_user_id'])
})
users: list[dict[str, list[dict[str, Any]]]] = data['users']
for user in users:
new_nodes = {}
for node in user['nodes']:
new_nodes[node['id']] = node
flags = 0
if user['enabled']:
flags = flags | 1
if user['admin']:
flags = flags | 2
xu = USER({
'id': str(user['id']),
'name': user['name'],
'password': user['password'],
'flags': str(flags),
'next-node': str(user['next_node_id'])
})
if user['tfa_enabled']:
xu.attrib['tfa'] = user['tfa_secret']
for c in new_nodes[0]['children']:
xu.append(parse_node(new_nodes, c))
doc.append(xu)
with open('data.xml', 'wb') as f:
f.write(lxml.etree.tostring(doc, pretty_print=True))