Files
YMS/yms/view/status.py
T

60 lines
2.2 KiB
Python

# SPDX-License-Identifier: WTFPL
import os
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import re
from yms import gtkutils, combobox, treeview
class StatusView:
def __init__(self, context):
self.context = context
self.title = _("Status")
builder = Gtk.Builder()
builder.set_translation_domain(context.textdomain)
builder.add_from_file(os.path.join(context.ui_path, "status.ui"))
builder.connect_signals(self)
self.topwidget = builder.get_object("topobject")
gtkutils.init_named_controls(self, builder)
# Generic information
self.label['dbinfo'].set_text("Database: {} on {}".format(context.cfg['db'], context.cfg['host']))
# Load table status info
self.lst_tables = Gtk.ListStore(str, int)
for row in self.get_table_counts():
self.lst_tables.append(row)
treeview.add_column(self.tree['tables'], 'Table', 0)
treeview.add_column(self.tree['tables'], '# Records', 1)
self.tree['tables'].set_model(self.lst_tables)
# Load current config file
self.label['config'].set_label(context.cfg['cfgfile'])
cfgfile = os.path.expanduser(context.cfg['cfgfile'])
try:
with open(cfgfile, "r", encoding="utf-8") as f:
contents = f.read()
except OSError as e:
contents = f"Could not read file: {e}"
contents = re.sub(r'(^\s*pass\s*=\s*).*$', r'\1********', contents, flags=re.MULTILINE)
self.text ['config'].get_buffer().set_text(contents)
def get_table_counts(self):
# Detect all tables in database
sql= ("SELECT table_name "
"FROM information_schema.tables "
"WHERE table_schema='yms' AND table_type='BASE TABLE' "
"ORDER BY table_name")
self.context.db.cur.execute(sql)
tables = [row[0] for row in self.context.db.cur.fetchall()]
# Loop over tables and get current record count
records = []
for table in tables:
sql = f"SELECT COUNT(*) FROM {table}"
self.context.db.cur.execute(sql)
records.append((table, self.context.db.cur.fetchone()[0]))
return records