Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement database access #25

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: build
on:
push:
branches:
- main
tags:
- 'v*.*.*'
jobs:
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ Changelog <https://keepachangelog.com/en/1.0.0/>`_, and this project
adheres to `Semantic
Versioning <https://semver.org/spec/v2.0.0.html>`_.

[v0.5.9 unreleased]
-------------------

In Progress
======

- a database interface

[v0.5.8]
--------

Hotfix
======

- plot figure saving fixed

[v0.5.7]
--------
Expand Down
92 changes: 92 additions & 0 deletions db/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import sqlite3
from sqlite3 import Error

PATH = r".\db\example.db"

# flake8: noqa


def create_connection():
"""create a database connection to a database that resides
in the memory
"""
conn = None
try:
conn = sqlite3.connect(PATH)
except Error as e:
print(e)
finally:
return conn


def create_table(conn, create_table_sql):
"""create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)


def main():
# trailing commas will throw a syntax error !?

sql_create_projects_table = """
CREATE TABLE IF NOT EXISTS projects (
id integer PRIMARY KEY,
name text NOT NULL
);
"""

sql_create_tests_table = """
CREATE TABLE IF NOT EXISTS tests (
id integer PRIMARY KEY,
name text NOT NULL,
project_id integer NOT NULL,
FOREIGN KEY (project_id) REFERENCES projects (id)
);
"""

sql_create_readings_table = """
CREATE TABLE IF NOT EXISTS readings (
id integer PRIMARY KEY,
name text NOT NULL,
pump_1 integer NOT NULL,
pump_2 integer NOT NULL,
test_id integer NOT NULL,
FOREIGN KEY (test_id) REFERENCES tests (id)
);
"""

sql_create_groups_table = """
CREATE TABLE IF NOT EXISTS groups (
id integer NOT NULL,
listID NOT NULL,
name text NOT NULL,
)
"""

# create a database connection
conn = create_connection()
# create tables
if conn is not None:
# create projects table
print("projects")
create_table(conn, sql_create_projects_table)

print("tasks")
# create tasks table
create_table(conn, sql_create_tests_table)
else:
print("Error! cannot create the database connection.")

conn.commit()
conn.close()


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions db/sampledb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"_default": {
"1": {
"uuid":"uuid",
"name": "project",
"customer": "customer",
"productionCo": "productionCo",
"submittedBy": "submittedBy",
"field": "field",
"sample": "sample",
"sampleDate": "sampleDate",
"recDate": "recDate",
"compDate": "compDate",
"analyst": "analyst",
"numbers": "numbers",
"notes": "notes",
"tests": [
{
"name": "testname",
"reportAs":"label",
"isBlank": true,
"chemical": "chemical",
"rate": 100,
"clarity": "clarity",
"toConsider": "toConsider",
"includeOnRep": "includeOnRep",
"obsBaseline": 75,
"notes": "notes",
"result": 100,
"readings":[
{
"average": 2,
"pump 1": 1,
"pump 2": 3,
"elapsedMin": "stamp"
}
]
}
]
}
}
}
33 changes: 33 additions & 0 deletions db/sampledb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
_default:
'1':
uuid: uuid
name: project
customer: customer
productionCo: productionCo
submittedBy: submittedBy
field: field
sample: sample
sampleDate: sampleDate
recDate: recDate
compDate: compDate
analyst: analyst
numbers: numbers
notes: notes
tests:
- name: testname
reportAs: label
isBlank: true
chemical: chemical
rate: 100
clarity: clarity
toConsider: toConsider
includeOnRep: includeOnRep
obsBaseline: 75
notes: notes
result: 100
readings:
- average: 2
pump 1: 1
pump 2: 3
elapsedMin: stamp
44 changes: 44 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--
-- File generated with SQLiteStudio v3.3.3 on Wed Jun 30 07:22:45 2021
--
-- Text encoding used: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: projects
CREATE TABLE projects (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);


-- Table: readings
CREATE TABLE readings (
id INTEGER PRIMARY KEY,
test_id INTEGER REFERENCES tests (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL
);


-- Table: reports
CREATE TABLE reports (
id INTEGER PRIMARY KEY,
test_id REFERENCES tests (id) ON DELETE CASCADE
ON UPDATE CASCADE
);


-- Table: tests
CREATE TABLE tests (
id INTEGER PRIMARY KEY,
project_id REFERENCES projects (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
name TEXT NOT NULL
);


COMMIT TRANSACTION;
PRAGMA foreign_keys = on;
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[tool.poetry]
name = "scalewiz"
version = "0.5.7"
version = "0.5.8"
description = "A graphical user interface for chemical performance testing designed to work with Teledyne SSI MX-class HPLC pumps."
readme = "README.rst"
license = "GPL-3.0-or-later"
license = "GPL-3.0"
authors = ["Alex Whittington <[email protected]>"]
repository = "https://github.com/teauxfu/scalewiz"
packages = [
{include = "scalewiz"}
]
Expand All @@ -29,6 +30,7 @@ pandas = "^1.2.2"
py-hplc = "^1.0.1"
tomlkit = "^0.7.0"
appdirs = "^1.4.4"
tinydb = "^4.4.0"

[tool.poetry.scripts]
scalewiz = "scalewiz.__main__:main"
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pytz==2021.1; python_full_version >= "3.7.1" \
six==1.16.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.3.0" and python_version >= "3.7" \
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254 \
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926
tinydb==4.4.0; python_version >= "3.5" and python_version < "4.0" \
--hash=sha256:30b0f718ebb288e42d2f69f3e1b18928739f25153e6b5308a234e95c1673de71 \
--hash=sha256:d57c29524ecacc081ebc24f96e0d787bba11dc20d52634a32a709b878be3545a
tkcalendar==1.6.1 \
--hash=sha256:9d3a80816a7b32d64fab696fa3d2a007fb23c87953267d5e343a38ff4cd7c15c \
--hash=sha256:c3ac34ab268734377ce73407893e8a5765e288aecbbb55136fb3ccea98006a96 \
Expand Down
3 changes: 3 additions & 0 deletions scalewiz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

from typing import TYPE_CHECKING

from tinydb import TinyDB

if TYPE_CHECKING:
from tkinter import Tk

from scalewiz.helpers.configuration import get_config

ROOT: Tk = None
CONFIG: dict = get_config()
DATABASE: TinyDB = TinyDB(CONFIG["recents"]["database"])
2 changes: 1 addition & 1 deletion scalewiz/components/evaluation_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def save(self) -> None:
)
parent_dir = Path(self.editor_project.path.get()).parent
plot_output = Path(parent_dir, plot_output).resolve()
self.plot_view.fig.savefig(plot_output)
self.plot_view.fig.savefig(str(plot_output))
self.editor_project.plot.set(str(plot_output))
# update log
log_output = (
Expand Down
19 changes: 17 additions & 2 deletions scalewiz/helpers/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

CONFIG_DIR = Path(user_config_dir("ScaleWiz", "teauxfu"))
CONFIG_FILE = Path(CONFIG_DIR, "config.toml")
DATABASE_FILE = Path(CONFIG_DIR, "db.json")


def ensure_database() -> None:
"""Ensures a database file exists."""
# make sure we have a place to store data
if DATABASE_FILE.is_file():
LOGGER.info("Found an existing database file %s", DATABASE_FILE)
else:
LOGGER.info("No database file found. Making one now at %s", DATABASE_FILE)
with DATABASE_FILE.open("w") as dbfile:
dbfile.write("")


def ensure_config() -> None:
Expand All @@ -34,6 +46,8 @@ def ensure_config() -> None:
"No config file found in %s. Making one now at %s", CONFIG_DIR, CONFIG_FILE
)
init_config()
ensure_database()
update_config("recents", "database", str(DATABASE_FILE))


def init_config() -> None:
Expand Down Expand Up @@ -84,6 +98,7 @@ def generate_default() -> document:
recents = table()
recents["analyst"] = "teauxfu"
recents["project"] = ""
recents["database"] = str(DATABASE_FILE)
doc["recents"] = recents
doc["recents"].comment("these will get updated between user sessions")

Expand Down Expand Up @@ -124,7 +139,6 @@ def generate_default() -> document:

def open_config() -> None:
"""Opens the config file."""
ensure_config()
if CONFIG_FILE.is_file():
os.startfile(CONFIG_FILE)

Expand All @@ -134,6 +148,8 @@ def get_config() -> dict[str, Union[float, int, str]]:
ensure_config()
with CONFIG_FILE.open("r") as file:
config = loads(file.read())
if "database" not in config["recents"]:
config["recents"]["database"] = str(DATABASE_FILE)
return config


Expand All @@ -145,7 +161,6 @@ def update_config(table: str, key: str, value: Union[float, int, str]) -> None:
key (str): the key to update
value (Union[float, int, str]): the new value of `key`
"""
ensure_config()
doc = loads(CONFIG_FILE.open("r").read())
if table in doc.keys() and key in doc[table].keys():
doc[table][key] = value
Expand Down
Loading