<s> '''<STR_LIT>''' <EOL> import csv <EOL> import sys <EOL> from pwman . data . nodes import Node <EOL> from pwman . util . crypto_engine import CryptoEngine <EOL> from pwman . ui . tools import CLICallback <EOL> class BaseImporter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> class CSVImporter ( BaseImporter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args , config , db ) : <EOL> self . args = args <EOL> self . config = config <EOL> self . _db = db <EOL> def _read_file ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fh , delim = open ( self . args . file_delim [ <NUM_LIT:0> ] ) , self . args . file_delim [ <NUM_LIT:1> ] <EOL> csv_f = csv . reader ( fh , delimiter = delim ) <EOL> except IOError : <EOL> fh , delim = open ( self . args . file_delim [ <NUM_LIT:1> ] ) , self . args . file_delim [ <NUM_LIT:0> ] <EOL> csv_f = csv . reader ( fh , delimiter = delim ) <EOL> lines = [ line for line in csv_f ] <EOL> lines = list ( filter ( None , lines ) ) <EOL> fh . close ( ) <EOL> return lines [ <NUM_LIT:1> : ] <EOL> def _create_node ( self , row ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> n = { '<STR_LIT>' : True , <EOL> '<STR_LIT:username>' : row [ <NUM_LIT:0> ] , '<STR_LIT:password>' : row [ <NUM_LIT:2> ] , '<STR_LIT:url>' : row [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : row [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : row [ <NUM_LIT:4> ] . split ( '<STR_LIT:U+002C>' ) } <EOL> node = Node ( ** n ) <EOL> except IndexError as err : <EOL> print ( '<STR_LIT>' . format ( err ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return node <EOL> def _insert_node ( self , node ) : <EOL> "<STR_LIT>" <EOL> self . _db . add_node ( node ) <EOL> def _open_db ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _db . _open ( ) <EOL> self . _db . _create_tables ( ) <EOL> self . _db . _con . commit ( ) <EOL> self . _db . open ( ) <EOL> def run ( self , callback = CLICallback ) : <EOL> enc = CryptoEngine . get ( ) <EOL> enc . callback = callback ( ) <EOL> self . _open_db ( ) <EOL> for row in self . _read_file ( ) : <EOL> node = self . _create_node ( row ) <EOL> self . _insert_node ( node ) <EOL> self . _db . close ( ) <EOL> class Importer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args , invoke = CSVImporter ) : <EOL> self . importer = invoke ( * args ) <EOL> def run ( self ) : <EOL> self . importer . run ( ) </s>
