From: <jb...@us...> - 2010-11-11 02:26:16
|
Revision: 692 http://pytrainer.svn.sourceforge.net/pytrainer/?rev=692&view=rev Author: jblance Date: 2010-11-11 02:26:09 +0000 (Thu, 11 Nov 2010) Log Message: ----------- Add delimited file import to unified import Modified Paths: -------------- pytrainer/trunk/glade/importdata.glade pytrainer/trunk/pytrainer/gui/windowimportdata.py pytrainer/trunk/pytrainer/gui/windowmain.py pytrainer/trunk/pytrainer/main.py Modified: pytrainer/trunk/glade/importdata.glade =================================================================== --- pytrainer/trunk/glade/importdata.glade 2010-11-10 22:01:21 UTC (rev 691) +++ pytrainer/trunk/glade/importdata.glade 2010-11-11 02:26:09 UTC (rev 692) @@ -842,139 +842,548 @@ <property name="visible">True</property> <property name="left_padding">12</property> <child> - <widget class="GtkTable" id="table1"> + <widget class="GtkScrolledWindow" id="scrolledwindow1"> <property name="visible">True</property> - <property name="n_rows">5</property> - <property name="n_columns">3</property> + <property name="can_focus">True</property> + <property name="hscrollbar_policy">never</property> + <property name="vscrollbar_policy">automatic</property> <child> - <widget class="GtkLabel" id="labelCSVDate"> + <widget class="GtkViewport" id="viewport1"> <property name="visible">True</property> - <property name="xalign">1</property> - <property name="label" translatable="yes">Date</property> + <property name="resize_mode">queue</property> + <property name="shadow_type">none</property> + <child> + <widget class="GtkTable" id="table1"> + <property name="visible">True</property> + <property name="n_rows">16</property> + <property name="n_columns">3</property> + <child> + <widget class="GtkLabel" id="labelCSVDate"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Date</property> + </widget> + <packing> + <property name="top_attach">1</property> + <property name="bottom_attach">2</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVDate"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">1</property> + <property name="bottom_attach">2</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVDistance"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Distance (km)</property> + </widget> + <packing> + <property name="top_attach">2</property> + <property name="bottom_attach">3</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVDuration"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Duration (sec)</property> + </widget> + <packing> + <property name="top_attach">3</property> + <property name="bottom_attach">4</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVDistance"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">2</property> + <property name="bottom_attach">3</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVDuration"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">3</property> + <property name="bottom_attach">4</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVDataField"> + <property name="visible">True</property> + <property name="label" translatable="yes"><b>PyTrainer Field</b></property> + <property name="use_markup">True</property> + </widget> + <packing> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVFileCol"> + <property name="visible">True</property> + <property name="label" translatable="yes"><b>File Column</b></property> + <property name="use_markup">True</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVTitle"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Title</property> + </widget> + <packing> + <property name="top_attach">4</property> + <property name="bottom_attach">5</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVDescent"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Descent (m)</property> + </widget> + <packing> + <property name="top_attach">10</property> + <property name="bottom_attach">11</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVAccent"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Accent (m)</property> + </widget> + <packing> + <property name="top_attach">9</property> + <property name="bottom_attach">10</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVCalories"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Calories (kcal)</property> + </widget> + <packing> + <property name="top_attach">8</property> + <property name="bottom_attach">9</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVMaxSpeed"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Max Speed (km/h)</property> + </widget> + <packing> + <property name="top_attach">7</property> + <property name="bottom_attach">8</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVAvgSpeed"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Average Speed (km/h)</property> + </widget> + <packing> + <property name="top_attach">6</property> + <property name="bottom_attach">7</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVSport"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Sport</property> + </widget> + <packing> + <property name="top_attach">5</property> + <property name="bottom_attach">6</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVHR"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Average Heartrate (bpm)</property> + </widget> + <packing> + <property name="top_attach">11</property> + <property name="bottom_attach">12</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVMaxHR"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Max Heartrate (bpm)</property> + </widget> + <packing> + <property name="top_attach">12</property> + <property name="bottom_attach">13</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVPace"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Pace (min/km)</property> + </widget> + <packing> + <property name="top_attach">13</property> + <property name="bottom_attach">14</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVMaxPace"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Max Pace (min/km)</property> + </widget> + <packing> + <property name="top_attach">14</property> + <property name="bottom_attach">15</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkLabel" id="labelCSVComments"> + <property name="visible">True</property> + <property name="xalign">1</property> + <property name="label" translatable="yes">Comments</property> + </widget> + <packing> + <property name="top_attach">15</property> + <property name="bottom_attach">16</property> + <property name="x_options">GTK_FILL</property> + <property name="y_options">GTK_FILL</property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVTitle"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">4</property> + <property name="bottom_attach">5</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVSport"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">5</property> + <property name="bottom_attach">6</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVAvgSpeed"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">6</property> + <property name="bottom_attach">7</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVMaxSpeed"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">7</property> + <property name="bottom_attach">8</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVCal"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">8</property> + <property name="bottom_attach">9</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVAccent"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">9</property> + <property name="bottom_attach">10</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVDescent"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">10</property> + <property name="bottom_attach">11</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVHR"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">11</property> + <property name="bottom_attach">12</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVMaxHR"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">12</property> + <property name="bottom_attach">13</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVPace"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">13</property> + <property name="bottom_attach">14</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVMaxPace"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">14</property> + <property name="bottom_attach">15</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="cbCSVComments"> + <property name="visible">True</property> + <property name="active">0</property> + <property name="items" translatable="yes">Exclude</property> + </widget> + <packing> + <property name="left_attach">1</property> + <property name="right_attach">2</property> + <property name="top_attach">15</property> + <property name="bottom_attach">16</property> + <property name="x_options"></property> + <property name="y_options"></property> + </packing> + </child> + <child> + <widget class="GtkHBox" id="hbox5"> + <property name="visible">True</property> + <child> + <widget class="GtkCheckButton" id="checkbCSVForceSport"> + <property name="label" translatable="yes">Force sport to:</property> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="receives_default">False</property> + <property name="draw_indicator">True</property> + </widget> + <packing> + <property name="expand">False</property> + <property name="position">0</property> + </packing> + </child> + <child> + <widget class="GtkComboBox" id="comboCSVForceSport"> + <property name="visible">True</property> + <property name="items" translatable="yes"> </property> + </widget> + <packing> + <property name="expand">False</property> + <property name="position">1</property> + </packing> + </child> + </widget> + <packing> + <property name="left_attach">2</property> + <property name="right_attach">3</property> + <property name="top_attach">5</property> + <property name="bottom_attach">6</property> + </packing> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + <child> + <placeholder/> + </child> + </widget> + </child> </widget> - <packing> - <property name="top_attach">1</property> - <property name="bottom_attach">2</property> - <property name="x_options">GTK_FILL</property> - <property name="y_options">GTK_FILL</property> - </packing> </child> - <child> - <widget class="GtkComboBox" id="cbCSVDate"> - <property name="visible">True</property> - <property name="active">0</property> - <property name="items" translatable="yes">Exclude</property> - </widget> - <packing> - <property name="left_attach">1</property> - <property name="right_attach">2</property> - <property name="top_attach">1</property> - <property name="bottom_attach">2</property> - <property name="x_options"></property> - <property name="y_options"></property> - </packing> - </child> - <child> - <widget class="GtkLabel" id="labelCSVDistance"> - <property name="visible">True</property> - <property name="xalign">1</property> - <property name="label" translatable="yes">Distance</property> - </widget> - <packing> - <property name="top_attach">2</property> - <property name="bottom_attach">3</property> - <property name="x_options">GTK_FILL</property> - <property name="y_options">GTK_FILL</property> - </packing> - </child> - <child> - <widget class="GtkLabel" id="labelCSVDuration"> - <property name="visible">True</property> - <property name="xalign">1</property> - <property name="label" translatable="yes">Duration</property> - </widget> - <packing> - <property name="top_attach">3</property> - <property name="bottom_attach">4</property> - <property name="x_options">GTK_FILL</property> - <property name="y_options">GTK_FILL</property> - </packing> - </child> - <child> - <widget class="GtkComboBox" id="cbCSVDistance"> - <property name="visible">True</property> - <property name="active">0</property> - <property name="items" translatable="yes">Exclude</property> - </widget> - <packing> - <property name="left_attach">1</property> - <property name="right_attach">2</property> - <property name="top_attach">2</property> - <property name="bottom_attach">3</property> - <property name="x_options"></property> - <property name="y_options"></property> - </packing> - </child> - <child> - <widget class="GtkComboBox" id="cbCSVDuration"> - <property name="visible">True</property> - <property name="active">0</property> - <property name="items" translatable="yes">Exclude</property> - </widget> - <packing> - <property name="left_attach">1</property> - <property name="right_attach">2</property> - <property name="top_attach">3</property> - <property name="bottom_attach">4</property> - <property name="x_options"></property> - <property name="y_options"></property> - </packing> - </child> - <child> - <widget class="GtkLabel" id="labelCSVDataField"> - <property name="visible">True</property> - <property name="label" translatable="yes"><b>PyTrainer Field</b></property> - <property name="use_markup">True</property> - </widget> - <packing> - <property name="x_options">GTK_FILL</property> - <property name="y_options">GTK_FILL</property> - </packing> - </child> - <child> - <widget class="GtkLabel" id="labelCSVFileCol"> - <property name="visible">True</property> - <property name="label" translatable="yes"><b>File Column</b></property> - <property name="use_markup">True</property> - </widget> - <packing> - <property name="left_attach">1</property> - <property name="right_attach">2</property> - <property name="x_options">GTK_FILL</property> - <property name="y_options">GTK_FILL</property> - </packing> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> - <child> - <placeholder/> - </child> </widget> </child> </widget> @@ -999,18 +1408,28 @@ <widget class="GtkHBox" id="hbox4"> <property name="visible">True</property> <child> - <placeholder/> + <widget class="GtkLabel" id="label1"> + <property name="visible">True</property> + <property name="xalign">0</property> + <property name="label" translatable="yes"><small>This is an experiemental import for delimited files +1) Select a file +2) Click 'Read File' to analyse the file +3) Map columns in the file to pytrainer data elements (Date is required) +4) Click 'Import Data'</small></property> + <property name="use_markup">True</property> + </widget> + <packing> + <property name="position">0</property> + </packing> </child> <child> - <placeholder/> - </child> - <child> <widget class="GtkButton" id="buttonCSVImport"> <property name="label" translatable="yes">Import Data</property> <property name="visible">True</property> <property name="sensitive">False</property> <property name="can_focus">True</property> <property name="receives_default">True</property> + <signal name="clicked" handler="on_buttonCSVImport_clicked"/> </widget> <packing> <property name="expand">False</property> Modified: pytrainer/trunk/pytrainer/gui/windowimportdata.py =================================================================== --- pytrainer/trunk/pytrainer/gui/windowimportdata.py 2010-11-10 22:01:21 UTC (rev 691) +++ pytrainer/trunk/pytrainer/gui/windowimportdata.py 2010-11-11 02:26:09 UTC (rev 692) @@ -25,9 +25,11 @@ import types from lxml import etree import csv +import locale from pytrainer.plugins import Plugins from pytrainer.gui.dialogs import fileChooserDialog +from pytrainer.lib.date import Date class WindowImportdata(SimpleGladeApp): def __init__(self, data_path = None, parent=None, config=None, pytrainer_main=None): @@ -68,7 +70,7 @@ def init_all_tabs(self): logging.debug(">>") - tabs = (0,1,2,3) + tabs = (0,1,2,3,4) for tab in tabs: self.init_tab(tab) @@ -88,6 +90,9 @@ elif page == 3: #'Options' tab self.init_options_tab() + elif page == 4: + #'CSV Import' tab + self.init_csvimport_tab() else: #unknown tab logging.error("Unknown page %d passed to init_tab" % page) @@ -193,6 +198,19 @@ self.checkbuttonAutoLaunch.set_active(1) logging.debug("<<") return + + def init_csvimport_tab(self): + logging.debug(">>") + #Populate Force Sport to combobox + sport_list = self.pytrainer_main.profile.getSportList() + #Remove placeholder item (needed to ensure correct model for combobox) + self.comboCSVForceSport.remove_text(0) + for sport in sport_list: + logging.debug('Adding sport: %s' % sport[0]) + self.comboCSVForceSport.append_text(sport[0]) + self.comboCSVForceSport.set_active(0) + logging.debug("<<") + return def detect_tools(self): ''' @@ -755,31 +773,32 @@ self.buttonCSVImport.set_sensitive(True) def on_buttonCSVProcess_clicked(self, widget): + logging.debug('>>') #Get selected file - filename = self.filechooserCSVImport.get_filename() - if not os.path.isfile(filename): + self.CSVfilename = self.filechooserCSVImport.get_filename() + if not os.path.isfile(self.CSVfilename): return #Determine delimiter if self.rbCSVTab.get_active(): - delimiter = "\t" + self.delimiter = "\t" elif self.rbCSVComma.get_active(): - delimiter = "," + self.delimiter = "," elif self.rbCSVOther.get_active(): - delimiter = self.entryCSVOther.get_text() + self.delimiter = self.entryCSVOther.get_text() else: - delimiter = " " + self.delimiter = " " #Read as delimited file - csvfile = open(filename, 'rb') + csvfile = open(self.CSVfilename, 'rb') #See if file has header row - has_header = csv.Sniffer().has_header(csvfile.read(1024)) + self.has_header = csv.Sniffer().has_header(csvfile.read(1024)) csvfile.seek(0) - reader = csv.DictReader(csvfile, delimiter=delimiter) + reader = csv.DictReader(csvfile, delimiter=self.delimiter) #Read file to determine fields (must be a better way of doing this) for row in reader: pass #Build array of column names - if has_header: + if self.has_header: #If the file has a header row, use the actual column names columns = reader.fieldnames else: @@ -792,8 +811,182 @@ self.cbCSVDate.append_text(column) self.cbCSVDistance.append_text(column) self.cbCSVDuration.append_text(column) + self.cbCSVTitle.append_text(column) + self.cbCSVSport.append_text(column) + self.cbCSVAvgSpeed.append_text(column) + self.cbCSVMaxSpeed.append_text(column) + self.cbCSVCal.append_text(column) + self.cbCSVAccent.append_text(column) + self.cbCSVDescent.append_text(column) + self.cbCSVHR.append_text(column) + self.cbCSVMaxHR.append_text(column) + self.cbCSVPace.append_text(column) + self.cbCSVMaxPace.append_text(column) + self.cbCSVComments.append_text(column) self.cbCSVDate.set_active(0) self.cbCSVDistance.set_active(0) self.cbCSVDuration.set_active(0) + self.cbCSVTitle.set_active(0) + self.cbCSVSport.set_active(0) + self.cbCSVAvgSpeed.set_active(0) + self.cbCSVMaxSpeed.set_active(0) + self.cbCSVCal.set_active(0) + self.cbCSVAccent.set_active(0) + self.cbCSVDescent.set_active(0) + self.cbCSVHR.set_active(0) + self.cbCSVMaxHR.set_active(0) + self.cbCSVPace.set_active(0) + self.cbCSVMaxPace.set_active(0) + self.cbCSVComments.set_active(0) + logging.debug('<<') + def on_buttonCSVImport_clicked(self, widget): + logging.debug('>>') + #Determine values + dateCol = self.cbCSVDate.get_active() + distanceCol = self.cbCSVDistance.get_active() + durationCol = self.cbCSVDuration.get_active() + titleCol = self.cbCSVTitle.get_active() + sportCol = self.cbCSVSport.get_active() + avgspeedCol = self.cbCSVAvgSpeed.get_active() + maxspeedCol = self.cbCSVMaxSpeed.get_active() + calCol = self.cbCSVCal.get_active() + accCol = self.cbCSVAccent.get_active() + desCol = self.cbCSVDescent.get_active() + hrCol = self.cbCSVHR.get_active() + maxHRCol = self.cbCSVMaxHR.get_active() + paceCol = self.cbCSVPace.get_active() + maxPaceCol = self.cbCSVMaxPace.get_active() + commentsCol = self.cbCSVComments.get_active() + + #print dateCol, distanceCol, durationCol, titleCol, sportCol, avgspeedCol, maxspeedCol, calCol, accCol, desCol, hrCol, maxHRCol, paceCol, maxPaceCol, commentsCol + + if dateCol == 0: + #Error need to have at least a date + self.updateStatusbar(self.statusbarCSVImport, "ERROR: Must define at least a date column") + return + + #Import... + #Get selected file + if not os.path.isfile(self.CSVfilename): + return + #Read as delimited file + csvfile = open(self.CSVfilename, 'rb') + reader = csv.reader(csvfile, delimiter=self.delimiter) + #Process File + + for i, row in enumerate(reader): + if self.has_header and i==0: + #Ignore first row + continue + data = {} + #Determine dates + _date = Date().getDateTime(row[dateCol-1]) + #year, month, day = date.split("-") + date = _date[1].strftime("%Y-%m-%d") + zuluDateTime = _date[0].strftime("%Y-%m-%dT%H:%M:%SZ") + localDateTime = str(_date[1]) + data['date'] = date + data['date_time_utc'] = zuluDateTime + data['date_time_local'] = localDateTime + if distanceCol: + try: + data['distance'] = locale.atof(row[distanceCol-1]) + except: + pass + if durationCol: + #calculate duration in sec... + _duration = row[durationCol-1] + if _duration.count(':') == 2: + #Have 00:00:00 duration + h, m, s = _duration.split(':') + try: + durationSec = int(h)*3600 + int(m)*60 + int(s) + except: + logging.debug("Error calculating duration for '%s'" % _duration) + print("Error calculating duration for '%s'" % _duration) + durationSec = None + else: + try: + durationSec = locale.atoi(_duration) + except: + #Unknown duration + logging.debug("Could not determine duration for '%s'" % _duration) + print("Could not determine duration for '%s'" % _duration) + durationSec = None + if durationSec is not None: + data['duration'] = durationSec + data['time'] = str(durationSec) + if titleCol: + data['title'] = row[titleCol-1] + if self.checkbCSVForceSport.get_active(): + sport_id = self.pytrainer_main.record.getSportId(self.comboCSVForceSport.get_active_text(),add=True) + data['sport'] = sport_id + elif sportCol: + #retrieving sport id (adding sport if it doesn't exist yet) + sport_id = self.pytrainer_main.record.getSportId(row[sportCol-1],add=True) + data['sport'] = sport_id + else: + self.comboCSVForceSport.set_active(0) + sport_id = self.pytrainer_main.record.getSportId(self.comboCSVForceSport.get_active_text(),add=True) + data['sport'] = sport_id + + if avgspeedCol: + # + try: + data['average'] = locale.atof(row[avgspeedCol-1]) + except: + pass + if maxspeedCol: + try: + data['maxspeed'] = locale.atof(row[maxspeedCol-1]) + except: + pass + if calCol: + try: + data['calories'] = locale.atoi(row[calCol-1]) + except: + pass + if accCol: + try: + data['upostive'] = locale.atof(row[accCol-1]) + except: + pass + if desCol: + try: + data['unegative'] = locale.atof(row[desCol-1]) + except: + pass + if hrCol: + try: + data['beats'] = locale.atof(row[hrCol-1]) + except: + pass + if maxHRCol: + try: + data['maxbeats'] = locale.atof(row[maxHRCol-1]) + except: + pass + if paceCol: + try: + data['pace'] = locale.atof(row[paceCol-1]) + except: + pass + if maxPaceCol: + try: + data['maxpace'] = locale.atof(row[maxPaceCol-1]) + except: + pass + if commentsCol: + data['comments'] = row[commentsCol-1] + + #Insert into DB + print "Data", data + self.pytrainer_main.ddbb.insert_dict('records', data) + #Display message.... + self.updateStatusbar(self.statusbarCSVImport, "Import completed. %d rows processed" % i) + #Disable import button + self.buttonCSVImport.set_sensitive(0) + + logging.debug('<<') Modified: pytrainer/trunk/pytrainer/gui/windowmain.py =================================================================== --- pytrainer/trunk/pytrainer/gui/windowmain.py 2010-11-10 22:01:21 UTC (rev 691) +++ pytrainer/trunk/pytrainer/gui/windowmain.py 2010-11-11 02:26:09 UTC (rev 692) @@ -971,7 +971,10 @@ gobject.TYPE_INT, object) for i in record_list: - hour,min,sec = date.second2time(int(i[6])) + try: + hour,min,sec = date.second2time(int(i[6])) + except (ValueError, TypeError): + hour,min,sec = (0,0,0) _time = "%2d:%02d:%02d" %(hour,min,sec) #original # experimental only if hour >0: @@ -987,11 +990,20 @@ continue _title = str(i[3]) _date = str(i[0]) - _distance = float(i[1]) + try: + _distance = float(i[1]) + except (ValueError, TypeError): + _distance = 0 _sport = str(i[4]) - _average = float(i[2]) - _calories = int(i[8]) try: + _average = float(i[2]) + except (ValueError, TypeError): + _average = 0 + try: + _calories = int(i[8]) + except (ValueError, TypeError): + _calories = 0 + try: _beats = round(float(i[7])) except (ValueError, TypeError) as e: logging.debug("Unable to parse beats for %s" % str(i[7]) ) Modified: pytrainer/trunk/pytrainer/main.py =================================================================== --- pytrainer/trunk/pytrainer/main.py 2010-11-10 22:01:21 UTC (rev 691) +++ pytrainer/trunk/pytrainer/main.py 2010-11-11 02:26:09 UTC (rev 692) @@ -50,7 +50,7 @@ class pyTrainer: def __init__(self,filename = None, data_path = None): #Version constants - self.version ="1.7.2_svn#690" + self.version ="1.7.2_svn#692" self.DB_version = 6 #Process command line options self.startup_options = self.get_options() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |