Skip to content
Snippets Groups Projects
Commit 16b9bc25 authored by Daniel Meißner's avatar Daniel Meißner Committed by Daniel Meißner
Browse files

gui: added information about free disk space and needed space

parent 7d509f43
No related branches found
No related tags found
No related merge requests found
# -*- encoding: utf-8 -*-
import os
class Disk(object):
"""
Class for disk operations.
"""
@staticmethod
def free_disk_space(path):
"""
Calculate free disk space for given path.
Returned available_bytes or None when path not exists or writable.
"""
if Disk.path_writeble(path):
stats = os.statvfs(path)
available_bytes = (stats.f_bavail * stats.f_frsize)
return available_bytes
else:
return None
@staticmethod
def path_writeble(path):
"""
Check if a given path is writable for user.
"""
if os.path.exists(path):
return os.access(path, os.W_OK)
elif os.access(os.path.dirname(path), os.W_OK) and \
os.access(path, os.W_OK):
return True
else:
return False
@staticmethod
def sizeof_fmt(num, suffix="B"):
"""
Convert number of bytes into human readable format.
Source: https://web.archive.org/web/20111010015624/http://blogmag.net/blog/read/38/Print_human_readable_file_size
"""
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, "Yi", suffix)
......@@ -10,6 +10,7 @@ from PyQt5 import QtCore, QtGui, QtWidgets, uic
from lib.hardware import Hardware
from lib.su import Su
from lib.disk import Disk
class SuGui(QtWidgets.QMainWindow):
"""
......@@ -36,6 +37,29 @@ class SuGui(QtWidgets.QMainWindow):
# menubar
self.actionQuit.triggered.connect(self.close_button)
self.actionAbout.triggered.connect(self.about_button)
# edit lines
self.lineEdit_dump_file.editingFinished. \
connect(self.check_dump_file_path)
# radio buttons
self.radio_buttons = {
# samp_rate
self.rb_1mhz: 1e6,
self.rb_2mhz: 2e6,
self.rb_3mhz: 3e6,
# time to measure in seconds
self.rb_1m: 60,
self.rb_5m: 300,
self.rb_10m: 600,
self.rb_1h: 3600,
self.rb_24h: 86400
}
# add released actions to radio buttons
for button in self.radio_buttons.keys():
button.released.connect(self._update_status_label)
# labels
self._update_status_label()
self.label_file_overwrite.setText("change path")
def start_button(self):
"""
......@@ -43,6 +67,8 @@ class SuGui(QtWidgets.QMainWindow):
"""
if self._check_dependencies_before_start_capture():
print self._extract_selected_hardware_device()
print self.lineEdit_dump_file.text()
self._get_checked_radio_buttons()
def stop_button(self):
"""
......@@ -75,6 +101,22 @@ class SuGui(QtWidgets.QMainWindow):
"Contact: daniel.meissner@smail.inf.h-brs.de\n" \
"Source: https://git.fslab.de/dmeiss2s/su")
def check_dump_file_path(self):
"""
Update labels for writable state and free disk space in path.
"""
free_disk_space = Disk.free_disk_space(self.lineEdit_dump_file.text())
if free_disk_space == None:
self.label_disk_space.setText("")
self.label_file_overwrite.setText("not writable")
self.label_file_overwrite.setStyleSheet('color: red')
else:
self.label_file_overwrite.setText("")
self.label_disk_space.setText("Free disk space:\n" \
"" + Disk.sizeof_fmt(free_disk_space))
if free_disk_space - self._calculate_needed_disk_space() < 0:
self.label_disk_space.setStyleSheet('color: red')
def _get_default_dump_file_path(self):
"""
......@@ -131,3 +173,31 @@ class SuGui(QtWidgets.QMainWindow):
return False
else:
return True
def _update_status_label(self):
needed_disk_space = Disk.sizeof_fmt(self._calculate_needed_disk_space())
self.label_status. \
setText("Needed disk space:\n" \
"" + needed_disk_space)
def _calculate_needed_disk_space(self):
"""
Calculate needed disk space based on current check box status. Every
sample needs 8 byte of disk space (32 bit I, 32 bit Q).
Returned integer value.
"""
time, samp_rate = self._get_checked_radio_buttons().values()
return int((time*samp_rate) * 8)
def _get_checked_radio_buttons(self):
"""
Return a list of all checked radio buttons.
"""
checked_buttons = {}
for radio_button, value in self.radio_buttons.iteritems():
if radio_button.isChecked():
checked_buttons[radio_button] = value
return checked_buttons
......@@ -93,6 +93,9 @@
<property name="text">
<string extracomment="60">1 min</string>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
<widget class="QRadioButton" name="rb_5m">
<property name="geometry">
......@@ -123,7 +126,7 @@
<bool>true</bool>
</property>
</widget>
<widget class="QRadioButton" name="rb_1h_2">
<widget class="QRadioButton" name="rb_1h">
<property name="geometry">
<rect>
<x>10</x>
......@@ -141,8 +144,11 @@
<property name="checked">
<bool>false</bool>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
<widget class="QRadioButton" name="rb_1h">
<widget class="QRadioButton" name="rb_24h">
<property name="geometry">
<rect>
<x>10</x>
......@@ -154,6 +160,9 @@
<property name="text">
<string extracomment="864000">24 h</string>
</property>
<property name="autoExclusive">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QPushButton" name="button_stop">
......@@ -304,6 +313,9 @@
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
<widget class="QGroupBox" name="box_hardware">
<property name="geometry">
......@@ -414,8 +426,8 @@
<tabstop>rb_1m</tabstop>
<tabstop>rb_5m</tabstop>
<tabstop>rb_10m</tabstop>
<tabstop>rb_1h_2</tabstop>
<tabstop>rb_1h</tabstop>
<tabstop>rb_24h</tabstop>
<tabstop>lineEdit_dump_file</tabstop>
<tabstop>comboBox_hardware</tabstop>
<tabstop>button_start</tabstop>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment