How to use getargv method in tox

Best Python code snippet using tox_python

print.py

Source:print.py Github

copy

Full Screen

1import os2import sys3import win32print4import win32api5import ghostscript6import locale7USELESS_PRINTER = ['OneNote for Windows 10', 'OneNote (Desktop)', 'Microsoft XPS Document Writer',8 'Microsoft Print to PDF', 'Fax']9HELP = """pyPrinter - Author: Arthur SICARD - Date: 19/05/202110\n-help11\tDisplay this message.12\n-list [-virtual]13\tReturn list of available printer (excepted: """ + ", ".join(USELESS_PRINTER) + """)14\n-file filepath [-printer printer_name] [-virtual]15\tPrint specified file on default system printer. Use -printer to specify printer to use. Printer name must be available un -list response.16\n-batch filepath [-printer printer_name] [-virtual]17\tPrint each document specified un batch file on default system printer. Batch file must be a .txt. Each file to print must be write on its own line.18\tUse -printer to specify printer to use. Printer name must be available un -list response.19\n-virtual20\tUse this option after all other arguments to enable printing on virtual printer 'Microsoft Print to PDF'21"""22# Safe accessor to argv. Return None if index is not set23def getArgv(index):24 try:25 return (sys.argv[1:])[index]26 except:27 return None28# Return list of local printer available without "virtual printer" define in USELESS_PRINTER list.29def getAvailablePrinters():30 printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)31 printer_list = []32 for x in range(len(printers)):33 if printers[x][2] not in USELESS_PRINTER:34 printer_list.append(printers[x][2])35 return printer_list36# Return printer name to use. If -printer is set it will return this value only if value match with available37# printers list. Return a error if -printer not in list. If no printer specified, retrieve default printer and return38# its name. Sometime default printer is on USELESS_PRINTER list so first printer return by getAvailablePrinters() is39# return. If no printer is return display an error.40def getPrinter():41 default_printer = win32print.GetDefaultPrinter()42 if default_printer in USELESS_PRINTER:43 if len(getAvailablePrinters()) == 0:44 print("No printer available, unable to print. Use -virtual if you want enable virtual printer.")45 sys.exit(1801)46 default_printer = getAvailablePrinters()[0]47 if getArgv(2) is not None:48 if getArgv(2) == "-printer":49 printer = getArgv(3)50 if printer in getAvailablePrinters():51 return printer52 else:53 if printer is not None:54 print("Given printer not found. Defaut printer configured: ", default_printer)55 return default_printer56# Use GhostScript API to silent print .pdf and .ps. Use win32api to print .txt. Return a error if printing failed or57# file ext doesn't match.58def printFile(filepath):59 try:60 if os.path.splitext(filepath)[1] in [".pdf", ".ps"]:61 args = [62 "-dPrinted", "-dBATCH", "-dNOSAFER", "-dNOPAUSE", "-dNOPROMPT"63 "-q",64 "-dNumCopies#1",65 "-sDEVICE#mswinpr2",66 f'-sOutputFile#"%printer%{getPrinter()}"',67 f'"{filepath}"'68 ]69 encoding = locale.getpreferredencoding()70 args = [a.encode(encoding) for a in args]71 ghostscript.Ghostscript(*args)72 elif os.path.splitext(filepath)[1] in [".txt"]:73 # '"%s"' % enable to encapsulate string with quote74 win32api.ShellExecute(0, "printto", '"%s"' % filepath, '"%s"' % getPrinter(), ".", 0)75 return True76 except:77 print("Printing error for file: ", '"%s"' % filepath, "| Printer: ", '"%s"' % getPrinter())78 return False79def main(argv):80 if len(argv) in [1, 2, 4, 5]:81 cmd1 = getArgv(0)82 filepath = getArgv(1)83 if argv[-1] == "-virtual":84 USELESS_PRINTER.remove('Microsoft Print to PDF')85 # Batch printing mode86 if cmd1 == "-batch" and len(argv) in [2, 4, 5]:87 if not os.path.isfile(filepath) and not os.path.exists(filepath):88 print("Path provide for batch file is not a valid file path or doesn't exist.")89 sys.exit(2)90 if os.path.splitext(filepath)[1] in [".txt"]:91 with open(filepath) as fp:92 line = fp.readline().strip('\n')93 while line:94 if not os.path.isfile(line) and not os.path.exists(line):95 print("Path provide is not a valid file path or doesn't exist: ", line)96 else:97 printFile(line)98 line = fp.readline().strip('\n')99 fp.close()100 else:101 print("Not supported file format for batch printing.")102 sys.exit(50)103 # Single file printing mode104 elif cmd1 == "-file" and len(argv) in [2, 4, 5]:105 if not os.path.isfile(filepath) and not os.path.exists(filepath):106 print("Path provide is not a file path.")107 sys.exit(2)108 if not printFile(filepath):109 sys.exit(1)110 # Get printers list111 elif cmd1 == "-list" and len(argv) in [1, 2]:112 for printer in getAvailablePrinters():113 print(printer)114 # Display help115 elif cmd1 == "-help" and len(argv) in [1]:116 print(HELP)117 sys.exit(0)118 else:119 print("Unknow option. Use -help to obtain more informations about supported options.")120 sys.exit(50)121 else:122 print("Wrong arguments number. Use -help to obtain more informations about supported options.")123 sys.exit(50)124 exit(0)125if __name__ == '__main__':...

