Implements a backup to csv. Uses StringIO because it has an own writer()

method, which is pretty nice to have when csv.writer() want's that on its
passed variable.

Also respects every flag yet introduced(-c, -f, -a, -u) and reuses the code
already written in ListUsers.py. It could be very nice to bring that code
into lib/ because it is probably needed way more often
pull/4/head
Darksider3 5 years ago
parent 90b2ee5236
commit 2afb4c79a2

@ -0,0 +1,25 @@
#!/usr/bin/env python3
from lib.sqlitedb import SQLitedb
import lib.CFG as CFG
import ListUsers, csv, os, io
if __name__ == "__main__":
try:
L = ListUsers.ListUsers()
fetch = L.getFetch()
ret = io.StringIO()
writer = csv.writer(ret, quoting=csv.QUOTE_NONNUMERIC) # @TODO: Should be a specific dialect instead?
writer.writerow(['id', 'username', 'email', 'name', 'pubkey' 'timestamp', 'status'])
for user in fetch:
writer.writerow([user['id'], user['username'], user['email'], user['name'], user['pubkey'],
user['timestamp'], user['status']])
if CFG.args.file == "stdout":
print(ret.getvalue())
else:
with open(CFG.args.file, "w") as f:
print(ret.getvalue(), file=f)
exit(0)
except KeyboardInterrupt as e:
pass
Loading…
Cancel
Save