forked from tilde/ssh-reg
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.1 KiB
Python
139 lines
4.1 KiB
Python
5 years ago
|
#!/usr/bin/env python3
|
||
5 years ago
|
""" This module is thought to be the main point to export and import users.
|
||
|
It's actually not really a module but a script ought to be run from the command line
|
||
|
|
||
|
@TODO: Wording of module header...
|
||
|
"""
|
||
5 years ago
|
|
||
5 years ago
|
import configparser
|
||
5 years ago
|
import csv
|
||
|
import io
|
||
5 years ago
|
|
||
|
import ListUsers
|
||
5 years ago
|
import lib.uis.default as default_cmd # Follows -u, -a, -f flags
|
||
|
|
||
|
|
||
5 years ago
|
class Backup:
|
||
5 years ago
|
"""Backups a Tilde database to an CSV file
|
||
5 years ago
|
@TODO: Move class into own file
|
||
5 years ago
|
|
||
|
:Example:
|
||
5 years ago
|
>>> from backup import Backup
|
||
5 years ago
|
>>> from ListUsers import ListUsers
|
||
|
>>> L = ListUsers.ListUsers("/path/to/sqlite").get_fetch()
|
||
|
>>> backup_db = Backup("stdout")
|
||
|
>>> backup_db.backup_to_file(L)
|
||
|
CSV-Separated list with headers in first row
|
||
|
|
||
|
"""
|
||
|
|
||
5 years ago
|
filename: str
|
||
|
quoting: int
|
||
|
dialect: str
|
||
|
field_names: tuple
|
||
|
|
||
5 years ago
|
def __init__(self, output: str, quoting: int = csv.QUOTE_NONNUMERIC, dialect: str = "excel"):
|
||
|
""" Constructs the Backup object
|
||
|
|
||
|
:param output: File name to backup to(set to stdout for stdout)
|
||
|
:type output: str
|
||
|
:param quoting: Set quoting for CSV Module
|
||
|
:type quoting: int
|
||
|
:param dialect: Set the CSV-Dialect. Defaults to excel, which is the classic CSV
|
||
|
:type dialect: str
|
||
|
"""
|
||
|
|
||
5 years ago
|
self.set_filename(output)
|
||
|
self.set_quoting(quoting)
|
||
|
self.set_dialect(dialect)
|
||
|
self.set_field_names(tuple(['id', 'username', 'email', 'name',
|
||
|
'pubkey', 'timestamp', 'status']))
|
||
5 years ago
|
|
||
5 years ago
|
def set_dialect(self, dialect: str) -> None:
|
||
5 years ago
|
""" Set dialect for Object
|
||
|
|
||
|
:param dialect: Dialect to set for Object
|
||
|
:type dialect: str
|
||
|
:return: None
|
||
|
:rtype: None
|
||
|
"""
|
||
|
|
||
5 years ago
|
self.dialect = dialect
|
||
|
|
||
5 years ago
|
def set_quoting(self, quoting: int) -> None:
|
||
5 years ago
|
""" Set quoting in the CSV(must be supported by the CSV Module!)
|
||
|
|
||
|
:param quoting: Quoting Integer given by csv.QUOTE_* constants
|
||
|
:type quoting: int
|
||
|
:return: None
|
||
|
:rtype: None
|
||
|
"""
|
||
|
|
||
5 years ago
|
self.quoting = quoting
|
||
|
|
||
5 years ago
|
def set_filename(self, filename: str) -> None:
|
||
5 years ago
|
""" Sets Filename to output to
|
||
|
|
||
|
:param filename: Filename to output to(set stdout for stdout)
|
||
|
:type filename: str
|
||
|
:return: None
|
||
|
:rtype: None
|
||
|
"""
|
||
|
|
||
5 years ago
|
self.filename = filename
|
||
|
|
||
5 years ago
|
def set_field_names(self, f_names: tuple) -> None:
|
||
|
""" Set field name to process
|
||
5 years ago
|
|
||
|
:param f_names: Fieldnames-Tuple
|
||
|
:type f_names: tuple
|
||
|
:return: None
|
||
|
:rtype: None
|
||
|
"""
|
||
|
|
||
5 years ago
|
self.field_names = f_names
|
||
|
|
||
5 years ago
|
def backup_to_file(self, fetched: list) -> bool:
|
||
|
"""Backup Userlist to File(or stdout)
|
||
|
|
||
|
:param fetched: List of values to write out CSV-formatted
|
||
|
:return: True, if success, None when not.
|
||
|
:rtype: bool
|
||
|
"""
|
||
|
|
||
5 years ago
|
returner = io.StringIO()
|
||
5 years ago
|
write_csv = csv.DictWriter(returner, fieldnames=self.field_names,
|
||
|
quoting=self.quoting, dialect=self.dialect)
|
||
5 years ago
|
write_csv.writeheader()
|
||
5 years ago
|
for row in fetched:
|
||
|
write_csv.writerow(dict(row))
|
||
5 years ago
|
# sqlite3.Row doesn't "easily" convert to a dict itself sadly,
|
||
|
# so just a quick help from us here
|
||
|
# it actually even delivers a list(sqlite3.Row) also,
|
||
|
# which doesnt make the life a whole lot easier
|
||
5 years ago
|
|
||
|
if self.filename == "stdout":
|
||
|
print(returner.getvalue())
|
||
|
else:
|
||
|
with open(self.filename, "w") as f:
|
||
|
print(returner.getvalue(), file=f)
|
||
5 years ago
|
return True
|
||
5 years ago
|
|
||
5 years ago
|
|
||
|
if __name__ == "__main__":
|
||
5 years ago
|
default_cmd.argparser.description += " - Backups Tilde Users to stdout or a file."
|
||
|
args = default_cmd.argparser.parse_args()
|
||
|
config = configparser.ConfigParser()
|
||
|
config.read(args.config)
|
||
5 years ago
|
L = ListUsers.ListUsers(config['DEFAULT']['applications_db'],
|
||
|
unapproved=args.unapproved, approved=args.approved)
|
||
|
fetch = L.get_fetch()
|
||
|
if fetch:
|
||
|
B = Backup(args.file)
|
||
5 years ago
|
B.set_field_names(fetch[0].keys()) # sqlite3.row delivers its keys for us! SO NICE!
|
||
5 years ago
|
B.backup_to_file(fetch)
|
||
|
else:
|
||
|
print("nothing to backup!")
|
||
|
exit(1)
|
||
|
exit(0)
|