Guest User

Untitled

a guest
Jan 14th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 KB | None | 0 0
  1. import pickle
  2. from core.lib.png import Reader
  3. from core.lib.logger import RobotLogger
  4. from core.lib.wrapper import Region, Pattern, Screen
  5.  
  6. """
  7. Allows for transforms to be stored in PNG files.  Transforms are used to control how the PNG
  8. is matched and how the pre-match and post-match regions are treated.
  9.  
  10. """
  11.  
  12.  
  13. class Transforms(object):
  14.    
  15.     log = None
  16.     transforms = None
  17.    
  18.     # CONTEXT TYPES
  19.     CONTEXT_PREVIOUS = "PREVIOUS"    
  20.     CONTEXT_NEXT = "NEXT"
  21.     CONTEXT_CURRENT = "CURRENT"    
  22.    
  23.     def __init__(self, source, entity=None, *args, **kargs):
  24.         """ Returns a pattern and attributes given a filename """        
  25.         super(Transforms, self).__init__(*args, **kargs)
  26.  
  27.         entity = entity if entity else self
  28.         self.log = RobotLogger(entity=entity) # get logger singleton            
  29.        
  30.         defaultStructure = { \
  31.                         Transforms.CONTEXT_PREVIOUS: [ \
  32.                             RegionNearby(10), \
  33.                                                       ], \
  34.                         Transforms.CONTEXT_CURRENT: [], \
  35.                         Transforms.CONTEXT_NEXT: [ \
  36.                                                   ] \
  37.                     }    
  38.        
  39.  
  40.         # If we have a string, a path is supplied to png
  41.         if isinstance(source, unicode) or isinstance(source, str):
  42.            
  43.             self.log.debug("Loading [%s]" % source)
  44.            
  45.             # Try and get extra attributes
  46.             try:                                        
  47.                 transforms = pickle.loads( Reader(filename=source).chunk('tEXt')[1] )
  48.             except TypeError, e: # If doesn't contain a tEXt section
  49.                 pass
  50.             except:
  51.                 self.log.warn("Unknown PNG transforms format, using defaults structure [%s](%s)" % (source, self.log.image(source)))      
  52.                 transforms = defaultStructure        
  53.        
  54.         # Otherwise a dictionary should be supplied
  55.         else:
  56.             transforms = source
  57.                        
  58.         try:
  59.             assert transforms.get(self.CONTEXT_PREVIOUS, None) != None
  60.             assert transforms.get(self.CONTEXT_CURRENT, None) != None
  61.             assert transforms.get(self.CONTEXT_NEXT, None) != None
  62.         except:
  63.             # If doesn't contain these keys, default to empty
  64.             self.log.debug("Unknown PNG transform structure, using defaults")
  65.             transforms = defaultStructure
  66.            
  67.         self.transforms = transforms        
  68.            
  69.     def apply(self, operand, context, override=None, *args, **kargs):
  70.        
  71.         # Allow for override on the operand
  72.         if override:
  73.             operand = override        
  74.                
  75.         # Apply the transforms
  76.         for transform in self.transforms[context]:            
  77.             result = transform.apply(operand, *args, **kargs)
  78.            
  79.             if isinstance(operand, Pattern):
  80.                 self.log.debug("[%s] Applied [%s] transform, [%s] -> [%s] " % (context, transform, operand, result))
  81.             else:
  82.                 self.log.debug("[%s] Applied [%s] transform, [%s](%s) -> [%s](%s) " % (context, transform, operand, self.log.region(operand), result, self.log.region(result)))
  83.            
  84.            
  85.             operand = result
  86.            
  87.         return operand
  88.  
  89.     def formatPrefix(self, *args, **kargs):
  90.        
  91.         return "[PNGTransforms] "
  92.  
  93. class PatternSimilarity(object):
  94.    
  95.     similarity = None
  96.    
  97.     def __init__(self, similarity):        
  98.         self.similarity = similarity
  99.    
  100.     def apply(self, operand):        
  101.         return operand.similar(self.similarity)
  102.        
  103.     def __str__(self, *args, **kwargs):
  104.         return "%s similarity=%d" % (self.__class__.__name__, self.similarity)        
  105.        
  106. class PatternTargetOffset(object):
  107.    
  108.     dx = 0
  109.     dy = 0
  110.    
  111.     def __init__(self, dx, dy):        
  112.         self.dx = dx
  113.         self.dy = dy
  114.    
  115.     def apply(self, operand):
  116.         return operand.targetOffset(self.dx, self.dy)
  117.  
  118.     def __str__(self, *args, **kwargs):
  119.         return "%s dx=%s dy=%s" % (self.__class__.__name__, self.dx, self.dy)
  120.  
  121. class RegionalTransform(object):    
  122.  
  123.     limit = None
  124.    
  125.     def __init__(self, value=None):
  126.         self.value = value
  127.    
  128.     def apply(self, operand):
  129.                
  130.         # Don't transform if we're working with screen
  131.         if isinstance(operand, Screen):
  132.             return operand
  133.         else:
  134.             return self.algorithm(operand)
  135.    
  136.     def __str__(self, *args, **kwargs):
  137.         return "%s value=%s" % (self.__class__.__name__, self.value)
  138.    
  139. class RegionAbove(RegionalTransform):
  140.    
  141.     def __init__(self, *args, **kargs):
  142.         super(RegionAbove, self).__init__(*args, **kargs)
  143.        
  144.     def algorithm(self, operand, *args, **kargs):
  145.        
  146.         if self.value:        
  147.             operand = Region(operand).above(self.value)
  148.         else:
  149.             operand = Region(operand).above()
  150.        
  151.    
  152. class RegionBelow(RegionalTransform):
  153.  
  154.     def __init__(self, *args, **kargs):
  155.         super(RegionBelow, self).__init__(*args, **kargs)
  156.        
  157.     def algorithm(self, operand, *args, **kargs):
  158.        
  159.         if self.value:
  160.             operand = Region(operand).below(self.value)
  161.         else:
  162.             operand = Region(operand).below()                                
  163.        
  164.        
  165. class RegionRight(RegionalTransform):
  166.        
  167.     def __init__(self, *args, **kargs):
  168.         super(RegionRight, self).__init__(*args, **kargs)
  169.        
  170.     def algorithm(self, operand, *args, **kargs):
  171.  
  172.         if self.value:
  173.             operand = Region(operand).right(self.value)
  174.         else:
  175.             operand = Region(operand).right()
  176.        
  177.  
  178.  
  179. class RegionLeft(RegionalTransform):
  180.  
  181.     def __init__(self, *args, **kargs):
  182.         super(RegionLeft, self).__init__(*args, **kargs)
  183.  
  184.     def algorithm(self, operand, *args, **kargs):
  185.        
  186.         if self.value:
  187.             operand = Region(operand).left(self.value)
  188.         else:
  189.             operand = Region(operand).left()
  190.        
  191.    
  192.  
  193. class RegionNearby(RegionalTransform):
  194.  
  195.     def __init__(self, *args, **kargs):
  196.         super(RegionNearby, self).__init__(*args, **kargs)
  197.  
  198.     def algorithm(self, operand, *args, **kargs):
  199.        
  200.         if self.value:
  201.             operand = Region(operand).nearby(self.value)
  202.         else:
  203.             operand = Region(operand).nearby()
Advertisement
Add Comment
Please, Sign In to add comment