-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPGuiDBModel.py
117 lines (99 loc) · 4.55 KB
/
SPGuiDBModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
# Copyright (c) 2006-2010 Stas Zykiewicz <[email protected]>
#
# SPGuiDBModel.py
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 3 of the GNU General Public License
# as published by the Free Software Foundation. A copy of this license should
# be included in the file GPL-3.
#
# This program 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 Library 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.
""" This module provides an communication layer between a GUI and the SP dbase.
GUI's should use this an not access the SP dbase stuff directly.
This module assumes the GUI is used independant of SP.
Use the 'get_datamanager' function to get a mapper object.
"""
import atexit,sys
sys.path.insert(0, 'gui')
#create logger, logger was configured in SPLogging
import logging
module_logger = logging.getLogger("schoolsplay.SPGuiDBModel")
# first parse commandline options, it's fake ofcourse as we are run from inside
# a python module but we need the options object
from SPOptionParser import OParser
op = OParser()
# this will return a class object with the options as attributes
CMD_Options = op.get_options()
class SPGoodiesWrapper:
"""Wrapper class, needed to run the datamanager"""
_cmd_options = CMD_Options
try:
import sqlalchemy as sqla
except ImportError:
module_logger.exception("No sqlalchemy package found")
raise Exception
else:
if sqla.__version__ < '0.4':
module_logger.error("Found sqlalchemy version %s" % sqla.__version__ )
module_logger.error("Your version of sqlalchemy is to old, please upgrade to version >= 0.4")
module_logger.debug("using sqlalchemy %s" % sqla.__version__)
import SPDataManager
from SPDataManagerCreateDbase import DbaseMaker
DEBUG = False
import SQLTables
from SPConstants import DBASEPATH
import utils
import sqlalchemy.exc as sqlae
import sqlalchemy.orm as sqlorm
# get a datamanager
def get_datamanager():
"""use this to get a datamanager instance which could be used by a GUI.
Typical usage would be:
import SPGuiDBModel
dm = SPGuiDBModel.get_datamanager()
tablemapper = dm.get_mapper('<table name>')
tabledata = tablemapper.get_table_data()
# inserting is related to the table design
newrow = [{id:<int>,'logon':<string>,'name':<string>}]
tablemapper.update_row(data)
"""
return DataManagerWrapper(SPGoodiesWrapper())
class DataManagerWrapper(SPDataManager.DataManager):
"""Wrapper class to set up a DataManager which can be used as a standalone.
This is meant for GUI's that want to interface with the SP dbase.
Don't use this class directly, use the 'get_datamanager' function."""
def __init__(self,spgoodies):
self.logger = logging.getLogger("schoolsplay.SPGuiDBModel.DataManagerWrapper")
self.logger.debug("Starting")
self.current_user_id = None # needed for the testsuite
self.spg = spgoodies
self.cmd_options = self.spg._cmd_options
atexit.register(self._cleanup)
try:
dbm = DbaseMaker(self.cmd_options.theme, debug_sql=DEBUG)
except (AttributeError, sqlae.SQLAlchemyError, utils.MyError) as info:
self.logger.exception("Failed to start the DBase, %s" % info)
raise MyError(info)
self.content_engine, self.user_engine = dbm.get_engines()
self.metadata_contentdb, self.metadata_usersdb = dbm.get_metadatas()
self.all_orms = dbm.get_all_orms()
self.orms_content_db, self.orms_userdb = dbm.get_orms()
self.UserSession = sqlorm.sessionmaker(bind=self.user_engine)
self.ContentSession = sqlorm.sessionmaker(bind=self.content_engine)
def get_mapper(self,activity):
"""Overrides the get_mapper from SPDataManager.
We don't have or use user id's"""
try:
mclass = SPDataManager.RowMapper(self.tables[activity])
except (sqla.exceptions.SQLError,KeyError):
self.logger.exception("Failed to get mapper: %s" % activity)
raise utils.NoSuchTable("Failed to get mapper: %s" % activity)
else:
return mclass