Skip to content

Commit 1707408

Browse files
Stephanie ReederStephanie Reeder
authored andcommitted
update code to use dbconnection from the api
1 parent c71cb39 commit 1707408

5 files changed

Lines changed: 35 additions & 120 deletions

File tree

odmtools/gui/frmODMTools.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,11 @@ def _init_database(self, quit_if_cancel=True):
126126

127127
while True:
128128
## Database connection is valid, therefore proceed through the rest of the program
129-
if newConnection:
130-
conn_dict = newConnection
131-
else:
132-
conn_dict = self.service_manager.is_valid_connection()
133-
if conn_dict:
134-
# conn_dict = None
129+
if self.service_manager.is_valid_connection():
130+
conn_dict = None
135131

136-
series_service = self.createService(conn_dict)
137-
# conn_dict = self.service_manager.get_current_conn_dict()
132+
series_service = self.createService()
133+
conn_dict = self.service_manager.get_current_conn_dict()
138134

139135
if self.servicesValid(series_service):
140136
self.service_manager.add_connection(conn_dict)

odmtools/odmdata/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
# Variable = ODM.Variable
3232
# VerticalDatumCV=ODM.VerticalDatumCV
3333
# returnDict = ODM.returnDict
34-
from odm2api.ODM1_1_1.services.series_service import ODM#, refreshDB
35-
from odm2api.ODMconnection import SessionFactory
34+
from odm2api.ODM1_1_1.services import SeriesService#, refreshDB
35+
from odm2api.ODMconnection import SessionFactory, dbconnection
3636
from odm2api.ODM2.models import _changeSchema as change_schema
3737
from odmtools.odmdata.memory_database import MemoryDatabase
38-
38+
ODM = SeriesService.ODM
3939

4040
from collections import OrderedDict
4141
def returnDict():
@@ -54,11 +54,13 @@ def returnDict():
5454
]
5555
return OrderedDict(zip(keys, values))
5656
__all__=[
57-
'SessionFactory',
57+
#'SessionFactory',
5858
'refreshDB',
5959
'change_schema',
6060
'returnDict',
61-
'ODM',
61+
#'ODM',
6262
'MemoryDatabase',
6363
'returnDict'
64+
'SeriesService'
65+
'dbconnection'
6466
]

odmtools/odmdata/memory_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from odmtools.common.logger import LoggerTool
66
from odmtools.odmservices import SeriesService
77
from odmtools.odmservices import ServiceManager
8-
from odmtools.odmdata import ODM
9-
8+
from odmtools.odmdata import SeriesService#ODM
9+
ODM = SeriesService.ODM
1010
# tool = LoggerTool()
1111
# logger = tool.setupLogger(__name__, __name__ + '.log', 'w', logging.DEBUG)
1212
logger =logging.getLogger('main')

odmtools/odmservices/service_manager.py

Lines changed: 21 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from odmtools.odmservices import SeriesService, EditService, ExportService
99
from odmtools.controller import EditTools
1010
from odmtools.lib.Appdirs.appdirs import user_config_dir
11-
from odmtools.odmdata import SessionFactory, change_schema, ODM#, refreshDB
11+
from odmtools.odmdata import SeriesService, dbconnection #ODM#, refreshDBSessionFactory,
12+
1213

1314

1415

@@ -44,6 +45,7 @@ def __init__(self, debug=False, conn_dict = None):
4445
line_dict['password'] = line[2]
4546
line_dict['address'] = line[3]
4647
line_dict['db'] = line[4]
48+
line_dict['version']= line[5]
4749
self._conn_dicts.append(line_dict)
4850
else:
4951
self._conn_dicts.append(conn_dict)
@@ -84,115 +86,30 @@ def add_connection(self, conn_dict):
8486
# logger.error("Unable to save connection due to invalid connection to database")
8587
# return False
8688

