Première version : mise en route du suivi.
[auf_montreal_asterisk_users.git] / toip-import-comptes.py~
1 import sys
2 import csv
3
4 import warnings
5 def customwarn(message, category, filename, lineno):
6    pass
7 warnings.showwarning = customwarn 
8
9 from sqlalchemy.ext.sqlsoup import SqlSoup
10
11 if __name__ == "__main__":
12     
13     uri_db = "mysql://asterisk-users:MYast-stat5!@new-dev.auf/asterisk-users"
14  
15     if  len(sys.argv) is not 2:
16         print "Usage: ./%s <filename>\n" % sys.argv[0]
17         print "Where filename is the path to the Asterisk comptes file to import (comptes.txt)\n"
18         sys.exit()
19     else:
20         csv_file = sys.argv[1]
21  
22     try:
23         rows = csv.reader(open(csv_file), delimiter=':', quotechar='"')
24     except:
25         print "File doesn't exist"
26         sys.exit()
27     
28     db = SqlSoup(uri_db)
29     
30     inserted_rows = 0
31     
32     for row in rows:
33         if len(row) > 0:
34             exten, categorie, fullname, cidname, email, secret, voicemailcode, pavillon, bureau = tuple(row)
35             print row
36             
37             rp = db.bind.execute("SELECT exten, fullname FROM auf_asterisk_users_comptes \
38                                   WHERE exten='%s' AND fullname='%s' \
39                                   LIMIT 1" % (exten, fullname))
40             
41             if len(rp.fetchall()) == 0:     
42                 db.cdr.insert(
43                     exten=exten,
44                     categorie=categorie,
45                     fullname=fullname,
46                     cidname=cidname,
47                     email=email,
48                     secret=secret,
49                     voicemailcode=voicemailcode,
50                     pavillon=pavillon,
51                     bureau=bureau
52                 )
53                 print "Inserted: %s %s\n" % (exten, fullname)
54                 inserted_rows = inserted_rows + 1
55             else:
56                 print("Not unique: %s %s\n" % (exten, fullname))
57     db.flush()
58     print("%d imported\n" % inserted_rows);