test
[retroconv] / csv_service.py
1 import csv
2 from recv import recv
3 from sent import sent
4 from contactsServ import *
5 import os
6
7 class csvServ:
8
9         def __init__(self):
10         
11                 
12                 self.pathToHome = "/home/user/"
13                 self.pathToApp = self.pathToHome+".retroconv/"
14                 self.pathToInbox = self.pathToApp+"inbox.csv"
15                 self.pathToSent = self.pathToApp+"sent.csv"
16                         
17                 self.recvLst=[]
18                 self.sentLst=[]
19                 self.recvNumbers=[]
20                 self.sentNumbers=[]
21                 
22                 self.checkInitialLaunch()
23                 
24         def checkInitialLaunch(self):
25                 if not os.path.isdir(self.pathToApp):
26                         os.system("mkdir "+self.pathToApp)
27                 
28                 
29         
30         
31         def checkFileValidity(self):
32                 print "Checking validity"
33                 
34                 #Check Extra Lines
35                 #Check number of fields
36                 #Check submit, deliver, sms (constant fields)
37                 
38         def fetchInbox(self):
39                 ifile = open(self.pathToInbox, "r")
40                 reader=csv.reader(ifile)
41                 del self.recvLst[:]
42                 #self.recvLst=[]
43                 #self.recvNumbers=[]
44                 for row in reader:
45                         self.recvLst.append(recv(row[2],row[5], row[7]))
46                         self.recvNumbers.append(row[2][-7:])
47                 
48                 
49                 ifile.close()
50                 
51         def fetchSent(self):
52                 ifile = open(self.pathToSent, "r")
53                 reader=csv.reader(ifile)
54                 del self.sentLst[:]
55                 #self.sentLst=[]
56                 #self.sentNumbers=[]
57                 for row in reader:
58                         self.sentLst.append(sent(row[3],row[5], row[7]))
59                         self.sentNumbers.append(row[3][-7:])
60                 
61                 
62                 
63                 
64                 
65                 ifile.close()
66         
67         def fetchContactsInfo(self):
68                 abook=address_book()
69                 abook.populateNames(self.recvLst, self.sentLst)
70         
71         def hasImportedSent(self):
72                  return os.path.isfile (self.pathToSent)
73                         
74                  
75         def hasImportedInbox(self):
76                  return os.path.isfile (self.pathToInbox)
77                         
78                 
79         def importSent(self, path):
80                 try:
81                         #if hasImportedSent:
82                         #       os.system("rm "+self.pathToSent)
83                                 
84                         os.system("cp "+str(path).replace(" ", "\ ")+ " "+self.pathToSent)
85                         self.fetchSent()
86                         return True
87                 except:
88                         return False
89         
90         def importInbox(self, path):
91                 try:
92                         #if hasImportedInbox:
93                         #       os.system("rm "+self.pathToInbox)
94                                 
95                         os.system("cp "+str(path).replace(" ", "\ ")+ " "+self.pathToInbox)
96                         self.fetchInbox()
97                         return True
98                 except:
99                         return False
100                                 
101                 
102         def getAllReceivedMsgs(self):
103                 msgs=[]
104                 for msg in self.recvLst:
105                         msgs.append(msg.getSummary())
106                 return msgs
107                 
108         
109         
110         
111         def getMsg(self, msgNum, msgType = 0):
112                 #msgType 0: recv, 1:sent
113                 
114                 if msgType == 0:
115                         return self.recvLst[msgNum]
116                 
117                 return self.sentLst[msgNum]
118                 
119         
120         def getMsgText(self, msgNum):
121                 return self.recvLst[msgNum].getMsg()