← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #154347]: Wait for this OR that

 

Question #154347 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/154347

RaiMan posted a new comment:
Since I found the problem interesting, I made a function, that can
search for an arbitrary number of images with one call.

Some sequences can be optimized in the script, but are written as is to
avoid long lines in this post.

It is only tested with images, not with text to search for!

def waitOne(secs, *more):
	# analyze parameters
	reg = SCREEN
	weWait = secs # max time to wait
	if more[0].__class__.__name__ == "Region": 
		# a Region is given to restrict the search
		reg = more[0]
		more = more[1:]
	classM0 = more[0].__class__.__name__
	# check how the list of images is given
	if classM0 == "tuple":
		images = more[0]
	elif classM0 == "list": 
		images = more[0]
	else: 
		images = more

	# prepare the dictionary to return
	result = {"first" : None}
	for img in images:
		result[img] = None
	nImg = len(images)

	# look for the images to appear
	while weWait > 1:
		start = time.time()
		for img in images:
			if reg.exists(img,0):
				# the first one appeared
				result[img] = reg.getLastMatch()
				result['first'] = img
				break				
		if result.values().count(None) < nImg: 
			# look again for the others
			for (key, value) in result.iteritems():
				if not value:
					result[key] = reg.exists(key,0)
			break
		actWait = time.time()-start
		if actWait < getAutoWaitTimeout():
			# we wait the standard wait time minimum
			actWait = getAutoWaitTimeout()
			wait(getAutoWaitTimeout())
		weWait -= actWait # count down wait time
	return result

--- the returned result
- is a dictionary whose keys are the image filenames
- the values are None for the images that did not appear
- the values contain the match if this image was found
- the key "first" contains None, if nothing could be found and the match of the first image that was found

--- usages:

--- search multiple images on the whole screen for about 20 seconds
anyFound = waitOne(20, "image1.png", "image2.png", "image3.png")

--- search multiple images in a given region for about 20 seconds
someRegion = Region(0,0,500,300) # are any other definition of a Region
anyFound = waitOne(20, someRegion, "image1.png", "image2.png", "image3.png")

--- search in a given region for about 20 seconds - images given as list
someRegion = Region(0,0,500,300) # are any other definition of a Region
someImages = ["image1.png", "image2.png", "image3.png"]
anyFound = waitOne(20, someRegion, someImages)

--- using the result in the last case
if not anyFound["first"]:
    # we had success
    print "this one came first:", anyFound["first"]
    for (key, value) in anyFound:
        if anyFound[key] == anyFound["first"]: continue # we know that
        if value: print key, "appeared"
        else: print key, "did not appear"
else:
    print "None of them appeared"; exit(1)

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.