gnutrition-commits
[Top][All Lists]
Advanced

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

[GNUtrition-commits] /srv/bzr/gnutrition/development r22: Conversion to


From: Thomas Sinclair
Subject: [GNUtrition-commits] /srv/bzr/gnutrition/development r22: Conversion to SQLite in progress. Updated configure.in so that check for MySQL is not done and Python version >= 2.6. Updated INSTALL. First run now skips MySQL-related dialogs. Updated browser list in help.py (incomplete list). Renumbered Soup, Drink, Alcohol +1 ea. because Salad and Soup were both 108. Conversion to sqlite3 syntax.
Date: Tue, 26 Jun 2012 15:06:03 -0400
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 22
committer: Thomas Sinclair <address@hidden>
branch nick: development
timestamp: Tue 2012-06-26 15:06:03 -0400
message:
  Conversion to SQLite in progress. Updated configure.in so that check for 
MySQL is not done and Python version >= 2.6. Updated INSTALL. First run now 
skips MySQL-related dialogs. Updated browser list in help.py (incomplete list). 
Renumbered Soup, Drink, Alcohol +1 ea. because Salad and Soup were both 108. 
Conversion to sqlite3 syntax.
modified:
  INSTALL
  configure.in
  data/CATEGORY.txt
  src/config.py
  src/database.py
  src/druid.py
  src/druid_ui.py
  src/gnutr_stock.py
  src/help.py
  src/install.py.in
  src/person.py
  src/plan_win.py
  src/recipe_win.py
  src/run_app.py
  src/version.py
=== modified file 'INSTALL'
--- a/INSTALL   2012-05-28 03:12:23 +0000
+++ b/INSTALL   2012-06-26 19:06:03 +0000
@@ -2,28 +2,24 @@
 
     GNUTRITION is written is Python. It requires the following:
 
+         * Python >= 2.6
+
       * The Python bindings to GTK+ and GNOME.
-
-      * MySQL database server and client - version 3.22 or above.
-        At present gnutrition requires a locally installed server.
-
-      * MySQLdb modules - a Python interface to MySQL.
-        The home page is at http://dustman.net/andy/python/MySQLdb and
-        the latest version can be downloaded from:
-            http://dustman.net/andy/python/MySQLdb/0.3.5
+        On Fedora 15 the package name is pygtk2
 
       For documentation:
       * docbook-utils for db2html command  (not critical)
 
 UPGRADING
 
-    Note that if you are upgrading from version 0.2 to 0.3 or above, 
-    gnutrition now uses a MySQL database to store recipes. This means that 
-    recipes created with version 0.2 cannot be loaded into 0.3. They must be
-    re-created using version 0.3. Sorry.
+    Note that if you are upgrading from versions < 0.32 GNUtrition now uses
+       the built-in SQLite3 Python module sqlite3. If you have been using a 
+       GNUtrition version which uses a MySQL database the gnutr_db database 
will
+       be converted to the SQLite3 format. There is no longer a need for 
database
+    username or password. The new format is stored in your home directory in
+       the .gnutrition directory (back it up!).
 
-    In future versions, if the database table format is changed, I will
-    provide smoother way to upgrade between without losing data.
+    If you have recipes created with version 0.2 they must be re-created.
 
 INSTALLATION
 
