PyXR

c:\projects\bitpim\src \ call_history_export.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2004 Joe Pham <djpham@bitpim.org>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: call_history_export.py 4377 2007-08-27 04:58:33Z djpham $
0009 
0010 "Deals with Call History import/export stuff"
0011 # System Module
0012 from __future__ import with_statement
0013 # wxPython modules
0014 import wx
0015 
0016 # BitPim Modules
0017 import bptime
0018 import guihelper
0019 import phonenumber
0020 
0021 #------------------------------------------------------------------------------
0022 class ExportCallHistoryDialog(wx.Dialog):
0023     def __init__(self, parent, title):
0024         super(ExportCallHistoryDialog, self).__init__(parent, -1, title)
0025         self._chwidget=parent.GetActiveCallHistoryWidget()
0026         self._sel_data=self._chwidget.get_selected_data()
0027         self._data=self._chwidget.get_data()
0028         # make the ui
0029         vbs=wx.BoxSizer(wx.VERTICAL)
0030         hbs=wx.BoxSizer(wx.HORIZONTAL)
0031         hbs.Add(wx.StaticText(self, -1, "File"), 0, wx.ALL|wx.ALIGN_CENTRE, 5)
0032         self.filenamectrl=wx.TextCtrl(self, -1, "callhistory_export.csv")
0033         hbs.Add(self.filenamectrl, 1, wx.ALL|wx.EXPAND, 5)
0034         self.browsectrl=wx.Button(self, wx.NewId(), "Browse...")
0035         hbs.Add(self.browsectrl, 0, wx.ALL|wx.EXPAND, 5)
0036         vbs.Add(hbs, 0, wx.EXPAND|wx.ALL, 5)
0037         # selection GUI
0038         vbs.Add(self.GetSelectionGui(self), 5, wx.EXPAND|wx.ALL, 5)
0039         # the buttons
0040         vbs.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL,5)
0041         vbs.Add(self.CreateButtonSizer(wx.OK|wx.CANCEL), 0, wx.ALIGN_CENTER|wx.ALL, 5)
0042         # event handlers
0043         wx.EVT_BUTTON(self, self.browsectrl.GetId(), self.OnBrowse)
0044         wx.EVT_BUTTON(self, wx.ID_OK, self.OnOk)
0045         # all done
0046         self.SetSizer(vbs)
0047         self.SetAutoLayout(True)
0048         vbs.Fit(self)
0049 
0050     def GetSelectionGui(self, parent):
0051         hbs=wx.BoxSizer(wx.HORIZONTAL)
0052         rbs=wx.StaticBoxSizer(wx.StaticBox(self, -1, "Call History"), wx.VERTICAL)
0053         lsel=len(self._sel_data)
0054         lall=len(self._data)
0055         self.rows_selected=wx.RadioButton(self, wx.NewId(), "Selected (%d)" % (lsel,), style=wx.RB_GROUP)
0056         self.rows_all=wx.RadioButton(self, wx.NewId(), "All (%d)" % (lall,))
0057         if lsel==0:
0058             self.rows_selected.Enable(False)
0059             self.rows_selected.SetValue(0)
0060             self.rows_all.SetValue(1)
0061         rbs.Add(self.rows_selected, 0, wx.EXPAND|wx.ALL, 2)
0062         hbs.Add(rbs, 3, wx.EXPAND|wx.ALL, 5)
0063         rbs.Add(self.rows_all, 0, wx.EXPAND|wx.ALL, 2)
0064         return hbs
0065 
0066     def OnBrowse(self, _):
0067         with guihelper.WXDialogWrapper(wx.FileDialog(self, defaultFile=self.filenamectrl.GetValue(),
0068                                                      wildcard="CSV files (*.csv)|*.csv", style=wx.SAVE|wx.CHANGE_DIR),
0069                                        True) as (dlg, retcode):
0070             if retcode==wx.ID_OK:
0071                 self.filenamectrl.SetValue(dlg.GetPath())
0072 
0073     def OnOk(self, _):
0074         # do export
0075         filename=self.filenamectrl.GetValue()
0076         try:
0077             _fp=file(filename, 'wt')
0078         except:
0079             _fp=None
0080         if _fp is None:
0081             guihelper.MessageDialog(self, 'Failed to open file ['+filename+']',
0082                                     'Export Error')
0083             self.EndModal(wx.ID_OK)
0084         if self.rows_all.GetValue():
0085             _data=self._data
0086         else:
0087             _data=self._sel_data
0088         self._export_csv(_fp, _data)
0089         _fp.close()
0090         self.EndModal(wx.ID_OK)
0091 
0092     def _datetime_str(self, v):
0093         _dt=bptime.BPTime(v)
0094         return _dt.date_str()+' '+_dt.time_str()
0095     def _phonenumber_str(self, v):
0096         return phonenumber.format(v)
0097     def _hms(self, v):
0098         if v is None or not isinstance(v, int):
0099             return ''
0100         else:
0101             return '%02d:%02d:%02d'%(v/3600, v/60, v%60)
0102 
0103     _csv_template=(
0104         ('Date', 'datetime', _datetime_str),
0105         ('Number', 'number', _phonenumber_str),
0106         ('Name', 'name', None),
0107         ('Duration', 'duration', _hms),
0108         ('Type', 'folder', None))
0109     def _export_csv(self, fp, ch):
0110         # export Call History data to CSV format
0111         # print out the header
0112         fp.write(','.join(['"'+e[0]+'"' for e in self._csv_template])+'\n')
0113         # and the entries
0114         _keys=ch.keys()
0115         _keys.sort()
0116         for k in _keys:
0117             try:
0118                 e=ch[k]
0119                 _l=[]
0120                 for _c in self._csv_template:
0121                     if _c[2] is None:
0122                         _s=str(getattr(e, _c[1], ''))
0123                     else:
0124                         _s=_c[2](self, getattr(e, _c[1], None))
0125                     _l.append('"'+_s.replace('"', '')+'"')
0126                 fp.write(','.join(_l)+'\n')
0127             except:
0128                 if __debug__:
0129                     raise
0130 

Generated by PyXR 0.9.4