def help(self): """all the available commands and how to use them use : help""" print_command("Available commands :") for command in self.commands: print_command("- {} : {}".format(command, self.commands[command].__doc__))
def display_websites(self): """Displays all the websites checked by the program use : display""" self.lock.acquire() for name in self.websites: print_command(str(self.websites[name])) self.lock.release()
def change_url(self, name, url): """change the url of the website use : change_url name new_url""" try: self.websites[name].change_url(url) except KeyError: print_command("The website {} doesn't exist".format(name))
def change_check_interval(self, name, check_interval): """change the period between two checks use : change_check_interval name new_check_interval""" try: self.websites[name].change_check_interval(int(check_interval)) except KeyError: print_command("The website {} doesn't exist".format(name))
def remove_website(self, name): """remove a website from the list of websites to check. use : remove name""" self.lock.acquire() if name in self.websites: del self.websites[name] else: print_command("Website {} doesn't exist.".format(name)) self.lock.release()
def add_website(self, name, url, check_interval): """add a website to the list of websites to check. use : add name url check_interval""" self.lock.acquire() if name in self.websites: print_command("Website {} already exists.".format(name)) else: self.websites[name] = website.Website( name, url, int(check_interval), self.config) self.lock.release()
def change_url(self, url): """change the url of the website""" self.wrong_url = False self.url = url self.responses = [] self.available = True if not self.check(): print_command( "Impossible to reach Website, check the URL and your internet connection" )
def execute_command(self, command): """execute the command if it is a correct command""" L = command.split(" ") if len(L) == 0 or L[0] == "help": self.help() elif L[0] in self.commands: try: self.commands[L[0]](self.websites, *L[1:]) except TypeError: print_command("Invalid use of command") else: print_command("Command {} does not exist".format(L[0]))
def load_config(self): """loads the configuration file that indicates in what case a website is considered down by default, only 200 response code means the website is ok""" self.config = [200] try: with open(self.config_file, "r") as f: f.readline() self.config = [int(x) for x in f.readline().split(",")] except FileNotFoundError: print_command("Configuration file not found, using default configuration") except ValueError: print_command("Configuration file incorrect, using default configuration")
def __init__(self, name, url, check_interval, ok_responses): """inits a website checker with an url and a check_interval : time between two checks""" self.url = url self.name = name self.check_interval = check_interval self.ok_responses = ok_responses self.responses = [] self.wrong_url = False self.available = True self.lock = threading.Lock() self.thread_check = threading.Thread(target=Website.run_check, args=(self, )) self.thread_check.daemon = True self.thread_check.start() if not self.check(): print_command( "Impossible to reach Website, check the URL and your internet connection" )
def change_check_interval(self, check_interval): """change the time between two checks""" if check_interval <= 0: print_command("Minimum check interval = 1s") check_interval = 1 self.check_interval = check_interval