[asterisk-commits] twilson: testsuite/asterisk/trunk r223 - /asterisk/trunk/lib/python/asterisk/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 13 17:18:13 CDT 2010


Author: twilson
Date: Tue Apr 13 17:18:11 2010
New Revision: 223

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=223
Log:
Use the python built-in CSV module for reading

Also, add some __str__ methods

Modified:
    asterisk/trunk/lib/python/asterisk/asterisk.py
    asterisk/trunk/lib/python/asterisk/cdr.py

Modified: asterisk/trunk/lib/python/asterisk/asterisk.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/lib/python/asterisk/asterisk.py?view=diff&rev=223&r1=222&r2=223
==============================================================================
--- asterisk/trunk/lib/python/asterisk/asterisk.py (original)
+++ asterisk/trunk/lib/python/asterisk/asterisk.py Tue Apr 13 17:18:11 2010
@@ -49,6 +49,7 @@
         self.asterisk = Asterisk(base=os.path.join(os.getcwd(),
                                                    "tests/ami-login/tmp"))
         """
+        self.astetcdir = None
         # Find the system installed asterisk.conf
         ast_confs = [
                 "/etc/asterisk/asterisk.conf",

Modified: asterisk/trunk/lib/python/asterisk/cdr.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/lib/python/asterisk/cdr.py?view=diff&rev=223&r1=222&r2=223
==============================================================================
--- asterisk/trunk/lib/python/asterisk/cdr.py (original)
+++ asterisk/trunk/lib/python/asterisk/cdr.py Tue Apr 13 17:18:11 2010
@@ -12,6 +12,7 @@
 
 import unittest
 import sys
+import csv
 
 class AsteriskCSVCDRLine:
     "A single Asterisk call detail record"
@@ -30,9 +31,8 @@
         """
 
         # make all arguments passed available as instance variables
-        tmp = locals()
-        del tmp['self']
-        self.__dict__.update(tmp)
+        self.__dict__.update(locals())
+        del self.__dict__['self']
 
     def match(self, other):
         """Matches if the subset of fields that exist in both records match.
@@ -65,6 +65,9 @@
     def get_field(self, i):
         return self.__fields[i]
 
+    def __str__(self):
+        return ",".join(["\"%s\"" % (self.__dict__[x]) for x in AsteriskCSVCDRLine.get_fields()])
+
 
 class AsteriskCSVCDR:
     """A representation of an Asterisk CSV CDR file"""
@@ -72,24 +75,22 @@
     def __init__(self, fn=None, records=None):
         """Initialize CDR records from an Asterisk cdr-csv file"""
 
+        self.filename = fn
         if records:
             self.__records = records
             return
 
         self.__records = []
         try:
-            f = open(fn, "r")
-            lines = f.read().splitlines()
-            f.close()
+            cdr = csv.DictReader(open(fn, "r"), AsteriskCSVCDRLine.get_fields(), ",")
         except IOError:
-            print "Failed to open CDR file '%s'" %s (fn)
+            print "Failed to open CDR file '%s'" % (fn)
             return
         except:
             print "Unexpected error: %s" % (sys.exc_info()[0])
             return
 
-        for line in lines:
-            r = dict([(AsteriskCSVCDRLine.get_field(i), x.strip('"')) for i,x in enumerate(line.split(','))])
+        for r in cdr:
             record = AsteriskCSVCDRLine(**r)
             self.__records.append(record)
 
@@ -114,6 +115,15 @@
                 return False
         return True
 
+    def __str__(self):
+        return "\n".join([str(x) for x in self.__records])
+
+    def empty(self):
+        try:
+            open(self.filename, "w").close()
+        except:
+            print "Unable to empty CDR file %s" % (self.filename)
+
 
 class AsteriskCSVCDRTests(unittest.TestCase):
     def test_cdr(self):




More information about the asterisk-commits mailing list