def changeColorSpecific(): """Changes color on a specific printer """ verticalLine() puts(colored.cyan("Example - Change color on a specific printer")) puts("Available printers: ") showPrintersBasic() ip_input = prompt.query("Enter printer ip: ") try: ip = ipaddress.ip_address(ip_input) ip = str(ip) for printer in printerList.getPrinters(): if printer.getIp() == ip: colors = getColorData() if not colors: #An empty dict is False return changeColor(printer, colors['hue'], colors['saturation'], colors['brightness']) except ValueError: puts( colored.yellow( "You entered an invalid ip adress. (Format: IPv4Address)"))
def addPrinter(): """ Procedure for adding a non existing printer by it's ip adress and writing down key infromation to printers.json """ verticalLine() puts(colored.magenta("Add a printer")) puts("Find the ip adress by clicking on System > Network on the machine") tries = 0 while tries < 3: ip_input = prompt.query( "What is the ip adress of the printer? e.g \"192.168.1.201\"") try: ip = ipaddress.ip_address(ip_input) ip = str(ip) #If a connection to the entered IP already exists, stop if (duplicateIP(ip)): return try: new_printer = Printer(ip) puts(colored.green("Successfully added a new printer:")) #Print out the data puts(colored.cyan(new_printer.getName())) with indent(4): puts("IP: " + new_printer.getIp()) puts("ID: " + new_printer.getId()) puts("Key: " + new_printer.getKey()) #Save back to the file by reading, adding and writing. printers = json.load(open("printers.json", "rt")) printers.append(new_printer.getPrinterAsDict()) sortedPrinters = sorted(printers, key=lambda k: k["name"]) json.dump(sortedPrinters, open("printers.json", "w"), indent=4) #Update printerList printerList.update() break except RuntimeError as e: puts(colored.red(str(e))) with indent(4): puts( colored.yellow( "Someone probably clicked \"deny\" on the printer, try again." )) except ValueError: puts( colored.yellow( "You entered an invalid ip adress, try again. (Format: IPv4Address)" )) tries += 1 if (tries >= 3): puts( colored.red( "3 tries of failing is enough, find the ip adress of the printer before attempting again." ))
def changeColorAll(): """Changes color on all printers """ verticalLine() puts(colored.cyan("Example - Change color on all printers")) colors = getColorData() if not colors: #An empty dict is False return for printer in printerList.getPrinters(): changeColor(printer, colors['hue'], colors['saturation'], colors['brightness'])
def examples(): """ Procedure for choosing and running example """ keep_asking = True while keep_asking: verticalLine() example = prompt.options( colored.magenta("What would you like to try out?"), example_menu) if (example == "ca"): changeColorAll() elif (example == "cs"): changeColorSpecific() elif (example == "exit"): keep_asking = False else: puts(colored.yellow("Wrong input, try again"))
def removePrinter(): verticalLine() puts(colored.magenta("Remove a printer")) printers = json.load(open("printers.json", "rt")) puts(colored.cyan("Printers")) for printer in printers: puts(printer["name"] + " - " + printer["ip"]) tries = 0 while tries < 3: ip_input = prompt.query( "Type the ip adress of the printer you wish to remove: ") try: ip = ipaddress.ip_address(ip_input) ip = str(ip) input = prompt.query("Are you sure you want to delete printer " + ip + ": <yes/no>") if (str(input).upper().lower() == "yes"): for printer in printers: if (printer["ip"] == ip): printers.remove(printer) #Save back to the file json.dump(printers, open("printers.json", "w"), indent=4) #Update printerList printerList.update() puts(colored.green("Printer sucessfully removed!")) return puts(colored.red("IP did not exist among printers.")) else: puts(colored.red("Write \"yes\" or \"no\" please")) except ValueError: puts( colored.yellow( "You entered an invalid ip adress, try again. (Format: IPv4Address)" )) tries += 1
}, { 'selector': 'r', 'prompt': 'Remove printer', 'return': 'r' }, { 'selector': 'u', 'prompt': 'Update printer', 'return': 'u' }, { 'selector': 'exit', 'prompt': 'Exit program', 'return': 'exit' }] while True: verticalLine() action = prompt.options(colored.magenta("What would you like to do?"), main_menu) if (action == "e"): examples() elif (action == "s"): showPrinters() elif (action == "a"): addPrinter() elif (action == "r"): removePrinter() elif (action == "u"): updatePrinters() elif (action == "exit"): exit() else: