[chronojump/chronojump-importer-warn-database-version] When importing sessions: if chronojump_importer.py or Python problem occurrs: shows an error message



commit 835833d80155af44b0e03b3e05d2247110683659
Author: Carles Pina i Estany <carles pina cat>
Date:   Thu Sep 29 23:40:26 2016 +0200

    When importing sessions: if chronojump_importer.py or Python problem occurrs: shows an error message.
    
    Previously some errors like syntax errors on chronojump_importer.py
    would have crashed chronojump. Now for errors like:
     * Invalid databases files (based on the Preferences and version tables)
     * Python is not executed
     * chronojump_importer.py file is not found
    
    A message dialogue is shown so the user can either address the issue or
    ask Chronojump team and we will be able to help based on the error
    message.

 src/chronojumpImporter.cs |  130 ++++++++++++++++++++++++++++++++++----------
 src/gui/chronojump.cs     |   10 ++--
 2 files changed, 105 insertions(+), 35 deletions(-)
---
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
index f3a04e9..633597d 100644
--- a/src/chronojumpImporter.cs
+++ b/src/chronojumpImporter.cs
@@ -5,12 +5,52 @@ using System.Diagnostics;
 using System;
 using Mono.Unix;
 
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or   
+ * (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016   Carles Pina i Estany <carles pina cat>
+ */
+
+
 class ChronojumpImporter
 {
        private string sourceFile;
        private string destinationFile;
        private string session;
 
+
+       // Result struct holds the output, error and success operations. It's used to pass
+       // errors from different layers (e.g. executing Python scripts) to the UI layer
+       public struct Result
+       {
+               public bool success;
+               public string output;
+               public string error;
+
+               public Result(bool success, string output, string error = "")
+               {
+                       this.success = success;
+                       this.output = output;
+                       this.error = error;
+               }
+       }
+
+       // ChronojumpImporter class imports a specific session from sourceFile to destinationFile.
+       // The main method is "import()" which does all the work.
        public ChronojumpImporter(string sourceFile, string destinationFile, string session)
        {
                this.sourceFile = sourceFile;
@@ -18,14 +58,25 @@ class ChronojumpImporter
                this.session = session;
        }
 
-       public bool import()
+       // Tries to import the session and files defined in the constructor and returns Result. See
+       // Result struct information to have a better idea.
+       //
+       // It checks the database versions and aborts (see Result information) if this Chronojump
+       // tries to import a newer chronojump version.
+       public Result import()
        {
-               string sourceDatabaseVersion = getSourceDatabaseVersion ();
-               string destinationDatabaseVersion = getDestinationDatabaseVersion ();
+               Result sourceDatabaseVersion = getSourceDatabaseVersion ();
+               Result destinationDatabaseVersion = getDestinationDatabaseVersion ();
+
+               if (! sourceDatabaseVersion.success)
+                       return sourceDatabaseVersion;
 
-               if (float.Parse(destinationDatabaseVersion) < float.Parse(sourceDatabaseVersion)) {
-                       new DialogMessage (Constants.MessageTypes.WARNING, Catalog.GetString ("Trying to 
import a newer database version than this Chronojump\nPlease, update the running Chronojump."));
-                       return false;
+               if (! destinationDatabaseVersion.success)
+                       return destinationDatabaseVersion;
+
+               if (float.Parse(destinationDatabaseVersion.output) < 
float.Parse(sourceDatabaseVersion.output)) {
+                       return new Result (false, Catalog.GetString ("Trying to import a newer database 
version than this Chronojump\n" +
+                               "Please, update the running Chronojump."));
                }
 
                List<string> parameters = new List<string> ();
@@ -36,22 +87,22 @@ class ChronojumpImporter
                parameters.Add ("--source_session");
                parameters.Add (CommandLineEncoder.EncodeArgText (session));
 
-               executeChronojumpImporter (parameters);
+               Result result = executeChronojumpImporter (parameters);
 
-               return true;
+               return result;
        }
 
-       public string getSourceDatabaseVersion()
+       private Result getSourceDatabaseVersion()
        {
                return getDatabaseVersionFromFile (sourceFile);
        }
 
-       public string getDestinationDatabaseVersion()
+       private Result getDestinationDatabaseVersion()
        {
                return getDatabaseVersionFromFile (destinationFile);
        }
 
-       private string getDatabaseVersionFromFile(string filePath)
+       private Result getDatabaseVersionFromFile(string filePath)
        {
                List<string> parameters = new List<string> ();
 
@@ -59,16 +110,28 @@ class ChronojumpImporter
                parameters.Add (filePath);
                parameters.Add ("--json_information");
 
-               string jsonText = executeChronojumpImporter (parameters);
+               Result result = executeChronojumpImporter (parameters);
 
-               JsonValue result = JsonValue.Parse(jsonText);
+               if (result.success) {
+                       JsonValue json = "";
+                       try {
+                               json = JsonValue.Parse (result.output);
+                       } catch (Exception e) {
+                               LogB.Debug ("getDatabaseVersionFromFile: invalid JSON content:" + 
result.output);
+                               return new Result(false, "", 
String.Format(Catalog.GetString("getDatabaseVersionFromFile: invalid JSON content:\n{0}\nException. {1}"), 
result.output, e.Message));
+                       }
 
-               string databaseVersion = result ["databaseVersion"];
+                       string databaseVersion = json ["databaseVersion"];
+                       
+                       return new Result (true, databaseVersion);
 
-               return databaseVersion;
+               } else {
+                       LogB.Debug ("getDatabaseVersionFromFile: no success fetching the database version" + 
filePath);
+                       return new Result(false, "", 
String.Format(Catalog.GetString("getDatabaseVersionFromFile: no success fetching the database version 
of:\n{0}\nError: {1}"), filePath, result.error));
+               }
        }
 
-       private string executeChronojumpImporter(List<string> parameters)
+       private Result executeChronojumpImporter(List<string> parameters)
        {
                string importer_executable;
                string importer_script_path = "";
@@ -85,7 +148,6 @@ class ChronojumpImporter
 
                processStartInfo = new ProcessStartInfo();
 
-               // processStartInfo.Arguments = importer_script_path + " --source \"" + 
CommandLineEncoder.EncodeArgText (sourceFile) + "\" --destination \"" + CommandLineEncoder.EncodeArgText 
(destinationFile) + "\" --source_session \"" + CommandLineEncoder.EncodeArgText (session) + "\"";
                processStartInfo.Arguments = importer_script_path + " " + string.Join (" ", parameters);
                processStartInfo.FileName = importer_executable;
 
@@ -100,30 +162,38 @@ class ChronojumpImporter
 
                process.StartInfo = processStartInfo;
 
-               bool started = false;
                try {
                        process.Start();
-                       started = true;
                }
                catch(Exception e) {
-                       string errorMessage;
-                       errorMessage = String.Format ("Cannot execute:\n   {0}\nwith the parameters:\n   
{1}\n\nThe exception is: {2}", processStartInfo.FileName, processStartInfo.Arguments, e.Message);
-                       ErrorWindow.Show (errorMessage);
-                       return "";
+                       string errorMessage = String.Format (Catalog.GetString("Cannot start:\n" +
+                                                                              "{0}\n" +
+                                                                              "with the parameters:" +
+                                                                              "{1}\n" +
+                                                                              "Exception:\n" +
+                                                                              "{2}"),
+                                                            processStartInfo.FileName, 
processStartInfo.Arguments, e.Message);
+                       LogB.Information (errorMessage);
+                       return new Result (false, "", errorMessage);
                }
 
-/*             if (started) {
-                       process.BeginOutputReadLine ();
-                       process.BeginErrorReadLine ();
-               }
-*/
                string allOutput = "";
                allOutput += process.StandardOutput.ReadToEnd();
                allOutput += process.StandardError.ReadToEnd();
-               Console.WriteLine(allOutput);
 
                process.WaitForExit ();
 
-               return allOutput;
+               if (process.ExitCode != 0) {
+                       string errorMessage = String.Format (Catalog.GetString("Error executing: {0}\n" +
+                                                                              "with the parameters: {1}\n" +
+                                                                              "output: {2}"),
+                                                            processStartInfo.FileName, 
processStartInfo.Arguments, allOutput);
+
+                       LogB.Information (errorMessage);
+                       // Python interpretar was executed but the Python file wasn't found or the script 
failed
+                       return new Result (false, allOutput, errorMessage);
+               }
+
+               return new Result (true, allOutput);
        }
 }
\ No newline at end of file
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 6906d20..887e06d 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2413,14 +2413,14 @@ public partial class ChronoJumpWindow
 
                ChronojumpImporter chronojumpImporter = new ChronojumpImporter (source_filename, 
destination_filename, session);
 
-               bool imported = chronojumpImporter.import ();
+               ChronojumpImporter.Result result = chronojumpImporter.import ();
 
-               if (imported) {
+               if (result.success) {
                        updateComboStats ();
-                       new DialogMessage (Constants.MessageTypes.INFO, Catalog.GetString ("Session 
imported"));
+                       new DialogMessage (Constants.MessageTypes.INFO, Catalog.GetString ("Session 
imported."));
                }
-               // If not imported ChronojumpImporter object has already shown errors to the user.
-
+               else
+                       new DialogMessage (Constants.MessageTypes.WARNING, result.error);
        }
 
        private void on_open_activate (object o, EventArgs args) 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]