File: addresses.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (346 lines) | stat: -rw-r--r-- 12,791 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/python

"""
A lot of the record structure and logic mirrors Winkler's approach used in
the textIndexer, but I coded this from scratch to see what kind of differences
would appear. I also didn't bother using a separate class to hold the internal
data structures initially, so this version will get refactored. I realized that
once I started porting the Addresses stack from HyperCard, we could use this
addresses sample to test different approaches to storage formats: ZODB, mySQL,
plain text files using lists and dictionaries, CSV text files, etc.
"""
__version__ = "$Revision: 1.36 $"
__date__ = "$Date: 2005/12/13 11:13:21 $"

from PythonCard import configuration, dialog, model, util
import os, sys
import pprint
import shutil
import outlook

DATA_FILE = 'data.txt'

"""
The Document class below has a reference to the view, so it is responsible for
updating the fields in the view.

There should be an abstract Document class, an abstract RecordsDocument subclass
and finally a DictionaryRecordsDocument or a name that reflects that the actual
records are stored as a list of dictionaries.

If the Document was stored as a DBM or dictionary of dictionaries then an additional
field would be needed to keep track of record order and goPrevRecord, goNextRecord,
goFirstRecord, and goLastRecord would have to be modified to use that ordering.
"""

class Document:
    def __init__(self, view, filename=None):
        self.view = view
        self.current = -1
        self.fieldNames = self.getFieldNames()
        if filename is None:
            self.records = []
        else:
            self.filename = filename
            self.openFile(filename)

    """
    This should probably be a list supplied as part of initialization.
    There might also be a list for field order and fields to search.
    There isn't any meta-data at this point, so there isn't field type
    info either.
    """
    def getFieldNames(self):
        fields = []
        for wName in self.view.components.iterkeys():
            widget = self.view.components[wName]
            wClass = widget.__class__.__name__
            if wClass in ['TextField', 'TextArea'] and widget.visible:
                fields.append(wName)
        if fields.count('Notes') == 0:
            fields.append('Notes')
        return fields

    def clearFields(self):
        for wName in self.fieldNames:
            self.view.components[wName].text = ""
        
    def displayRecord(self, recordNumber):
        self.clearFields()
        self.current = recordNumber
        if self.current != -1:
            record = self.records[recordNumber]
            for fld in record:
                self.view.components[fld].text = record[fld]

    def goPrevRecord(self):
        if len(self.records) > 1:
            prevRec = self.current - 1
            if prevRec == -1:
                prevRec = len(self.records) - 1
            self.saveRecord(self.current)
            self.displayRecord(prevRec)
            
    def goNextRecord(self):
        if len(self.records) > 1:
            nextRec = self.current + 1
            if nextRec == len(self.records):
                nextRec = 0
            self.saveRecord(self.current)
            self.displayRecord(nextRec)

    def goFirstRecord(self):
        if len(self.records) > 1:
            if self.current != 0:
                self.saveRecord(self.current)
                self.displayRecord(0)

    def goLastRecord(self):
        n = len(self.records) - 1
        if n > 0:
            if self.current != n:
                self.saveRecord(self.current)
                self.displayRecord(n)

    def findRecord(self, searchText, caseSensitive):
        # we can't search unless there is at least one record
        if len(self.records) > 1:
            self.saveRecord(self.current)
            current = self.current
            if not caseSensitive:
                searchText = searchText.lower()
            while 1:
                for name in self.fieldNames:
                    if caseSensitive:
                        fieldText = self.records[self.current][name]
                    else:
                        fieldText = self.records[self.current][name].lower()
                    offset = fieldText.find(searchText)
                    if offset != -1:
                        # if the text is found in the Notes field and it isn't visible
                        # then the selection below doesn't work without making the field
                        # visible, but then the Show Notes/Hide Notes button will be out
                        # of sync if we just set visible = 1
                        # so some different logic is really needed here
                        #self.view.components[name].visible = 1
                        # the solution below is a hack
                        if not self.view.components[name].visible:
                            self.view.on_showNotes_command(self.view.components['ShowNotes'], None)
                        self.view.components[name].setSelection(offset, offset + len(searchText))
                        self.view.components[name].setFocus()
                        return
                self.goNextRecord()
                if current == self.current:
                    return

    def newRecord(self):
        self.saveRecord(self.current)
        if self.current == -1:
            self.current = 0
            #self.records.append({})
        else:
            self.current += 1
        self.records.insert(self.current, {})
        self.clearFields()

    def deleteRecord(self):
        # should probably do a dialog here to give the user
        # a chance to cancel
        if self.current != -1:
            del self.records[self.current]
            if len(self.records) == 0:
                self.current = -1
            elif self.current == len(self.records):
                # the last record was deleted,
                # so display the next to last record
                self.current = self.current - 1
        self.displayRecord(self.current)

    def saveRecord(self, recordNumber):
        """
        it would be more efficient to set a 'dirty' flag per field
        or at least per record if any fields change
        but this still works
        """
        n = len(self.records)
        # in HyperCard there is always at least one record
        # which is not always what you want, so if we're going to
        # support 0 records our logic will be different for creating
        # an empty record initially

        # the boundary condition of no records will break the code right now        
        if recordNumber == -1:
            pass

        if n > 0 and recordNumber > -1 and recordNumber < n:
            for wName in self.fieldNames:
                self.records[recordNumber][wName] = self.view.components[wName].text

    def openFile(self, filename):
        self.records = []
        self.filename = filename
        try:
            if os.path.exists(filename):
                addresses = util.readAndEvalFile(filename)
            for c in addresses:
                self.records.append(c)
            if self.current == -1 and len(self.records) > 0:
                self.displayRecord(0)
        except IOError:
            pass

    def saveFile(self):
        # always save the records list
        # for transparent saves
        self.saveRecord(self.current)
        try:
            f = open(self.filename, "w")
            pprint.pprint(self.records, f)
            f.close()
        except IOError:
            pass

