From e9ff738c4465032c8f9a0f41832df1cb5fa88f1c Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sat, 12 Oct 2019 18:49:08 -0700 Subject: [PATCH] gui: some refactoring --- nuxhash/gui/about.py | 9 ++--- nuxhash/gui/benchmarks.py | 15 +++---- nuxhash/gui/main.py | 37 +++++++++--------- nuxhash/gui/mining.py | 63 +++++++++++++----------------- nuxhash/gui/settings.py | 82 ++++++++++++++++----------------------- nuxhash/settings.py | 4 +- nuxhash/version.py | 1 + 7 files changed, 95 insertions(+), 116 deletions(-) diff --git a/nuxhash/gui/about.py b/nuxhash/gui/about.py index 653b467..2a84be6 100644 --- a/nuxhash/gui/about.py +++ b/nuxhash/gui/about.py @@ -4,7 +4,7 @@ import wx from wx.lib.agw.hyperlink import HyperLinkCtrl -from nuxhash.version import __version__ +from nuxhash.version import __copyright__, __version__ WEBSITE = 'https://github.com/YoRyan/nuxhash' @@ -30,8 +30,8 @@ def __init__(self, parent, *args, **kwargs): v_sizer.AddSpacer(15) - appName = wx.StaticText(self, label=f'nuxhash {__version__}', - style=wx.ALIGN_CENTER) + appName = wx.StaticText( + self, label=f'nuxhash {__version__}', style=wx.ALIGN_CENTER) appName.SetFont(self.GetFont().Scale(2.0)) v_sizer.Add(appName, wx.SizerFlags().Expand()) @@ -43,8 +43,7 @@ def __init__(self, parent, *args, **kwargs): v_sizer.AddSpacer(15) - copyright = wx.StaticText(self, label='Copyright © 2018\nRyan Young', - style=wx.ALIGN_CENTER) + copyright = wx.StaticText(self, label=__copyright__, style=wx.ALIGN_CENTER) copyright.SetFont(self.GetFont().Scale(0.8)) v_sizer.Add(copyright, wx.SizerFlags().Expand()) diff --git a/nuxhash/gui/benchmarks.py b/nuxhash/gui/benchmarks.py index 48aef21..97c49fe 100644 --- a/nuxhash/gui/benchmarks.py +++ b/nuxhash/gui/benchmarks.py @@ -260,9 +260,9 @@ def run(self): for target in self._targets: def report(sample, secs_remaining): main.sendMessage( - self._window, 'benchmarking.status', - target=target, speeds=sample, time=abs(secs_remaining), - warmup=(secs_remaining < 0)) + self._window, 'benchmarking.status', + target=target, speeds=sample, time=abs(secs_remaining), + warmup=(secs_remaining < 0)) device, algorithm = target speeds = utils.run_benchmark( algorithm, device, algorithm.warmup_secs, BENCHMARK_SECS, @@ -306,9 +306,9 @@ def deselect(self): class SpeedCtrl(wx.TextCtrl): def __init__(self, parent, *args, **kwargs): - wx.StaticText.__init__(self, parent, *args, - style=wx.BORDER_NONE|wx.TE_CENTRE, - size=(-1, 20), **kwargs) + wx.StaticText.__init__( + self, parent, *args, style=wx.BORDER_NONE|wx.TE_CENTRE, + size=(-1, 20), **kwargs) self._StatusPos = 0 self.Bind(wx.EVT_KILL_FOCUS, self._OnUnfocus) @@ -317,7 +317,8 @@ def SetValues(self, values): if all(speed == 0.0 for speed in values): s = '--' else: - s = '; '.join(utils.format_speed(speed).strip() for speed in values) + speeds = (utils.format_speed(speed).strip() for speed in values) + s = '; '.join(speeds) self.ChangeValue(s) def SetWarmup(self, remaining): diff --git a/nuxhash/gui/main.py b/nuxhash/gui/main.py index 69be33c..7e7b648 100644 --- a/nuxhash/gui/main.py +++ b/nuxhash/gui/main.py @@ -39,17 +39,17 @@ def __init__(self, parent, *args, **kwargs): # Create notebook and its pages. notebook = wx.Notebook(self) notebook.AddPage( - MiningScreen(notebook, devices=self._Devices), - text='Mining') + MiningScreen(notebook, devices=self._Devices), + text='Mining') notebook.AddPage( - BenchmarksScreen(notebook, devices=self._Devices), - text='Benchmarks') + BenchmarksScreen(notebook, devices=self._Devices), + text='Benchmarks') notebook.AddPage( - SettingsScreen(notebook), - text='Settings') + SettingsScreen(notebook), + text='Settings') notebook.AddPage( - AboutScreen(notebook), - text='About') + AboutScreen(notebook), + text='About') # Check miner downloads. pub.subscribe(self._OnDownloadProgress, 'download.progress') @@ -65,9 +65,8 @@ def __init__(self, parent, *args, **kwargs): self._FirstRun() pub.sendMessage('data.settings', settings=loaded_settings) - pub.sendMessage( - 'data.benchmarks', - benchmarks=nuxhash.settings.load_benchmarks(CONFIG_DIR, self._Devices)) + benchmarks = nuxhash.settings.load_benchmarks(CONFIG_DIR, self._Devices) + pub.sendMessage('data.benchmarks', benchmarks=benchmarks) def _DownloadMiners(self): to_download = [item for item in make_miners(CONFIG_DIR) @@ -81,10 +80,10 @@ def _DownloadMiners(self): def _FirstRun(self): dialog = wx.MessageDialog( - self, - 'Welcome to nuxhash!\n\nSet your NiceHash wallet address and run ' - 'some benchmarks, and then you can start mining.', - style=wx.OK) + self, + 'Welcome to nuxhash!\n\nSet your NiceHash wallet address and run ' + 'some benchmarks, and then you can start mining.', + style=wx.OK) dialog.ShowModal() def _OnDownloadProgress(self, progress, message): @@ -127,12 +126,12 @@ def run(self): n_downloads = len(self._downloads) for i, item in enumerate(self._downloads): sendMessage( - self._frame, 'download.progress', - progress=i/n_downloads, message=f'Downloading {item.name}') + self._frame, 'download.progress', + progress=i/n_downloads, message=f'Downloading {item.name}') item.download() sendMessage( - self._frame, 'download.progress', - progress=(i+1)/n_downloads, message='') + self._frame, 'download.progress', + progress=(i+1)/n_downloads, message='') def sendMessage(window, topic, **data): diff --git a/nuxhash/gui/mining.py b/nuxhash/gui/mining.py index f2d5b75..d39a1fb 100644 --- a/nuxhash/gui/mining.py +++ b/nuxhash/gui/mining.py @@ -53,7 +53,7 @@ def __init__(self, parent, *args, devices=[], **kwargs): # Update balance periodically. self._Timer = wx.Timer(self) - self.Bind(wx.EVT_TIMER, lambda event: self._UpdateBalance(), self._Timer) + self.Bind(wx.EVT_TIMER, self._OnBalanceTimer, self._Timer) self._Timer.Start(milliseconds=BALANCE_UPDATE_MIN*60*1e3) # Add mining panel. @@ -130,6 +130,9 @@ def _OnClose(self): if self._Thread: self._Thread.stop() + def _OnBalanceTimer(self, event): + self._UpdateBalance() + def _UpdateBalance(self): address = self._Settings['nicehash']['wallet'] if check_bc(address): @@ -150,10 +153,10 @@ def OnStartStop(self, event): def _StartMining(self): pub.sendMessage('mining.start') self._StartStop.SetLabel('Stop Mining') - self._Thread = MiningThread(devices=self._Devices, - window=self, - settings=deepcopy(self._Settings), - benchmarks=deepcopy(self._Benchmarks)) + self._Thread = MiningThread( + devices=self._Devices, window=self, + settings=deepcopy(self._Settings), + benchmarks=deepcopy(self._Benchmarks)) self._Thread.start() def _StopMining(self): @@ -184,10 +187,10 @@ def __init__(self, parent, *args, **kwargs): self.Disable() self.AppendTextColumn('Algorithm', width=wx.COL_WIDTH_AUTOSIZE) self.AppendColumn( - wx.dataview.DataViewColumn('Devices', DeviceListRenderer(), - 1, align=wx.ALIGN_LEFT, - width=wx.COL_WIDTH_AUTOSIZE), - 'string') + wx.dataview.DataViewColumn('Devices', DeviceListRenderer(), + 1, align=wx.ALIGN_LEFT, + width=wx.COL_WIDTH_AUTOSIZE), + 'string') self.AppendTextColumn('Speed', width=wx.COL_WIDTH_AUTOSIZE) self.AppendTextColumn('Revenue') @@ -233,32 +236,28 @@ def __init__(self, *args, **kwargs): self._ColorDb = wx.ColourDatabase() def SetValue(self, value): - vendors = { - 'N': 'nvidia' - } + vendors = { 'N': 'nvidia' } self._Devices = [{ 'name': s[2:], 'vendor': vendors[s[0]] } for s in value.split(',')] return True def GetValue(self): - tags = { - 'nvidia': 'N' - } + tags = { 'nvidia': 'N' } return ','.join([f"{tags[device['vendor']]}:{device['name']}" for device in self._Devices]) def GetSize(self): boxes = [self.GetTextExtent(device['name']) for device in self._Devices] - return wx.Size((max(box.GetWidth() for box in boxes) - + DeviceListRenderer.CORNER_RADIUS*2), + RADIUS = DeviceListRenderer.CORNER_RADIUS + return wx.Size(max(box.GetWidth() for box in boxes) + RADIUS*2, (sum(box.GetHeight() for box in boxes) - + DeviceListRenderer.CORNER_RADIUS*2*len(boxes) - + DeviceListRenderer.CORNER_RADIUS*(len(boxes) - 1))) + + RADIUS*2*len(boxes) + RADIUS*(len(boxes) - 1))) def Render(self, cell, dc, state): position = cell.GetPosition() for device in self._Devices: box = self.GetTextExtent(device['name']) + RADIUS = DeviceListRenderer.CORNER_RADIUS if device['vendor'] == 'nvidia': color = self._ColorDb.Find('LIME GREEN') @@ -267,21 +266,16 @@ def Render(self, cell, dc, state): dc.SetBrush(wx.Brush(color)) dc.SetPen(wx.TRANSPARENT_PEN) shadeRect = wx.Rect( - position, - wx.Size(box.GetWidth() + DeviceListRenderer.CORNER_RADIUS*2, - box.GetHeight() + DeviceListRenderer.CORNER_RADIUS*2)) - dc.DrawRoundedRectangle(shadeRect, DeviceListRenderer.CORNER_RADIUS) + position, + wx.Size(box.GetWidth() + RADIUS*2, box.GetHeight() + RADIUS*2)) + dc.DrawRoundedRectangle(shadeRect, RADIUS) textRect = wx.Rect( - wx.Point(position.x + DeviceListRenderer.CORNER_RADIUS, - position.y + DeviceListRenderer.CORNER_RADIUS), - box) + wx.Point(position.x + RADIUS, position.y + RADIUS), box) self.RenderText(device['name'], 0, textRect, dc, state) - position = wx.Point(position.x, - (position.y + box.GetHeight() - + DeviceListRenderer.CORNER_RADIUS*2 - + DeviceListRenderer.CORNER_RADIUS)) + position = wx.Point( + position.x, (position.y + box.GetHeight() + RADIUS*2 + RADIUS)) return True def _DeviceToString(device): @@ -402,11 +396,10 @@ def _read_status(self): running_algorithms = self._assignments.values() speeds = {algorithm: algorithm.current_speeds() for algorithm in running_algorithms} - revenue = {algorithm: sum([payrates[multialgorithm] - *speeds[algorithm][i] - for i, multialgorithm - in enumerate(algorithm.algorithms)]) - for algorithm in running_algorithms} + revenue = {algorithm: sum([payrates[sub_algo]*speeds[algorithm][i] + for i, sub_algo + in enumerate(algorithm.algorithms)]) + for algorithm in running_algorithms} devices = {algorithm: [device for device, this_algorithm in self._assignments.items() if this_algorithm == algorithm] diff --git a/nuxhash/gui/settings.py b/nuxhash/gui/settings.py index b55bfdd..3c66739 100644 --- a/nuxhash/gui/settings.py +++ b/nuxhash/gui/settings.py @@ -30,38 +30,37 @@ def __init__(self, parent, *args, frame=None, **kwargs): def add_divider(sizer): sizer.Add(wx.StaticLine(self), wx.SizerFlags().Expand()) + def add_valign(sizer, window, sizerflags=wx.SizerFlags()): + sizer.Add(window, sizerflags.Align(wx.ALIGN_CENTER_VERTICAL)) + + def two_col_sizer(rows): + sizer = wx.FlexGridSizer(rows, 2, main.PADDING_PX, main.PADDING_PX) + sizer.AddGrowableCol(1) + return sizer + # Add basic setting controls. basicForm = wx.Window(self) sizer.Add(basicForm, wx.SizerFlags().Border(wx.ALL, main.PADDING_PX) .Expand()) - basicSizer = wx.FlexGridSizer(3, 2, main.PADDING_PX, main.PADDING_PX) - basicSizer.AddGrowableCol(1) + basicSizer = two_col_sizer(3) basicForm.SetSizer(basicSizer) - basicSizer.Add(wx.StaticText(basicForm, label='Wallet address'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(basicSizer, wx.StaticText(basicForm, label='Wallet address')) self._Wallet = AddressCtrl(basicForm, size=(-1, -1)) self.Bind(wx.EVT_TEXT, self.OnWalletChange, self._Wallet) - basicSizer.Add( - self._Wallet, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL).Expand()) + add_valign(basicSizer, self._Wallet, wx.SizerFlags().Expand()) - basicSizer.Add(wx.StaticText(basicForm, label='Worker name'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(basicSizer, wx.StaticText(basicForm, label='Worker name')) self._Worker = wx.TextCtrl(basicForm, size=(200, -1)) self.Bind(wx.EVT_TEXT, self.OnWorkerChange, self._Worker) - basicSizer.Add( - self._Worker, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(basicSizer, self._Worker) - basicSizer.Add(wx.StaticText(basicForm, label='Region'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(basicSizer, wx.StaticText(basicForm, label='Region')) self._Region = ChoiceByValue( basicForm, choices=REGIONS, fallbackChoice=settings.DEFAULT_SETTINGS['nicehash']['region']) self.Bind(wx.EVT_CHOICE, self.OnRegionChange, self._Region) - basicSizer.Add( - self._Region, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) - - add_divider(sizer) + add_valign(basicSizer, self._Region) # Add API key controls. apiCollapsible = wx.CollapsiblePane( @@ -73,32 +72,25 @@ def add_divider(sizer): apiPane = apiCollapsible.GetPane() apiPaneSizer = wx.BoxSizer(orient=wx.VERTICAL) apiPane.SetSizer(apiPaneSizer) - apiForm = wx.Window(apiPane) apiPaneSizer.Add(apiForm, wx.SizerFlags().Expand()) - apiFormSizer = wx.FlexGridSizer(3, 2, main.PADDING_PX, main.PADDING_PX) - apiFormSizer.AddGrowableCol(1) + apiFormSizer = two_col_sizer(3) apiForm.SetSizer(apiFormSizer) - apiFormSizer.Add(wx.StaticText(apiForm, label='Organization ID'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(apiFormSizer, wx.StaticText(apiForm, label='Organization ID')) self._Organization = wx.TextCtrl(apiForm, size=(-1, -1)) - apiFormSizer.Add(self._Organization, - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL).Expand()) + add_valign(apiFormSizer, self._Organization, wx.SizerFlags().Expand()) - apiFormSizer.Add(wx.StaticText(apiForm, label='API Key Code'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(apiFormSizer, wx.StaticText(apiForm, label='API Key Code')) self._ApiKey = wx.TextCtrl( apiForm, size=(-1, -1), style=wx.TE_PASSWORD) - apiFormSizer.Add(self._ApiKey, - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL).Expand()) + add_valign(apiFormSizer, self._ApiKey, wx.SizerFlags().Expand()) - apiFormSizer.Add(wx.StaticText(apiForm, label='API Secret Key Code'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(apiFormSizer, + wx.StaticText(apiForm, label='API Secret Key Code')) self._ApiSecret = wx.TextCtrl( apiForm, size=(-1, -1), style=wx.TE_PASSWORD) - apiFormSizer.Add(self._ApiSecret, - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL).Expand()) + add_valign(apiFormSizer, self._ApiSecret, wx.SizerFlags().Expand()) apiPaneSizer.AddSpacer(main.PADDING_PX) @@ -113,37 +105,31 @@ def add_divider(sizer): advancedForm = wx.Window(self) sizer.Add(advancedForm, wx.SizerFlags().Border(wx.ALL, main.PADDING_PX) .Expand()) - advancedSizer = wx.FlexGridSizer(3, 2, main.PADDING_PX, main.PADDING_PX) - advancedSizer.AddGrowableCol(1) + advancedSizer = two_col_sizer(3) advancedForm.SetSizer(advancedSizer) - advancedSizer.Add( - wx.StaticText(advancedForm, label='Update interval (secs)'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, + wx.StaticText(advancedForm, label='Update interval (secs)')) self._Interval = wx.SpinCtrl(advancedForm, size=(125, -1), min=10, max=300, initial=60) self.Bind(wx.EVT_SPINCTRL, self.OnIntervalChange, self._Interval) - advancedSizer.Add( - self._Interval, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, self._Interval) - advancedSizer.Add( - wx.StaticText(advancedForm, label='Profitability switch threshold (%)'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, + wx.StaticText(advancedForm, + label='Profitability switch threshold (%)')) self._Threshold = wx.SpinCtrl(advancedForm, size=(125, -1), min=1, max=50, initial=10) self.Bind(wx.EVT_SPINCTRL, self.OnThresholdChange, self._Threshold) - advancedSizer.Add( - self._Threshold, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, self._Threshold) - advancedSizer.Add( - wx.StaticText(advancedForm, label='Display units'), - wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, + wx.StaticText(advancedForm, label='Display units')) self._Units = ChoiceByValue( advancedForm, choices=UNITS, fallbackChoice=settings.DEFAULT_SETTINGS['gui']['units']) self.Bind(wx.EVT_CHOICE, self.OnUnitsChange, self._Units) - advancedSizer.Add( - self._Units, wx.SizerFlags().Align(wx.ALIGN_CENTER_VERTICAL)) + add_valign(advancedSizer, self._Units) sizer.AddStretchSpacer() diff --git a/nuxhash/settings.py b/nuxhash/settings.py index 6dfeb37..bc8d384 100644 --- a/nuxhash/settings.py +++ b/nuxhash/settings.py @@ -16,7 +16,7 @@ 'region': 'usa', 'api_organization': '', 'api_key': '', - 'api_code': '' + 'api_secret': '' }, 'switching': { 'interval': 60, @@ -46,7 +46,7 @@ def read_settings_from_file(fd): 'region': parser.get, 'api_organization': parser.get, 'api_key': parser.get, - 'api_code': parser.get + 'api_secret': parser.get }, 'switching': { 'interval': parser.getint, diff --git a/nuxhash/version.py b/nuxhash/version.py index 6f6463d..70e6541 100644 --- a/nuxhash/version.py +++ b/nuxhash/version.py @@ -1 +1,2 @@ __version__ = '1.0.0b2' +__copyright__ = 'Copyright © 2018-2019\nRyan Young'