Load MySQL connection from config file.

This commit is contained in:
luxick
2018-02-28 21:49:02 +01:00
parent 2bc59cd8d2
commit 56ce7205c1
11 changed files with 407 additions and 271 deletions

View File

@@ -74,10 +74,10 @@ def show_episode_dialog(builder: Gtk.Builder, title: str, season_id: int, episod
# Update Date of the Episode
cal_value = builder.get_object('episode_calendar').get_date()
selected_date = datetime(*cal_value).date()
episode.update(date=selected_date,
number=str(builder.get_object("episode_no_spin_button").get_value()),
name=builder.get_object("episode_name_entry").get_text())\
.execute()
episode.date = selected_date,
episode.number = int(builder.get_object("episode_no_spin_button").get_value())
episode.name = builder.get_object("episode_name_entry").get_text()
episode.save()
return True
@@ -117,7 +117,7 @@ def show_edit_death_dialog(builder: Gtk.Builder, episode_id: int, death: sql.Dea
dialog.set_transient_for(builder.get_object("main_window"))
with sql.db.atomic():
if death:
index = util.Util.get_index_of_combo_model(builder.get_object('edit_death_enemy_combo'), 0, death.enemy.id)
index = util.get_index_of_combo_model(builder.get_object('edit_death_enemy_combo'), 0, death.enemy.id)
builder.get_object('edit_death_enemy_combo').set_active(index)
# TODO Default drink should be set in config
@@ -135,8 +135,8 @@ def show_edit_death_dialog(builder: Gtk.Builder, episode_id: int, death: sql.Dea
return result
# Collect info from widgets and save to database
player_id = util.Util.get_combo_value(builder.get_object('edit_death_player_combo'), 0)
enemy_id = util.Util.get_combo_value(builder.get_object('edit_death_enemy_combo'), 3)
player_id = util.get_combo_value(builder.get_object('edit_death_player_combo'), 0)
enemy_id = util.get_combo_value(builder.get_object('edit_death_enemy_combo'), 3)
comment = builder.get_object('edit_death_comment_entry').get_text()
if not death:
death = sql.Death.create(episode=episode_id, player=player_id, enemy=enemy_id, info=comment)
@@ -165,7 +165,7 @@ def show_edit_victory_dialog(builder: Gtk.Builder, episode_id: int, victory: sql
['edit_victory_enemy_combo', victory.enemy.id]]
for info in infos:
combo = builder.get_object(info[0])
index = util.Util.get_index_of_combo_model(combo, 0, info[1])
index = util.get_index_of_combo_model(combo, 0, info[1])
combo.set_active(index)
builder.get_object('victory_comment_entry').set_text(victory.info)
@@ -177,12 +177,15 @@ def show_edit_victory_dialog(builder: Gtk.Builder, episode_id: int, victory: sql
return result
# Collect info from widgets and save to database
player_id = util.Util.get_combo_value(builder.get_object('edit_victory_player_combo'), 0)
enemy_id = util.Util.get_combo_value(builder.get_object('edit_victory_enemy_combo'), 3)
player_id = util.get_combo_value(builder.get_object('edit_victory_player_combo'), 0)
enemy_id = util.get_combo_value(builder.get_object('edit_victory_enemy_combo'), 3)
comment = builder.get_object('victory_comment_entry').get_text()
if not victory:
sql.Victory.create(episode=episode_id, player=player_id, enemy=enemy_id, info=comment)
else:
victory.update(player=player_id, enemy=enemy_id, info=comment).execute()
victory.player = player_id
victory.enemy = enemy_id
victory.info = comment
victory.save()
return result

View File

@@ -1,6 +1,10 @@
from collections import Counter
import gi
import math
import os
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from dsst_gtk3.handlers import handlers
@@ -10,7 +14,7 @@ from dsst_sql import sql, sql_func
class GtkUi:
""" The main UI class """
def __init__(self):
def __init__(self, config: dict):
# Load Glade UI files
self.ui = Gtk.Builder()
glade_resources = [
@@ -18,15 +22,15 @@ class GtkUi:
['dsst_gtk3', 'resources', 'glade', 'dialogs.glade']
]
for path in glade_resources:
self.ui.add_from_string(util.Util.load_ui_resource_string(path))
self.ui.add_from_string(util.load_ui_resource_string(path))
# Connect signal handlers to UI
self.handlers = handlers.Handlers(self)
self.ui.connect_signals(self.handlers)
# Show all widgets
self.ui.get_object('main_window').show_all()
db_config = config['sql_connections'][0]
# Initialize the database
# TODO User input to select database
sql.db.init('dsst', user='dsst', password='dsst')
sql.db.init(db_config['db_name'], user=db_config['user'], password=db_config['password'])
# Create database if not exists
sql_func.create_tables()
@@ -41,7 +45,7 @@ class GtkUi:
# Rebuild drink store
self.ui.get_object('drink_store').clear()
for drink in sql.Drink.select():
self.ui.get_object('drink_store').append([drink.id, drink.name, str(drink.vol)])
self.ui.get_object('drink_store').append([drink.id, drink.name, '{:.2f}%'.format(drink.vol)])
# Rebuild seasons store
store = self.ui.get_object('seasons_store')
store.clear()
@@ -115,8 +119,13 @@ class GtkUi:
self.ui.get_object('ep_death_count_label').set_text(str(len(episode.deaths)))
drink_count = sum(len(death.penalties) for death in episode.deaths)
self.ui.get_object('ep_drinks_label').set_text(str(drink_count))
cl_booze = sum(len(death.penalties) * death.penalties[0].size for death in episode.deaths)
self.ui.get_object('ep_booze_label').set_text(str(cl_booze) + "cl")
self.ui.get_object('ep_player_drinks_label').set_text(str(len(episode.deaths)))
dl_booze = sum(len(death.penalties) * death.penalties[0].size for death in episode.deaths)
l_booze = round(dl_booze / 10, 2)
self.ui.get_object('ep_booze_label').set_text('{}l'.format(l_booze))
dl_booze = sum(len(death.penalties) * death.penalties[0].size for death in episode.deaths)
ml_booze = round(dl_booze * 10, 0)
self.ui.get_object('ep_player_booze_label').set_text('{}ml'.format(ml_booze))
enemy_list = [death.enemy.name for death in episode.deaths]
sorted_list = Counter(enemy_list).most_common(1)
if sorted_list:
@@ -127,7 +136,7 @@ class GtkUi:
"""Read ID of the selected season from the UI
:return: ID of the selected season
"""
season_id = util.Util.get_combo_value(self.ui.get_object('season_combo_box'), 0)
season_id = util.get_combo_value(self.ui.get_object('season_combo_box'), 0)
return season_id if season_id != -1 else None
def get_selected_episode_id(self):
@@ -139,5 +148,8 @@ class GtkUi:
def main():
GtkUi()
if not os.path.isfile(util.CONFIG_PATH):
util.save_config(util.DEFAULT_CONFIG, util.CONFIG_PATH)
config = util.load_config(util.CONFIG_PATH)
GtkUi(config)
Gtk.main()

View File

@@ -8,7 +8,7 @@ class BaseDataHandlers:
self.app = app
def do_manage_players(self, *_):
result = dialogs.show_manage_players_dialog(self.app.ui, 'Manage Players')
dialogs.show_manage_players_dialog(self.app.ui, 'Manage Players')
def do_add_player(self, entry):
if entry.get_text():
@@ -17,7 +17,7 @@ class BaseDataHandlers:
self.app.reload_base_data()
def do_manage_enemies(self, *_):
result = dialogs.show_manage_enemies_dialog(self.app.ui, self.app.get_selected_season_id())
dialogs.show_manage_enemies_dialog(self.app.ui, self.app.get_selected_season_id())
def on_player_name_edited(self, _, index, value):
row = self.app.ui.get_object('all_players_store')[index]
@@ -33,6 +33,12 @@ class BaseDataHandlers:
.execute()
self.app.reload_base_data()
def do_add_drink(self, entry):
if entry.get_text():
sql.Drink.create(name=entry.get_text(), vol=0)
entry.set_text('')
self.app.reload_base_data()
def on_drink_name_edited(self, _, index, value):
row = self.app.ui.get_object('drink_store')[index]
sql.Drink.update(name=value)\

View File

@@ -16,5 +16,5 @@ class DeathHandlers:
self.app.reload_for_season()
self.app.reload_for_episode()
def on_penalty_drink_changed(self, widget, path, text):
def on_penalty_drink_changed(self, _, path, text):
self.app.ui.get_object('player_penalties_store')[path][2] = text

View File

@@ -11,7 +11,7 @@ class DialogHandlers:
""" Signal Handler for Add Player to Episode Button in Manage Episode Dialog
:param combo: Combo box with all the available players
"""
player_id = util.Util.get_combo_value(combo, 0)
player_id = util.get_combo_value(combo, 0)
if player_id:
self.app.ui.get_object('add_player_combo_box').set_active(-1)
player = sql.Player.get(sql.Player.id == player_id)
@@ -28,10 +28,3 @@ class DialogHandlers:
def do_manage_drinks(self, *_):
result = dialogs.show_manage_drinks_dialog(self.app.ui)
def do_add_drink(self, entry):
if entry.get_text():
store = self.app.ui.get_object('drink_store')
drink = sql.Drink.create(name=entry.get_text(), vol='0')
store.append([drink.id, drink.name, drink.vol])
entry.set_text('')

View File

@@ -24,9 +24,9 @@ class Handlers(SeasonHandlers, BaseDataHandlers, DialogHandlers, DeathHandlers,
VictoryHandlers.__init__(self, app)
@staticmethod
def do_delete_event(*args):
def do_delete_event(*_):
""" Signal will be sent when app should close
:param args: Arguments to the delete event
:param _: Arguments to the delete event
"""
Gtk.main_quit()

View File

@@ -20,7 +20,7 @@ class SeasonHandlers:
season_id = self.app.get_selected_season_id()
if not season_id:
return
episode = dialogs.show_episode_dialog(self.app.ui, 'Create new Episode', season_id)
dialogs.show_episode_dialog(self.app.ui, 'Create new Episode', season_id)
self.app.reload_for_season()
def on_selected_episode_changed(self, *_):

View File

@@ -232,150 +232,6 @@
<column type="gint"/>
</columns>
</object>
<object class="GtkDialog" id="manage_enemies_dialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Manage Enemies For This Season</property>
<property name="resizable">False</property>
<property name="modal">True</property>
<property name="default_height">300</property>
<property name="type_hint">dialog</property>
<property name="deletable">False</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">4</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="okButtonRename1">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Add Enemy</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="manage_add_enemy_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="activate" handler="do_add_enemy" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">All Enemies</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkTreeView" id="manage_enemies_tree_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="vexpand">True</property>
<property name="model">enemy_season_store</property>
<property name="search_column">0</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="title" translatable="yes">Name</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-5">okButtonRename1</action-widget>
</action-widgets>
<child>
<placeholder/>
</child>
</object>
<object class="GtkAdjustment" id="ep_number_ajustment">
<property name="upper">1000000</property>
<property name="step_increment">1</property>
@@ -901,6 +757,157 @@
<placeholder/>
</child>
</object>
<object class="GtkDialog" id="manage_enemies_dialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Manage Enemies For This Season</property>
<property name="resizable">False</property>
<property name="modal">True</property>
<property name="default_height">300</property>
<property name="type_hint">dialog</property>
<property name="deletable">False</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">4</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="okButtonRename1">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Add Enemy</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="manage_add_enemy_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="activate" handler="do_add_enemy" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">All Enemies</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="manage_enemies_tree_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="vexpand">True</property>
<property name="model">enemy_season_store</property>
<property name="search_column">0</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="title" translatable="yes">Name</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-5">okButtonRename1</action-widget>
</action-widgets>
<child>
<placeholder/>
</child>
</object>
<object class="GtkDialog" id="manage_players_dialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Manage Players</property>
@@ -1557,7 +1564,7 @@
<property name="wide_handle">True</property>
<child>
<object class="GtkBox">
<property name="width_request">150</property>
<property name="width_request">180</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
@@ -1814,27 +1821,14 @@
</packing>
</child>
<child>
<object class="GtkLabel">
<object class="GtkLabel" id="ep_death_count_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Total Drinks:</property>
<property name="xalign">1</property>
<property name="label" translatable="yes">[DeathCount]</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hardest Enemy:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
@@ -1846,6 +1840,61 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">7</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hardest Enemy:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">8</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_enemy_name_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">7</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_worst_player_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">8</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Total Drinks:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_drinks_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[DrinkCount]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">4</property>
</packing>
</child>
@@ -1858,29 +1907,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_death_count_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[DeathCount]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_drinks_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[DrinkCount]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
<property name="top_attach">5</property>
</packing>
</child>
<child>
@@ -1891,13 +1918,84 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Player Drinks:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Player Booze:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_enemy_name_label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Player Alc.:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Total Alc:</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">6</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_player_drinks_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[PlayerDrinks]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_player_booze_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[PlayerBooze]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_player_alc_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[PlayerAlc]</property>
</object>
<packing>
<property name="left_attach">1</property>
@@ -1905,13 +2003,14 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="ep_worst_player_label">
<object class="GtkLabel" id="ep_alc_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">[TotalAlc]</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">4</property>
<property name="top_attach">6</property>
</packing>
</child>
</object>
@@ -2261,7 +2360,7 @@
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="title" translatable="yes">Attempts</property>
<property name="title" translatable="yes">Deaths</property>
<property name="clickable">True</property>
<property name="sort_indicator">True</property>
<property name="sort_column_id">1</property>

View File

@@ -1,57 +1,79 @@
"""
This modules contains general utilities for the GTK application to use.
"""
import json
import os
from zipfile import ZipFile
CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.config', 'dsst', 'config.json')
DEFAULT_CONFIG = {
'auto_connect': False,
'sql_connections': [{
'host': 'localhost',
'db_name': 'dsst',
'user': 'dsst',
'password': 'dsst'}
]
}
class Util:
@staticmethod
def get_combo_value(combo, index: int):
""" Retrieve the selected value of a combo box at the selected index in the model
:param combo: Any Gtk Widget that supports 'get_active_iter()'
:param index: Index of the value in the widgets model to be retrieved
:return: The value of the model at the selected index (Default -1)
"""
tree_iter = combo.get_active_iter()
if tree_iter:
return combo.get_model().get_value(tree_iter, index)
else:
return -1
@staticmethod
def get_index_of_combo_model(widget, column: int, value: int):
"""Get the index of a value within a Gtk widgets model based on column an value
:param widget: Any Gtk widget that can be bound to a ListStore or TreeStore
:param column: Column in the model where to look for the value
:param value: Value to look for in the model
:return: List of the indexes where the value occurs
"""
model = widget.get_model()
return [model.index(entry) for entry in model if entry[column] == value]
def get_combo_value(combo, index: int):
""" Retrieve the selected value of a combo box at the selected index in the model
:param combo: Any Gtk Widget that supports 'get_active_iter()'
:param index: Index of the value in the widgets model to be retrieved
:return: The value of the model at the selected index (Default -1)
"""
tree_iter = combo.get_active_iter()
if tree_iter:
return combo.get_model().get_value(tree_iter, index)
else:
return -1
@staticmethod
def load_ui_resource_string(resource_path: list) -> str:
""" Load content of Glade UI files from resources path
:param resource_path: List of directory names from 'dsst' base directory
:return: String content of the Glade file
"""
if os.path.isdir(os.path.dirname(__file__)):
return Util.load_ui_resource_from_file(resource_path)
else:
return Util.load_ui_resource_from_archive(resource_path)
def get_index_of_combo_model(widget, column: int, value: int):
"""Get the index of a value within a Gtk widgets model based on column an value
:param widget: Any Gtk widget that can be bound to a ListStore or TreeStore
:param column: Column in the model where to look for the value
:param value: Value to look for in the model
:return: List of the indexes where the value occurs
"""
model = widget.get_model()
return [model.index(entry) for entry in model if entry[column] == value]
@staticmethod
def load_ui_resource_from_file(resource_path: list) -> str:
project_base_dir = os.path.dirname(os.path.dirname(__file__))
full_path = os.path.join(project_base_dir, *resource_path)
with open(full_path, 'r') as file:
return file.read()
@staticmethod
def load_ui_resource_from_archive(resource_path: list) -> str:
zip_path = os.path.dirname(os.path.dirname(__file__))
with ZipFile(zip_path, 'r') as archive:
return archive.read(str(os.path.join(*resource_path))).decode('utf-8')
def load_ui_resource_from_file(resource_path: list) -> str:
project_base_dir = os.path.dirname(os.path.dirname(__file__))
full_path = os.path.join(project_base_dir, *resource_path)
with open(full_path, 'r') as file:
return file.read()
def load_ui_resource_from_archive(resource_path: list) -> str:
zip_path = os.path.dirname(os.path.dirname(__file__))
with ZipFile(zip_path, 'r') as archive:
return archive.read(str(os.path.join(*resource_path))).decode('utf-8')
def load_ui_resource_string(resource_path: list) -> str:
""" Load content of Glade UI files from resources path
:param resource_path: List of directory names from 'dsst' base directory
:return: String content of the Glade file
"""
if os.path.isdir(os.path.dirname(__file__)):
return load_ui_resource_from_file(resource_path)
else:
return load_ui_resource_from_archive(resource_path)
def load_config(config_path: str) -> dict:
with open(config_path) as config_file:
return json.load(config_file)
def save_config(config: dict, config_path: str):
path = os.path.dirname(config_path)
if not os.path.isdir(path):
os.mkdir(path)
with open(config_path, 'wb') as file:
file.write(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')).encode('utf-8'))

View File

@@ -56,6 +56,7 @@ class Drink(Model):
class Enemy(Model):
id = AutoField()
name = CharField()
optional = BooleanField()
season = ForeignKeyField(Season, backref='enemies')
class Meta: