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