Repository: aristocratos/bpytop
File: bpytop.py (Python)

Code before:
		filtering: Tuple = ()
		filter_exclude: bool = False
		io_string: str
		u_percent: int
		disk_list: List[str] = []
		fstab_filter: List[str] = []
		cls.disks = {}

		if CONFIG.disks_filter:
			if CONFIG.disks_filter.startswith("exclude="):
				filter_exclude = True
				filtering = tuple(v.strip() for v in CONFIG.disks_filter.replace("exclude=", "").strip().split(","))
			else:
				filtering = tuple(v.strip() for v in CONFIG.disks_filter.strip().split(","))

		try:
			io_counters = psutil.disk_io_counters(perdisk=SYSTEM == "Linux", nowrap=True)
		except ValueError as e:
			if not cls.io_error:
				cls.io_error = True
				errlog.error(f'Non fatal error during disk io collection!')
				if psutil.version_info[0] < 5 or (psutil.version_info[0] == 5 and psutil.version_info[1] < 7):
					errlog.error(f'Caused by outdated psutil version.')
				errlog.exception(f'{e}')
			io_counters = None
		try:
			with open('/etc/fstab','r') as fstab:
				for line in fstab:
					line = line.strip()
					if line and not line.startswith('#'):
						mount_data = (line.split())
						if mount_data[2].lower() != "swap":
							fstab_filter += [mount_data[1]]
		except IOError:
			pass

		for disk in psutil.disk_partitions(CONFIG.use_fstab):
			disk_io = None
			io_string = ""
			if CONFIG.use_fstab and disk.mountpoint not in fstab_filter:
				continue
			disk_name = disk.mountpoint.rsplit('/', 1)[-1] if not disk.mountpoint == "/" else "root"
			#while disk_name in disk_list: disk_name += "_"
			disk_list += [disk_name]
			if cls.excludes and disk.fstype in cls.excludes:
				continue
			if filtering and ((not filter_exclude and not disk.mountpoint in filtering) or (filter_exclude and disk.mountpoint in filtering)):
				continue
			#elif filtering and disk_name.endswith(filtering)
			if SYSTEM == "MacOS" and disk.mountpoint == "/private/var/vm":
				continue

Review comment (performance):
Should probably add some conditions to this block since it doesn't need to run if `CONFIG.use_fstab == False` and also if `SYSTEM == "MacOS"` since OsX don't use `/etc/fstab`.

For performance you could possibly store `fstab_filter` as a class variable also and only run the code block if the list is empty. `/etc/fstab` should be unlikely to change right?

Code after:
		dev_name: str
		disk_name: str
		filtering: Tuple = ()
		filter_exclude: bool = False
		io_string: str
		u_percent: int
		disk_list: List[str] = []
		cls.disks = {}

		if CONFIG.disks_filter:
			if CONFIG.disks_filter.startswith("exclude="):
				filter_exclude = True
				filtering = tuple(v.strip() for v in CONFIG.disks_filter.replace("exclude=", "").strip().split(","))
			else:
				filtering = tuple(v.strip() for v in CONFIG.disks_filter.strip().split(","))

		try:
			io_counters = psutil.disk_io_counters(perdisk=SYSTEM == "Linux", nowrap=True)
		except ValueError as e:
			if not cls.io_error:
				cls.io_error = True
				errlog.error(f'Non fatal error during disk io collection!')
				if psutil.version_info[0] < 5 or (psutil.version_info[0] == 5 and psutil.version_info[1] < 7):
					errlog.error(f'Caused by outdated psutil version.')
				errlog.exception(f'{e}')
			io_counters = None

		if CONFIG.use_fstab and not cls.fstab_filter:
			try:
				with open('/etc/fstab','r') as fstab:
					for line in fstab:
						line = line.strip()
						if line and not line.startswith('#'):
							mount_data = (line.split())
							if mount_data[2].lower() != "swap":
								cls.fstab_filter += [mount_data[1]]
				errlog.debug(f'new fstab_filter set : {cls.fstab_filter}')
			except IOError:
				CONFIG.use_fstab = False
				errlog.debug(f'Error reading fstab, use_fstab flag reset to {CONFIG.use_fstab}')
		if not CONFIG.use_fstab and cls.fstab_filter:
			cls.fstab_filter = []
			errlog.debug(f'use_fstab flag has been turned to {CONFIG.use_fstab}, fstab_filter cleared')

		for disk in psutil.disk_partitions(CONFIG.use_fstab):
			disk_io = None
			io_string = ""
			if CONFIG.use_fstab and disk.mountpoint not in cls.fstab_filter:
				continue
			disk_name = disk.mountpoint.rsplit('/', 1)[-1] if not disk.mountpoint == "/" else "root"
			#while disk_name in disk_list: disk_name += "_"