1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
3 |
|
4 |
# $Id: setup.py,v 1.1 2007/10/14 07:04:37 joko Exp $ |
5 |
|
6 |
# (c) 2007 Andreas Motl <andreas.motl@ilo.de> |
7 |
|
8 |
|
9 |
SHARED_FOLDER = '/Öffentliche Ordner/Alle Öffentlichen Ordner/EDV/plugin_test' |
10 |
|
11 |
|
12 |
# essential base packages |
13 |
import os, sys |
14 |
|
15 |
import getopt |
16 |
import win32com.client |
17 |
|
18 |
# for reading .ini files |
19 |
import ConfigParser |
20 |
import string |
21 |
|
22 |
|
23 |
|
24 |
# ---------------------------------------------------------------------------- |
25 |
# bootstrapping |
26 |
# ---------------------------------------------------------------------------- |
27 |
|
28 |
# find path to ourself |
29 |
script_path = os.path.dirname(sys.argv[0]) |
30 |
|
31 |
# read config from ini-style file |
32 |
config = ConfigParser.ConfigParser() |
33 |
ini_filename = 'MapiContactsDumper.ini' |
34 |
config.read(os.path.join(script_path, ini_filename)) |
35 |
|
36 |
|
37 |
# ---------------------------------------------------------------------------- |
38 |
# main |
39 |
# ---------------------------------------------------------------------------- |
40 |
|
41 |
def searchFolder(folders, foldername): |
42 |
for i in range(1, len(folders)): |
43 |
if str(folders[i].Name) == foldername: |
44 |
return folders[i] |
45 |
|
46 |
def getFolderByPath(rootfolders, folderpath): |
47 |
folder_parts = folderpath.split('/') |
48 |
folder_parts = folder_parts[1:] |
49 |
for part in folder_parts: |
50 |
childfolder = searchFolder(rootfolders, part) |
51 |
try: |
52 |
rootfolders = childfolder.Folders |
53 |
except: |
54 |
pass |
55 |
return childfolder |
56 |
|
57 |
def readMapiContactsFolder_faxAddresses(folder): |
58 |
items = folder.Items |
59 |
entries = [] |
60 |
for i in range(1, len(items)): |
61 |
contact = items[i] |
62 |
entry = { |
63 |
'label': contact.LastName + ' ' + contact.FirstName, |
64 |
'faxnumber': contact.BusinessFaxNumber, |
65 |
} |
66 |
entries.append(entry) |
67 |
return entries |
68 |
|
69 |
|
70 |
def export_addressbook(entries): |
71 |
|
72 |
if config.get('export', 'type') == 'twofiles': |
73 |
path = config.get('export', 'path') |
74 |
|
75 |
if not os.path.exists(path): |
76 |
raise 'ERROR: Export path "%s" does not exist!' % path |
77 |
|
78 |
names = open(os.path.join(path, 'names.txt'), 'w') |
79 |
numbers = open(os.path.join(path, 'numbers.txt'), 'w') |
80 |
|
81 |
for entry in entries: |
82 |
print entry['label'] + ": " + entry['faxnumber'] |
83 |
names.write(entry['label'] + "\n") |
84 |
numbers.write(entry['faxnumber'] + "\n") |
85 |
|
86 |
names.close() |
87 |
numbers.close() |
88 |
|
89 |
|
90 |
def main(): |
91 |
opts_definition = ["folder=", "verbose"] |
92 |
opts, args = getopt.gnu_getopt(sys.argv[1:], '', opts_definition) |
93 |
#print opts |
94 |
#print args |
95 |
|
96 |
# put option list into dict |
97 |
global options |
98 |
options = {} |
99 |
for option, value in opts: |
100 |
option = option.replace('--', '') |
101 |
#value = value.decode('cp1250') |
102 |
#value = unicode(value, 'cp850') |
103 |
#print option |
104 |
options[option] = value |
105 |
|
106 |
#print options |
107 |
|
108 |
# http://www.boddie.org.uk/python/COM.html |
109 |
mailer = win32com.client.Dispatch("Outlook.Application") |
110 |
mapiroot = mailer.GetNamespace("MAPI") |
111 |
#print mapi.Name |
112 |
|
113 |
#sharedroot = mapiroot.Folders('Öffentliche Ordner') |
114 |
#print sharedroot.Name |
115 |
|
116 |
# hardcode folder |
117 |
#options['folder'] = SHARED_FOLDER |
118 |
|
119 |
# read folder from command line options |
120 |
#contactsFolder = getFolderByPath(mapiroot.Folders, options['folder']) |
121 |
|
122 |
# read folder from ini file |
123 |
contactsFolder = getFolderByPath(mapiroot.Folders, config.get('mapi-contact-folders', 'folder1')) |
124 |
|
125 |
#print contactsFolder.Name |
126 |
|
127 |
entries = readMapiContactsFolder_faxAddresses(contactsFolder) |
128 |
#print entries |
129 |
export_addressbook(entries) |
130 |
|
131 |
|
132 |
if __name__ == '__main__': |
133 |
main() |