@ -1,14 +1,17 @@
#!/usr/bin/env python3
"""
Import users or sanitize backup files
"""
import configparser
import csv
import os
import lib . UserExceptions
import lib . uis . config_ui # dont go to default, just following -c flag
import lib . Validator
def import_from_file ( file_path : str , d b: str , user_ids : tuple = tuple ( [ ] ) ) - > bool :
def import_from_file ( file_path : str , d ata base_file : str , user_ids : tuple = tuple ( [ ] ) ) - > bool :
""" Imports Users from a given CSV-file to the system and DB
: param file_path :
@ -23,22 +26,21 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo
if not os . path . isfile ( file_path ) :
print ( f " File { file_path } don ' t exist " )
return False
if not os . path . isfile ( d b) :
print ( f " The database file { d b} don ' t exist " )
if not os . path . isfile ( d ata base_file ) :
print ( f " The database file { d ata base_file } don ' t exist " )
return False
if user_ids :
pass # empty tuple means everything
# noinspection PyBroadException
try :
with open ( file_path , ' r ' , newline = ' ' ) as f :
import lib . Validator
sql = lib . sqlitedb . SQLiteDB ( db )
err = lib . Validator . checkImportFile ( file_path , db )
import lib . sqlitedb
import lib . System
sql = lib . sqlitedb . SQLiteDB ( database_file )
err = lib . Validator . checkImportFile ( file_path , database_file )
if err is not True :
print ( err )
exit ( 0 )
import lib . sqlitedb
import lib . System
sys_ctl = lib . System . System ( " root " )
reader = csv . DictReader ( f ) # @TODO csv.Sniffer to compare? When yes, give force-accept option
for row in reader :
@ -47,8 +49,8 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo
sys_ctl . setUser ( row [ " username " ] )
sys_ctl . aio_approve ( row [ " pubkey " ] )
print ( row [ ' username ' ] , " ====> Registered. " )
except lib . UserExceptions . General as GeneralE xcept:
print ( f " Something didnt work out! { GeneralE xcept} " )
except lib . UserExceptions . General as general_e xcept:
print ( f " Something didnt work out! { general_e xcept} " )
elif row [ " status " ] == " 0 " :
print ( row [ ' username ' ] + " not approved, therefore not registered. " )
try :
@ -56,11 +58,10 @@ def import_from_file(file_path: str, db: str, user_ids: tuple = tuple([])) -> bo
" INSERT INTO `applications` (username, name, timestamp, email, pubkey, status) "
" VALUES (?,?,?,?,?,?) " , tuple ( [ row [ " username " ] , row [ " name " ] , row [ " timestamp " ] ,
row [ " email " ] , row [ " pubkey " ] , row [ " status " ] ] ) )
except OSError as E :
pass
print ( f " UUFFF, something went WRONG with the file { file_path } : { E } " )
except Exception as didntCatch :
print ( f " Exception! UNCATCHED! { type ( didntCatch ) } : { didntCatch } " )
except OSError as os_exception :
print ( f " UUFFF, something went WRONG with the file { file_path } : { os_exception } " )
except Exception as didnt_catch :
print ( f " Exception! UNCATCHED! { type ( didnt_catch ) } : { didnt_catch } " )
return True
@ -71,17 +72,24 @@ if __name__ == "__main__":
ArgParser . add_argument ( ' -f ' , ' --file ' , default = " stdout " ,
type = str , help = ' Import from CSV file ' , required = True )
ArgParser . add_argument ( ' --Import ' , default = False , action = " store_true " ,
help = " Import Users. " , required = Tru e)
help = " Import Users. If not set, just sanitize the supplied csv " , required = Fals e)
args = ArgParser . parse_args ( )
config = configparser . ConfigParser ( )
config . read ( args . config )
if not args . Import :
print ( " Error, need the import flag " )
if not args . file :
print ( " Error, need the import file " )
if not args . file :
print ( " You MUST set a CSV-file with the -f/--file flag that already exist " )
if not args . Import :
# we assume that you just want to sanitize the backup
if not os . path . isfile ( args . file ) :
print ( f " File { args . file } doesnt exist " )
exit ( 1 )
import_from_file ( args . file , config [ ' DEFAULT ' ] [ ' applications_db ' ] )
sanitized = lib . Validator . checkImportFile ( args . file , config [ ' DEFAULT ' ] [ ' applications_db ' ] )
if sanitized is not True :
print ( sanitized )
else :
print ( f " { args . file } is valid! " )
exit ( 0 )
elif args . Import :
import_from_file ( args . file , config [ ' DEFAULT ' ] [ ' applications_db ' ] )
exit ( 0 )