You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rizzlehash/BF_GUI.py

292 lines
9.5 KiB

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2015 Matthieu DUVAL, Rudy DUCHE
# This file is part of Foobar.
# Foobar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>
import os
import platform
import locale
from datetime import datetime
import time
import BF_Utils
from BF_Constants import *
class GUI():
def __init__(self):
self.guiStartTime = str(datetime.now().time().strftime("%H:%M:%S"))
self.guiTime=time.time()
self.appName = APPLICATION_CONSTANTS.NAME+ " V" +APPLICATION_CONSTANTS.VERSION
self.headerLines = self.prepareHeader()
return
def prepareHeader(self):
lines = list()
info = list()
lenGui = len(self.appName)
info.append("System family : " + str(platform.system()))
info.append("System release : " + str(platform.release()))
info.append("Processor Core : " + str(os.cpu_count()))
info.append("App Started at : "+self.guiStartTime)
for value in info:
if len(value) > lenGui:
lenGui = len(value)
lines.append(self.separationLineCreator("+","+","-",lenGui+2))
lines.append(self.centredLine("| "," |"," ",self.appName,lenGui))
lines.append(self.separationLineCreator("+","+","-",lenGui+2))
for content in info:
lines.append(str(self.leftAlignLine("| "," |"," ",content,lenGui)))
lines.append(self.separationLineCreator("+","+","-",lenGui+2))
return lines
def separationLineCreator(self,leftBorder,rightBorder,separator,lenGui):
listSeparationLine = list()
listSeparationLine.append(leftBorder)
for i in range (0, lenGui):
listSeparationLine.append(separator)
listSeparationLine.append(rightBorder)
separationLine = "".join(listSeparationLine)
return separationLine
def centredLine(self,leftBorder,rightBorder,offsetChar,value,lenGui):
lenSpace = (lenGui - len(value))/2
if lenSpace.is_integer():
lenSpaceLeft = int(lenSpace)
lenSpaceRight = int(lenSpace)
else :
lenSpaceLeft = round(lenSpace)
if( round(lenSpace) < lenSpace ):
lenSpaceRight = round(lenSpace)+ 1
else:
lenSpaceRight = round(lenSpace)- 1
listSpaceLeft = list()
for i in range(0,lenSpaceLeft):
listSpaceLeft.append(offsetChar)
listSpaceRight = list()
for i in range(0,lenSpaceRight):
listSpaceRight.append(offsetChar)
centeredLine = leftBorder+"".join(listSpaceLeft) + value + "".join(listSpaceRight)+rightBorder
return centeredLine
def tableLine(self,offsetChar,value1,value2,lenMaxValue1):
tableLine = ""
listSpaceSeparator = list()
for i in range(0,lenMaxValue1 - len(value1)):
listSpaceSeparator.append(offsetChar)
tableLine = value1+"".join(listSpaceSeparator)+value2
return tableLine
def leftAlignLine(self,leftBorder,rightBorder,offsetChar,value,lenGui):
lenSpaceRight = lenGui - len(value)
listSpaceRight = list()
for i in range(0,lenSpaceRight):
listSpaceRight.append(offsetChar)
leftAlignLine = leftBorder+value+"".join(listSpaceRight)+rightBorder
return leftAlignLine
def clearScreen(self):
if platform.system() == 'Linux':
os.system('clear')
else:
os.system('cls')
def setLocale(self):
if platform.system() == 'Linux':
locale.setlocale(locale.LC_ALL, 'french')
else:
locale.setlocale(locale.LC_ALL, 'french_france')
def displayHeader(self):
self.clearScreen()
for line in self.headerLines:
print(line)
def displayProcessPerfs(self, host, processName, WordsSeconds, curentWord):
self.setLocale()
WordsSeconds = locale.format('%d', WordsSeconds, grouping=True)
print("Host : " + host )
print("\t"+ processName+" : " + '{:>10}'.format(WordsSeconds) + " Word/s \tCurrent word: " + curentWord)
return
def displayProcessPerfsList(self, host, processName, WordsSeconds, curentWord):
self.setLocale()
print("Host : " + host )
for i in range(0,len(processName)):
WordsSeconds[i] = locale.format('%d', WordsSeconds[i], grouping=True)
print("\t"+ processName[i]+" : " + '{:>10}'.format(WordsSeconds[i]) + " Word/s \tCurrent word: " + curentWord[i])
return
def displayTotalPerfs(self, host,WordsSeconds):
self.setLocale()
WordsSeconds = locale.format('%d', WordsSeconds, grouping=True)
print("Word Per Second : " +'{:>10}'.format(WordsSeconds)+ " Word/s")
def displayError(self, host,errorCode, errorMessage):
print("Error at : " + host)
print("\t"+errorCode + " : " + errorMessage)
def displayMessage(self,host,message):
print("From " + host + ": " + message )
def displayResult(self,value,timeSeconds):
if(time.time() - timeSeconds) > 60 :
seconds = time.time() - timeSeconds
d,h,m,s = self.formatTime(seconds)
if d != 0:
print("Found ! : " + value +" in " + "%d:%02d:%02d:%02d" % (d,h, m, s))
elif h != 0 :
print("Found ! : " + value +" in " + "%d:%02d:%02d" % (h, m, s))
else:
print("Found ! : " + value +" in " + "%d:%02d" % (m, s))
else:
print("Found ! : " + value +" in " + str(round(time.time() - timeSeconds,2)) + " second")
return
def displayCopyright(self):
lines = list()
info = list()
lenGui = 0
info.append("")
info.append(APPLICATION_CONSTANTS.NAME + " Copyright © " + APPLICATION_CONSTANTS.YEAR + " " + APPLICATION_CONSTANTS.AUTHORS)
info.append("This program comes with ABSOLUTELY NO WARRANTY")#; for details type `show w'.")
info.append("This is free software, and you are welcome to redistribute it")
info.append("under certain conditions. Please read the COPYING and README")#; type `show c' for details.")
info.append("")
for value in info:
if len(value) > lenGui:
lenGui = len(value)
lines.append(self.separationLineCreator("*","*","*",lenGui+2))
for content in info:
lines.append(str(self.leftAlignLine("* "," *"," ",content,lenGui)))
lines.append(self.separationLineCreator("*","*","*",lenGui+2))
self.clearScreen()
for line in lines:
print(line)
print()
input("press any key to continue...")
def formatTime(self,seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
d ,h = divmod(h, 24)
return d,h,m,s
def displayShortHelp(self):
print()
print("Usage : " )
leftTab = list()
leftTab.append("\t"+PARAMS_CONSTANTS.LONG_PARAMS.HELP.value+", "+ PARAMS_CONSTANTS.SHORT_PARAMS.HELP.value)
leftTab.append("\t"+PARAMS_CONSTANTS.LONG_PARAMS.HASH.value+"=[passwordSearch]")
leftTab.append("\t"+PARAMS_CONSTANTS.LONG_PARAMS.VERBOSE.value+", "+ PARAMS_CONSTANTS.SHORT_PARAMS.VERBOSE.value)
leftTab.append("\t"+PARAMS_CONSTANTS.LONG_PARAMS.MODE.value+", " +PARAMS_CONSTANTS.SHORT_PARAMS.MODE.value)
rightTab = list()
rightTab.append("Display help message")
rightTab.append("Set hash to search")
rightTab.append("Set verbose mode")
rightTab.append("Set application mode")
maxLenLeft = 0
for value in leftTab:
if len(value) > maxLenLeft:
maxLenLeft = len(value)
for i in range(0,len(leftTab)):
print(self.tableLine(" ",leftTab[i],rightTab[i],maxLenLeft+4))
print()
def displayFullHelp(self):
pass
def displayVersion(self):
print()
print(APPLICATION_CONSTANTS.NAME + " version " + APPLICATION_CONSTANTS.VERSION)
def getStringInputValue(self,question):
print()
return input(question +" ")
def getIntInputValue(self,question):
print()
userChoice = input(question +" ")
if not BF_Utils.is_int(userChoice):
self.displayHeader()
print()
self.displayError("localhost","EntryError","Input is not a Number !")
userChoice = self.getIntInputValue(question)
return int(userChoice)
def createMenu(self,title,entryList,margin):
print()
print(title)
print()
for i in range(0,len(entryList)):
print(margin+ str(i + 1) + " - " + entryList[i])
print()
userChoice = input()
if BF_Utils.is_int(userChoice):
if not (int(userChoice)> 0 and int(userChoice) <= len(entryList)):
self.displayHeader()
print()
self.displayError("localhost","EntryError","Choice not in the list !")
userChoice = self.displayMenu(title,entryList,margin)
else:
self.displayHeader()
print()
self.displayError("localhost","EntryError","Input is not a Number !")
userChoice = self.displayMenu(title,entryList,margin)
return int(userChoice)
def createMenuValue(self,title,entryList,margin):
print()
print(title)
print()
for i in range(0,len(entryList)):
print(margin+ str(i + 1) + " - " + entryList[i])
print()
userChoice = input()
if BF_Utils.is_int(userChoice):
if not (int(userChoice)> 0 and int(userChoice) <= len(entryList)):
self.displayHeader()
print()
self.displayError("localhost","EntryError","Choice not in the list !")
userChoice = self.displayMenu(title,entryList,margin)
else:
self.displayHeader()
print()
self.displayError("localhost","EntryError","Input is not a Number !")
userChoice = self.displayMenu(title,entryList,margin)
return entryList[int(userChoice) - 1]