Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- from core.lib.png import Reader
- from core.lib.logger import RobotLogger
- from core.lib.wrapper import Region, Pattern, Screen
- """
- Allows for transforms to be stored in PNG files. Transforms are used to control how the PNG
- is matched and how the pre-match and post-match regions are treated.
- """
- class Transforms(object):
- log = None
- transforms = None
- # CONTEXT TYPES
- CONTEXT_PREVIOUS = "PREVIOUS"
- CONTEXT_NEXT = "NEXT"
- CONTEXT_CURRENT = "CURRENT"
- def __init__(self, source, entity=None, *args, **kargs):
- """ Returns a pattern and attributes given a filename """
- super(Transforms, self).__init__(*args, **kargs)
- entity = entity if entity else self
- self.log = RobotLogger(entity=entity) # get logger singleton
- defaultStructure = { \
- Transforms.CONTEXT_PREVIOUS: [ \
- RegionNearby(10), \
- ], \
- Transforms.CONTEXT_CURRENT: [], \
- Transforms.CONTEXT_NEXT: [ \
- ] \
- }
- # If we have a string, a path is supplied to png
- if isinstance(source, unicode) or isinstance(source, str):
- self.log.debug("Loading [%s]" % source)
- # Try and get extra attributes
- try:
- transforms = pickle.loads( Reader(filename=source).chunk('tEXt')[1] )
- except TypeError, e: # If doesn't contain a tEXt section
- pass
- except:
- self.log.warn("Unknown PNG transforms format, using defaults structure [%s](%s)" % (source, self.log.image(source)))
- transforms = defaultStructure
- # Otherwise a dictionary should be supplied
- else:
- transforms = source
- try:
- assert transforms.get(self.CONTEXT_PREVIOUS, None) != None
- assert transforms.get(self.CONTEXT_CURRENT, None) != None
- assert transforms.get(self.CONTEXT_NEXT, None) != None
- except:
- # If doesn't contain these keys, default to empty
- self.log.debug("Unknown PNG transform structure, using defaults")
- transforms = defaultStructure
- self.transforms = transforms
- def apply(self, operand, context, override=None, *args, **kargs):
- # Allow for override on the operand
- if override:
- operand = override
- # Apply the transforms
- for transform in self.transforms[context]:
- result = transform.apply(operand, *args, **kargs)
- if isinstance(operand, Pattern):
- self.log.debug("[%s] Applied [%s] transform, [%s] -> [%s] " % (context, transform, operand, result))
- else:
- self.log.debug("[%s] Applied [%s] transform, [%s](%s) -> [%s](%s) " % (context, transform, operand, self.log.region(operand), result, self.log.region(result)))
- operand = result
- return operand
- def formatPrefix(self, *args, **kargs):
- return "[PNGTransforms] "
- class PatternSimilarity(object):
- similarity = None
- def __init__(self, similarity):
- self.similarity = similarity
- def apply(self, operand):
- return operand.similar(self.similarity)
- def __str__(self, *args, **kwargs):
- return "%s similarity=%d" % (self.__class__.__name__, self.similarity)
- class PatternTargetOffset(object):
- dx = 0
- dy = 0
- def __init__(self, dx, dy):
- self.dx = dx
- self.dy = dy
- def apply(self, operand):
- return operand.targetOffset(self.dx, self.dy)
- def __str__(self, *args, **kwargs):
- return "%s dx=%s dy=%s" % (self.__class__.__name__, self.dx, self.dy)
- class RegionalTransform(object):
- limit = None
- def __init__(self, value=None):
- self.value = value
- def apply(self, operand):
- # Don't transform if we're working with screen
- if isinstance(operand, Screen):
- return operand
- else:
- return self.algorithm(operand)
- def __str__(self, *args, **kwargs):
- return "%s value=%s" % (self.__class__.__name__, self.value)
- class RegionAbove(RegionalTransform):
- def __init__(self, *args, **kargs):
- super(RegionAbove, self).__init__(*args, **kargs)
- def algorithm(self, operand, *args, **kargs):
- if self.value:
- operand = Region(operand).above(self.value)
- else:
- operand = Region(operand).above()
- class RegionBelow(RegionalTransform):
- def __init__(self, *args, **kargs):
- super(RegionBelow, self).__init__(*args, **kargs)
- def algorithm(self, operand, *args, **kargs):
- if self.value:
- operand = Region(operand).below(self.value)
- else:
- operand = Region(operand).below()
- class RegionRight(RegionalTransform):
- def __init__(self, *args, **kargs):
- super(RegionRight, self).__init__(*args, **kargs)
- def algorithm(self, operand, *args, **kargs):
- if self.value:
- operand = Region(operand).right(self.value)
- else:
- operand = Region(operand).right()
- class RegionLeft(RegionalTransform):
- def __init__(self, *args, **kargs):
- super(RegionLeft, self).__init__(*args, **kargs)
- def algorithm(self, operand, *args, **kargs):
- if self.value:
- operand = Region(operand).left(self.value)
- else:
- operand = Region(operand).left()
- class RegionNearby(RegionalTransform):
- def __init__(self, *args, **kargs):
- super(RegionNearby, self).__init__(*args, **kargs)
- def algorithm(self, operand, *args, **kargs):
- if self.value:
- operand = Region(operand).nearby(self.value)
- else:
- operand = Region(operand).nearby()
Advertisement
Add Comment
Please, Sign In to add comment