Preparation for native gui development

This commit is contained in:
2024-04-09 12:39:16 +02:00
parent 92d602c984
commit 502605067f
4 changed files with 111 additions and 1 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
YMS - Yacht Managemet System
YMS - Yacht Management System
Prototype
+1
View File
@@ -0,0 +1 @@
__doc__ = """YMS"""
+32
View File
@@ -0,0 +1,32 @@
import MySQLdb
class Database():
def __init__(self, hostname, dbname):
self.hostname = hostname
self.schema = dbname
self.conn = None
self.cur = None
self.dcur = None
def connect(self, dbuser, dbpass):
if self.conn:
return True
MySQLdb.paramstyle = 'pyformat'
try:
self.conn = MySQLdb.connect(host=self.hostname, db=self.schema,
user=dbuser, passwd=dbpass, charset='utf8')
except MySQLdb.Error as e:
print(e)
return False
self.cur = self.conn.cursor()
self.dcur = self.conn.cursor(MySQLdb.cursors.DictCursor)
return True
def disconnect(self):
self.cur.close()
self.dcur.close()
self.conn.close()
def is_connected(self):
return self.conn.open
Executable
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/python3
"""
Configuration file located at ~/.config/ymsgui.conf:
[DB]
host = localhost
db = yms
user = yms
pass = topsecret
"""
import os
import sys
import locale
import configparser
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk
from yms import database
cfg = {
'cfgfile': '~/.config/ymsgui.conf',
'host': 'localhost',
'db': 'yms',
'user': 'yms'
}
class Frontend(Gtk.Window):
def __init__(self):
super().__init__()
self.connect("destroy", self.on_destroy)
def run(self):
self.show_all()
Gtk.main()
def on_destroy(self, widget, data=None):
Gtk.main_quit()
def messagebox(messagetext, symbol=None, title=None, parent=None):
if not symbol:
symbol = Gtk.MessageType.INFO
if parent:
symbol |= Gtk.DIALOG_DESTROY_WITH_PARENT
md = Gtk.MessageDialog(parent=parent, flags=0, message_type=symbol, buttons=Gtk.ButtonsType.CLOSE)
md.set_markup(messagetext)
if title:
md.set_title(title)
if not parent:
md.set_position(Gtk.WindowPosition.CENTER)
md.run()
md.destroy()
if __name__ == "__main__":
locale.setlocale(locale.LC_ALL, '')
config = configparser.ConfigParser()
if not config.read(os.path.expanduser(cfg['cfgfile'])):
messagebox("Configuration file not found", title="YMS")
else:
cfg['host'] = config.get('DB', 'host')
cfg['db'] = config.get('DB', 'db')
cfg['user'] = config.get('DB', 'user')
cfg['pass'] = config.get('DB', 'pass')
db = database.Database(cfg['host'], cfg['db'])
if db.connect(cfg['user'], cfg['pass']):
app = Frontend()
app.run()
db.disconnect()
else:
messagebox("Database connection cannot be established", title="YMS")
# Another fine product of the sirius cybernetics corporation