How to use module_exists method in lisa

Best Python code snippet using lisa_python

verify_pipsta_install.py

Source:verify_pipsta_install.py Github

copy

Full Screen

...31 struct.pack('iL', bytes, names.buffer_info()[0])32 ))[0]33 namestr = names.tostring()34 return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]35def module_exists(module_name):36 try:37 __import__(module_name)38 except ImportError:39 return False40 return True41def os_check():42 return (platform.system() == 'Linux')43 44def python_check():45 ok = (sys.version_info > (2, 6) and sys.version_info < (3, 0))46 if not ok:47 print('The demoes are targeted at python2 versions 2.6 and above')48 return ok49 50def pyusb_check():51 ok = module_exists('usb') == True and module_exists('usb.core') == True \52 and module_exists('usb.util') == True53 if not ok:54 print('The Pipsta is connected over USB, the demoes all depend on '55 'the pyusb module')56 return ok57 58def pillow_check():59 if not module_exists('PIL.Image'):60 print('The Pillow library for python is not installed and is required '61 'by most of the demoes')62 return False63 64 return True65 66def usb_enumeration_check():67 import usb.core68 dev = usb.core.find(idVendor=PIPSTA_USB_VENDOR_ID,69 idProduct=PIPSTA_USB_PRODUCT_ID)70 assert dev, 'Printer failed to enumerate'71 return dev72 73def wait_for_printer(dev):74 import struct75 status = struct.unpack('B', dev.ctrl_transfer(0xC0, 0x0d, 0x0200, 0, 1))[0]76 while status & 0x40 == 0x40:77 print('Printer is in error state can you check the following -')78 print(' Paper is installed')79 print(' Printers power supply is connected')80 print(' The LED on the front of the panel is a constant green')81 raw_input('Press return to continue ')82 status = struct.unpack('B', dev.ctrl_transfer(0xC0, 0x0d, 0x0200, 0, 1))[0]83 84def get_default_interface(dev, cfg):85 import usb.core86 interface_number = cfg[(0, 0)].bInterfaceNumber87 usb.util.claim_interface(dev, interface_number)88 alternate_setting = usb.control.get_interface(dev, interface_number)89 interface = usb.util.find_descriptor(90 cfg, bInterfaceNumber=interface_number,91 bAlternateSetting=alternate_setting)92 return interface93def check_bulk_read(interface): 94 import usb.core 95 usb_in = usb.util.find_descriptor(96 interface,97 custom_match=lambda e:98 usb.util.endpoint_direction(e.bEndpointAddress) ==99 usb.util.ENDPOINT_IN100 )101 assert usb_in, 'No bulk in endpoint found for printer'102 from usb.core import USBError103 try:104 junk = usb_in.read(1)105 while junk:106 print(junk)107 except USBError as unused:108 pass109 110 return usb_in111 112def check_bulk_write(interface):113 import usb.core114 usb_out = usb.util.find_descriptor(115 interface,116 custom_match=lambda e:117 usb.util.endpoint_direction(e.bEndpointAddress) ==118 usb.util.ENDPOINT_OUT119 )120 assert usb_out, 'No bulk out endpoint found for printer'121 return usb_out122def ok_if_found(found):123 return 'Ok' if found else 'Missing'124def perform_connection_test(core, web, gui):125 dev = usb_enumeration_check()126 # Configure USB connection127 dev.reset()128 dev.set_configuration()129 cfg = dev.get_active_configuration()130 assert cfg, 'Failed to find an active configuration for the printer'131 # Check printer is powered132 wait_for_printer(dev)133 interface = get_default_interface(dev, cfg)134 usb_in = check_bulk_read(interface)135 usb_out = check_bulk_write(interface)136 # Check full bulk communications137 usb_out.write(b'\x1dI\x06')138 printer_id = usb_in.read(9)139 assert printer_id and printer_id != '', 'Printer did not respond with a valid ID'140 141 print('Successful bi-directional bulk communications established with '142 'printer')143 # Print, results144 printer_id = ''.join([chr(c) for c in printer_id]).strip()145 usb_out.write('Welcome to Pipsta ({})\n'.format(printer_id))146 (distname, linux_version, linux_id) = platform.linux_distribution()147 usb_out.write('Running on {} V{}\n'.format(distname, linux_version))148 (system, node, release, version, machine, processor) = platform.uname()149 usb_out.write('Based on {} V{} for\n'.format(system, release))150 usb_out.write('{} with {} arch.\n\n'.format(node, machine))151 152 for interface in all_interfaces():153 address = get_ip_address(interface)154 usb_out.write('{}\t{}\n'.format(interface, address))155 156 usb_out.write('\nRunning python V{}.{}\n\n'.format(157 sys.version_info[0], sys.version_info[1]))158 usb_out.write('OS = Ok\n')159 usb_out.write('Python = Ok\n')160 usb_out.write(' struct = Ok\n')161 usb_out.write(' Pillow = {0}\n'.format(ok_if_found(core[0])))162 usb_out.write(' bitarray = {0}\n'.format(ok_if_found(core[1])))163 usb_out.write(' qrcode = {0}\n'.format(ok_if_found(core[2])))164 usb_out.write(' MySQLdb = {0}\n'.format(ok_if_found(web)))165 usb_out.write(' PyQt4 = {0}\n'.format(ok_if_found(gui[0])))166 usb_out.write(' fclist = {0}\n'.format(ok_if_found(gui[1])))167 168 usb_out.write(FEED_PAST_CUTTER)169def main():170 print('Testing the Pipsta installation')171 os_ok = os_check()172 if os_ok == False:173 sys.exit("The demoes are written for Raspberry-Pi's running Raspbian")174 175 py_ok = python_check()176 pyusb_ok = False177 bitarray_ok = False178 pillow_ok = False179 qr_code_ok = False180 mysql_db_ok = False181 struct_ok = False182 pyqt4_ok = False183 fclist_ok = False184 if py_ok == False:185 print('Install python2 versions 2.6 or greater. Try one of -')186 print(' sudo apt-get install python2.6')187 print('or')188 print(' sudo apt-get install python2.7')189 print190 else:191 try:192 pyusb_ok = pyusb_check()193 bitarray_ok = module_exists('bitarray')194 pillow_ok = pillow_check()195 qrcode_ok = module_exists('qrcode')196 mysql_db_ok = module_exists('MySQLdb')197 struct_ok = module_exists('struct')198 pyqt4_ok = module_exists('PyQt4')199 fclist_ok = module_exists('fclist')200 except AssertionError as e:201 print(e)202 pass203 204 print('The following problems were found that may prevent you '205 'running all of the examples.')206 print207 print('To install python modules we uses pip. To install pip try -')208 print(' sudo apt-get install python-pip python-dev')209 print210 if pyusb_ok == False or pillow_ok == False or bitarray_ok == False \211 or qrcode_ok == False or struct_ok == False:212 print('The following will install the basic dependencies -')213 cmd_str = ' sudo pip install '...

Full Screen

Full Screen

extensions.py

Source:extensions.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2from .helpers import module_exists3if module_exists('pytz.gae') and module_exists('flaskext.babel'):4 from pytz.gae import pytz5 from flaskext.babel import Babel6 babel = Babel()7if module_exists('flaskext.sqlalchemy'):8 from flaskext.sqlalchemy import SQLAlchemy9 db = SQLAlchemy()10if module_exists('flaskext.mail'):11 from flaskext.mail import Mail12 mail = Mail()13if module_exists('flaskext.cache'):14 from flaskext.cache import Cache15 cache = Cache()16if module_exists('flaskext.login'):17 from flaskext.login import LoginManager18 login_manager = LoginManager()19if module_exists('flaskext.debugtoolbar'):20 from flaskext.debugtoolbar import DebugToolbarExtension...

Full Screen

Full Screen

env_supports.py

Source:env_supports.py Github

copy

Full Screen

1"""each attribute indicates a supported module or feature."""2import os3import sys4 5def module_exists(mod):6 try:7 __import__(mod)8 except ImportError:9 return False10 else:11 return True12sqlobject = module_exists('sqlobject')13sqlalchemy = module_exists('sqlalchemy')14elixir = module_exists('elixir')...

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 lisa 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