fah-control/fah/SlotConfig.py
2023-07-19 22:56:47 +02:00

112 lines
4.1 KiB
Python

###############################################################################
# #
# Folding@Home Client Control (FAHControl) #
# Copyright (C) 2016-2020 foldingathome.org #
# Copyright (C) 2010-2016 Stanford University #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
from gi.repository import Gtk
import gobject
import copy
from fah.util import parse_bool
from fah.util import status_to_color
class SlotConfig:
def __init__(
self, id=-1, status=None, description=None, reason=None,
idle=False, options={}, **kw):
self.id = int(id)
self.status = status
self.description = description
self.reason = reason
self.idle = idle
self.options = options
if status is not None:
self.status = status.title()
if description is None:
self.description = 'cpu'
# Type
if self.description.startswith('cpu'):
self.type = 'cpu'
elif self.description.startswith('gpu'):
self.type = 'gpu'
else:
raise Exception('Invalid slot type "%s"' % description)
def add_to_ui(self, app):
wrapper = gobject.GObject()
wrapper.slot = copy.deepcopy(self)
app.slot_list.append((self.id, self.type, wrapper))
@staticmethod
def clear_dialog(app):
app.slot_option_list.clear()
app.slot_type_cpu.set_active(True)
app.slot_option_widgets['cpus'].set_value(-1)
def save_dialog(self, app):
self.options = {}
if app.slot_type_cpu.get_active():
self.type = 'cpu'
cpus = str(int(app.slot_option_widgets['cpus'].get_value()))
if int(cpus):
self.options['cpus'] = cpus
elif app.slot_type_gpu.get_active():
self.type = 'gpu'
# Extra options
def add_option(model, path, iter, data=None):
name = model.get(iter, 0)[0]
value = model.get(iter, 1)[0]
self.options[name] = value
app.slot_option_list.foreach(add_option)
def load_dialog(self, app):
used = set()
# Type
if self.type == 'cpu':
app.slot_type_cpu.set_active(True)
elif self.type == 'gpu':
app.slot_type_gpu.set_active(True)
else:
raise Exception('Invalid slot type "%s"' % self.type)
used.add('gpu')
# SMP
if 'cpus' in self.options:
cpus = float(self.options['cpus'])
else:
cpus = -1
app.slot_option_widgets['cpus'].set_value(cpus)
used.add('cpus')
# Options
app.slot_option_list.clear()
for name, value in list(self.options.items()):
if name not in used:
app.slot_option_list.append((name, value))