home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32_Demos_win32clipboardDemo.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  4.5 KB  |  131 lines

  1. # win32clipboardDemo.py
  2. #
  3. # Demo/test of the win32clipboard module.
  4. from win32clipboard import *
  5. import win32con
  6. import types
  7.  
  8. if not __debug__:
  9.     print "WARNING: The test code in this module uses assert"
  10.     print "This instance of Python has asserts disabled, so many tests will be skipped"
  11.  
  12. cf_names = {}
  13. # Build map of CF_* constants to names.
  14. for name, val in win32con.__dict__.items():
  15.     if name[:3]=="CF_" and name != "CF_SCREENFONTS": # CF_SCREEN_FONTS==CF_TEXT!?!?
  16.         cf_names[val] = name
  17.  
  18. def TestEmptyClipboard():
  19.     OpenClipboard()
  20.     try:
  21.         EmptyClipboard()
  22.         assert EnumClipboardFormats(0)==0, "Clipboard formats were available after emptying it!"
  23.     finally:
  24.         CloseClipboard()
  25.  
  26. def TestText():
  27.     OpenClipboard()
  28.     try:
  29.         text = "Hello from Python"
  30.         SetClipboardText(text)
  31.         got = GetClipboardData(win32con.CF_TEXT)
  32.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  33.         # Win32 documentation says I can get the result back as CF_UNICODE  or CF_OEMTEXT.
  34.         # But it appears I need to close the clipboard for this to kick-in.
  35.         # but if I attempt to, it fails!
  36.     finally:
  37.         CloseClipboard()
  38.  
  39.     OpenClipboard()
  40.     try:
  41.         got = GetClipboardData(win32con.CF_UNICODETEXT)
  42.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  43.         assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
  44.  
  45.         got = GetClipboardData(win32con.CF_OEMTEXT)
  46.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  47.  
  48.         # Unicode tests        
  49.         EmptyClipboard()
  50.         text = u"Hello from Python unicode"
  51.         # Now set the Unicode value
  52.         SetClipboardData(win32con.CF_UNICODETEXT, text)
  53.         # Get it in Unicode.
  54.         got = GetClipboardData(win32con.CF_UNICODETEXT)
  55.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  56.         assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
  57.  
  58.         # Close and open the clipboard to ensure auto-conversions take place.
  59.     finally:
  60.         CloseClipboard()
  61.  
  62.     OpenClipboard()
  63.     try:
  64.         
  65.         # Make sure I can still get the text.
  66.         got = GetClipboardData(win32con.CF_TEXT)
  67.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  68.         # Make sure we get back the correct types.
  69.         got = GetClipboardData(win32con.CF_UNICODETEXT)
  70.         assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
  71.         got = GetClipboardData(win32con.CF_OEMTEXT)
  72.         assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
  73.         print "Clipboard text tests worked correctly"       
  74.     finally:
  75.         CloseClipboard()
  76.  
  77. def TestClipboardEnum():
  78.     OpenClipboard()
  79.     try:
  80.         # Enumerate over the clipboard types
  81.         enum = 0
  82.         while 1:
  83.             enum = EnumClipboardFormats(enum)
  84.             if enum==0:
  85.                 break
  86.             assert IsClipboardFormatAvailable(enum), "Have format, but clipboard says it is not available!"
  87.             n = cf_names.get(enum,"")
  88.             if not n:
  89.                 try:
  90.                     n = GetClipboardFormatName(enum)
  91.                 except error:
  92.                     n = "unknown (%s)" % (enum,)
  93.  
  94.             print "Have format", n
  95.         print "Clipboard enumerator tests worked correctly"       
  96.     finally:
  97.         CloseClipboard()
  98.  
  99. class Foo:
  100.     def __init__(self, **kw):
  101.         self.__dict__.update(kw)
  102.     def __cmp__(self, other):
  103.         return cmp(self.__dict__, other.__dict__)
  104.  
  105. def TestCustomFormat():
  106.     OpenClipboard()
  107.     try:
  108.         # Just for the fun of it pickle Python objects through the clipboard
  109.         fmt = RegisterClipboardFormat("Python Pickle Format")
  110.         import cPickle
  111.         pickled_object = Foo(a=1, b=2, Hi=3)
  112.         SetClipboardData(fmt, cPickle.dumps( pickled_object ) )
  113.         # Now read it back.
  114.         data = GetClipboardData(fmt)
  115.         loaded_object = cPickle.loads(data)
  116.         assert cPickle.loads(data) == pickled_object, "Didnt get the correct data!"
  117.  
  118.         print "Clipboard custom format tests worked correctly"       
  119.     finally:
  120.         CloseClipboard()
  121.     
  122.     
  123. if __name__=='__main__':
  124.     TestEmptyClipboard()
  125.     TestText()
  126.     TestCustomFormat()
  127.     TestClipboardEnum()
  128.     # And leave it empty at the end!
  129.     TestEmptyClipboard()
  130.     
  131.