class Addresses(model.Background):

    def on_initialize(self, event):
        if not outlook.WIN32_FOUND:
            self.menuBar.setEnabled('menuFileImportOutlook', 0)
        self.loadConfig()

    def loadConfig(self):
        self.configPath = os.path.join(configuration.homedir, 'addresses')
        if not os.path.exists(self.configPath):
            os.mkdir(self.configPath)
        basePath = self.application.applicationDirectory
        self.dataPath = os.path.join(self.configPath, DATA_FILE)
        if not os.path.exists(self.dataPath):
            shutil.copy2(os.path.join(basePath, DATA_FILE), self.dataPath)

        self.document = Document(self, self.dataPath)

    def doExit(self):
        self.document.saveFile()

    def on_close(self, event):
        self.doExit()
        event.skip()

    def on_fileImportOutlook_command(self, event):
        addressesToOutlookMap = {'Name':'FullName',
                                 'Company':'CompanyName',
                                 'Street':'MailingAddressStreet',
                                 'City':'MailingAddressCity',
                                 'State':'MailingAddressState',
                                 'Zip':'MailingAddressPostalCode',
                                 'Phone1':'HomeTelephoneNumber',
                                 'Phone2':'BusinessTelephoneNumber',
                                 'Phone3':'MobileTelephoneNumber',
                                 'Phone4':'Email1Address',
                                 'Notes':'Body'
                                 }
        print "attempting to load Outlook"
        oOutlook = outlook.MSOutlook()
        # delayed check for Outlook on win32 box
        if not oOutlook.outlookFound:
            # could also display an error message here
            self.menuBar.setEnabled('menuFileImportOutlook', 0)
            return

        print "loading records..."
        oOutlook.loadRecords()
        print "importing into addresses"
        for r in oOutlook.records:
            aRecord = {}
            for fld in addressesToOutlookMap:
                """
                if fld == 'Name':
                    names = addressesToOutlookMap[fld]
                     fullName = r[names[0]] + " " + r[names[1]]
                     aRecord['Name'] = fullName.strip()
                else:
                """
                aRecord[fld] = r[addressesToOutlookMap[fld]]
            self.document.records.append(aRecord)
        print "done importing"
        self.document.goNextRecord() # show that we imported at least one record

    def on_exit_command(self, event):
        self.close()

    def on_goPrev_command(self, event):
        self.document.goPrevRecord()

    def on_goNext_command(self, event):
        self.document.goNextRecord()

    def on_goFirst_command(self, event):
        self.document.goFirstRecord()

    def on_goLast_command(self, event):
        self.document.goLastRecord()

    def on_findRecord_command(self, event):
        result = dialog.findDialog(self)
        if result.accepted:
            self.document.findRecord(result.searchText, result.caseSensitive)

    def on_editUndo_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canUndo():
            widget.undo()

    def on_editRedo_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canRedo():
            widget.redo()

    def on_editCut_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canCut():
            widget.cut()

    def on_editCopy_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canCopy():
            widget.copy()

    def on_editPaste_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canPaste():
            widget.paste()

    def on_editClear_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable'):
            if widget.canCut():
                # delete the current selection,
                # if we can't do a Cut we shouldn't be able to delete either
                # which is why i used the test above
                sel = widget.replaceSelection('')
            else:
                ins = widget.getInsertionPoint()
                widget.replace(ins, ins + 1, '')

    def on_editSelectAll_command(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable'):
            widget.setSelection(0, widget.getLastPosition())

    def on_editNewCard_command(self, event):
        self.document.newRecord()

    def on_editDeleteCard_command(self, event):
        self.document.deleteRecord()

    def on_showNotes_command(self, event):
        target = event.target
        if target.label == 'Show Notes':
            self.components.Notes.visible = 1
            target.label = 'Hide Notes'
        else:
            self.components.Notes.visible = 0
            target.label = 'Show Notes'

if __name__ == '__main__':
    app = model.Application(Addresses)
    app.MainLoop()