#!/sw/bin/python2.4 # coding=utf-8 import csv import xmlrpclib import time import sys import getopt import re from itertools import cycle class ProgressBar(object): """Visualize a status bar on the console.""" def __init__(self, max_width): """Prepare the visualization.""" self.max_width = max_width self.spin = cycle(r'-\|/').next self.tpl = '%-' + str(max_width) + 's ] %c %5.1f%%' show(' [ ') self.last_output_length = 0 def update(self, percent): """Update the visualization.""" # Remove last state. show('\b' * self.last_output_length) # Generate new state. width = int(percent / 100.0 * self.max_width) output = self.tpl % ('-' * width, self.spin(), percent) # Show the new state and store its length. show(output) self.last_output_length = len(output) def show(string): """Show a string instantly on STDOUT.""" sys.stdout.write(string) sys.stdout.flush() class importCSV: def __init__(self,server_ip,port,dbname,username,pwd): # Get the uid sock_common = xmlrpclib.ServerProxy ('http://'+server_ip+':'+port+'/xmlrpc/common') self.uid = sock_common.login(dbname, username, pwd) self.dbname=dbname self.pwd=pwd self.sock= xmlrpclib.ServerProxy('http://'+server_ip+':'+port+'/xmlrpc/object') def _do_create_job(self,job): domain=[('jobid', '=', job['jobid']),('vc_id','=',job['vc_id'])] job_ids=self.sock.execute(self.dbname, self.uid, self.pwd, 'print.job.audit', 'search', domain)#searching the job module whether # there is a job record with vc_id and jobid if job_ids: job_id=job_ids[0] self.sock.execute(self.dbname, self.uid, self.pwd, 'print.job.audit', 'write', job_id, job)#if search gives some records #then writing to this record else: job_id = self.sock.execute(self.dbname, self.uid, self.pwd, 'print.job.audit', 'create', job) def _do_create_doc(self,doc,address): domain = [('docid', '=', doc['jobid']),('vc_id','=',doc['vc_id'])] doc.update(address) doc_ids=self.sock.execute(self.dbname, self.uid, self.pwd, 'print.doc.audit', 'search', domain) if doc_ids: doc_id=doc_ids[0] self.sock.execute(self.dbname, self.uid, self.pwd, 'print.doc.audit', 'write', doc_id, doc) else: doc_id = self.sock.execute(self.dbname, self.uid, self.pwd, 'print.doc.audit', 'create', doc) def do_import_job_from_csv(self,csv_file): csvReader = csv.DictReader(open(csv_file)) count=1 for row in csvReader: print row job={} sb = ProgressBar(40) job={ ##Taking fields from the csv file 'vc_id':row['VC_ID'], 'jobid':row['JOB_ID'], 'name':row['JOB_NAME'], 'short_name':row['JOB_SHORT_NAME'], 'job_guid':row['JOB_GUID'], 'orderref':row['ORDER_REF'], 'vendor_id': row['VENDOR_NO'], 'prod':row['ENVIRONMENT'], 'version': row['VERSION'], 'platform': row['PLATFORM'] , 'partner_id':row['CUSTOMER_NO'], } self._do_create_job(job) sb.update(count/2421) count+=1 def do_import_doc_from_csv(self,csv_file): csvReader = csv.DictReader(open(csv_file)) count=1 for row in csvReader: doc={} sb = ProgressBar(40) doc={ 'job_print_id':row['JOB_PRINT_ID'], 'vc_id':row['VC_ID'], 'docid':row['DOC_ID'], 'docref':row['DOC_REF'], 'partner_id':row['CUSTOMER_NO'], } address={ 'name':row['CUST_CONTCT_1'], 'street':row['CUST_ST_1'], 'street2':row['CUST_ST_2'], 'street3':row['CUST_ST_3'], 'zip':row['CUST_ZIP_COD'], 'city':row['CUST_CITY'], 'email': row['CUST_EMAIL_NO_1'] , 'country_id':row['CUST_COUNTRY'],'state_id':row['CUST_STATE'] } ## self._do_create_doc(doc,address,type) sb.update(count/2421) count+=1 __doc__ =""" ######################################################################################## Usage: python csv_Import.py type server port dbname usrname passwd csvFileName Where type can be ***j*** for job, used to import job_accounting using csvfile ***d*** for document, used to import document_accounting using csv file server is the server ip address, port is the port number in which the server is running, dbname is the Database name, usrname is the username to login to the database, passwd is the password, csvFilename is the csv file name with path. ######################################################################################### """ def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) if len(args)<7: print __doc__ else: type = args[0] server_ip = args[1] port = args[2] database = args[3] username = args[4] password = args[5] csvFileName = args[6] if type in ['d','j']: ip = importCSV(server_ip,port,database,username,password) else : print __doc__ sys.exit(0) if type=='j': ip.do_import_job_from_csv(csvFileName) if type=='d': ip.do_import_doc_from_csv(csvFileName) if __name__ == "__main__": main()