cdinfo: replace task with packagegroup for new OE
[enigma2-plugins.git] / fritzcall / src / FritzOutlookCSV.py
1 # -*- coding: utf-8 -*-
2 '''
3 $Author: michael $
4 $Revision: 699 $
5 $Date: 2012-10-25 19:04:03 +0200 (Thu, 25 Oct 2012) $
6 $Id: FritzOutlookCSV.py 699 2012-10-25 17:04:03Z michael $
7 '''
8 #
9 # needs python-textutils for csv
10 #
11 try:
12         from . import _, debug, normalizePhoneNumber #@UnresolvedImport # pylint: disable-msg=W0613,F0401
13 except ValueError:
14         def _(string): # pylint: disable-msg=C0103
15                 return string
16         
17         def debug(text):
18                 print text
19         
20         import re
21         def normalizePhoneNumber(intNo):
22                 found = re.match('^\+49(.*)', intNo)
23                 if found:
24                         intNo = '0' + found.group(1)
25                 found = re.match('^\+(.*)', intNo)
26                 if found:
27                         intNo = '00' + found.group(1)
28                 intNo = intNo.replace('(', '').replace(')', '').replace(' ', '').replace('/', '').replace('-', '')
29                 found = re.match('^49(.*)', intNo) # this is most probably an error
30                 if found:
31                         intNo = '0' + found.group(1)
32                 found = re.match('.*?([0-9]+)', intNo)
33                 if found:
34                         return found.group(1)
35                 else:
36                         return '0'
37
38 def out(number, name):
39         print number + '#' + name
40
41 import csv
42 #
43 # 31: Telefon geschäftlich
44 # 37: Telefon privat
45 # 40: Mobiltelefon
46 # 1: Vorname
47 # 3: Nachname
48 # 5: Firma
49 # 8: Straße geschäftlich
50 # 11: Ort geschäftlich
51 # 13: Postleitzahl geschäftlich
52 # 14: Land/Region geschäftlich
53 # 15: Straße privat
54 # 18: Ort privat
55 # 20: Postleitzahl privat
56 # 21: Land/Region privat
57 #
58
59 def findNumber(number, filename):
60         fileD = open(filename)
61         if not fileD:
62                 return
63         addrs = csv.reader(fileD, delimiter=',', quotechar='"')
64         addrs.next() # skip header
65         for row in addrs:
66                 row = map(lambda w: w.decode('cp1252').encode('utf-8'), row)
67                 name = u""
68                 nameB = u""
69                 address = u""
70                 addressB = u""
71                 try: # this is just to catch wrong lines
72                         if row[31] or (row[37] and number == normalizePhoneNumber(row[37])) or (row[40] and number == normalizePhoneNumber(row[40])): # Telefon geschäftlich
73                                 no = normalizePhoneNumber(row[31])
74                                 # debug("[FritzOutlookCSV] findNumber compare (business) %s with %s for %s" %(no,number,name))
75                                 if no == number or (row[37] and number == normalizePhoneNumber(row[37])) or (row[40] and number == normalizePhoneNumber(row[40])):
76                                         if row[3]:
77                                                 name = row[3] # Nachname
78                                         if row[1]:
79                                                 if name:
80                                                         name = row[1] + ' ' + name # Vorname
81                                                 else:
82                                                         name = row[1]
83                                         if row[5]: # Firma
84                                                 if name:
85                                                         nameB = name
86                                                         addressB = row[5]
87                                                 else:
88                                                         nameB = row[5]
89                                         else:
90                                                 nameB = name
91                                         if not nameB:
92                                                 continue
93                                         nameB = (nameB + ' (' + _('work') + ')')
94                                         if row[11]: # Ort geschäftlich
95                                                 addressB = row[11]
96                                                 if row[13]:
97                                                         addressB =  row[13] + ' ' + addressB# Postleitzahl geschäftlich
98                                                 if row[14]:
99                                                         addressB = addressB + ', ' + row[14] # Land/Region geschäftlich
100                                                 if row[8]:
101                                                         addressB = row[8] + ', ' + addressB# Stra￟e gesch¦ftlich
102                                                 nameB = (nameB + ', ' + addressB).replace('\n', ', ').replace('\r', '').replace('#', '')
103         
104                                         if no == number:
105                                                 debug("[FritzCallPhonebook] findNumber result: " + no + ' ' + nameB)
106                                                 fileD.close()
107                                                 return nameB
108                         for i in [37, 40]:
109                                 if row[i]:
110                                         number = normalizePhoneNumber(row[i])
111                                         # debug("[FritzOutlookCSV] findNumber compare (home,mobile) %s with %s for %s" %(number,number,name))
112                                         if number == number:
113                                                 if row[3]:
114                                                         name = row[3] # Nachname
115                                                 if row[1]:
116                                                         if name:
117                                                                 name = row[1] + ' ' + name # Vorname
118                                                         else:
119                                                                 name = row[1]
120                                                 if i == 40: # Mobiltelefon
121                                                         nameHM = name + ' (' + _('mobile') + ')'
122                                                 else:
123                                                         nameHM = name + ' (' + _('home') + ')'
124                                                 if row[18]: # Ort privat
125                                                         address = row[18]
126                                                         if row[20]:
127                                                                 address = row[20] + ' ' + address # Postleitzahl privat
128                                                         if row[21]:
129                                                                 address = address + ', ' + row[21] # Land/Region privat
130                                                         if row[15]:
131                                                                 address = row[15] + ', ' + address # Straße privat
132                                                 if not address:
133                                                         address = addressB
134                                                 if address:
135                                                         nameHM = nameHM + ', ' + address
136                                                 nameHM = nameHM.replace('\n', ', ').replace('\r', '').replace('#', '')
137                                                 fileD.close()
138                                                 debug("[FritzCallPhonebook] findNumber result: " + number + ' ' + nameHM)
139                                                 return nameHM
140                 except IndexError:
141                         continue
142         fileD.close()
143         return ""
144         
145 def readNumbers(filename, outFun):
146         fileD = open(filename, "rb")
147         if not fileD:
148                 return
149         addrs = csv.reader(fileD, delimiter=',', quotechar='"')
150         addrs.next() # skip header
151         for row in addrs:
152                 row = map(lambda w: w.decode('cp1252'), row)
153                 name = u""
154                 nameB = u""
155                 address = u""
156                 addressB = u""
157                 try:
158                         if row[31] or row[37] or row[40]:
159                                 if row[3]:
160                                         name = row[3] # Nachname
161                                 if row[1]:
162                                         if name:
163                                                 name = row[1] + ' ' + name # Vorname
164                                         else:
165                                                 name = row[1]
166                                 if row[5]: # Firma
167                                         if name:
168                                                 nameB = name
169                                                 addressB = row[5]
170                                         else:
171                                                 nameB = row[5]
172                                 else:
173                                         nameB = name
174                                 if not nameB:
175                                         continue
176                                 nameB = (nameB + ' (' + _('work') + ')')
177                                 if row[11]: # Ort gesch¦ftlich
178                                         addressB = row[11]
179                                         if row[13]:
180                                                 addressB =  row[13] + ' ' + addressB# Postleitzahl gesch¦ftlich
181                                         if row[14]:
182                                                 addressB = addressB + ', ' + row[14] # Land/Region gesch¦ftlich
183                                         if row[8]:
184                                                 addressB = row[8] + ', ' + addressB# Stra?e gesch?ftlich
185                                         nameB = (nameB + ', ' + addressB).replace('\n', ', ').replace('\r', '').replace('#', '')
186                                 if row[31]:
187                                         number = normalizePhoneNumber(row[31])
188                                         outFun(number, nameB)
189
190                         for i in [37, 40]:
191                                 if row[i]:
192                                         number = normalizePhoneNumber(row[i])
193                                         nameHM = nameB
194                                         if row[3]:
195                                                 nameHM = row[3] # Nachname
196                                         if row[1]:
197                                                 if nameHM:
198                                                         nameHM = row[1] + ' ' + nameHM # Vorname
199                                                 else:
200                                                         nameHM = row[1]
201                                         if i == 40: # Mobiltelefon
202                                                 nameHM = nameHM + ' (' + _('mobile') + ')'
203                                         else:
204                                                 nameHM = nameHM + ' (' + _('home') + ')'
205                                         if row[18]: # Ort privat
206                                                 address = row[18]
207                                                 if row[20]:
208                                                         address = row[20] + ' ' + address # Postleitzahl privat
209                                                 if row[21]:
210                                                         address = address + ', ' + row[21] # Land/Region privat
211                                                 if row[15]:
212                                                         address = row[15] + ', ' + address # Stra￟e privat
213                                         if not address:
214                                                 address = addressB
215                                         if address:
216                                                 nameHM = nameHM + ', ' + address
217                                         nameHM = nameHM.replace('\n', ', ').replace('\r', '').replace('#', '')
218                                         outFun(number, nameHM)
219
220                 except IndexError:
221                         continue
222         fileD.close()
223
224 if __name__ == '__main__':
225         import os, sys
226         cwd = os.path.dirname(sys.argv[0])
227         if (len(sys.argv) == 1):
228                 readNumbers("Kontakte.csv", out)
229         elif (len(sys.argv) == 2):
230                 # nrzuname.py Nummer
231                 findNumber(sys.argv[1], "Kontakte.csv")