Skip to content

Commit

Permalink
- fix
Browse files Browse the repository at this point in the history
  • Loading branch information
brettatoms committed Mar 7, 2009
1 parent 5c7d664 commit 0c11b15
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 51 deletions.
5 changes: 3 additions & 2 deletions .emacs-prj
@@ -1,4 +1,4 @@
; -*- lisp -*-
r; -*- lisp -*-
;
; project settings for Bauble
;
Expand All @@ -8,4 +8,5 @@
;(setq org-agenda-files (list (expand-file-name (concat bauble-root "/org/bauble.org"))))
(add-to-list org-agenda-files (expand-file-name (concat bauble-root "/org/bauble.org")))
(setq tags-file-name (concat bauble-root "/TAGS"))
(setenv "PYLINTRC" (concat bauble-root ".pylintrc"))
(setenv "PYLINTRC" (concat bauble-root ".pylintrc"))
(define-key run-project ("\C-pr")
112 changes: 88 additions & 24 deletions bauble/db.py
@@ -1,9 +1,13 @@
# Copyright (c) 2005,2006,2007,2008 Brett Adams <brett@belizebotanic.org>
# This is free software, see GNU General Public License v2 for details.

import traceback

from bauble.i18n import _
import bauble.error as error

SQLALCHEMY_DEBUG = False

try:
import sqlalchemy as sa
parts = sa.__version__.split('.')
Expand All @@ -27,6 +31,14 @@
from bauble.utils.log import debug


if SQLALCHEMY_DEBUG:
import logging
global engine
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.DEBUG)


