[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUtrition-commits] /srv/bzr/gnutrition/trunk r11: Miscellaneous bug fi
From: |
Thomas Sinclair |
Subject: |
[GNUtrition-commits] /srv/bzr/gnutrition/trunk r11: Miscellaneous bug fixes |
Date: |
Sun, 16 Sep 2012 19:02:12 -0400 |
User-agent: |
Bazaar (2.5.0) |
------------------------------------------------------------
revno: 11
committer: Thomas Sinclair <address@hidden>
branch nick: trunk
timestamp: Sun 2012-09-16 19:02:12 -0400
message:
Miscellaneous bug fixes
modified:
src/database.py
src/food_srch_res_dlg.py
src/person.py
src/plan_compute_dlg.py
src/plan_win.py
src/run_app.py
=== modified file 'src/database.py'
--- a/src/database.py 2012-07-27 01:47:47 +0000
+++ b/src/database.py 2012-09-16 23:02:12 +0000
@@ -29,6 +29,14 @@
"""Return todays date as yyyy-mm-dd"""
return str(dbms.DateFromTicks(ticks()))
+def to_days(datestr):
+ # J F M A M J J A S O N D
+ months = [31,28,31,20,31,30,31,31,30,31,30,31]
+ ymd = datestr.split('-')
+ days = (int(ymd[0]) - 1900) * 365;
+ days = days + months[int(ymd[1])] + int(ymd[2])
+ return days
+
dbms.register_adapter(datetime.datetime, curtime)
dbms.register_adapter(datetime.datetime, curdate)
@@ -46,24 +54,29 @@
from os import path
import config
self.user = config.user
- dbfile = path.join(config.udir, 'gnutr_db')
+ dbfile = path.join(config.udir, 'gnutr_db.lt3')
try:
con = dbms.connect(dbfile)
+ # text_factory must be set to 'str' due to current limitations
+ # in csv.reader()
+ con.text_factory = str
+ con.create_function('REGEXP', 2, regexp)
+ con.create_function('TO_DAYS', 1, to_days)
+ cur = con.cursor()
except self.Error, e:
"Error {0:s}:".format(e.args[0])
raise self.Error
- # text_factory must be set to 'str' due to current limitations
- # in csv.reader()
- con.text_factory = str
- con.create_function('REGEXP', 2, regexp)
self.con = con
- self.cursor = con.cursor()
- self.rows = 0
- self.result = None
+ self.cur = cur
+
+ def close(self):
+ if self.con:
+ self.con.close()
def initialize(self):
# Create Food Description (food_des) table.
# Data file FOOD_DES.
+ self.query("DROP TABLE IF EXISTS food_des")
self.create_load_table("CREATE TABLE food_des" +
"(NDB_No INTEGER NOT NULL, " +
"FdGrp_Cd INTEGER NOT NULL, " +
@@ -89,6 +102,7 @@
# Create Food Group Description (fd_group) table.
# Data file FD_GROUP.
+ self.query("DROP TABLE IF EXISTS fd_group")
self.create_load_table("CREATE TABLE fd_group " +
"(FdGrp_Cd INTEGER PRIMARY KEY NOT NULL, " +
"FdGrp_Desc TEXT NOT NULL)",
@@ -98,6 +112,7 @@
# Create Nutrient Data (nut_data) table.
# Data file NUT_DATA
+ self.query("DROP TABLE IF EXISTS nut_data")
self.create_load_table("CREATE TABLE nut_data " +
"(NDB_No INTEGER NOT NULL, " +
"Nutr_No INTEGER NOT NULL, " +
@@ -126,6 +141,7 @@
# Create Nutrient Definition (nutr_def table.
# Data file NUTR_DEF
+ self.query("DROP TABLE IF EXISTS nutr_def")
self.create_load_table("CREATE TABLE nutr_def " +
"(Nutr_No INTEGER PRIMARY KEY NOT NULL, " +
"Units TEXT NOT NULL, " +
@@ -141,6 +157,7 @@
# Create temporary weight table.
# Data file WEIGHT.
+ self.query("DROP TABLE IF EXISTS weight")
self.create_load_table("CREATE TABLE weight" +
"(NDB_No INTEGER NOT NULL, " +
# Seq == Sequence number for measure description (Msre_Desc)
@@ -164,24 +181,42 @@
"(?, ?, ?, ?, ?, ?, ?)",
'weight')
+ # May have user data from previous install that we don't want to lose
+ try:
+ self.query("SELECT name FROM sqlite_master WHERE type='table'")
+ except self.Error, sqlerr:
+ self.con.rollback()
+ import sys
+ print 'Error :', sqlerr, '\nquery:', sql
+ if caller: print 'Caller ', caller
+ sys.exit()
+ search = ['recipe', 'ingredient', 'preparation', 'person',
+ 'food_plan', 'recipe_plan', 'nutr_goal']
+ tables = []
+ for t in self.get_result():
+ if t[0] in search:
+ tables.append(t[0])
+
# create recipe table
- # HERE: recipe_no had AUTOINCREMENT in MySQL version
- self.create_table("CREATE TABLE recipe " +
- "(recipe_no INTEGER NOT NULL, " +
+ if not 'recipe' in tables:
+ self.create_table("CREATE TABLE recipe " +
+ "(recipe_no INTEGER PRIMARY KEY AUTOINCREMENT, " +
"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')
+ "category_no INTEGER NOT NULL)", 'recipe')
+ # Want index on recipe_name, category_no
# create ingredient table
- self.create_table("CREATE TABLE ingredient " +
- "(recipe_no INTEGER PRIMARY KEY NOT NULL, " +
+ if not 'ingredient' in tables:
+ self.create_table("CREATE TABLE ingredient " +
+ "(recipe_no NOT NULL, " +
"amount REAL NOT NULL, " +
"Msre_Desc TEXT NOT NULL, " +
"NDB_No INTEGER NOT NULL)", 'ingredient')
# create recipe category table
+ self.query("DROP TABLE IF EXISTS category")
self.create_load_table("CREATE TABLE category " +
"(category_no INTEGER PRIMARY KEY NOT NULL, " +
"category_desc TEXT NOT NULL)",
@@ -190,19 +225,22 @@
'category')
# create recipe preparation table
- self.create_table("CREATE TABLE preparation " +
+ if not 'preparation' in tables:
+ self.create_table("CREATE TABLE preparation " +
"(recipe_no INTEGER PRIMARY KEY NOT NULL, " +
"prep_time TEXT, " +
"prep_desc TEXT)", 'preparation')
# create person table
- self.create_table("CREATE TABLE person " +
+ if not 'person' in tables:
+ self.create_table("CREATE TABLE 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 " +
+ if not 'food_plan' in tables:
+ self.create_table("CREATE TABLE food_plan " +
"(person_no INTEGER NOT NULL, " +
"date TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
@@ -211,7 +249,8 @@
"Ndb_No INTEGER NOT NULL)", 'food_plan')
# create recipe_plan table
- self.create_table("CREATE TABLE recipe_plan " +
+ if not 'recipe_plan' in tables:
+ self.create_table("CREATE TABLE recipe_plan " +
"(person_no INTEGER NOT NULL, " +
"date TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
@@ -219,13 +258,12 @@
"recipe_no INTEGER NOT NULL)", 'recipe_plan')
# create nutr_goal table
- self.create_table("CREATE TABLE nutr_goal " +
+ if not 'nutr_goal' in tables:
+ self.create_table("CREATE TABLE 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.con.cursor()
return 1
def curtime(self):
@@ -239,25 +277,25 @@
try:
if sql_params:
if many:
- self.cursor.executemany(sql, sql_params)
+ self.cur.executemany(sql, sql_params)
else:
- self.cursor.execute(sql, sql_params)
+ self.cur.execute(sql, sql_params)
elif many:
- self.cursor.executemany(sql)
+ self.cur.executemany(sql)
else:
- self.cursor.execute(sql)
+ self.cur.execute(sql)
+ self.con.commit()
+ result = self.cur.fetchall()
except self.Error, sqlerr:
self.con.rollback()
import sys
print 'Error :', sqlerr, '\nquery:', sql
if caller: print 'Caller ', caller
sys.exit()
- 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.con.commit()
def get_result(self):
result = self.result
@@ -307,7 +345,7 @@
# "INTO TABLE " + table + " FIELDS TERMINATED BY '^'")
import csv
try:
- data = csv.reader(open(data_fn, 'r'), delimiter='^', quotechar="'")
+ 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
=== modified file 'src/food_srch_res_dlg.py'
--- a/src/food_srch_res_dlg.py 2012-07-27 01:59:48 +0000
+++ b/src/food_srch_res_dlg.py 2012-09-16 23:02:12 +0000
@@ -38,24 +38,25 @@
self.ui.treeview.connect('key-press-event',
self.on_treeview_key_press_event)
self.ui.treeview.connect('button-press-event',
self.on_treeview_button_press_event)
+ def clear_results(self):
+ self.ui.combo.clear_rows()
+ self.ui.amount_entry.set_text('')
+ self.ui.food_entry.set_text('')
+
def on_selection_changed(self, selection, d=None):
(model, iter) = selection.get_selected()
+ self.clear_results()
if iter:
food_num = model.get_value(iter, 1)
if food_num:
- food_desc = self.store.fd_num2desc[ food_num]
+ food_desc = self.store.fd_num2desc[food_num]
self.ui.food_entry.set_text(food_desc)
else:
- self.ui.combo.clear_rows()
- self.ui.amount_entry.set_text('')
- self.ui.food_entry.set_text('')
+ self.clear_results()
def show(self, food_num_list, view):
self.view_type = view
- self.ui.combo.clear_rows()
- self.ui.amount_entry.set_text('')
- self.ui.food_entry.set_text('')
-
+ self.clear_results()
self.ui.treeview.freeze_child_notify()
self.ui.treeview.set_model(None)
self.create_tree(self.ui.tree, food_num_list)
=== modified file 'src/person.py'
--- a/src/person.py 2012-07-27 01:47:47 +0000
+++ b/src/person.py 2012-09-16 23:02:12 +0000
@@ -91,7 +91,7 @@
result = self.db.get_result()
if result and len(result) != 0:
- for person_num, date, time, wum_portions, recipe_num in result:
+ for person_num, date, time, num_portions, recipe_num in result:
self.db.query("INSERT INTO recipe_plan_temp VALUES" +
" ('%d', '%s', '%s', '%f', '%d' )"
% (person_num, str(date), str(time), num_portions,
=== modified file 'src/plan_compute_dlg.py'
--- a/src/plan_compute_dlg.py 2012-07-27 01:47:47 +0000
+++ b/src/plan_compute_dlg.py 2012-09-16 23:02:12 +0000
@@ -122,7 +122,7 @@
self.db.query("SELECT Nutr_No, Nutr_Val FROM nut_data " +
"WHERE NDB_No = '%d'" %(food_num))
return self.db.get_result()
-#HERE: take into account Amount unit modifier
+#HERE: take into account Amount unit modifier?
def get_gm_per_measure(self, food_num, msre_desc):
self.db.query("SELECT Gm_wgt FROM weight WHERE " +
"NDB_No = '%d' AND Msre_Desc = '%s'" %(food_num, msre_desc))
=== modified file 'src/plan_win.py'
--- a/src/plan_win.py 2012-07-27 01:47:47 +0000
+++ b/src/plan_win.py 2012-09-16 23:02:12 +0000
@@ -212,19 +212,15 @@
text_list = string.split(time_string_next, ':')
return str(int(text_list[0]) - 1) + ':00'
- def iter_for_time(self, time_string1):
+ def iter_for_time(self, time_string):
iter = self.ui.treemodel.get_iter_root()
pos = 0
+ lastv = ''
while iter:
value = self.ui.treemodel.get_value(iter, 0)
if value:
- value_list = string.split(value, ':')
- if int(value_list[0]) < 10:
- #time_string2 = '0' + value + ':00'
- time_string2 = value + ':00'
- else:
- time_string2 = value + ':00'
- if time_string1 == time_string2:
+ lastv = value
+ if time_string == value:
return iter, pos
pos = pos + 1
iter = self.ui.treemodel.iter_next(iter)
@@ -232,16 +228,13 @@
def update(self):
date = self.ui.date.entry.get_text()
-
self.ui.treemodel.clear()
self.set_times()
-
food_list = self.get_foods_for_date(date)
recipe_list = self.get_recipes_for_date(date)
for recipe in recipe_list:
iter, pos = self.iter_for_time(recipe.time)
-
# is there already a recipe or food in either of the two
# rows available for the time? If both occupied create a
# new one.
@@ -268,7 +261,6 @@
for food in food_list:
iter, pos = self.iter_for_time(food.time)
-
# is there already a recipe or food in either of the two
# rows available for the time? If both occupied create a
# new one.
@@ -290,7 +282,7 @@
self.ui.treemodel.set_value(iter, 4, food)
continue
- iter = self.ui.treemodel.insert(pos + 2)
+ iter = self.ui.treemodel.insert(pos + 1)
self.ui.treemodel.set_value(iter, 1, food.amount)
self.ui.treemodel.set_value(iter, 2, food.msre_desc)
self.ui.treemodel.set_value(iter, 3, food.food_desc)
=== modified file 'src/run_app.py'
--- a/src/run_app.py 2012-06-26 19:06:03 +0000
+++ b/src/run_app.py 2012-09-16 23:02:12 +0000
@@ -65,8 +65,14 @@
self.base_win = base_win.BaseWin(self)
self.base_win.show()
+ def shutdown(self):
+ import database
+ db = database.Database()
+ db.close()
+
def run_app():
app = RunApp()
gtk.main()
+ app.shutdown()
run_app()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUtrition-commits] /srv/bzr/gnutrition/trunk r11: Miscellaneous bug fixes,
Thomas Sinclair <=