Full Screen

Full Screen

sort.py

Source:sort.py Github

copy

Full Screen

1import os2import sys3def subSort(copyFold, originFold):4 listFold = os.listdir(originFold)5 for file in listFold:6 if os.path.isdir(originFold + "/" + file):7 os.makedirs(copyFold + "/" + file)8 subSort(copyFold + "/" + file, originFold + "/" + file)9 else:10 archive = open(copyFold + "/" + file, 'w')11 archive.close()12 os.rename(originFold + "/" + file, copyFold + "/" + file)13 os.rmdir(originFold) 14def sortStandar(letter, Dir, data):15 listDir = os.listdir(Dir)16 for file in listDir:17 if ((file[0]==letter or file[0]==letter.upper()) and 18 not(data.isNoSort(file))):19 if not(os.path.isdir(Dir + "/" + letter.upper())):20 print(f"Creating folder {letter}..")21 os.makedirs(Dir+ "/" + letter.upper())22 print(f"Folder {letter} created")23 if os.path.isdir(Dir + "/" + file):24 os.makedirs(Dir + "/" + letter.upper() + "/" + file)25 subSort(Dir + "/" + letter.upper() + "/" + file, Dir + "/" + file)26 else:27 archive = open(Dir + "/" + letter.upper() + "/" + file, 'w')28 archive.close()29 os.rename(Dir + "/" + file, Dir + "/" + letter.upper() + "/" + file)30def sortMono(folderName, Dir, data):31 listDir = os.listdir(Dir)32 for file in listDir:33 if (file[0:(len(data.getArgv()[3]))]==data.getArgv()[3] and34 not(data.isNoSort(file)) and file!=folderName):35 if not(os.path.isdir(Dir + "/" + folderName)):36 print(f"Creating folder {folderName}..")37 os.makedirs(Dir+ "/" + folderName)38 print(f"Folder {folderName} created")39 if os.path.isdir(Dir + "/" + file):40 os.makedirs(Dir + "/" + folderName + "/" + file)41 subSort(Dir + "/" + folderName + "/" + file, Dir + "/" + file)42 else:43 archive = open(Dir + "/" + folderName + "/" + file, 'w')44 archive.close()45 os.rename(Dir + "/" + file, Dir + "/" + folderName + "/" + file)46def standar(data, Dir, i):47 for i in range(i,len(data.getArgv())):48 if not(os.path.isdir(data.getArgv()[i])):49 sys.exit(f"[ERROR] Does not exist the Directory: {data.getArgv()[i]}")50 print(f"[SORTING]: {data.getArgv()[i]}")51 for letter in data.getListAlphabet():52 print(f"Sorting folders and/or files with the letter {letter}..")53 sortStandar(letter,Dir + "/" + data.getArgv()[i], data)54 print("[FINALIZED]")55def mono(data, Dir, i):56 if not(os.path.isdir(data.getArgv()[i])):57 sys.exit(f"[ERROR] Does not exist the Directory: {data.getArgv()[i]}")58 print(f"[SORTING]: {data.getArgv()[i]}")59 print(f"Sorting folders and/or files with {data.getArgv()[2]}..")60 sortMono(data.getArgv()[2],Dir + "/" + data.getArgv()[i], data)61 return 062def run(data, i):63 while 1:64 if (os.path.isdir(f"Escritorio") or os.path.isdir(f"Desktop")):65 break66 elif(os.getcwd()=="/"):67 sys.exit("[ERROR] Desktop not found")68 os.chdir("../")69 Dir = os.getcwd()70 print("Running..")71 if i==1:72 standar(data, Dir, i)73 elif i==4:74 mono(data, Dir, i)75 else:76 sys.exit("[ERROR]")77def help():78 print("Running mode:\n\n"79 "[STANDARD] Sort folders and files in the directories given as argument. For running:\n"80 " python3 Sort.py \"<directory1>\" \"<directory2>\" ... \"<directoryN>\"\n\n"81 "[MONO] Sort folders and files on the <nameFolder> that meets a <condition>\n"82 " (that condition is a word found in the names of the folders and files\n"83 " to sort) on the directories given as argument. For running:\n"84 " python3 run.py -l <nameFolder> <condition> <directory>\n\n"85 "[HELP] For run:\n"86 " python3 run.py --help\n"...

Full Screen

Full Screen

Run.py

Source:Run.py Github

copy

Full Screen

1import sys, Sort2from data import Data3def commands(data):4 if(len(data.getArgv())>1):5 if(len(data.getArgv())==2 and data.getArgv()[1] == '--help'):6 Sort.help()7 elif(len(data.getArgv())==5 and data.getArgv()[1] == '-l'):8 Sort.run(data, 4)9 elif(data.getArgv()[1]!='--help' and data.getArgv()[1]!='-l'): 10 Sort.run(data, 1)11 else:12 sys.exit("[ERROR] Invalid arguments") 13 else:14 Sort.help()15data = Data()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful