Skip to content

Commit

Permalink
REFACTOR: Move core to module level.
Browse files Browse the repository at this point in the history
  • Loading branch information
genedan committed Jan 17, 2025
1 parent 841b64a commit a03ddd3
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 167 deletions.
15 changes: 5 additions & 10 deletions faslr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
CONFIG_TEMPLATES_PATH
)

from faslr.core import (
FCore
)
import faslr.core as core

from faslr.menu import (
MainMenuBar
Expand Down Expand Up @@ -63,13 +61,11 @@ class MainWindow(QMainWindow):
def __init__(
self,
application: QApplication = None,
core: FCore = None
):
super().__init__()
logging.info("Main window initialized.")

self.application = application
self.core = core

self.resize(
MAIN_WINDOW_WIDTH,
Expand All @@ -83,8 +79,7 @@ def __init__(
self.body_layout = QHBoxLayout()

self.menu_bar = MainMenuBar(
parent=self,
core=self.core
parent=self
)

self.setStatusBar(QStatusBar(self))
Expand Down Expand Up @@ -150,6 +145,8 @@ def __init__(
main_window=self
)

print('asdf')

def remove_tab(
self,
index: int
Expand Down Expand Up @@ -207,11 +204,9 @@ def closeEvent(
)

app = QApplication(sys.argv)
fcore = FCore()

window = MainWindow(
application=app,
core=fcore
application=app
)

window.show()
Expand Down
28 changes: 7 additions & 21 deletions faslr/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
QT_FILEPATH_OPTION
)

import faslr.core as core

from faslr.schema import (
CountryTable,
LOBTable,
Expand Down Expand Up @@ -47,7 +49,6 @@

if TYPE_CHECKING: # pragma: no cover
from faslr.__main__ import MainWindow
from faslr.core import FCore
from faslr.menu import MainMenuBar


Expand All @@ -60,12 +61,10 @@ class ConnectionDialog(QDialog):
def __init__(
self,
parent: MainMenuBar = None,
core: FCore = None
):
super().__init__(parent)
logging.info("Connection window initialized.")

self.core = core
self.parent = parent

self.setWindowTitle("Connection")
Expand Down Expand Up @@ -110,10 +109,10 @@ def make_connection(
main_window = None

if self.existing_connection.isChecked():
self.core.db = self.open_existing_db(main_window=main_window)
core.db = self.open_existing_db(main_window=main_window)

elif self.new_connection.isChecked():
self.core.db = self.create_new_db()
core.db = self.create_new_db()

def create_new_db(
self
Expand Down Expand Up @@ -155,7 +154,7 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
self.close()

if db_filename != "":
self.core.connection_established = True
core.connection_established = True

if self.parent:
self.parent.toggle_project_actions()
Expand All @@ -180,7 +179,7 @@ def open_existing_db(

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

main_window.core.connection_established = True
core.connection_established = True

populate_project_tree(
db_filename=db_filename,
Expand Down Expand Up @@ -316,7 +315,7 @@ def connect_db(db_path: str) -> (Session, Connection):
"""
Connects the db. Shortens amount of code required to do so.
"""

print(db_path)
if not os.path.isfile(db_path):
raise FileNotFoundError(DB_NOT_FOUND_TEXT)

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


def get_startup_db_path(
config_path: str = CONFIG_PATH
) -> str:
"""
Extracts the db path when the user opts to connect to one automatically upon startup.
"""
config = configparser.ConfigParser()
config.read(config_path)
config.sections()
startup_db = config['STARTUP_CONNECTION']['startup_db']

return startup_db
40 changes: 40 additions & 0 deletions faslr/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import configparser
import os
from faslr.constants import CONFIG_PATH


def get_startup_db_path(
config_path: str = CONFIG_PATH
) -> str:
"""
Extracts the db path when the user opts to connect to one automatically upon startup.
"""
config = configparser.ConfigParser()
config.read(config_path)
config.sections()
startup_db = config['STARTUP_CONNECTION']['startup_db']

return startup_db


config_path: str = CONFIG_PATH

# Flag to determine whether there is an active database connection. Most project-related functions
# should be disabled unless a connection is established.
connection_established = False
db = None


# If a startup db has been indicated, get the path.
if os.path.isfile(config_path):
startup_db: str = get_startup_db_path(config_path=config_path)
if startup_db is not None:
db = startup_db
else:
startup_db: None = None

def set_db(path: str) -> None:
global db
db = path


7 changes: 0 additions & 7 deletions faslr/core/__init__.py

This file was deleted.

14 changes: 0 additions & 14 deletions faslr/core/application.py

This file was deleted.

25 changes: 0 additions & 25 deletions faslr/core/core.py

This file was deleted.

19 changes: 6 additions & 13 deletions faslr/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
FaslrConnection
)

from faslr.core import (
FCore
)
import faslr.core as core

from faslr.constants import (
DEVELOPMENT_FIELDS,
Expand Down Expand Up @@ -113,13 +111,11 @@ def __init__(
project_id: str = None,
parent: QTabWidget = None,
main_window: MainWindow = None,
core: FCore = None
):
super().__init__()

self.wizard = None
self.main_window = main_window
self.core = core
self.parent = parent
self.project_id = project_id
self.triangle = None # for testing purposes, will store triangle data in db later so remove once that is done
Expand All @@ -138,7 +134,6 @@ def __init__(
self.data_view = ProjectDataView(parent=self)
self.data_model = ProjectDataModel(
parent=self,
core=core
)
self.data_view.setModel(self.data_model)
self.layout.addWidget(self.data_view)
Expand Down Expand Up @@ -204,7 +199,7 @@ def save_to_db(
modified,
):

faslr_conn = FaslrConnection(db_path=self.core.db)
faslr_conn = FaslrConnection(db_path=core.db)

project_view = ProjectViewTable(
name=name,
Expand Down Expand Up @@ -856,12 +851,10 @@ class ProjectDataModel(FAbstractTableModel):
def __init__(
self,
parent: DataPane = None,
core: FCore = None
):
super().__init__()

self.parent = parent
self.core = core

column_list = [
'View Id',
Expand Down Expand Up @@ -896,15 +889,15 @@ def read_sql(fc: FaslrConnection) -> DataFrame:
if self.parent.main_window:

faslr_connection = FaslrConnection(
db_path=self.parent.main_window.core.db
db_path=core.db
)

df = read_sql(fc=faslr_connection)

elif self.core:
elif core:

faslr_connection = FaslrConnection(
db_path=self.core.db
db_path=core.db
)

df = read_sql(fc=faslr_connection)
Expand Down Expand Up @@ -987,7 +980,7 @@ def open_triangle(
val: QModelIndex
) -> None:

fc = FaslrConnection(db_path=self.parent.core.db)
fc = FaslrConnection(db_path=core.db)

view_id = self.model().sibling(val.row(), 0, val).data()
query = fc.session.query(
Expand Down
5 changes: 2 additions & 3 deletions faslr/demos/data_pane_demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from faslr.constants import DEFAULT_DIALOG_PATH
from faslr.core import FCore
import faslr.core as core
from faslr.data import DataPane
from faslr.__main__ import MainWindow

Expand All @@ -11,12 +11,11 @@
# main_window = MainWindow()
# main_window.db = DEFAULT_DIALOG_PATH + '/sample.db'

core = FCore()
core.set_db(path=DEFAULT_DIALOG_PATH + '/sample.db')

parent_tab = QTabWidget()

data_pane = DataPane(core=core, parent=parent_tab)
data_pane = DataPane(parent=parent_tab)
data_pane.setWindowTitle("Project Data Views")

parent_tab.addTab(data_pane, "Data Pane")
Expand Down
6 changes: 1 addition & 5 deletions faslr/demos/settings_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""
import sys

from faslr.core import FCore

from faslr.settings import (
SettingsDialog
)
Expand All @@ -15,9 +13,7 @@

app = QApplication(sys.argv)

core = FCore()

settings = SettingsDialog(core=core)
settings = SettingsDialog()

settings.show()

Expand Down
Loading

0 comments on commit a03ddd3

Please sign in to comment.