def capture(*args): f_arg = args[0] if f_arg is None: return elif isinstance(f_arg, Region): return IrisCore.get_region(f_arg) elif len(args) is 4: return IrisCore.get_region((args[0], args[1], args[2], args[3]))
def _convert_hi_res_images(): """Function resizes all the project's hi-resolution images.""" for root, dirs, files in os.walk(IrisCore.get_module_dir()): for file_name in files: if file_name.endswith('.png'): if 'images' in root or 'local_web' in root: if '@' in file_name: logger.debug('Found hi-resolution image at: %s' % os.path.join(root, file_name)) temp = file_name.split('@') name = temp[0] scale = int(temp[1].split('x')[0]) new_name = '%s.png' % name img = Image.open(os.path.join(root, file_name)) logger.debug('Resizing image from %sx scale' % scale) new_img = img.resize( (img.width / scale, img.height / scale), Image.ANTIALIAS) logger.debug( 'Creating newly converted image file at: %s' % os.path.join(root, new_name)) new_img.save(os.path.join(root, new_name)) logger.debug('Removing unused image at: %s' % os.path.join(root, file_name)) os.remove(os.path.join(root, file_name))
def _load_all_patterns(): if parse_args().resize: _convert_hi_res_images() result_list = [] for root, dirs, files in os.walk(IrisCore.get_module_dir()): for file_name in files: if file_name.endswith('.png'): if IrisCore.get_images_path( ) in root or 'common' in root or 'local_web' in root: pattern_name, pattern_scale = _parse_name(file_name) pattern_path = os.path.join(root, file_name) pattern = { 'name': pattern_name, 'path': pattern_path, 'scale': pattern_scale } result_list.append(pattern) return result_list
def _get_image_path(caller, image): """Enforce proper location for all Pattern creation. :param caller: Path of calling Python module. :param image: String filename of image. :return: Full path to image on disk. """ module = os.path.split(caller)[1] module_directory = os.path.split(caller)[0] parent_directory = os.path.basename(module_directory) file_name = image.split('.')[0] names = [ image, '*****@*****.**' % file_name, '*****@*****.**' % file_name, '*****@*****.**' % file_name ] # We will look at all possible paths relative to the calling file, with this priority: # # - current platform locale folder # - common locale folder # - current platform root # - common root # # Each directory is scanned for four possible file names, depending on resolution. # If the above fails, we will look up the file name in the list of project-wide images, # and return whatever we find, with a warning message. # If we find nothing, we will raise an exception. if Settings.get_os_version() == 'win7': os_version = 'win7' else: os_version = Settings.get_os() paths = [] current_locale = parse_args().locale platform_directory = os.path.join(module_directory, 'images', os_version) platform_locale_directory = os.path.join(platform_directory, current_locale) for name in names: paths.append(os.path.join(platform_locale_directory, name)) common_directory = os.path.join(module_directory, 'images', 'common') common_locale_directory = os.path.join(common_directory, current_locale) for name in names: paths.append(os.path.join(common_locale_directory, name)) for name in names: paths.append(os.path.join(platform_directory, name)) for name in names: paths.append(os.path.join(common_directory, name)) found = False image_path = None for path in paths: if os.path.exists(path): found = True image_path = path break if found: logger.debug('Module %s requests image %s' % (module, image)) logger.debug('Found %s' % image_path) return image_path else: # If not found in correct location, fall back to global image search for now. result_list = filter(lambda x: x['name'] == image, _load_all_patterns()) if len(result_list) > 0: res = result_list[0] logger.warning( 'Failed to find image %s in default locations for module %s.' % (image, module)) logger.warning('Using this one instead: %s' % res['path']) logger.warning( 'Please move image to correct location relative to caller.') location_1 = os.path.join(parent_directory, 'images', 'common') location_2 = os.path.join(parent_directory, IrisCore.get_images_path()) logger.warning('Suggested locations: %s, %s' % (location_1, location_2)) return res['path'] else: logger.error('Pattern creation for %s failed for caller %s.' % (image, caller)) logger.error( 'Image not found. Either it is in the wrong platform folder, or it does not exist.' ) logger.debug('Paths searched:') logger.debug('\n'.join(paths)) raise FindError('Pattern not found.')
def is_lock_on(keyboard_key): """Static method which determines if a keyboard key(CAPS LOCK, NUM LOCK or SCROLL LOCK) is ON. :param keyboard_key: Keyboard key(CAPS LOCK, NUM LOCK or SCROLL LOCK). :return: TRUE if keyboard_key state is ON or FALSE if keyboard_key state is OFF. """ if Settings.get_os() == Platform.WINDOWS: hll_dll = ctypes.WinDLL("User32.dll") keyboard_code = 0 if keyboard_key == Key.CAPS_LOCK: keyboard_code = 0x14 elif keyboard_key == Key.NUM_LOCK: keyboard_code = 0x90 elif keyboard_key == Key.SCROLL_LOCK: keyboard_code = 0x91 try: key_state = hll_dll.GetKeyState(keyboard_code) & 1 except Exception: raise Exception('Unable to run Command.') if key_state == 1: return True else: return False elif Settings.get_os() == Platform.LINUX or Settings.get_os( ) == Platform.MAC: try: cmd = subprocess.Popen('xset q', shell=True, stdout=subprocess.PIPE) IrisCore.shutdown_process('Xquartz') except subprocess.CalledProcessError as e: logger.error('Command failed: %s' % repr(e.cmd)) raise Exception('Unable to run Command.') else: processed_lock_key = keyboard_key.label if 'caps' in processed_lock_key: processed_lock_key = 'Caps' elif 'num' in processed_lock_key: processed_lock_key = 'Num' elif 'scroll' in processed_lock_key: processed_lock_key = 'Scroll' for line in cmd.stdout: if processed_lock_key in line: value = ' '.join(line.split()) if processed_lock_key in value[0:len(value) / 3]: button = value[0:len(value) / 3] if "off" in button: return False else: return True elif processed_lock_key in value[len(value) / 3:len(value) / 3 + len(value) / 3]: button = value[len(value) / 3:len(value) / 3 + len(value) / 3] if "off" in button: return False else: return True else: button = value[len(value) / 3 * 2:len(value)] if "off" in button: return False else: return True