Ticket #46351: patch-DSV-DSV.py.diff
File patch-DSV-DSV.py.diff, 20.8 KB (added by mojca (Mojca Miklavec), 10 years ago) |
---|
-
DSV/DSV.py
preliminary attempt to make py-dsv work with wxPython 3.0
old new import os 134 134 os.putenv('LANG', 'C') 135 135 136 136 try: 137 from wx Pythonimport wx, grid137 from wx import wx, grid 138 138 except ImportError: 139 139 wx = None 140 140 … … def exportDSV(input, delimiter = ',', textQualifier = '"', quoteall = 0, newline 625 625 626 626 if wx is not None: 627 627 # ------------------------------------------------------------------------------ 628 class ImportWizardPanel_Delimiters(wx. wxPanel):628 class ImportWizardPanel_Delimiters(wx.Panel): 629 629 """ 630 630 CLASS(SUPERCLASS): 631 ImportWizardPanel_Delimiters(wx. wxPanel)631 ImportWizardPanel_Delimiters(wx.Panel) 632 632 DESCRIPTION: 633 A wx. wxPanel that provides a basic interface for validating and changing the633 A wx.Panel that provides a basic interface for validating and changing the 634 634 parameters for importing a delimited text file. Similar to MS Excel's 635 635 CSV import wizard. Can be used in a series of wizards or embedded in an 636 636 application. 637 637 PROTOTYPE: 638 638 ImportWizardPanel_Delimiters(parent, id, file, data, isValidCallback = None, 639 pos = wx. wxDefaultPosition, size = wx.wxDefaultSize,640 style = wx. wxTAB_TRAVERSAL, name = 'ImportWizardPanel')639 pos = wx.DefaultPosition, size = wx.DefaultSize, 640 style = wx.TAB_TRAVERSAL, name = 'ImportWizardPanel') 641 641 ARGUMENTS: 642 642 - parent is the parent window 643 643 - id is the id of this wizard panel … … if wx is not None: 657 657 """ 658 658 659 659 def __init__(self, parent, id, file, data, isValidCallback = None, 660 pos = wx. wxDefaultPosition, size = wx.wxDefaultSize,661 style = wx. wxTAB_TRAVERSAL, name = "ImportWizardPanel"):662 wx. wxPanel.__init__(self, parent, id, pos, size, style, name)663 self.SetAutoLayout( wx.true)664 mainSizer = wx. wxFlexGridSizer(3, 1)660 pos = wx.DefaultPosition, size = wx.DefaultSize, 661 style = wx.TAB_TRAVERSAL, name = "ImportWizardPanel"): 662 wx.Panel.__init__(self, parent, id, pos, size, style, name) 663 self.SetAutoLayout(True) 664 mainSizer = wx.FlexGridSizer(3, 1) 665 665 self.SetSizer(mainSizer) 666 666 mainSizer.AddGrowableCol(0) 667 667 668 self.initialized = wx.false668 self.initialized = False 669 669 self.data = data 670 670 self.isValidCallback = isValidCallback 671 671 self.Validate = (isValidCallback and self.Validate) or self.BuildPreview 672 672 673 dlg = wx. wxProgressDialog("Import Wizard",673 dlg = wx.ProgressDialog("Import Wizard", 674 674 "Analyzing %s... Please wait." % file, 675 675 3, 676 676 parent, 677 wx. wxPD_APP_MODAL | wx.wxPD_AUTO_HIDE)677 wx.PD_APP_MODAL | wx.PD_AUTO_HIDE) 678 678 textQualifier = guessTextQualifier(data) 679 679 dlg.Update(1) 680 680 newdata = organizeIntoLines(data, textQualifier = textQualifier, limit = 100) … … if wx is not None: 686 686 # ------------- 687 687 msg = ("This screen lets you set the delimiters your data contains.\n" 688 688 "You can see how your data is affected in the preview below.") 689 message1 = wx. wxStaticText(self, -1, msg)689 message1 = wx.StaticText(self, -1, msg) 690 690 691 691 # ------------- 692 delimiterBox = wx. wxBoxSizer(wx.wxHORIZONTAL)693 delimStaticBox = wx. wxStaticBox(self, -1, "Delimiters")694 delimStaticSizer = wx. wxStaticBoxSizer(delimStaticBox, wx.wxVERTICAL)695 delimGridSizer = wx. wxFlexGridSizer(2, 3)692 delimiterBox = wx.BoxSizer(wx.HORIZONTAL) 693 delimStaticBox = wx.StaticBox(self, -1, "Delimiters") 694 delimStaticSizer = wx.StaticBoxSizer(delimStaticBox, wx.VERTICAL) 695 delimGridSizer = wx.FlexGridSizer(2, 3) 696 696 697 697 delims = { 698 698 'Tab': '\t', … … if wx is not None: 704 704 self.delimChecks = {} 705 705 706 706 for label, value in delims.items(): 707 self.delimChecks[value] = wx. wxCheckBox(self, -1, label)708 delimGridSizer.Add(self.delimChecks[value], 0, wx. wxALL, 3)707 self.delimChecks[value] = wx.CheckBox(self, -1, label) 708 delimGridSizer.Add(self.delimChecks[value], 0, wx.ALL, 3) 709 709 wx.EVT_CHECKBOX(self, self.delimChecks[value].GetId(), self.Validate) 710 710 711 otherSizer = wx. wxBoxSizer(wx.wxHORIZONTAL)711 otherSizer = wx.BoxSizer(wx.HORIZONTAL) 712 712 713 self.delimChecks['Other'] = wx. wxCheckBox(self, -1, 'Other:')713 self.delimChecks['Other'] = wx.CheckBox(self, -1, 'Other:') 714 714 wx.EVT_CHECKBOX(self, self.delimChecks['Other'].GetId(), self.Validate) 715 715 716 self.otherDelim = wx. wxTextCtrl(self, -1, size = (20, -1))716 self.otherDelim = wx.TextCtrl(self, -1, size = (20, -1)) 717 717 wx.EVT_TEXT(self, self.otherDelim.GetId(), self.OnCustomDelim) 718 718 719 719 if self.delimChecks.has_key(delimiter): 720 self.delimChecks[delimiter].SetValue( wx.true)720 self.delimChecks[delimiter].SetValue(True) 721 721 elif delimiter is not None: 722 self.delimChecks['Other'].SetValue( wx.true)722 self.delimChecks['Other'].SetValue(True) 723 723 self.otherDelim.SetValue(delimiter) 724 724 725 725 otherSizer.AddMany([ 726 (self.delimChecks['Other'], 0, wx. wxALL, 3),727 (self.otherDelim, 0, wx. wxALIGN_CENTER),726 (self.delimChecks['Other'], 0, wx.ALL, 3), 727 (self.otherDelim, 0, wx.ALIGN_CENTER), 728 728 ]) 729 729 730 730 delimGridSizer.Add(otherSizer) 731 delimStaticSizer.Add(delimGridSizer, 1, wx. wxEXPAND)732 delimOtherSizer = wx. wxBoxSizer(wx.wxVERTICAL)733 self.consecutiveDelimsAs1 = wx. wxCheckBox(self, -1, "Treat consecutive delimiters as one")734 self.consecutiveDelimsAs1.Enable( wx.false)735 tqSizer = wx. wxBoxSizer(wx.wxHORIZONTAL)736 self.textQualifierChoice = wx. wxChoice(self, -1, choices = ['"', "'", "{None}"])731 delimStaticSizer.Add(delimGridSizer, 1, wx.EXPAND) 732 delimOtherSizer = wx.BoxSizer(wx.VERTICAL) 733 self.consecutiveDelimsAs1 = wx.CheckBox(self, -1, "Treat consecutive delimiters as one") 734 self.consecutiveDelimsAs1.Enable(False) 735 tqSizer = wx.BoxSizer(wx.HORIZONTAL) 736 self.textQualifierChoice = wx.Choice(self, -1, choices = ['"', "'", "{None}"]) 737 737 wx.EVT_CHOICE(self, self.textQualifierChoice.GetId(), self.BuildPreview) 738 738 if textQualifier is not None: 739 739 self.textQualifierChoice.SetStringSelection(textQualifier) … … if wx is not None: 741 741 self.textQualifierChoice.SetStringSelection('{None}') 742 742 743 743 tqSizer.AddMany([ 744 (wx. wxStaticText(self, -1, "Text qualifier:"), 0, wx.wxALIGN_RIGHT | wx.wxALIGN_CENTER_VERTICAL),745 (self.textQualifierChoice, 0, wx. wxALL | wx.wxALIGN_LEFT | wx.wxALIGN_CENTER_VERTICAL, 5),744 (wx.StaticText(self, -1, "Text qualifier:"), 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL), 745 (self.textQualifierChoice, 0, wx.ALL | wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 5), 746 746 ]) 747 747 748 748 delimOtherSizer.AddMany([ 749 (self.consecutiveDelimsAs1, 1, wx. wxEXPAND | wx.wxALL, 5),750 (tqSizer, 1, wx. wxALL | wx.wxALIGN_CENTER, 5),749 (self.consecutiveDelimsAs1, 1, wx.EXPAND | wx.ALL, 5), 750 (tqSizer, 1, wx.ALL | wx.ALIGN_CENTER, 5), 751 751 ]) 752 752 753 753 delimiterBox.AddMany([ 754 (delimStaticSizer, 0, wx. wxALIGN_CENTER),755 (delimOtherSizer, 0, wx. wxALIGN_CENTER),754 (delimStaticSizer, 0, wx.ALIGN_CENTER), 755 (delimOtherSizer, 0, wx.ALIGN_CENTER), 756 756 ]) 757 757 758 758 delimStaticBox.Fit() 759 759 760 760 # ------------- 761 761 self.displayRows = 6 762 previewSettingsBox = wx. wxBoxSizer(wx.wxHORIZONTAL)763 self.hasHeaderRow = wx. wxCheckBox(self, -1, "First row is header")762 previewSettingsBox = wx.BoxSizer(wx.HORIZONTAL) 763 self.hasHeaderRow = wx.CheckBox(self, -1, "First row is header") 764 764 wx.EVT_CHECKBOX(self, self.hasHeaderRow.GetId(), self.BuildPreview) 765 765 766 if wx. wxPlatform in ('__WX.WXGTK__', '__WX.WXMSW__'):767 # wx. wxSpinCtrl causes seg fault under GTK when <enter> is hit in text - use wx.wxSpinButton instead768 self.previewRowsText = wx. wxTextCtrl(self, -1, str(self.displayRows),769 size = (30, -1), style = wx. wxTE_PROCESS_ENTER)766 if wx.Platform in ('__WX.WXGTK__', '__WX.WXMSW__'): 767 # wx.SpinCtrl causes seg fault under GTK when <enter> is hit in text - use wx.SpinButton instead 768 self.previewRowsText = wx.TextCtrl(self, -1, str(self.displayRows), 769 size = (30, -1), style = wx.TE_PROCESS_ENTER) 770 770 h = self.previewRowsText.GetSize().height 771 self.previewRows = wx. wxSpinButton(self, -1, size = (-1, h), style = wx.wxSP_VERTICAL)771 self.previewRows = wx.SpinButton(self, -1, size = (-1, h), style = wx.SP_VERTICAL) 772 772 self.previewRows.SetRange(self.displayRows, 100) 773 773 self.previewRows.SetValue(self.displayRows) 774 774 wx.EVT_SPIN(self, self.previewRows.GetId(), self.OnSpinPreviewRows) 775 775 wx.EVT_TEXT_ENTER(self, self.previewRowsText.GetId(), self.OnTextPreviewRows) 776 776 else: 777 self.previewRows = wx. wxSpinCtrl(self, -1, str(self.displayRows),777 self.previewRows = wx.SpinCtrl(self, -1, str(self.displayRows), 778 778 min = self.displayRows, max = 100, size = (50, -1)) 779 779 wx.EVT_SPINCTRL(self, self.previewRows.GetId(), self.BuildPreview) 780 780 781 781 previewSettingsBox.AddMany([ 782 (self.hasHeaderRow, 1, wx. wxALL | wx.wxEXPAND, 5),783 (wx. wxStaticText(self, -1, "Preview"), 0, wx.wxWEST | wx.wxALIGN_RIGHT | wx.wxALIGN_CENTER_VERTICAL, 10),782 (self.hasHeaderRow, 1, wx.ALL | wx.EXPAND, 5), 783 (wx.StaticText(self, -1, "Preview"), 0, wx.WEST | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 10), 784 784 ]) 785 if wx. wxPlatform in ('__WX.WXGTK__', '__WX.WXMSW__'):786 previewSettingsBox.Add(self.previewRowsText, 0, wx. wxALIGN_CENTER | wx.wxALL, 3)785 if wx.Platform in ('__WX.WXGTK__', '__WX.WXMSW__'): 786 previewSettingsBox.Add(self.previewRowsText, 0, wx.ALIGN_CENTER | wx.ALL, 3) 787 787 previewSettingsBox.AddMany([ 788 (self.previewRows, 0, wx. wxALIGN_CENTER | wx.wxALL, 3),789 (wx. wxStaticText(self, -1, "rows"), 0, wx.wxALIGN_RIGHT | wx.wxALIGN_CENTER_VERTICAL),788 (self.previewRows, 0, wx.ALIGN_CENTER | wx.ALL, 3), 789 (wx.StaticText(self, -1, "rows"), 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL), 790 790 ]) 791 791 792 792 # ------------- … … if wx is not None: 804 804 hasHeaders = 0 805 805 cols = 1 806 806 807 previewStaticBox = wx. wxStaticBox(self, -1, "Data Preview")808 previewStaticSizer = wx. wxStaticBoxSizer(previewStaticBox, wx.wxVERTICAL)809 self.preview = grid. wxGrid(self, -1)807 previewStaticBox = wx.StaticBox(self, -1, "Data Preview") 808 previewStaticSizer = wx.StaticBoxSizer(previewStaticBox, wx.VERTICAL) 809 self.preview = grid.Grid(self, -1) 810 810 self.preview.CreateGrid(self.displayRows, cols) 811 self.preview.SetDefaultRowSize(self.preview.GetCharHeight() + 4, wx.true)812 self.preview.EnableEditing( wx.false)811 self.preview.SetDefaultRowSize(self.preview.GetCharHeight() + 4, True) 812 self.preview.EnableEditing(False) 813 813 self.preview.SetColLabelSize(0) 814 814 self.preview.SetRowLabelSize(0) 815 815 self.preview.SetMargins(1, 0) 816 self.initialized = wx.true816 self.initialized = True 817 817 self.BuildPreview() 818 818 819 819 rowheight = self.preview.GetRowSize(0) + 2 820 820 self.preview.SetSize((-1, rowheight * self.displayRows)) 821 previewStaticSizer.Add(self.preview, 0, wx. wxALL | wx.wxEXPAND, 5)821 previewStaticSizer.Add(self.preview, 0, wx.ALL | wx.EXPAND, 5) 822 822 823 823 # ------------- 824 824 mainSizer.AddMany([ 825 (message1, 0, wx. wxALL, 5),826 (delimiterBox, 0, wx. wxALL, 5),827 (previewSettingsBox, 0, wx. wxALL, 5),828 (previewStaticSizer, 0, wx. wxALL | wx.wxEXPAND, 5),825 (message1, 0, wx.ALL, 5), 826 (delimiterBox, 0, wx.ALL, 5), 827 (previewSettingsBox, 0, wx.ALL, 5), 828 (previewStaticSizer, 0, wx.ALL | wx.EXPAND, 5), 829 829 ]) 830 830 831 831 self.Layout() … … if wx is not None: 855 855 if not self.initialized: 856 856 return # got triggered before initialization was completed 857 857 858 if wx. wxPlatform != '__WX.WXGTK__':859 wx. wxBeginBusyCursor() # causes a spurious spin event under GTK860 wx. wxYield() # allow controls to update first, in case of slow preview858 if wx.Platform != '__WX.WXGTK__': 859 wx.BeginBusyCursor() # causes a spurious spin event under GTK 860 wx.Yield() # allow controls to update first, in case of slow preview 861 861 self.preview.BeginBatch() 862 862 self.preview.DeleteCols(0, self.preview.GetNumberCols()) 863 863 self.preview.DeleteRows(0, self.preview.GetNumberRows()) … … if wx is not None: 892 892 for col in range(cols): 893 893 try: self.preview.SetColLabelValue(col, str(previewData[0][col])) 894 894 except: self.preview.SetColLabelValue(col, "") 895 # self.preview.AutoSizeColumns( wx.true) # size columns to headers895 # self.preview.AutoSizeColumns(True) # size columns to headers 896 896 else: 897 897 self.preview.SetColLabelSize(0) 898 898 … … if wx is not None: 902 902 except: pass 903 903 904 904 # if not hasHeaders: 905 self.preview.AutoSizeColumns( wx.true) # size columns to data905 self.preview.AutoSizeColumns(True) # size columns to data 906 906 907 907 rowheight = self.preview.GetRowSize(0) 908 908 self.preview.SetRowSize(0, rowheight) 909 909 self.preview.EndBatch() 910 if wx. wxPlatform != '__WX.WXGTK__':911 wx. wxEndBusyCursor()910 if wx.Platform != '__WX.WXGTK__': 911 wx.EndBusyCursor() 912 912 913 913 self.delimiters = delimiter 914 914 self.textQualifier = textQualifier … … if wx is not None: 928 928 return self.hasHeaders 929 929 930 930 # ------------------------------------------------------------------------------ 931 class ImportWizardDialog(wx. wxDialog):931 class ImportWizardDialog(wx.Dialog): 932 932 """ 933 933 CLASS(SUPERCLASS): 934 ImportWizardDialog(wx. wxDialog)934 ImportWizardDialog(wx.Dialog) 935 935 DESCRIPTION: 936 936 A dialog allowing the user to preview and change the options for importing 937 937 a file. 938 938 PROTOTYPE: 939 939 ImportWizardDialog(parent, id, title, file, 940 pos = wx. wxDefaultPosition, size = wx.wxDefaultSize,941 style = wx. wxDEFAULT_DIALOG_STYLE, name = 'ImportWizardDialog')940 pos = wx.DefaultPosition, size = wx.DefaultSize, 941 style = wx.DEFAULT_DIALOG_STYLE, name = 'ImportWizardDialog') 942 942 ARGUMENTS: 943 943 - parent: the parent window 944 944 - id: the id of this window … … if wx is not None: 955 955 """ 956 956 957 957 def __init__(self, parent, id, title, file, 958 pos = wx. wxDefaultPosition, size = wx.wxDefaultSize,959 style = wx. wxDEFAULT_DIALOG_STYLE, name = "ImportWizardDialog"):960 wx. wxDialog.__init__(self, parent, id, title, pos, size, style, name)961 self.SetAutoLayout( wx.true)958 pos = wx.DefaultPosition, size = wx.DefaultSize, 959 style = wx.DEFAULT_DIALOG_STYLE, name = "ImportWizardDialog"): 960 wx.Dialog.__init__(self, parent, id, title, pos, size, style, name) 961 self.SetAutoLayout(True) 962 962 963 963 self.file = file 964 964 f = open(file, 'r') 965 965 self.data = f.read() 966 966 f.close() 967 967 968 sizer = wx. wxBoxSizer(wx.wxVERTICAL)968 sizer = wx.BoxSizer(wx.VERTICAL) 969 969 self.delimPanel = ImportWizardPanel_Delimiters(self, -1, file, self.data, self.ValidState) 970 970 buttonBox = self.ButtonBox() 971 971 sizer.AddMany([ 972 (self.delimPanel, 0, wx. wxALL, 5),973 (buttonBox, 0, wx. wxSOUTH | wx.wxALIGN_CENTER_HORIZONTAL | wx.wxALIGN_TOP, 0),972 (self.delimPanel, 0, wx.ALL, 5), 973 (buttonBox, 0, wx.SOUTH | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_TOP, 0), 974 974 ]) 975 975 976 976 self.SetSizer(sizer) … … if wx is not None: 980 980 self.Centre() 981 981 982 982 def ButtonBox(self): 983 panel = wx. wxPanel(self, -1)984 panel.SetAutoLayout( wx.true)985 sizer = wx. wxBoxSizer(wx.wxHORIZONTAL)983 panel = wx.Panel(self, -1) 984 panel.SetAutoLayout(True) 985 sizer = wx.BoxSizer(wx.HORIZONTAL) 986 986 panel.SetSizer(sizer) 987 self.ok = wx. wxButton(panel, wx.wxID_OK, "Ok")988 cancel = wx. wxButton(panel, wx.wxID_CANCEL, "Cancel")987 self.ok = wx.Button(panel, wx.ID_OK, "Ok") 988 cancel = wx.Button(panel, wx.ID_CANCEL, "Cancel") 989 989 sizer.AddMany([ 990 (self.ok, 0, wx. wxALIGN_TOP | wx.wxEAST | wx.wxSOUTH, 10),991 (cancel, 0, wx. wxALIGN_TOP | wx.wxWEST | wx.wxSOUTH, 10),990 (self.ok, 0, wx.ALIGN_TOP | wx.EAST | wx.SOUTH, 10), 991 (cancel, 0, wx.ALIGN_TOP | wx.WEST | wx.SOUTH, 10), 992 992 ]) 993 993 panel.Layout() 994 994 panel.Fit() … … if wx is not None: 1002 1002 def ImportData(self, errorHandler = skipRow): 1003 1003 delimiters, qualifier, hasHeaders = self.GetImportInfo() 1004 1004 self.data = organizeIntoLines(self.data, textQualifier = qualifier) 1005 dlg = wx. wxProgressDialog("Import DSV File",1005 dlg = wx.ProgressDialog("Import DSV File", 1006 1006 self.file, 1007 1007 100, 1008 1008 self, 1009 wx. wxPD_CAN_ABORT | wx.wxPD_APP_MODAL | wx.wxPD_AUTO_HIDE)1009 wx.PD_CAN_ABORT | wx.PD_APP_MODAL | wx.PD_AUTO_HIDE) 1010 1010 self.data = importDSV(self.data, 1011 1011 delimiter = delimiters, 1012 1012 textQualifier = qualifier, … … if __name__ == '__main__': 1034 1034 1035 1035 1036 1036 def demo(): 1037 class SampleApp(wx. wxApp):1037 class SampleApp(wx.App): 1038 1038 def OnInit(self): 1039 dlg = wx. wxFileDialog(None, "Choose a file", ".", "",1039 dlg = wx.FileDialog(None, "Choose a file", ".", "", 1040 1040 "CSV files (*.csv)|*.csv|Text files (*.txt)|*.txt|All files (*.*)|*.*", 1041 wx. wxOPEN)1042 if dlg.ShowModal() == wx. wxID_OK:1041 wx.OPEN) 1042 if dlg.ShowModal() == wx.ID_OK: 1043 1043 path = dlg.GetPath() 1044 1044 dlg.Destroy() 1045 1045 … … if __name__ == '__main__': 1049 1049 file.write("LINE %d: %s\n" % (linenumber, oldrow)) 1050 1050 1051 1051 dlg = ImportWizardDialog(None, -1, 'CSV Import Wizard (v.%s)' % __version__, path) 1052 if dlg.ShowModal() == wx. wxID_OK:1052 if dlg.ShowModal() == wx.ID_OK: 1053 1053 results = dlg.ImportData(errorHandler = logErrors) 1054 1054 dlg.Destroy() 1055 1055 errorLog.close() … … if __name__ == '__main__': 1074 1074 else: 1075 1075 dlg.Destroy() 1076 1076 1077 return wx.true1077 return True 1078 1078 1079 1079 app = SampleApp() 1080 1080 app.MainLoop()