Added some docstrings, changed doubled code in MainPresenter.

This commit is contained in:
Zloooy 2019-01-31 21:19:44 +03:00
parent aa3c795f9a
commit 6b8b65d127
5 changed files with 34 additions and 31 deletions

View File

@ -6,6 +6,7 @@ from PyQt5.Qt import *
from address_generation.IpGenerator import IpGenerator from address_generation.IpGenerator import IpGenerator
from storage.JSONStorage import JSONStorage from storage.JSONStorage import JSONStorage
class MainPresenter: class MainPresenter:
def __init__(self, ui): def __init__(self, ui):
self.ui = ui self.ui = ui
@ -30,33 +31,30 @@ class MainPresenter:
thread.exit_signal.connect(self.on_thread_exit) thread.exit_signal.connect(self.on_thread_exit)
thread.start() thread.start()
def on_thread_exit(self, is_last): def on_thread_exit(self):
if is_last:
self.isScanEnabled = False
self.ui.startButton.setText("Start")
self.storage.save()
return
count = 0 count = 0
for thr in self.threads: for thr in self.threads:
if thr.is_running: if thr.is_running:
count = count + 1 count = count + 1
if count == len(self.threads):
self.on_end_scanning()
self.setCurrentThreadsLabel(count) self.setCurrentThreadsLabel(count)
def on_end_scanning(self):
self.isScanEnabled = False
self.ui.startButton.setText("Start")
self.storage.save()
def stopScan(self): def stopScan(self):
self.isScanEnabled = False self.isScanEnabled = False
for thread in self.threads: for thread in self.threads:
thread.exit() thread.exit()
thread.is_running = False for thread in self.threads:
count = 0 thread.wait()
is_last_thread = False self.on_end_scanning()
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)
self.threads.clear() self.threads.clear()
self.ui.currentThreadsLabel.setText("0") self.ui.currentThreadsLabel.setText("0")
def setLogText(self, string): def setLogText(self, string):
self.ui.dataText.append("[" + str(datetime.datetime.now()) + "] " + str(string)) self.ui.dataText.append("[" + str(datetime.datetime.now()) + "] " + str(string))
@ -67,7 +65,7 @@ class MainPresenter:
class ScanThread(QThread): class ScanThread(QThread):
signal = pyqtSignal(str) signal = pyqtSignal(str)
exit_signal = pyqtSignal(bool) exit_signal = pyqtSignal()
def __init__(self, ip_generator, ports, timeout, presenter, parent=None): def __init__(self, ip_generator, ports, timeout, presenter, parent=None):
QThread.__init__(self, parent) QThread.__init__(self, parent)
@ -80,7 +78,6 @@ class ScanThread(QThread):
self.is_running = True self.is_running = True
def run(self): def run(self):
print("Thread sterted")
while True: while True:
scan_address = self.ip_generator.get_next_address(self.previous_address) scan_address = self.ip_generator.get_next_address(self.previous_address)
if not scan_address: if not scan_address:
@ -88,18 +85,10 @@ class ScanThread(QThread):
self.previous_address = scan_address self.previous_address = scan_address
scan_result = self.coreModel.scan_address(scan_address) scan_result = self.coreModel.scan_address(scan_address)
self.presenter.storage.put_responce(scan_address, scan_result) self.presenter.storage.put_responce(scan_address, scan_result)
if scan_result==0: if scan_result == 0:
self.signal.emit( '%s has open port: %s' % scan_address) self.signal.emit('%s has open port: %s' % scan_address)
else: 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 self.is_running = False
print("Thread stopped") self.exit_signal.emit()
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(1) self.exit(1)

View File

@ -2,8 +2,14 @@ from abc import abstractmethod, ABC
class AbstractAddressGenerator(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 @classmethod
@abstractmethod @abstractmethod
def get_next_address(self, previous_address, **kwargs): 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 pass

View File

@ -2,13 +2,17 @@ from abc import ABC, abstractmethod
class AbstractParser(ABC): class AbstractParser(ABC):
'''The class describes fields parsing mechanisms'''
@classmethod @classmethod
@abstractmethod @abstractmethod
def parse_address_field(self, field): def parse_address_field(self, field):
'''In address field can be plased any text, describing address of
scanning target'''
pass pass
@classmethod @classmethod
@abstractmethod @abstractmethod
def parse_port_field(self, field): def parse_port_field(self, field):
'''In port field only numbers, whitespaces, comma and '-' allowed'''
pass pass

View File

@ -2,13 +2,18 @@ from abc import ABC, abstractmethod
class AbstractScanner(ABC): class AbstractScanner(ABC):
'''The class is used by one thread to scan targets'''
@classmethod @classmethod
@abstractmethod @abstractmethod
def __init__(self, **kwargs): def __init__(self, **kwargs):
'''In this method you can init some
reusable resources needed for scan'''
pass pass
@classmethod @classmethod
@abstractmethod @abstractmethod
def scan_address(self, address): 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 pass

View File

@ -3,5 +3,4 @@ import os
fil = __file__[:__file__.rindex(os.sep)] fil = __file__[:__file__.rindex(os.sep)]
print(fil)
sys.path.insert(0,fil) sys.path.insert(0,fil)