1
0
Fork 0
mirror of https://github.com/dbarzin/pandora-box.git synced 2025-07-19 21:39:40 +02:00

code quality

This commit is contained in:
dbarzin 2023-02-25 11:19:37 +01:00
parent 73f5d0c5d3
commit 49a1f2e3af

View file

@ -33,6 +33,7 @@ import psutil
import pypandora import pypandora
class PandoraBox: class PandoraBox:
"""The PandoraBox class""" """The PandoraBox class"""
@ -83,7 +84,6 @@ class PandoraBox:
# Curses # Curses
self.has_curses = config_parser['DEFAULT']['CURSES'].lower() == "true" self.has_curses = config_parser['DEFAULT']['CURSES'].lower() == "true"
# ---------------------------------------------------------- # ----------------------------------------------------------
def _human_readable_size(self, size, decimal_places=1): def _human_readable_size(self, size, decimal_places=1):
@ -94,7 +94,6 @@ class PandoraBox:
size /= 1024.0 size /= 1024.0
return f"{size:.{decimal_places}f}{unit}" return f"{size:.{decimal_places}f}{unit}"
# ----------------------------------------------------------- # -----------------------------------------------------------
# Image Screen # Image Screen
# ----------------------------------------------------------- # -----------------------------------------------------------
@ -119,13 +118,12 @@ class PandoraBox:
# display image # display image
if "*" in image: if "*" in image:
# slide show # slide show
os.system(f"fim -qa -c 'while(1){{display;sleep 1;next;}}' {image} "\ os.system(f"fim -qa -c 'while(1){{display;sleep 1;next;}}' {image} "
"</dev/null 2>/dev/null >/dev/null &") "</dev/null 2>/dev/null >/dev/null &")
else: else:
# only one image # only one image
os.system(f"fim -qa {image} </dev/null 2>/dev/null >/dev/null &") os.system(f"fim -qa {image} </dev/null 2>/dev/null >/dev/null &")
# ----------------------------------------------------------- # -----------------------------------------------------------
def wait_mouse_click(self): def wait_mouse_click(self):
@ -274,6 +272,7 @@ class PandoraBox:
) )
logs = [] logs = []
def _log(self, msg): def _log(self, msg):
"""log something""" """log something"""
if self.has_curses: if self.has_curses:
@ -334,18 +333,18 @@ class PandoraBox:
def _log_device_info(self, dev): def _log_device_info(self, dev):
"""Log device information""" """Log device information"""
logging.info( logging.info(
'device_name="%s", ' \ 'device_name="%s", '
'path_id="%s", ' \ 'path_id="%s", '
'bus system="%s", ' \ 'bus system="%s", '
'USB_driver="%s", ' \ 'USB_driver="%s", '
'device_type="%s", ' \ 'device_type="%s", '
'device_usage="%s", ' \ 'device_usage="%s", '
'partition type="%s", ' \ 'partition type="%s", '
'fs_type="%s", ' \ 'fs_type="%s", '
'partition_label="%s", ' \ 'partition_label="%s", '
'device_model="%s", ' \ 'device_model="%s", '
'model_id="%s", ' \ 'model_id="%s", '
'serial_short="%s", '\ 'serial_short="%s", '
'serial="%s"', 'serial="%s"',
dev.get("DEVNAME"), dev.get("DEVNAME"),
dev.get("ID_PATH"), dev.get("ID_PATH"),
@ -366,8 +365,24 @@ class PandoraBox:
# pandora # pandora
# ----------------------------------------------------------- # -----------------------------------------------------------
def scan(self, used): def scan(self):
"""Scan a mount point with Pandora""" """Scan devce with pypandora"""
# get device size
try:
statvfs = os.statvfs(self.mount_point)
except Exception as ex:
self._log(f"error={ex}")
logging.info("An exception was thrown!", exc_info=True)
if not self.has_curses:
self.display_image("ERROR")
return "ERROR"
self._print_size(self._human_readable_size(statvfs.f_frsize * statvfs.f_blocks))
self._print_used(self._human_readable_size(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)))
self._human_readable_size(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
used = statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)
# scan device
self.infected_files = [] self.infected_files = []
scanned = 0 scanned = 0
file_count = 0 file_count = 0
@ -405,14 +420,14 @@ class PandoraBox:
file_scan_end_time = time.time() file_scan_end_time = time.time()
self._log( self._log(
f'Scan {file} '\ f'Scan {file} '
f'[{self._human_readable_size(file_size)}] '\ f'[{self._human_readable_size(file_size)}] '
'-> '\ '-> '
f'{status} ({int(file_scan_end_time - file_scan_start_time)}s)') f'{status} ({int(file_scan_end_time - file_scan_start_time)}s)')
logging.info( logging.info(
f'file="{file}", '\ f'file="{file}", '
f'size="{file_size}", '\ f'size="{file_size}", '
f'status="{status}"", '\ f'status="{status}"", '
f'duration="{int(file_scan_end_time - file_scan_start_time)}"') f'duration="{int(file_scan_end_time - file_scan_start_time)}"')
scanned += os.path.getsize(full_path) scanned += os.path.getsize(full_path)
file_count += 1 file_count += 1
@ -432,14 +447,14 @@ class PandoraBox:
self._log("Scan done in %ds, %d files scanned, %d files infected" % self._log("Scan done in %ds, %d files scanned, %d files infected" %
((time.time() - scan_start_time), file_count, len(self.infected_files))) ((time.time() - scan_start_time), file_count, len(self.infected_files)))
logging.info( logging.info(
f'duration="{int(time.time() - scan_start_time)}", '\ f'duration="{int(time.time() - scan_start_time)}", '
f'files_scanned="{file_count}", '\ f'files_scanned="{file_count}", '
f'files_infected="{len(self.infected_files)}"') f'files_infected="{len(self.infected_files)}"')
return "CLEAN" return "CLEAN"
# -------------------------------------- # --------------------------------------
def wait_device(self): def wait(self):
"""Wait for insert of remove of USB device""" """Wait for insert of remove of USB device"""
# Loop # Loop
context = pyudev.Context() context = pyudev.Context()
@ -467,8 +482,7 @@ class PandoraBox:
else: else:
# display device type # display device type
self._print_fslabel(self.device.get("ID_FS_LABEL")) self._print_fslabel(self.device.get("ID_FS_LABEL"))
self._print_fstype(self.device.get("ID_PART_TABLE_TYPE") self._print_fstype(self.device.get("ID_PART_TABLE_TYPE") + " " + self.device.get("ID_FS_TYPE"))
+ " " + self.device.get("ID_FS_TYPE"))
self._print_model(self.device.get("ID_MODEL")) self._print_model(self.device.get("ID_MODEL"))
self._print_serial(self.device.get("ID_SERIAL_SHORT")) self._print_serial(self.device.get("ID_SERIAL_SHORT"))
return "INSERTED" return "INSERTED"
@ -511,23 +525,6 @@ class PandoraBox:
# -------------------------------------- # --------------------------------------
def scan_device(self):
"""Scan devce with pypandora"""
try:
statvfs=os.statvfs(self.mount_point)
except Exception as ex :
self._log(f"error={ex}")
logging.info("An exception was thrown!", exc_info=True)
if not self.has_curses:
self.display_image("WAIT")
return "WAIT"
self._print_size(self._human_readable_size(statvfs.f_frsize * statvfs.f_blocks))
self._print_used(
self._human_readable_size(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)))
return self.scan(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
# --------------------------------------
def error(self): def error(self):
""" Display error message """ """ Display error message """
if not self.has_curses: if not self.has_curses:
@ -607,11 +604,11 @@ class PandoraBox:
case "START": case "START":
return self.startup() return self.startup()
case "WAIT": case "WAIT":
return self.wait_device() return self.wait()
case "INSERTED": case "INSERTED":
return self.mount() return self.mount()
case "SCAN": case "SCAN":
return self.scan_device() return self.scan()
case "CLEAN": case "CLEAN":
return self.clean() return self.clean()
case "ERROR": case "ERROR":
@ -633,10 +630,12 @@ class PandoraBox:
finally: finally:
self._end_curses() self._end_curses()
def main(_): def main(_):
"""Main entry point""" """Main entry point"""
pandora_box = PandoraBox() pandora_box = PandoraBox()
pandora_box.main() pandora_box.main()
if __name__ == "__main__": if __name__ == "__main__":
wrapper(main) wrapper(main)