From 6b8b65d127a43d9eab592f1631f904e40f57de0f Mon Sep 17 00:00:00 2001 From: Zloooy Date: Thu, 31 Jan 2019 21:19:44 +0300 Subject: [PATCH] Added some docstrings, changed doubled code in MainPresenter. --- MainPresenter.py | 47 +++++++------------ .../AbstractAddressGenerator.py | 8 +++- address_generation/AbstractParser.py | 4 ++ network_scan/AbstractScanner.py | 5 ++ network_scan/__init__.py | 1 - 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/MainPresenter.py b/MainPresenter.py index 95f8dbb..e8f7f57 100644 --- a/MainPresenter.py +++ b/MainPresenter.py @@ -6,6 +6,7 @@ from PyQt5.Qt import * from address_generation.IpGenerator import IpGenerator from storage.JSONStorage import JSONStorage + class MainPresenter: def __init__(self, ui): self.ui = ui @@ -30,33 +31,30 @@ class MainPresenter: thread.exit_signal.connect(self.on_thread_exit) thread.start() - def on_thread_exit(self, is_last): - if is_last: - self.isScanEnabled = False - self.ui.startButton.setText("Start") - self.storage.save() - return + def on_thread_exit(self): count = 0 for thr in self.threads: if thr.is_running: count = count + 1 + if count == len(self.threads): + self.on_end_scanning() self.setCurrentThreadsLabel(count) + def on_end_scanning(self): + self.isScanEnabled = False + self.ui.startButton.setText("Start") + self.storage.save() + def stopScan(self): self.isScanEnabled = False for thread in self.threads: thread.exit() - thread.is_running = False - count = 0 - is_last_thread = False - for i in self.threads: - if not i.is_running: - count += 1 - if count == len(self.threads): - is_last_thread = True - thread.exit_signal.emit(is_last_thread) + for thread in self.threads: + thread.wait() + self.on_end_scanning() self.threads.clear() self.ui.currentThreadsLabel.setText("0") + def setLogText(self, string): self.ui.dataText.append("[" + str(datetime.datetime.now()) + "] " + str(string)) @@ -67,7 +65,7 @@ class MainPresenter: class ScanThread(QThread): signal = pyqtSignal(str) - exit_signal = pyqtSignal(bool) + exit_signal = pyqtSignal() def __init__(self, ip_generator, ports, timeout, presenter, parent=None): QThread.__init__(self, parent) @@ -80,7 +78,6 @@ class ScanThread(QThread): self.is_running = True def run(self): - print("Thread sterted") while True: scan_address = self.ip_generator.get_next_address(self.previous_address) if not scan_address: @@ -88,18 +85,10 @@ class ScanThread(QThread): self.previous_address = scan_address scan_result = self.coreModel.scan_address(scan_address) self.presenter.storage.put_responce(scan_address, scan_result) - if scan_result==0: - self.signal.emit( '%s has open port: %s' % scan_address) + if scan_result == 0: + self.signal.emit('%s has open port: %s' % scan_address) else: - self.signal.emit( '%s has closed port: %s' % scan_address) + self.signal.emit('%s has closed port: %s' % scan_address) self.is_running = False - print("Thread stopped") - count = 0 - is_last_thread = False - for i in self.presenter.threads: - if not i.isRunning(): - count += 1 - if count == len(self.presenter.threads): - is_last_thread = True - self.exit_signal.emit(is_last_thread) + self.exit_signal.emit() self.exit(1) diff --git a/address_generation/AbstractAddressGenerator.py b/address_generation/AbstractAddressGenerator.py index ea1ead1..1e8bfbc 100644 --- a/address_generation/AbstractAddressGenerator.py +++ b/address_generation/AbstractAddressGenerator.py @@ -2,8 +2,14 @@ from abc import abstractmethod, ABC class AbstractAddressGenerator(ABC): - + '''The class describes addess generation mechanism. + In __init__ method it should get results of parsing fields + and then it returns addresses.''' @classmethod @abstractmethod def get_next_address(self, previous_address, **kwargs): + '''Address - an only, indivisible object, that describes single scan + target address. This method should return next address to scan based on + previous scanned address and result of scanning previous address, that + can be placed in kwargs.''' pass diff --git a/address_generation/AbstractParser.py b/address_generation/AbstractParser.py index 36bd85f..e492982 100644 --- a/address_generation/AbstractParser.py +++ b/address_generation/AbstractParser.py @@ -2,13 +2,17 @@ from abc import ABC, abstractmethod class AbstractParser(ABC): + '''The class describes fields parsing mechanisms''' @classmethod @abstractmethod def parse_address_field(self, field): + '''In address field can be plased any text, describing address of + scanning target''' pass @classmethod @abstractmethod def parse_port_field(self, field): + '''In port field only numbers, whitespaces, comma and '-' allowed''' pass diff --git a/network_scan/AbstractScanner.py b/network_scan/AbstractScanner.py index 53d11ae..72716f6 100644 --- a/network_scan/AbstractScanner.py +++ b/network_scan/AbstractScanner.py @@ -2,13 +2,18 @@ from abc import ABC, abstractmethod class AbstractScanner(ABC): + '''The class is used by one thread to scan targets''' @classmethod @abstractmethod def __init__(self, **kwargs): + '''In this method you can init some + reusable resources needed for scan''' pass @classmethod @abstractmethod def scan_address(self, address): + '''This method should contain scanning process of given address. All + items returned will be passed to AbstractStorage and ui''' pass diff --git a/network_scan/__init__.py b/network_scan/__init__.py index f882043..d98799d 100644 --- a/network_scan/__init__.py +++ b/network_scan/__init__.py @@ -3,5 +3,4 @@ import os fil = __file__[:__file__.rindex(os.sep)] -print(fil) sys.path.insert(0,fil)