illume avatar illume committed 6ff6f77

Updated mac_scrap to pass some tests, and raise NotImplementedError otherwise.

Comments (0)

Files changed (3)

 # BUG	= fixed a bug that was (or could have been) crashing
 #
 #
-Feb 12, 2008 (svn 1107)
+
+
+Feb 11, 2008
+    An example of using pygame with no windowing system.  Useful for webserver
+      scripts, or for little utilities.
+
+    Updated mac_scrap to pass some tests, and raise NotImplementedError 
+      otherwise.
+
     Further Windows build updates:
       For MinGW/MSYS the build process now finds the MSYS and MinGW
       directories automatically. gcc links to msvcr71.dll without
 import sys
 import tempfile
 import pygame.image
-from pygame.locals import SCRAP_TEXT, SCRAP_BMP
+from pygame.locals import SCRAP_TEXT, SCRAP_BMP, SCRAP_SELECTION, SCRAP_CLIPBOARD
 from cStringIO import StringIO
 
 ScrapPboardType = u'org.pygame.scrap'
 
+
+err = "Only text has been implemented for scrap on mac. See lib/mac_scrap.py to debug."
+
+
+
 def init():
     return 1
 
 def get(scrap_type):
     board = NSPasteboard.generalPasteboard()
+    
+    if 0:
+        print board.types
+        print dir(board.types)
+        print dir(board)
+        print board.__doc__
+
     if scrap_type == SCRAP_TEXT:
         return board.stringForType_(NSStringPboardType)
-    elif scrap_type == SCRAP_BMP:
+    elif 1:
+        raise NotImplementedError(err)
+
+
+    elif 0 and scrap_type == SCRAP_BMP:
         # We could try loading directly but I don't trust pygame's TIFF
         # loading.  This is slow and stupid but it does happen to work.
         if not NSImage.canInitWithPasteboard_(board):
         data = rep.representationUsingType_properties_(NSBMPFileType, None)
         bmp = StringIO(data)
         return pygame.image.load(bmp, "scrap.bmp")
-    elif scrap_type in board.types:
+    #elif scrap_type in board.types:
+    elif scrap_type == SCRAP_BMP:
+        return board.dataForType_(scrap_type)
+    else:
         return board.stringForType_(scrap_type)
 
 def put(scrap_type, thing):
         if isinstance(thing, unicode):
             text_thing = thing
         else:
-            text_thing = unicode(text_thing, 'utf-8')
+            text_thing = unicode(thing, 'utf-8')
         board.setString_forType_(text_thing, NSStringPboardType)
         board.setString_forType_(u'', ScrapPboardType)
-    elif scrap_type == SCRAP_BMP:
+    elif 1:
+        raise NotImplementedError(err)
+
+
+
+
+
+    elif 0 and scrap_type == SCRAP_BMP:
+        # Don't use this code... we put the data in as a string.
+
+        #if type(thing) != type(pygame.Surface((1,1))):
+        #    thing = pygame.image.fromstring(thing, len(thing) * 4, "RGBA")
+
+
         # This is pretty silly, we shouldn't have to do this...
         fh = tempfile.NamedTemporaryFile(suffix='.png')
         pygame.image.save(thing, fh.name)
         board.declareTypes_owner_([NSTIFFPboardType, ScrapPboardType], None)
         board.setData_forType_(tiff, NSTIFFPboardType)
         board.setString_forType_(u'', ScrapPboardType)
+    elif scrap_type == SCRAP_BMP:
+        
+        other_type = scrap_type
+        board.declareTypes_owner_([other_type], None)
+        board.setData_forType_(thing, other_type)
+
     else:
-        pass
+        other_type = scrap_type
+        if 0:
+            board.declareTypes_owner_([NSStringPboardType, other_type], None)
+            board.setString_forType_(text_thing, NSStringPboardType)
+        elif 0:
+            board.declareTypes_owner_([other_type], None)
+            #board.setString_forType_(thing, other_type)
+            board.setData_forType_(thing, other_type)
+        else:
+            board.declareTypes_owner_([NSStringPboardType, other_type], None)
+            board.setString_forType_(thing, NSStringPboardType)
+
+        #board.setData_forType_(thing, other_type)
+
+
+
+
 
 def set_mode (mode):
     # No diversion between clipboard and selection modes on MacOS X.
-    pass
+    if mode not in [SCRAP_SELECTION, SCRAP_CLIPBOARD]:
+        raise ValueError("invalid clipboard mode")
 
 def contains (scrap_type):
     return scrap_type in NSPasteboard.generalPasteboard ().types ()

test/scrap_test.py

         sf = pygame.image.load ("examples/data/asprite.bmp")
         string = pygame.image.tostring (sf, "RGBA")
         scrap.put (pygame.SCRAP_BMP, string)
-        self.assertEquals (scrap.get (pygame.SCRAP_BMP), string)
+        self.assertEquals (scrap.get(pygame.SCRAP_BMP), string)
 
     def test_scrap_put (self):
         scrap.put ("arbitrary buffer", "buf")
-        self.assertEquals (scrap.get ("arbitrary buffer"), "buf")
+        r = scrap.get ("arbitrary buffer")
+        self.assertEquals (r, "buf")
 
 if __name__ == '__main__':
     pygame.init ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.