class MapperBase(DeclarativeMeta):
"""
MapperBase adds the id, _created and _last_updated columns to all tables.
Expand All @@ -35,7 +47,7 @@ class MapperBase(DeclarativeMeta):
def __init__(cls, classname, bases, dict_):
#print >>sys.stderr, dict_
if '__tablename__' in dict_:
seqname = '%s_seq_id' % dict_['__tablename__']
seqname = '%s_id_seq' % dict_['__tablename__']
dict_['id'] = sa.Column('id', sa.Integer, sa.Sequence(seqname),
primary_key=True)
dict_['_created'] = sa.Column('_created', DateTime(True),
Expand Down Expand Up @@ -79,8 +91,9 @@ def open(uri, verify=True, show_error_dialogs=False):
import bauble
global engine
new_engine = None
new_engine = sa.create_engine(uri)
new_engine.contextual_connect()
new_engine = sa.create_engine(uri, echo=SQLALCHEMY_DEBUG)
#new_engine.contextual_connect()
new_engine.connect()#.close()
def _bind():
"""bind metadata to engine and create sessionmaker """
global Session, engine
Expand All @@ -95,7 +108,7 @@ def _bind():
elif new_engine is None:
return None

_verify_connection(new_engine, show_error_dialogs)
#_verify_connection(new_engine, show_error_dialogs)
_bind()
return engine

Expand Down Expand Up @@ -128,15 +141,43 @@ def create(import_defaults=True):
import bauble.pluginmgr as pluginmgr
from bauble.task import TaskQuitting
import datetime
#transaction = engine.contextual_connect().begin()
connection = engine.contextual_connect()
transaction = connection.begin()
session = Session()
try:
# TODO: here we are creating all the tables in the metadata whether
# they are in the registry or not, we should really only be creating
# those tables in the registry
metadata.drop_all(checkfirst=True)
metadata.create_all()
## debug('dropped and created')
# create fresh plugin registry seperate since
# pluginmgr.install() will be changing it
#debug('dropping and creating plugin registry')
from bauble.pluginmgr import PluginRegistry
#connection = None
#PluginRegistry.__table__.drop(bind=connection, checkfirst=True)
PluginRegistry.__table__.drop(bind=connection, checkfirst=True)
#debug(' - d')
PluginRegistry.__table__.create(bind=connection)
#PluginRegistry.__table__.create()
#debug(' - c')
except Exception, e:
debug(e)
transaction.rollback()
#connection.close()
raise
else:
transaction.commit()

connection = engine.contextual_connect()
transaction = connection.begin()
try:
# TODO: here we are dropping/creating all the tables in the
# metadata whether they are in the registry or not, we should
# really only be creating those tables from registered
# plugins, maybe with an uninstall() method on Plugin
#debug('drop all')
most_tables = metadata.sorted_tables
most_tables.remove(pluginmgr.PluginRegistry.__table__)
metadata.drop_all(bind=connection, tables=most_tables, checkfirst=True)
#debug(' -- dropped')
metadata.create_all(bind=connection, tables=most_tables)
#debug('dropped and created')

# TODO: clearing the insert menu probably shouldn't be here and should
# probably be pushed into db.create, the problem is at the
Expand All @@ -153,33 +194,53 @@ def create(import_defaults=True):

# fill in the bauble meta table and install all the plugins
meta_table = meta.BaubleMeta.__table__
meta_table.insert().execute(name=meta.VERSION_KEY,
value=unicode(bauble.version))
meta_table.insert().execute(name=meta.CREATED_KEY,
meta_table.insert(bind=connection).execute(name=meta.VERSION_KEY,
value=unicode(bauble.version))
meta_table.insert(bind=connection).execute(name=meta.CREATED_KEY,
value=unicode(datetime.datetime.now()))

except (GeneratorExit, TaskQuitting), e:
# this is here in case the main windows is closed in the middle
# of a task
debug(e)
# debug('db.create(): rollback')
transaction.rollback()
#session.rollback()
raise
except Exception, e:
debug(e)
#debug('db.create(): rollback')
transaction.rollback()
raise
else:
transaction.commit()

connection = engine.contextual_connect()
transaction = connection.begin()
try:
pluginmgr.install('all', import_defaults, force=True)
except (GeneratorExit, TaskQuitting), e:
# this is here in case the main windows is closed in the middle
# of a task
debug(e)
# debug('db.create(): rollback')
#transaction.rollback()
session.rollback()
transaction.rollback()
#session.rollback()
raise
except Exception, e:
debug(e)
#debug('db.create(): rollback')
#transaction.rollback()
session.rollback()
msg = _('Error creating tables.\n\n%s') % utils.xml_safe_utf8(e)
debug(traceback.format_exc())
utils.message_details_dialog(msg, traceback.format_exc(),
gtk.MESSAGE_ERROR)
transaction.rollback()
#session.rollback()
# msg = _('Error creating the database.\n\n%s') % utils.xml_safe_utf8(e)
# debug(traceback.format_exc())
# utils.message_details_dialog(msg, traceback.format_exc(),
# gtk.MESSAGE_ERROR)
raise
else:
## debug('db.create(): committing')
session.commit()
#transaction.commit()
#session.commit()
transaction.commit()


def _verify_connection(engine, show_error_dialogs=False):
Expand Down Expand Up @@ -260,9 +321,12 @@ def _verify_connection(engine, show_error_dialogs=False):
try:
major, minor, revision = result.value.split('.')
except:
session.close()
raise error.VersionError(result.value)

if major != bauble.version_tuple[0] or minor != bauble.version_tuple[1]:
session.close()
raise error.VersionError(result.value)

session.close()
return True
13 changes: 6 additions & 7 deletions bauble/plugins/plants/__init__.py
Expand Up @@ -100,17 +100,16 @@ def install(cls, import_defaults=True):
if not import_defaults:
return
path = os.path.join(paths.lib_dir(), "plugins", "plants", "default")
filenames = [os.path.join(path, f) for f in 'family.txt',
'genus.txt', 'genus_synonym.txt', 'geography.txt']
# filenames = [os.path.join(path, f) for f in 'family.txt',
# 'genus.txt',
# 'genus_synonym.txt',
# 'geography.txt']
filenames = [os.path.join(path, 'genus_synonym.txt')]
from bauble.plugins.imex.csv_ import CSVImporter
csv = CSVImporter()
import_error = False
import_exc = None
try:
csv.start(filenames, metadata=db.metadata, force=True)
except Exception, e:
error(e)
raise
csv.start(filenames, metadata=db.metadata, force=True)



Expand Down
2 changes: 1 addition & 1 deletion bauble/plugins/report/template/__init__.py
Expand Up @@ -82,7 +82,7 @@ def format(objs, **kwargs):
utils.message_dialog(_('Could not open the report with the '\
'default program. You can open the '\
'file manually at %s') % filename)
return True
return report


formatter_plugin = TemplateFormatterPlugin
Expand Down
15 changes: 12 additions & 3 deletions bauble/plugins/report/template/test.py
@@ -1,4 +1,5 @@
import os
import sys

from sqlalchemy import *
from sqlalchemy.exc import *
Expand All @@ -14,7 +15,8 @@
#import bauble.plugins.report as report_plugin
from bauble.plugins.report import get_all_species, get_all_accessions, \
get_all_plants
from bauble.plugins.plants import Family, Genus, Species, VernacularName
from bauble.plugins.plants import Family, Genus, Species, \
SpeciesDistribution, VernacularName, Geography
from bauble.plugins.garden import Accession, Plant, Location
from bauble.plugins.tag import tag_objects, Tag
from bauble.plugins.report.template import TemplateFormatterPlugin
Expand All @@ -39,9 +41,14 @@ def setUp(self, *args):
for s in range(2):
sctr+=1
sp = Species(id=sctr, genus=genus, sp=u'sp%s' % sctr)
# TODO: why doesn't this geography, species
# distribution stuff seem to work
geo = Geography(id=sctr, name=u'Mexico%s' % sctr)
dist = SpeciesDistribution(geography_id=sctr)
sp.distribution.append(dist)
vn = VernacularName(id=sctr, species=sp,
name=u'name%s' % sctr)
self.session.add_all([sp, vn])
self.session.add_all([sp, geo, dist, vn])
for a in range(2):
actr+=1
acc = Accession(id=actr, species=sp, code=u'%s' % actr)
Expand All @@ -61,8 +68,10 @@ def tearDown(self, *args):

def test_format(self):
plants = self.session.query(Plant).all()
filename = os.path.join(os.path.dirname(__file__), 'test.html')
filename = os.path.join(os.path.dirname(__file__), 'labels.html')
report = TemplateFormatterPlugin.format(plants, template=filename)
open('/tmp/testlabels.html', 'w').write(report)
#print >>sys.stderr, report
# TODO: need to make some sort of assertion here


4 changes: 2 additions & 2 deletions bauble/plugins/tag/__init__.py
Expand Up @@ -447,8 +447,8 @@ def _reset_tags_menu():
except:
debug(traceback.format_exc())
msg = _('Could not create the tags menus')
tb = utils.xml_safe_utf8(traceback.format_exc())
utils.message_details_dialog(msg, tb, gtk.MESSAGE_ERROR)
utils.message_details_dialog(msg, traceback.format_exc(),
gtk.MESSAGE_ERROR)
# raise
#debug('** maybe the tags table hasn\'t been created yet')

Expand Down
6 changes: 4 additions & 2 deletions bauble/task.py
Expand Up @@ -122,7 +122,7 @@ def flush():
# pending tasks would be able to complete...on_error also
# shouldn't raise an exception
#gobject.idle_add(on_error, e)
error(e)
#error(e)
if on_error:
on_error(e)

Expand All @@ -149,7 +149,9 @@ def queue(task, on_quit, on_error, *args):
Queue a new task
:param task: the task to queue
:param callback: the function to call when the task is finished
:param on_quit: the function to call when the task is finished
:param on_error: the function to call when there is an error, this
method should not raise an exception or the other tasks will not complete
:param args: the arguments to pass to the task
"""
global _task_queue
Expand Down
31 changes: 21 additions & 10 deletions test/itest.py
Expand Up @@ -15,22 +15,33 @@
import logging
logging.basicConfig()

