~xpresser-team/xpresser/xpresser-type-with-hold

« back to all changes in this revision

Viewing changes to xpresser/xp.py

  • Committer: Gustavo Niemeyer
  • Date: 2010-05-16 22:59:48 UTC
  • Revision ID: gustavo@niemeyer.net-20100516225948-hm96jar095u9jhm2
Creating branch with initial version of Xpresser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from xpresser import xutils
 
2
from xpresser.image import Image
 
3
from xpresser.imagedir import ImageDir
 
4
from xpresser.imagematch import ImageMatch
 
5
from xpresser.opencvfinder import OpenCVFinder
 
6
 
 
7
 
 
8
class XP(object):
 
9
 
 
10
    def __init__(self):
 
11
        self._imagedir = ImageDir()
 
12
        self._imagefinder = OpenCVFinder()
 
13
 
 
14
    def load_images(self, path):
 
15
        self._imagedir.load(path)
 
16
 
 
17
    def get_image(self, name):
 
18
        return self._imagedir.get(name)
 
19
 
 
20
    def _compute_focus_point(self, args):
 
21
        if (len(args) == 2 and
 
22
            isinstance(args[0], (int, long)) and
 
23
            isinstance(args[1], (int, long))):
 
24
            return args
 
25
        elif len(args) == 1:
 
26
            if type(args[0]) == ImageMatch:
 
27
                match = args[0]
 
28
            else:
 
29
                match = self.find(args[0])
 
30
            #for i in range(10):
 
31
            #    if match:
 
32
            #        break
 
33
            return match.focus_point
 
34
 
 
35
    def click(self, *args):
 
36
        """Click on the position specified by the provided arguments.
 
37
 
 
38
        The following examples show valid ways of specifying the position:
 
39
        
 
40
            xp.click("image-name")
 
41
            xp.click(image_match)
 
42
            xp.click(x, y)
 
43
        """
 
44
        xutils.click(*self._compute_focus_point(args))
 
45
            
 
46
    def hover(self, *args):
 
47
        """Hover over the position specified by the provided arguments.
 
48
 
 
49
        The following examples show valid ways of specifying the position:
 
50
        
 
51
            xp.hover("image-name")
 
52
            xp.hover(image_match)
 
53
            xp.hover(x, y)
 
54
        """
 
55
        xutils.hover(*self._compute_focus_point(args))
 
56
 
 
57
    def find(self, image):
 
58
        """Given an image or an image name, find it on the screen.
 
59
 
 
60
        @param image: Image or image name to be searched for.
 
61
        @return: An ImageMatch instance, or None.
 
62
        """
 
63
        if isinstance(image, basestring):
 
64
            image = self._imagedir.get(image)
 
65
        screenshot_image = xutils.take_screenshot()
 
66
        return self._imagefinder.find(screenshot_image, image)