87-
@staticmethod
88-
def _getSchema(engine):
89-
from sqlalchemy.engine import reflection
90-
91-
insp=reflection.Inspector.from_engine(engine)
92-
93-
for name in insp.get_schema_names():
94-
if 'odm2'== name.lower():
95-
return name
96-
else:
97-
return insp.default_schema_name
98-
99-
@classmethod
100-
def _setSchema(self, engine):
101-
102-
s = self._getSchema(engine)
103-
change_schema(s)
104-
105-
106-
@classmethod
107-
def testEngine(self, connection_string):
108-
109-
s = SessionFactory(connection_string, echo=False)
110-
try:
111-
# s.ms_test_Session().query(Variable1).limit(1).first()
112-
s.test_Session().query(ODM.Variable.code).limit(1).first()
113-
114-
115-
except Exception as e:
116-
print "Connection was unsuccessful ", e.message
117-
return False
118-
return True
119-
# def is_valid_connection(self):
120-
# if self._current_conn_dict:
121-
# conn_string = self._build_connection_string(self._current_conn_dict)
122-
# logger.debug("Conn_string: %s" % conn_string)
123-
# try:
124-
# if self.testEngine(conn_string):
125-
# return self._current_conn_dict
126-
#
127-
# return None
128-
129-
def is_valid_connection(self):
130-
if self._current_conn_dict:
131-
conn_string = self._build_connection_string(self._current_conn_dict)
132-
logger.debug("Conn_string: %s" % conn_string)
133-
dbtype = float(self._current_conn_dict['version'])
134-
#dbtype =1.1
135-
#refreshDB(dbtype)
136-
137-
try:
138-
if self.testEngine(conn_string):
139-
return self._current_conn_dict
140-
except Exception as e:
141-
logger.fatal("The previous database for some reason isn't accessible, please enter a new connection %s" % e.message)
142-
return None
143-
144-
def test_connection(self, conn_dict):
145-
try:
146-
conn_string = self._build_connection_string(conn_dict)
147-
148-
dbtype = float(conn_dict['version'])
149-
#dbtype =1.1
150-
#refreshDB(dbtype)
151-
if self.testEngine(conn_string):# and self.get_db_version(conn_string) == '1.1.1':
152-
153-
return True
154-
except SQLAlchemyError as e:
155-
logger.error("SQLAlchemy Error: %s" % e.message)
156-
raise e
157-
except Exception as e:
158-
logger.error("Error: %s" % e)
159-
raise e
160-
return False
16189

16290
def delete_connection(self, conn_dict):
16391
self._conn_dicts[:] = [x for x in self._conn_dicts if x != conn_dict]
16492

165-
# Create and return services based on the currently active connection
166-
# def get_db_version_dict(self, conn_dict):
167-
# conn_string = self._build_connection_string(conn_dict)
168-
# self.get_db_version(conn_string)
169-
170-
# def get_db_version(self, conn_string):
171-
# if isinstance(conn_string, dict):
172-
# conn_string = self._build_connection_string(conn_string)
173-
# service = SeriesService(conn_string)
174-
# #if not self.version:
175-
# try:
176-
# self.version = service.get_db_version()
177-
# except Exception as e:
178-
# logger.error("Exception: %s" % e.message)
179-
# return None
180-
# return self.version
18193

18294
def get_series_service(self, conn_dict=None, conn_string=""):
183-
version = 1.1
184-
if conn_dict:
185-
conn_string = self._build_connection_string(conn_dict)
186-
#self._current_conn_dict = conn_dict
187-
188-
version = float(conn_dict['version'])
189-
elif not conn_dict and not conn_string:
190-
conn_string = self._build_connection_string(self._current_conn_dict)
191-
version = float(self._current_conn_dict['version'])
192-
193-
sf = SessionFactory(conn_string, self.debug, version = version)
194-
ss= SeriesService(sf)
195-
ss.refreshDB(sf.version)
95+
if not conn_dict and not conn_string:
96+
conn_dict = self._current_conn_dict
97+
conn = dbconnection.createConnection(conn_dict['engine'], conn_dict['address'], conn_dict['db'], conn_dict['user'],
98+
conn_dict['password'], conn_dict['version'])
99+
100+
# version = 1.1
101+
# if conn_dict:
102+
# conn_string = self._build_connection_string(conn_dict)
103+
# #self._current_conn_dict = conn_dict
104+
#
105+
# version = float(conn_dict['version'])
106+
# elif not conn_dict and not conn_string:
107+
# conn_string = self._build_connection_string(self._current_conn_dict)
108+
# version = float(self._current_conn_dict['version'])
109+
#
110+
# sf = SessionFactory(conn_string, self.debug, version = version)
111+
ss= SeriesService(conn)
112+
ss.refreshDB(conn.version)
196113
return ss
197114

198115
# def get_cv_service(self):

setup/make.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def run_pyinstaller(console=False):
160160
'--version-file=%s ' % VERSION_FILE +
161161
'--onedir '
162162
# '--onefile ' +
163-
'--exclude=
163+
#'--exclude=
164164
'--noconfirm ' + APP_FILE)
165165
else:
166166
## Non Console Version

0 commit comments

Comments
 (0)