@@ -39,37 +35,18 @@
         /usr/local/share
        
     Use the `--prefix' command line option to install elsewhere. 
-    For example to install in your home directory:
-
-        ./configure --prefix=/usr
+    For example to install in your home directory under gnutrition:
+               
+               mkdir ~/gnutrition
+        ./configure --prefix=$HOME/gnutrition
         make
         make install
 
-    After installing gnutrition, you will need to make sure that
-    MySQL is set up properly, especially if you have installed
-    MySQL for the first time.
-
-    MySQL uses an access privilege system. When it is installed, the
-    initial root password is empty. To get gnutrition working, all you
-    need to do is make sure that you specify a root password.
-
-    To save you the trouble of reading the MySQL manual, this can be
-    achieved as follows:
-
-    From a shell prompt type:
-
-        mysql -uroot mysql
-
-    You should now get a MySQL prompt of "mysql>".
-
-    From then MySQL prompt type the following lines:
-
-        mysql> UPDATE user SET Password=PASSWORD('new_password') 
-                    WHERE user='root';
-        mysql> FLUSH PRIVILEGES;
-        mysql> quit
-
-    Note that in the UPDATE line above, the single quotes are needed
-    around new_password and root.
+       To put gnutrition in your $PATH (for above example) add
+
+       export PATH=$PATH:$HOME/gnutrition/bin
+
+       to your .bash_profile (or .profile, whichever you use). Then log out
+       out and log back in.
 
     That's it! You can now type "gnutrition".

=== modified file 'configure.in'
--- a/configure.in      2012-05-28 00:06:39 +0000
+++ b/configure.in      2012-06-26 19:06:03 +0000
@@ -1,5 +1,5 @@
 # Copyright (C) 2000, 2001, 2002 Edgar Denny
-# Copyright (C) 2010 Free Software Foundation, Inc.
+# Copyright (C) 2010, 2012 Free Software Foundation, Inc.
 #
 # This file is part of GNUtrition.
 # 
@@ -24,8 +24,8 @@
 
 MAJOR_VERSION=0
 <<<<<<< TREE
-MINOR_VERSION=31.2
-VERSION="0.31.2"
+MINOR_VERSION=32
+VERSION="0.32"
 =======
 >>>>>>> MERGE-SOURCE
 AC_SUBST(MAJOR_VERSION)
@@ -36,8 +36,8 @@
 AC_PATH_PROG(PYTHON, python)
 AC_SUBST(PYTHON)
 
-dnl Checking for Python >= 2.2
-AC_MSG_CHECKING(for python >= 2.2)
+dnl Checking for Python >= 2.6
+AC_MSG_CHECKING(for python >= 2.6)
 
 prog1="
 import sys
@@ -51,7 +51,7 @@
 
 if $PYTHON -c "$prog1" 1>&AC_FD_CC 2>&AC_FD_CC
 then  
-    dnl The default Python path is 2.2 or higher.
+    dnl The default Python path is 2.6 or higher.
     AC_MSG_RESULT(yes)
 else
     dnl The default Python path is lower that 2.2. Can we find
@@ -88,23 +88,6 @@
 **** The python interpreter can't find the python bindings for gtk.])
 fi
 
-dnl Check that the python bindings for MySQL are installed
-AC_MSG_CHECKING(for MySQLdb)
-prog4="
-import sys
-try:
-    import MySQLdb
-except ImportError:
-    sys.exit(1)
-sys.exit(0)"
-if $PYTHON -c "$prog4" 1>&AC_FD_CC 2>&AC_FD_CC
-then  
-    AC_MSG_RESULT(yes)
-else
-    AC_MSG_ERROR([
-**** The python interpreter can't find the python bindings for MySQL.])
-fi
-
 AC_OUTPUT( run-gnutrition.py\
        src/install.py\
        Makefile\

=== modified file 'data/CATEGORY.txt'
--- a/data/CATEGORY.txt 2012-05-21 00:15:02 +0000
+++ b/data/CATEGORY.txt 2012-06-26 19:06:03 +0000
@@ -6,6 +6,6 @@
 106^Appetizer
 107^Bread
 108^Salad
-108^Soup
-109^Drink
-110^Alcohol
+109^Soup
+110^Drink
+111^Alcohol

=== modified file 'src/config.py'
--- a/src/config.py     2012-06-04 17:03:48 +0000
+++ b/src/config.py     2012-06-26 19:06:03 +0000
@@ -17,13 +17,14 @@
 #
 
 import shelve
-import os
-
-dir = os.environ['HOME'] + '/.gnutrition'
-fn = dir + '/config'
-
-if not os.access(dir, os.F_OK):
-    os.mkdir(dir)
+from os import environ, path, access, F_OK, mkdir
+
+home = environ['HOME']
+user = path.basename(home)
+udir = path.join(home, '.gnutrition')
+fn = path.join(udir, 'config')
+if not access(udir, F_OK):
+    mkdir(udir)
 
 def get_value(key):
     db = shelve.open(fn, 'c')

=== modified file 'src/database.py'
--- a/src/database.py   2012-05-28 14:46:37 +0000
+++ b/src/database.py   2012-06-26 19:06:03 +0000
@@ -14,133 +14,129 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-import MySQLdb
-import install
-import warnings
+import sqlite3 as dbms
+import datetime, time
+
+def ticks():
+    return time.time()
+
+def curtime():
+    """Return current local time as hh:mm"""
+    return str(dbms.TimeFromTicks(ticks()))[:5]
+
+def curdate():
+    """Return todays date as yyyy-mm-dd"""
+    return str(dbms.DateFromTicks(ticks()))
+
+dbms.register_adapter(datetime.datetime, curtime)
+dbms.register_adapter(datetime.datetime, curdate)
 
 class Database:
     _shared_state = {}
-    def __init__(self, uname=None, pword=None):
+    def __init__(self):
         self.__dict__ = self._shared_state
         if self._shared_state:
             return
-    # supress warning on "DROP TABLE IF EXISTS" for temp tables
-        warnings.filterwarnings("ignore", "Unknown table.*_temp")
-    # supress 'Data truncated ...' 
-        warnings.filterwarnings("ignore", "Data truncated*")
-
-        self.db = MySQLdb.Connect(user=uname, passwd=pword)
-        self.cursor = self.db.cursor()
-        self.user = uname
-        self.rows = 0
-        self.result = None
-
-    def change_user(self, uname, pword, dbase): 
+        self.Error = dbms.Error
+        from os import path
+        import config
+        self.user = config.user
+        dbfile =  path.join(config.udir, 'gnutr_db')
         try:
-            self.db = MySQLdb.Connect(user=uname, passwd=pword, db=dbase)
+            self.db = dbms.connect(dbfile)
+        except self.Error, e:
+            "Error {0:s}:".format(e.args[0])
+            raise self.Error
+        else:
+            # text_factory must be set to 'str' due to current limitations
+            # in csv.reader()
+            self.db.text_factory = str
             self.cursor = self.db.cursor()
-            self.user = uname
-        except:
-            return 0
-        return 1
+            self.rows = 0
+            self.result = None
 
     def initialize(self):
-        try:
-            self.cursor.execute('SHOW DATABASES')
-            list_db = self.cursor.fetchall()
-        except:
-            import sys
-            import traceback
-            traceback.print_exc()
-            sys.exit()
-#            pass
-
-        match = 0
-        for b in list_db:
-            if b[0] == 'gnutr_db':
-                match = 1
-        if match == 1: 
-            return 0
-
-        self.query('CREATE DATABASE gnutr_db')
-        self.query('USE gnutr_db')
-
         # Create Food Description (food_des) table.
         # Data file FOOD_DES.
-        self.create_load_table("CREATE TABLE food_des " +
-            "(NDB_No SMALLINT(5) UNSIGNED NOT NULL, " + 
-            "FdGrp_Cd SMALLINT(4) UNSIGNED NOT NULL, " + 
-            "Long_Desc CHAR(200) NOT NULL, " + 
-            "Shrt_Desc CHAR(60) NOT NULL, " + 
+        self.create_load_table("CREATE TABLE food_des" +
+            "(NDB_No INTEGER NOT NULL, " + 
+            "FdGrp_Cd INTEGER NOT NULL, " + 
+            "Long_Desc TEXT NOT NULL, " + 
+            "Shrt_Desc TEXT NOT NULL, " + 
             # Three new fields for sr24
-            "ComName CHAR(100), " + 
-            "ManufacName CHAR(65), " +
-            "Survey CHAR(1), " +
+            "ComName TEXT, " + 
+            "ManufacName TEXT, " +
+            "Survey TEXT, " +
             # end new
-            "Ref_desc CHAR(135), " + 
-            "Refuse SMALLINT(3), " + 
-            "SciName CHAR(65), " + 
-            "N_Factor FLOAT(4,2), " +
-            "Pro_Factor FLOAT(4,2), " + 
-            "Fat_Factor FLOAT(4,2), " + 
-            "CHO_Factor FLOAT(4,2), " + 
-            "INDEX (NDB_No), INDEX (FdGrp_Cd) " +
-            ") ENGINE=InnoDB", 'food_des')
+            "Ref_desc TEXT, " + 
+            "Refuse INTEGER, " + 
+            "SciName TEXT, " + 
+            "N_Factor REAL, " +
+            "Pro_Factor REAL, " + 
+            "Fat_Factor REAL, " + 
+            "CHO_Factor REAL, " +
+            "PRIMARY KEY(NDB_No, FdGrp_Cd))",
+            ### Insert statement
+            "INSERT INTO 'food_des' VALUES " +
+            "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+            'food_des')
 
         # Create Food Group Description (fd_group) table.
         # Data file FD_GROUP.
         self.create_load_table("CREATE TABLE fd_group " + 
-            "(FdGrp_Cd SMALLINT(4) UNSIGNED NOT NULL, " + 
-            "FdGrp_Desc CHAR(60) NOT NULL, " + 
-            "INDEX (FdGrp_Cd)) " +
-            "ENGINE=InnoDB", 'fd_group')
+            "(FdGrp_Cd INTEGER PRIMARY KEY NOT NULL, " + 
+            "FdGrp_Desc TEXT NOT NULL)",
+            ### Insert statement
+            "INSERT INTO 'fd_group' VALUES (?, ?)",
+            'fd_group')
 
         # Create Nutrient Data (nut_data) table.
         # Data file NUT_DATA
         self.create_load_table("CREATE TABLE nut_data " + 
-            "(NDB_No SMALLINT(5) UNSIGNED NOT NULL, " + 
-            "Nutr_No SMALLINT(3) UNSIGNED NOT NULL, " + 
-            "Nutr_Val FLOAT(10,3) NOT NULL, " + 
-            "Num_Data_Pts FLOAT(5,0) NOT NULL, " + 
-            "Std_Error FLOAT(8,3), " + 
-            "Src_Cd CHAR(2) NOT NULL, " +
+            "(NDB_No INTEGER NOT NULL, " + 
+            "Nutr_No INTEGER NOT NULL, " + 
+            "Nutr_Val REAL NOT NULL, " + 
+            "Num_Data_Pts REAL NOT NULL, " + 
+            "Std_Error REAL, " + 
+            "Src_Cd TEXT NOT NULL, " +
             # New fields in sr24
-            "Deriv_Cd CHAR(4), " +
-            "Ref_NDB_No CHAR(5), " +
-            "Add_Nutr_Mark CHAR(1), " +
-            "Num_Studies SMALLINT(2), " +
-            "Min FLOAT(10,3), " +
-            "Max FLOAT(10,3), " +
-            "DF SMALLINT(2), " +
-            "Low_EB FLOAT(10,3), " +
-            "Up_EB FLOAT(10,3), " +
-            "Stat_cmt CHAR(10), " +
-            "AddMod_Date CHAR(10), " +
-            "CC CHAR(1), " +
-            # end new fields
-            "INDEX (NDB_No, Nutr_No) " +
-            ") " +
-            "ENGINE=InnoDB", 'nut_data')
+            "Deriv_Cd TEXT, " +
+            "Ref_NDB_No TEXT, " +
+            "Add_Nutr_Mark TEXT, " +
+            "Num_Studies INTEGER, " +
+            "Min REAL, " +
+            "Max REAL, " +
+            "DF INTEGER, " +
+            "Low_EB REAL, " +
+            "Up_EB REAL, " +
+            "Stat_cmt TEXT, " +
+            "AddMod_Date TEXT, " +
+            "CC TEXT, " +
+            "PRIMARY KEY(NDB_No, Nutr_No))",
+            ### Insert statement
+            "INSERT INTO 'nut_data' VALUES " +
+            "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+            'nut_data') 
 
         # Create Nutrient Definition (nutr_def table.
         # Data file NUTR_DEF
         self.create_load_table("CREATE TABLE nutr_def " + 
-            "(Nutr_No SMALLINT(3) UNSIGNED NOT NULL, " + 
-            "Units CHAR(7) NOT NULL, " +
-            "Tagname CHAR(20), " +
-            "NutrDesc CHAR(60) NOT NULL, " +
+            "(Nutr_No INTEGER PRIMARY KEY NOT NULL, " + 
+            "Units TEXT NOT NULL, " +
+            "Tagname TEXT, " +
+            "NutrDesc TEXT NOT NULL, " +
             # Two new in sr24
-            "Num_Dec SMALLINT(1) NOT NULL, " +
-            "SR_Order MEDIUMINT(6) NOT NULL, " +
-            #
-            "INDEX (Nutr_No)) " +
-            "ENGINE=InnoDB", 'nutr_def')
+            "Num_Dec INTEGER NOT NULL, " +
+            "SR_Order INTEGER NOT NULL)",
+            ### Insert statement
+            "INSERT INTO 'nutr_def' VALUES " +
+            "(?, ?, ?, ?, ?, ?)",
+            'nutr_def')
 
         # Create temporary weight table.
         # Data file WEIGHT.
         self.create_load_table("CREATE TABLE weight" +
-            "(NDB_No SMALLINT(5) UNSIGNED NOT NULL, " +
+            "(NDB_No INTEGER NOT NULL, " +
             # Seq == Sequence number for measure description (Msre_Desc)
             # The NDB_No for a food item will appear once for each measure
             # description. Measure descriptions are sequenced. For example:
@@ -149,121 +145,145 @@
             # 01001   2    1     tbsp                       14.2
             # 01001   3    1     pat (1" sq, 1/3" high)      5.0
             # 01001   4    1     stick                     113
-            "Seq SMALLINT(2) NOT NULL, " +
+            "Seq INTEGER NOT NULL, " +
             # Amount == Unit modifier (for example, 1 in "1 cup").
-            "Amount FLOAT(5,3) NOT NULL, " +
-            "Msre_No MEDIUMINT(6) UNSIGNED NOT NULL, " +
-            "Gm_wgt FLOAT(7,1) NOT NULL, " +
-            "INDEX (NDB_No, Seq) " +
-            ")ENGINE=InnoDB", 'weight')
+            "Amount REAL NOT NULL, " +
+            "Msre_No INTEGER NOT NULL, " +
+            "Gm_wgt REAL NOT NULL, " +
+            "PRIMARY KEY(NDB_No, Seq))",
+            ### Insert statement
+            "INSERT INTO 'weight' VALUES " +
+            "(?, ?, ?, ?, ?)",
+            'weight')
 
         # create measure table
         self.create_load_table("CREATE TABLE measure " +
-            "(Msre_No MEDIUMINT(6) UNSIGNED NOT NULL, " +
-            "Msre_Desc CHAR(80) NOT NULL, " +
-            "INDEX(Msre_No) ) " +
-            "ENGINE=InnoDB", 'measure')
+            "(Msre_No INTEGER PRIMARY KEY NOT NULL, " +
+            "Msre_Desc TEXT NOT NULL)",
+            "INSERT INTO 'measure' VALUES (?, ?)",
+            'measure')
 
         # create recipe table
+        # HERE: recipe_no had AUTOINCREMENT but barfs on the syntax
         self.create_table("CREATE TABLE recipe " +
-            "(recipe_no MEDIUMINT(6) UNSIGNED NOT NULL AUTO_INCREMENT, " +
-            "recipe_name CHAR(200) NOT NULL, " +
-            "no_serv SMALLINT(4) UNSIGNED NOT NULL, " +
-            "no_ingr SMALLINT(4) UNSIGNED NOT NULL, " +
-            "category_no TINYINT(3) UNSIGNED NOT NULL, " +
-            "PRIMARY KEY (recipe_no), " +
-            "INDEX (recipe_name(20), category_no)) " +
-            "ENGINE=InnoDB", 'recipe')
+            "(recipe_no INTEGER NOT NULL, " +
+            "recipe_name TEXT NOT NULL, " +
+            "no_serv INTEGER NOT NULL, " +
+            "no_ingr INTEGER NOT NULL, " +
+            "category_no INTEGER NOT NULL, " +
+            "PRIMARY KEY (recipe_no , recipe_name, category_no))", 'recipe')
+
+        #trigger = """\
+        #CREATE TRIGGER recipe_number BEFORE INSERT ON recipe
+        #  BEGIN
+        #    UPDATE recipe SET recipe_no = recipe.ROWID WHERE recipe_no = NULL;
+        #  END;
+        #"""
+        #self.query(trigger)
+        
 
         # create ingredient table
         self.create_table("CREATE TABLE ingredient " + 
-            "(recipe_no MEDIUMINT(6) NOT NULL, " + 
-            "amount FLOAT(7,2) NOT NULL, " +
-            "Msre_No MEDIUMINT(5) UNSIGNED NOT NULL, " +
-            "NDB_No SMALLINT(5) UNSIGNED NOT NULL, " +
-            "INDEX (recipe_no)) " +
-            "ENGINE=InnoDB", 'ingredient')
+            "(recipe_no INTEGER NOT NULL, " + 
+            "amount REAL NOT NULL, " +
+            "Msre_No INTEGER NOT NULL, " +
+            "NDB_No INTEGER NOT NULL)", 'ingredient')
 
         # create recipe category table
         self.create_load_table("CREATE TABLE category " +
-            "(category_no TINYINT(3) UNSIGNED NOT NULL, " +
-            "category_desc CHAR(40) NOT NULL, " +
-            "INDEX (category_no)) " +
-            "ENGINE=InnoDB", 'category')
+            "(category_no INTEGER PRIMARY KEY NOT NULL, " +
+            "category_desc TEXT NOT NULL)",
+            ### Insert statement
+            "INSERT INTO 'category' VALUES (?, ?)",
+            'category')
 
         # create recipe preparation table
         self.create_table("CREATE TABLE preparation " +
-            "(recipe_no MEDIUMINT(6) UNSIGNED NOT NULL, " +
-            "prep_time CHAR(50), " +
-            "prep_desc TEXT, " +
-            "INDEX (recipe_no)) " +
-            "ENGINE=InnoDB", 'preparation')
+            "(recipe_no INTEGER PRIMARY KEY NOT NULL, " +
+            "prep_time TEXT, " +
+            "prep_desc TEXT)", 'preparation')
 
         # create person table
         self.create_table("CREATE TABLE person " +
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL AUTO_INCREMENT " +
-            "PRIMARY KEY, " +
-            "person_name CHAR(100), INDEX person_name (person_name(10)), " +
-            "user_name CHAR(50)) " +
-            "ENGINE=InnoDB", 'person')
+            "(person_no INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
+            "person_name TEXT, " +
+            "user_name TEXT)", 'person')
 
         # create food_plan table
         self.create_table("CREATE TABLE food_plan " +
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL, " +
-            "date DATE NOT NULL, " +
-            "time TIME NOT NULL, " +
-            "amount FLOAT(7,2) NOT NULL, " +
-            "Msre_No MEDIUMINT(6) NOT NULL, " +
-            "Ndb_No SMALLINT(5) UNSIGNED NOT NULL) " +
-            "ENGINE=InnoDB", 'food_plan')
+            "(person_no INTEGER NOT NULL, " +
+            "date TEXT NOT NULL, " +
+            "time TEXT NOT NULL, " +
+            "amount REAL NOT NULL, " +
+            "Msre_No INTEGER NOT NULL, " +
+            "Ndb_No INTEGER NOT NULL)", 'food_plan')
 
         # create recipe_plan table
         self.create_table("CREATE TABLE recipe_plan " +
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL, " +
-            "date DATE NOT NULL, " +
-            "time TIME NOT NULL, " +
-            "no_portions FLOAT(7,2) NOT NULL, " +
-            "recipe_no MEDIUMINT(6) UNSIGNED NOT NULL) " +
-            "ENGINE=InnoDB", 'recipe_plan')
+            "(person_no INTEGER NOT NULL, " +
+            "date TEXT NOT NULL, " +
+            "time TEXT NOT NULL, " +
+            "no_portions REAL NOT NULL, " +
+            "recipe_no INTEGER NOT NULL)", 'recipe_plan')
 
         # create nutr_goal table
         self.create_table("CREATE TABLE nutr_goal " +
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL, " +
-            "Nutr_No SMALLINT(3) UNSIGNED NOT NULL, " +
-            "goal_val FLOAT(11,4) NOT NULL) " +
-            "ENGINE=InnoDB", 'nutr_goal')
+            "(person_no INTEGER NOT NULL, " +
+            "Nutr_No INTEGER NOT NULL, " +
+            "goal_val REAL NOT NULL)", 'nutr_goal')
+
         self.cursor.close()
         self.cursor = self.db.cursor()
         return 1
 
-    def query(self, query, caller=None):
+    def curtime(self):
+        return curtime()
+
+    def curdate(self):
+        return curdate()
+
+    def query(self, sql, many=False, sql_params=None, caller=None):
+        """Execute the SQL statement with given SQL parameters."""
         try:
-            self.cursor.execute(query)
-        except MySQLdb.Error, sqlerr:
-            print 'Error :', sqlerr, '\nquery:', query
-            self.cursor.execute('SHOW ERRORS');
-            import traceback
+            if sql_params:
+                if many:
+                    self.cursor.executemany(sql, sql_params)
+                else:
+                    self.cursor.execute(sql, sql_params)
+            elif many:
+                self.cursor.executemany(sql)
+            else:
+                self.cursor.execute(sql)
+        except self.Error, sqlerr:
             import sys
-            traceback.print_exc()
+            print 'Error :', sqlerr, '\nquery:', sql
             if caller: print 'Caller ', caller
             sys.exit()
-        self.result = self.cursor.fetchall()
-        self.rows = self.db.affected_rows()
+        result = self.cursor.fetchall()
+               # Convert to tuple as GNUtrition code expects MySQLdb tuple 
return
+        self.result = tuple(result)
+        self.last_query = sql
+        self.last_query_params = sql_params
         self.db.commit()
-#        return self.get_result()
 
     def get_result(self):
         result = self.result
         self.result = None
         if not result:
-            print 'No result'
+            print 'No result from:'
+            print 'sql:', self.last_query
+            if self.last_query_params:
+                print 'sql params:', self.last_query_params
         return result
 
     def get_row_result(self):
         result = self.result
         self.result = None
         if not result:
-            print 'No result'
+            print 'No result from:'
+            print 'sql:', self.last_query
+            if self.last_query_params:
+                print 'sql params:', self.last_query_params
             return None
         if len(result) == 1:
             return result[0]
@@ -274,7 +294,10 @@
         result = self.result
         self.result = None
         if not result:
-            print 'No result'
+            print 'No result from:'
+            print 'sql:', self.last_query
+            if self.last_query_params:
+                print 'sql params:', self.last_query_params
             return None
         if len(result) == 1:
             if len(result[0] ) == 1:
@@ -282,20 +305,36 @@
         print 'Error: not a single value'
         return None
 
-    def create_table(self, query, tablename):
-        self.query(query)
-        print "table created: ", tablename
-
-    def load_table(self, fn, table):
-        self.query("LOAD DATA LOCAL INFILE '"+ fn + "' " +
-            "INTO TABLE " + table + " FIELDS TERMINATED BY '^'")
-
-    def create_load_table(self, query, filename):
+    def create_table(self, sql, tablename):
+        self.query(sql)
+        print "created table '{0:s}'".format(tablename)
+
+    def load_table(self, sql, data_fn):
+        #self.query("LOAD DATA LOCAL INFILE '"+ fn + "' " +
+        #    "INTO TABLE " + table + " FIELDS TERMINATED BY '^'")
+        import csv
+        try:
+            data = csv.reader(open(data_fn, 'r'), delimiter='^', quotechar="'")
+        except Exception, e:
+            print "Failed to read data file '{0:s}'".format(data_fn)
+            return False
+        self.query(sql, many=True, sql_params=data)
+        return True
+
+    def create_load_table(self, create_sql, insert_sql, table_name):
+        """Create and load table from file.
+        'create_sql' is the SQL statement for table creation.
+        'insert_sql' is the SQL statement given to executemany.
+        'table_name' serves as both the database table name and the data file 
name.
+        """
+        import install
         from os import path
-        self.create_table(query, filename)
-        fn = path.join(install.dir,'data',filename.upper() + '.txt')
-        self.load_table(fn, filename)
-        print "table loaded: ", filename
+        self.create_table(create_sql, table_name)
+        data_file = path.join(install.idir, 'data', table_name.upper() + 
'.txt')
+        if self.load_table(insert_sql, data_file):
+            print "loaded table '{0:s}'".format(table_name)
+        else:
+            print "failed to load table '{0:s}'".format(table_name)
 
     def add_user(self, user, password):
         self.query("GRANT USAGE ON *.* TO " + user +
@@ -306,3 +345,16 @@
 
     def delete_db(self):
         self.query("DROP DATABASE gnutr_db")
+
+    def next_row(self, col, table):
+        self.query("SELECT MAX({0:s}) from {1:s}".format(col, table))
+        m = self.get_single_result()
+        if not m:
+            m = 1
+        else:
+            m += 1
+        return m
+
+if __name__ == '__main__':
+    db = Database()
+    #db.initialize()

=== modified file 'src/druid.py'
--- a/src/druid.py      2012-06-04 17:03:48 +0000
+++ b/src/druid.py      2012-06-26 19:06:03 +0000
@@ -49,12 +49,13 @@
 
         # Database Create
         elif self.ui.page_num == 1:
-            uname = self.ui.page_list[1].root_user_entry.get_text()
-            pword = self.ui.page_list[1].root_pass_entry.get_text()
-            if (not uname) or (not pword):
-                return
+            #uname = self.ui.page_list[1].root_user_entry.get_text()
+            #pword = self.ui.page_list[1].root_pass_entry.get_text()
+            #if (not uname) or (not pword):
+            #    return
             try:
-                self.db = database.Database(uname, pword)
+                self.db = database.Database()
+                #self.db = database.Database(uname, pword)
             except Exception, ex:
                 self.ui.set_page(2)
                 return
@@ -62,24 +63,25 @@
             self.db.initialize()
             
             # no error, so skip over page_db_error
-            self.ui.set_page(3)
+            #self.ui.set_page(3)
+            self.ui.set_page(5)
             return
 
-        # User Setup
+        # User Setup (skipped if using SQLite)
         elif self.ui.page_num == 3:
-            uname = self.ui.page_list[3].user_entry.get_text()
-            pword = self.ui.page_list[3].pass_entry.get_text()
-            if (not uname) or (not pword):
-                return
-            success = self.user_setup(uname, pword)
-            if not success:
-                self.ui.set_page(4)
-                return
-            self.db.change_user(uname, pword, 'gnutr_db')
+            #uname = self.ui.page_list[3].user_entry.get_text()
+            #pword = self.ui.page_list[3].pass_entry.get_text()
+            #if (not uname) or (not pword):
+            #    return
+            #success = self.user_setup(uname, pword)
+            #if not success:
+            #    self.ui.set_page(4)
+            #    return
+            #self.db.change_user(uname, pword, 'gnutr_db')
 
             # does the user have an entry in the person table?
             self.person = person.Person()
-            person_name = self.person.get_name(uname)
+            person_name = self.person.get_name(self.db.user)
             if person_name:
                 config.set_key_value('Name', person_name)
                 self.person.setup()
@@ -91,6 +93,17 @@
 
         # Personal details
         elif self.ui.page_num == 5:
+######
+            self.person = person.Person()
+            person_name = self.person.get_name(self.db.user)
+            if person_name:
+                config.set_key_value('Name', person_name)
+                self.person.setup()
+
+                self.ui.set_page(7)
+                return
+
+######
             name = self.ui.page_list[5].name_entry.get_text()
             age = self.ui.page_list[5].age_entry.get_text()
             weight_txt = self.ui.page_list[5].weight_entry.get_text()
@@ -177,6 +190,15 @@
 
     def on_back(self, w, d=None):
         # skip back over page_db_error
+        if self.ui.page_num == 5:
+            self.ui.set_page(1)
+        elif self.ui.page_num == 7:
+            self.ui.set_page(5)
+        else:
+            self.ui.set_page(self.ui.page_num - 1)
+
+    def on_back_original(self, w, d=None):
+        # skip back over page_db_error
         if self.ui.page_num == 3:
             self.ui.set_page(1)
         elif self.ui.page_num == 5:

=== modified file 'src/druid_ui.py'
--- a/src/druid_ui.py   2012-06-04 17:03:48 +0000
+++ b/src/druid_ui.py   2012-06-26 19:06:03 +0000
@@ -29,7 +29,7 @@
             label1.set_alignment(0.0, 0.5)
             self.vbox.pack_start(label1, False, False, 5)
 
-            label2 = gtk.Label('You seem to be running GNUtrition for the 
first time.\n\nThe following steps will set up GNUtrition by asking a few 
simple questions. Before you start, you will need to have the MySQL database 
system installed in your system. You will also need to know the MySQL root 
username and password.')
+            label2 = gtk.Label('You seem to be running GNUtrition for the 
first time.\n\nThe following steps will set up GNUtrition by asking a few 
simple questions.')
             label2.set_alignment(0.0, 0.5)
             label2.set_line_wrap(True)
             label2.set_justify(gtk.JUSTIFY_FILL)
@@ -46,27 +46,27 @@
             label1.set_alignment(0.0, 0.5)
             table1.attach(label1, 0, 2, 0, 1, gtk.FILL, 0, 0, 0)
 
-            label2 = gtk.Label(
-                'Enter the root username and password for MySQL.') 
-            label2.set_alignment(0.0, 0.5)
-            table1.attach(label2, 0, 2, 1, 2, gtk.FILL, 0, 0, 0)
-
-            label3 = gtk.Label('MySQL root username') 
-            label3.set_alignment(1.0, 0.5)
-            table1.attach(label3, 0, 1, 2, 3, gtk.FILL, 0, 0, 0)
-
-            self.root_user_entry = gtk.Entry()
-            table1.attach(self.root_user_entry, 1, 2, 2, 3, 
-                gtk.FILL | gtk.EXPAND, 0, 0, 0)
-
-            label4 = gtk.Label('MySQL root password') 
-            label4.set_alignment(1.0, 0.5)
-            table1.attach(label4, 0, 1, 3, 4, gtk.FILL, 0, 0, 0)
-
-            self.root_pass_entry = gtk.Entry()
-            self.root_pass_entry.set_visibility(False);
-            table1.attach(self.root_pass_entry, 1, 2, 3, 4, 
-                gtk.FILL | gtk.EXPAND, 0, 0, 0)
+#            label2 = gtk.Label(
+#                'Enter the root username and password for MySQL.') 
+#            label2.set_alignment(0.0, 0.5)
+#            table1.attach(label2, 0, 2, 1, 2, gtk.FILL, 0, 0, 0)
+
+#            label3 = gtk.Label('MySQL root username') 
+#            label3.set_alignment(1.0, 0.5)
+#            table1.attach(label3, 0, 1, 2, 3, gtk.FILL, 0, 0, 0)
+
+#            self.root_user_entry = gtk.Entry()
+#            table1.attach(self.root_user_entry, 1, 2, 2, 3, 
+#                gtk.FILL | gtk.EXPAND, 0, 0, 0)
+
+#            label4 = gtk.Label('MySQL root password') 
+#            label4.set_alignment(1.0, 0.5)
+#            table1.attach(label4, 0, 1, 3, 4, gtk.FILL, 0, 0, 0)
+
+#            self.root_pass_entry = gtk.Entry()
+#            self.root_pass_entry.set_visibility(False);
+#            table1.attach(self.root_pass_entry, 1, 2, 3, 4, 
+#                gtk.FILL | gtk.EXPAND, 0, 0, 0)
 
             label5 = gtk.Label('When you press the "Next" button GNUtrition 
will check to see if the GNUtrition Database is already installed. If not, 
GNUtrition will install it for you.') 
             label5.set_alignment(0.0, 0.5)
@@ -95,38 +95,39 @@
 
         # User Setup
         elif page_num == 3:
-            table1 = gtk.Table(2, 4, False)
-            table1.set_row_spacings(5)
-            table1.set_col_spacings(5)
-            self.vbox.pack_start(table1, True, True, 0)
-
-            label1 = gtk.Label('User Setup.')
-            label1.set_alignment(0.0, 0.5)
-            table1.attach(label1, 0, 2, 0, 1, gtk.FILL, 0, 0, 0)
-
-            label2 = gtk.Label('In order to access the database you need to 
create a normal MySQL username and password. Specify your choice below. 
GNUtrition will remember them for you.')
-            label2.set_alignment(0.0, 0.5)
-            label2.set_line_wrap(True)
-            label2.set_justify(gtk.JUSTIFY_FILL)
-            table1.attach(label2, 0, 2, 1, 2, gtk.FILL, 0, 0, 0)
-
-            label3 = gtk.Label('MySQL username') 
-            label3.set_alignment(1.0, 0.5)
-            table1.attach(label3, 0, 1, 2, 3, gtk.FILL, 0, 0, 0)
-
-            self.user_entry = gtk.Entry()
-            self.user_entry.set_text(getpass.getuser())
-            table1.attach(self.user_entry, 1, 2, 2, 3, 
-                gtk.FILL | gtk.EXPAND, 0, 0, 0)
-
-            label4 = gtk.Label('MySQL password') 
-            label4.set_alignment(1.0, 0.5)
-            table1.attach(label4, 0, 1, 3, 4, gtk.FILL, 0, 0, 0)
-
-            self.pass_entry = gtk.Entry()
-            self.pass_entry.set_visibility(False)
-            table1.attach(self.pass_entry, 1, 2, 3, 4, 
-                gtk.FILL | gtk.EXPAND, 0, 0, 0)
+            pass
+            #table1 = gtk.Table(2, 4, False)
+            #table1.set_row_spacings(5)
+            #table1.set_col_spacings(5)
+            #self.vbox.pack_start(table1, True, True, 0)
+
+            #label1 = gtk.Label('User Setup.')
+            #label1.set_alignment(0.0, 0.5)
+            #table1.attach(label1, 0, 2, 0, 1, gtk.FILL, 0, 0, 0)
+
+            #label2 = gtk.Label('In order to access the database you need to 
create a normal MySQL username and password. Specify your choice below. 
GNUtrition will remember them for you.')
+            #label2.set_alignment(0.0, 0.5)
+            #label2.set_line_wrap(True)
+            #label2.set_justify(gtk.JUSTIFY_FILL)
+            #table1.attach(label2, 0, 2, 1, 2, gtk.FILL, 0, 0, 0)
+
+            #label3 = gtk.Label('MySQL username') 
+            #label3.set_alignment(1.0, 0.5)
+            #table1.attach(label3, 0, 1, 2, 3, gtk.FILL, 0, 0, 0)
+
+            #self.user_entry = gtk.Entry()
+            #self.user_entry.set_text(getpass.getuser())
+            #table1.attach(self.user_entry, 1, 2, 2, 3, 
+            #    gtk.FILL | gtk.EXPAND, 0, 0, 0)
+
+            #label4 = gtk.Label('MySQL password') 
+            #label4.set_alignment(1.0, 0.5)
+            #table1.attach(label4, 0, 1, 3, 4, gtk.FILL, 0, 0, 0)
+
+            #self.pass_entry = gtk.Entry()
+            #self.pass_entry.set_visibility(False)
+            #table1.attach(self.pass_entry, 1, 2, 3, 4, 
+            #    gtk.FILL | gtk.EXPAND, 0, 0, 0)
 
         # Error in User Setup
         elif page_num == 4:

=== modified file 'src/gnutr_stock.py'
--- a/src/gnutr_stock.py        2012-06-04 17:03:48 +0000
+++ b/src/gnutr_stock.py        2012-06-26 19:06:03 +0000
@@ -20,7 +20,7 @@
 import install
 
 def create_stock():
-    drct = install.dir + '/pixmaps/'
+    drct = install.idir + '/pixmaps/'
     recipe_pixbuf = gtk.gdk.pixbuf_new_from_file(drct + 'cake.png')
     plan_pixbuf = gtk.gdk.pixbuf_new_from_file(drct + 'plan.png')
     food_pixbuf = gtk.gdk.pixbuf_new_from_file(drct + 'banana.png')

=== modified file 'src/help.py'
--- a/src/help.py       2012-06-04 17:03:48 +0000
+++ b/src/help.py       2012-06-26 19:06:03 +0000
@@ -31,7 +31,8 @@
     return 0
 
 def get_browser():
-    browser_list = ['galeon', 'mozilla', 'lynx']
+    browser_list = ['galeon', 'mozilla', 'lynx', 'firefox', 'seamonkey',
+                    'google-chrome']
     for b in browser_list:
         if test_access(b):
             browser = webbrowser.GenericBrowser(b + ' %s')
@@ -41,7 +42,7 @@
     return ''
 
 def open(html_page):
-    url = 'file://' + install.dir + '/doc/' + html_page
+    url = 'file://' + install.idir + '/doc/' + html_page
 
     if selected_browser:
         controller = webbrowser.get(selected_browser)

=== modified file 'src/install.py.in'
--- a/src/install.py.in 2012-05-19 23:15:46 +0000
+++ b/src/install.py.in 2012-06-26 19:06:03 +0000
@@ -1,5 +1,4 @@
-# GNUtrition - a nutrition and diet analysis program.
-# Copyright (C) 2001-2002 Edgar Denny (address@hidden)
+# GNUtrition - a nutrition and diet analysis program.  # Copyright (C) 
2001-2002 Edgar Denny (address@hidden)
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -17,4 +16,4 @@
 MAJOR_VERSION = @MAJOR_VERSION@
 MINOR_VERSION = @MINOR_VERSION@
 VERSION = "@address@hidden@MINOR_VERSION@"
-dir = "@prefix@/share/gnutrition"
+idir = "@prefix@/share/gnutrition"

=== modified file 'src/person.py'
--- a/src/person.py     2012-05-28 03:12:23 +0000
+++ b/src/person.py     2012-06-26 19:06:03 +0000
@@ -59,21 +59,21 @@
 
         # create a series of temporary tables
         self.db.query("CREATE TEMPORARY TABLE food_plan_temp " + 
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL, " + 
-            "date DATE NOT NULL, " +
-            "time TIME NOT NULL, " + 
-            "amount FLOAT(7,2), " +
-            "Msre_No MEDIUMINT(6) UNSIGNED NOT NULL, " +
-            "NDB_No SMALLINT(5) UNSIGNED NOT NULL, " +
-            "INDEX (date, time, NDB_No) )")
+            "(person_no INTEGER NOT NULL, " + 
+            "date TEXT NOT NULL, " +
+            "time TEXT NOT NULL, " + 
+            "amount REAL NOT NULL, " +
+            "Msre_No INTEGER NOT NULL, " +
+            "NDB_No INTEGER NOT NULL, " +
+            "PRIMARY KEY (date, time, NDB_No))")
 
         self.db.query("CREATE TEMPORARY TABLE recipe_plan_temp " +
-            "(person_no SMALLINT(6) UNSIGNED NOT NULL, " +
-            "date DATE NOT NULL, " +
-            "time TIME NOT NULL, " +
-            "no_portions FLOAT(7,2) NOT NULL, " +
-            "recipe_no MEDIUMINT(6) UNSIGNED NOT NULL, " +
-            "INDEX (date, recipe_no, time) )")
+            "(person_no INTEGER NOT NULL, " +
+            "date TEXT NOT NULL, " +
+            "time TEXT NOT NULL, " +
+            "no_portions REAL NOT NULL, " +
+            "recipe_no INTEGER NOT NULL, " +
+            "PRIMARY KEY (date, recipe_no, time) )")
 
         # copy any data from stored tables to temporary ones
         self.db.query("SELECT * FROM food_plan WHERE person_no = '%d'" 

=== modified file 'src/plan_win.py'
--- a/src/plan_win.py   2012-05-28 03:12:23 +0000
+++ b/src/plan_win.py   2012-06-26 19:06:03 +0000
@@ -41,8 +41,7 @@
         self.update()
 
     def get_current_date(self):
-        self.db.query("SELECT CURDATE()")
-        return self.db.get_single_result()
+        return self.db.curdate()
 
     def connect_signals(self):
         self.ui.save_button.connect('clicked', self.on_save_released)

=== modified file 'src/recipe_win.py'
--- a/src/recipe_win.py 2012-06-04 17:03:48 +0000
+++ b/src/recipe_win.py 2012-06-26 19:06:03 +0000
@@ -183,29 +183,27 @@
 
     def save_recipe(self, recipe):
         print 'Saving recipe:', recipe.desc
+        recipe_no = self.db.next_row('recipe_no', 'recipe')
         self.db.query("""INSERT INTO recipe VALUES
-            (NULL, '%s', '%s', '%s', '%s')""" % (recipe.desc,
+            ('%d', '%s', '%s', '%s', '%s')""" % (recipe_no, recipe.desc,
             recipe.num_serv, str(self.num_ingr), str(recipe.cat_num)))
 
         for ingr in recipe.ingr_list:
             self.db.query("""INSERT INTO ingredient VALUES
-                (LAST_INSERT_ID(), '%s', '%s', '%s' )""" % (
+                ('%d', '%s', '%s', '%s' )""" % (recipe_no,
                 str(ingr.amount), str(ingr.msre_num), str(ingr.food_num)))
 
         self.db.query("""INSERT INTO preparation VALUES
-            (LAST_INSERT_ID(), '0.0', "%s")"""  %(recipe.prep_desc))
+            ('%d', '0.0', "%s")"""  % (recipe_no, recipe.prep_desc))
 
     def delete_recipe(self, recipe_name):
         self.db.query("""SELECT recipe_no FROM recipe
             WHERE recipe_name = '%s'""" %(recipe_name))
         recipe_num = str(self.db.get_single_result())
-
         self.db.query("DELETE FROM recipe WHERE recipe_no = '%s'" 
             % (recipe_num))
-
         self.db.query("DELETE FROM ingredient WHERE recipe_no = '%s'" 
             % (recipe_num))
-
         self.db.query("DELETE FROM recipe_plan WHERE recipe_no = '%s'" 
             % (recipe_num))
         self.db.query("DELETE FROM preparation WHERE recipe_no = '%s'"

=== modified file 'src/run_app.py'
--- a/src/run_app.py    2012-05-28 03:12:23 +0000
+++ b/src/run_app.py    2012-06-26 19:06:03 +0000
@@ -41,18 +41,18 @@
         db_pword = config.get_value('Password')
 
         import database 
-        self.db = database.Database(db_uname, db_pword)
-        success = self.db.change_user(db_uname, db_pword, 'gnutr_db')
+        self.db = database.Database()
+        #success = self.db.change_user(db_uname, db_pword, 'gnutr_db')
 
-        if success == 0:
-            import gnutr
-            import sys
-            gnutr.Dialog('error', 
-                'Failed to connect to the database.\n\n' +
-                'I suggest that you delete the file\n ' +
-                '"~/.gnutrition/config" and run "gnutrition" again.')
-            gtk.main_quit()
-            sys.exit()
+        #if success == 0:
+        #    import gnutr
+        #    import sys
+        #    gnutr.Dialog('error', 
+        #        'Failed to connect to the database.\n\n' +
+        #        'I suggest that you delete the file\n ' +
+        #        '"~/.gnutrition/config" and run "gnutrition" again.')
+        #    gtk.main_quit()
+        #    sys.exit()
 
         import store
         self.store = store.Store()

=== modified file 'src/version.py'
--- a/src/version.py    2012-05-19 23:13:10 +0000
+++ b/src/version.py    2012-06-26 19:06:03 +0000
@@ -71,15 +71,15 @@
 
 def check_version():
     import gnutr_consts
+    import install
+    this_ver = install.VERSION
     if config.get_value('check_disabled') or not 
config.get_value('check_version'):
-        return
+        return 0
     import time
     interval = config.get_value('check_interval')
     last_check = config.get_value('last_check')
     time_now = time.time()
     if (time_now - last_check > interval):
-        import install
-        this_ver = install.VERSION
         (curr_ver, mesg) = get_latest_version(gnutr_consts.LATEST_VERSION) 
         update = False
         if this_ver == curr_ver:
@@ -87,8 +87,10 @@
         else:
             update = cmp_version_strings(this_ver,curr_ver)
         if update:
+#HERE: Check for existing MySQL db and ask about migration/deletion
             update_version(curr_ver, mesg)
     last_check = config.set_key_value('last_check',time_now)
+    return 1
 
 if __name__ == '__main__':
     def str_cmp_test():


reply via email to

[Prev in Thread] Current Thread [Next in Thread]