From 2afb4c79a2d8c89ccfbd42eb406d2a736e65004e Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Sun, 13 Oct 2019 11:01:08 +0200 Subject: [PATCH] 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 --- private/Backup.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/private/Backup.py b/private/Backup.py index e69de29..f60d444 100644 --- a/private/Backup.py +++ b/private/Backup.py @@ -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 \ No newline at end of file