def _load_all_patterns(application: str) -> list: """Function returns a list with all the project's Patterns.""" if get_core_args().resize: _convert_hi_res_images() result_list = [] for root, dirs, files in os.walk(PathManager.get_module_dir()): for file_name in files: if file_name.endswith('.png'): if application in root and (PathManager.get_images_path() in root or 'common' 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: str, target: str) -> str: """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. 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. """ 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] if OSHelper.get_os_version() == 'win7': os_version = 'win7' else: os_version = OSHelper.get_os().value paths = [] current_locale = '' try: current_locale = get_core_args().locale except AttributeError: pass 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: result_list = [ x for x in _load_all_patterns(target) if x['name'] == image ] 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, PathManager.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.')