uri = 'sqlite:///:memory:'
#uri = 'sqlite:///:memory:'
uri = 'postgres://admin:3riven&@ceiba/bbg'
db.open(uri, False)
pluginmgr.load()
db.create(import_defaults=False)
# the one thing this script doesn't do that bauble does is called
# pluginmgr.init()
#pluginmgr.init(force=True)
#db.create(import_defaults=True)

from bauble.plugins.plants import Family, Genus, Species
from bauble.plugins.garden import Accession, Plant, Location

session = bauble.Session()

f = Family(family=u'family')
g = Genus(family=f, genus=u'genus')
s = Species(genus=g, sp=u's')
a = Accession(species=s, code=u'1')
l = Location(site=u'site')
p = Plant(accession=a, location=l, code=u'1')
# f = Family(family=u'family')
# g = Genus(family=f, genus=u'genus')
# s = Species(genus=g, sp=u's')
# a = Accession(species=s, code=u'1')
# l = Location(site=u'site')
# p = Plant(accession=a, location=l, code=u'1')

# session.add_all([f, g, s, a, p])
# session.commit()

# print 'drop'
# pluginmgr.PluginRegistry.__table__.drop()
# print 'create'
# pluginmgr.PluginRegistry.__table__.create()
# print 'done.'

session.add_all([f, g, s, a, p])
session.commit()

0 comments on commit 0c11b15

Please sign in to comment.