5
from xpresser.errors import XpresserError
6
from xpresser.image import Image
9
CLICK_POSITION_RE = re.compile(r"^\s*(?P<x>[-+][0-9]+)\s+(?P<y>[-+][0-9]+)\s*$")
12
class ImageDirError(XpresserError):
13
"""Error related to the image directory."""
16
class ImageDir(object):
17
"""Represents a directory with data about images.
19
This class doesn't know about any details regarding the images
20
themselves, besides the existence of the file in which they reside.
21
It will give access to generic ImageData objects containing the
22
details about these images. It's up to an ImageLoader to make sense
23
of the actual image data contained in the image files.
29
def get(self, image_name):
30
return self._images.get(image_name)
32
def load(self, dirname):
33
"""Load image information from C{dirname}.
35
@param dirname: Path of directory containing xpresser.ini.
37
ini_filename = os.path.join(dirname, "xpresser.ini")
38
if not os.path.exists(ini_filename):
39
raise ImageDirError("File %s not found" % ini_filename)
40
config = ConfigParser.ConfigParser()
41
config.read(ini_filename)
42
for section_name in config.sections():
43
if section_name.startswith("image "):
44
image_name = section_name.split(None, 1)[1]
46
image_filename = config.get(section_name, "filename")
47
except ConfigParser.NoOptionError:
48
raise ImageDirError("Image %s missing filename option"
50
image_filename = os.path.join(dirname, image_filename)
51
if not os.path.exists(image_filename):
52
raise ImageDirError("Image %s file not found: %s" %
53
(image_name, image_filename))
55
image_similarity = config.getfloat(section_name,
57
except ConfigParser.NoOptionError:
58
image_similarity = None
60
value = config.get(section_name, "similarity")
61
raise ImageDirError("Image %s has bad similarity: %s"
62
% (image_name, value))
65
value = config.get(section_name, "focus_delta")
66
match = CLICK_POSITION_RE.match(value)
68
raise ImageDirError("Image %s has invalid click "
71
image_focus_delta = (int(match.group("x")),
72
int(match.group("y")))
73
except ConfigParser.NoOptionError:
74
image_focus_delta = None
75
image = Image(name=image_name,
76
filename=image_filename,
77
similarity=image_similarity,
78
focus_delta=image_focus_delta)
79
self._images[image_name] = image