Skip to content

Commit a03ddd3

Browse files
committed
REFACTOR: Move core to module level.
1 parent 841b64a commit a03ddd3

18 files changed

+95
-167
lines changed

faslr/__main__.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
CONFIG_TEMPLATES_PATH
1919
)
2020

21-
from faslr.core import (
22-
FCore
23-
)
21+
import faslr.core as core
2422

2523
from faslr.menu import (
2624
MainMenuBar
@@ -63,13 +61,11 @@ class MainWindow(QMainWindow):
6361
def __init__(
6462
self,
6563
application: QApplication = None,
66-
core: FCore = None
6764
):
6865
super().__init__()
6966
logging.info("Main window initialized.")
7067

7168
self.application = application
72-
self.core = core
7369

7470
self.resize(
7571
MAIN_WINDOW_WIDTH,
@@ -83,8 +79,7 @@ def __init__(
8379
self.body_layout = QHBoxLayout()
8480

8581
self.menu_bar = MainMenuBar(
86-
parent=self,
87-
core=self.core
82+
parent=self
8883
)
8984

9085
self.setStatusBar(QStatusBar(self))
@@ -150,6 +145,8 @@ def __init__(
150145
main_window=self
151146
)
152147

148+
print('asdf')
149+
153150
def remove_tab(
154151
self,
155152
index: int
@@ -207,11 +204,9 @@ def closeEvent(
207204
)
208205

209206
app = QApplication(sys.argv)
210-
fcore = FCore()
211207

212208
window = MainWindow(
213-
application=app,
214-
core=fcore
209+
application=app
215210
)
216211

217212
window.show()

faslr/connection.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
QT_FILEPATH_OPTION
1313
)
1414

15+
import faslr.core as core
16+
1517
from faslr.schema import (
1618
CountryTable,
1719
LOBTable,
@@ -47,7 +49,6 @@
4749

4850
if TYPE_CHECKING: # pragma: no cover
4951
from faslr.__main__ import MainWindow
50-
from faslr.core import FCore
5152
from faslr.menu import MainMenuBar
5253

5354

@@ -60,12 +61,10 @@ class ConnectionDialog(QDialog):
6061
def __init__(
6162
self,
6263
parent: MainMenuBar = None,
63-
core: FCore = None
6464
):
6565
super().__init__(parent)
6666
logging.info("Connection window initialized.")
6767

68-
self.core = core
6968
self.parent = parent
7069

7170
self.setWindowTitle("Connection")
@@ -110,10 +109,10 @@ def make_connection(
110109
main_window = None
111110

112111
if self.existing_connection.isChecked():
113-
self.core.db = self.open_existing_db(main_window=main_window)
112+
core.db = self.open_existing_db(main_window=main_window)
114113

115114
elif self.new_connection.isChecked():
116-
self.core.db = self.create_new_db()
115+
core.db = self.create_new_db()
117116

118117
def create_new_db(
119118
self
@@ -155,7 +154,7 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
155154
self.close()
156155

157156
if db_filename != "":
158-
self.core.connection_established = True
157+
core.connection_established = True
159158

160159
if self.parent:
161160
self.parent.toggle_project_actions()
@@ -180,7 +179,7 @@ def open_existing_db(
180179

181180
if main_window and (not db_filename == ""):
182181

183-
main_window.core.connection_established = True
182+
core.connection_established = True
184183

185184
populate_project_tree(
186185
db_filename=db_filename,
@@ -316,7 +315,7 @@ def connect_db(db_path: str) -> (Session, Connection):
316315
"""
317316
Connects the db. Shortens amount of code required to do so.
318317
"""
319-
318+
print(db_path)
320319
if not os.path.isfile(db_path):
321320
raise FileNotFoundError(DB_NOT_FOUND_TEXT)
322321

@@ -328,16 +327,3 @@ def connect_db(db_path: str) -> (Session, Connection):
328327
connection = engine.connect()
329328
return session, connection
330329

331-
332-
def get_startup_db_path(
333-
config_path: str = CONFIG_PATH
334-
) -> str:
335-
"""
336-
Extracts the db path when the user opts to connect to one automatically upon startup.
337-
"""
338-
config = configparser.ConfigParser()
339-
config.read(config_path)
340-
config.sections()
341-
startup_db = config['STARTUP_CONNECTION']['startup_db']
342-
343-
return startup_db

faslr/core.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import configparser
2+
import os
3+
from faslr.constants import CONFIG_PATH
4+
5+
6+
def get_startup_db_path(
7+
config_path: str = CONFIG_PATH
8+
) -> str:
9+
"""
10+
Extracts the db path when the user opts to connect to one automatically upon startup.
11+
"""
12+
config = configparser.ConfigParser()
13+
config.read(config_path)
14+
config.sections()
15+
startup_db = config['STARTUP_CONNECTION']['startup_db']
16+
17+
return startup_db
18+
19+
20+
config_path: str = CONFIG_PATH
21+
22+
# Flag to determine whether there is an active database connection. Most project-related functions
23+
# should be disabled unless a connection is established.
24+
connection_established = False
25+
db = None
26+
27+
28+
# If a startup db has been indicated, get the path.
29+
if os.path.isfile(config_path):
30+
startup_db: str = get_startup_db_path(config_path=config_path)
31+
if startup_db is not None:
32+
db = startup_db
33+
else:
34+
startup_db: None = None
35+
36+
def set_db(path: str) -> None:
37+
global db
38+
db = path
39+
40+

faslr/core/__init__.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

faslr/core/application.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

faslr/core/core.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

faslr/data.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
FaslrConnection
1717
)
1818

19-
from faslr.core import (
20-
FCore
21-
)
19+
import faslr.core as core
2220

2321
from faslr.constants import (
2422
DEVELOPMENT_FIELDS,
@@ -113,13 +111,11 @@ def __init__(
113111
project_id: str = None,
114112
parent: QTabWidget = None,
115113
main_window: MainWindow = None,
116-
core: FCore = None
117114
):
118115
super().__init__()
119116

120117
self.wizard = None
121118
self.main_window = main_window
122-
self.core = core
123119
self.parent = parent
124120
self.project_id = project_id
125121
self.triangle = None # for testing purposes, will store triangle data in db later so remove once that is done
@@ -138,7 +134,6 @@ def __init__(
138134
self.data_view = ProjectDataView(parent=self)
139135
self.data_model = ProjectDataModel(
140136
parent=self,
141-
core=core
142137
)
143138
self.data_view.setModel(self.data_model)
144139
self.layout.addWidget(self.data_view)
@@ -204,7 +199,7 @@ def save_to_db(
204199
modified,
205200
):
206201

207-
faslr_conn = FaslrConnection(db_path=self.core.db)
202+
faslr_conn = FaslrConnection(db_path=core.db)
208203

209204
project_view = ProjectViewTable(
210205
name=name,
@@ -856,12 +851,10 @@ class ProjectDataModel(FAbstractTableModel):
856851
def __init__(
857852
self,
858853
parent: DataPane = None,
859-
core: FCore = None
860854
):
861855
super().__init__()
862856

863857
self.parent = parent
864-
self.core = core
865858

866859
column_list = [
867860
'View Id',
@@ -896,15 +889,15 @@ def read_sql(fc: FaslrConnection) -> DataFrame:
896889
if self.parent.main_window:
897890

898891
faslr_connection = FaslrConnection(
899-
db_path=self.parent.main_window.core.db
892+
db_path=core.db
900893
)
901894

902895
df = read_sql(fc=faslr_connection)
903896

904-
elif self.core:
897+
elif core:
905898

906899
faslr_connection = FaslrConnection(
907-
db_path=self.core.db
900+
db_path=core.db
908901
)
909902

910903
df = read_sql(fc=faslr_connection)
@@ -987,7 +980,7 @@ def open_triangle(
987980
val: QModelIndex
988981
) -> None:
989982

990-
fc = FaslrConnection(db_path=self.parent.core.db)
983+
fc = FaslrConnection(db_path=core.db)
991984

992985
view_id = self.model().sibling(val.row(), 0, val).data()
993986
query = fc.session.query(

faslr/demos/data_pane_demo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
from faslr.constants import DEFAULT_DIALOG_PATH
4-
from faslr.core import FCore
4+
import faslr.core as core
55
from faslr.data import DataPane
66
from faslr.__main__ import MainWindow
77

@@ -11,12 +11,11 @@
1111
# main_window = MainWindow()
1212
# main_window.db = DEFAULT_DIALOG_PATH + '/sample.db'
1313

14-
core = FCore()
1514
core.set_db(path=DEFAULT_DIALOG_PATH + '/sample.db')
1615

1716
parent_tab = QTabWidget()
1817

19-
data_pane = DataPane(core=core, parent=parent_tab)
18+
data_pane = DataPane(parent=parent_tab)
2019
data_pane.setWindowTitle("Project Data Views")
2120

2221
parent_tab.addTab(data_pane, "Data Pane")

faslr/demos/settings_demo.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"""
44
import sys
55

6-
from faslr.core import FCore
7-
86
from faslr.settings import (
97
SettingsDialog
108
)
@@ -15,9 +13,7 @@
1513

1614
app = QApplication(sys.argv)
1715

18-
core = FCore()
19-
20-
settings = SettingsDialog(core=core)
16+
settings = SettingsDialog()
2117

2218
settings.show()
2319

0 commit comments

Comments
 (0)