<s> '''<STR_LIT>''' <EOL> import sys <EOL> from ctypes import * <EOL> import pyglet . lib <EOL> from pyglet . window . carbon import _create_cfstring , carbon , _oscheck <EOL> from pyglet . window . carbon . constants import * <EOL> from pyglet . window . carbon . types import * <EOL> carbon = pyglet . lib . load_library ( <EOL> framework = '<STR_LIT>' ) <EOL> PasteboardRef = c_void_p <EOL> PasteboardItemID = c_void_p <EOL> CFArrayRef = c_void_p <EOL> CFDataRef = c_void_p <EOL> ItemCount = c_int32 <EOL> kPasteboardClipboard = _create_cfstring ( "<STR_LIT>" ) <EOL> kPasteboardModified = <NUM_LIT:1> << <NUM_LIT:0> <EOL> kPasteboardClientIsOwner = <NUM_LIT:1> << <NUM_LIT:1> <EOL> utf16_plain_text = _create_cfstring ( "<STR_LIT>" ) <EOL> traditional_mac_plain_text = _create_cfstring ( "<STR_LIT>" ) <EOL> carbon . CFDataGetBytePtr . restype = POINTER ( c_char ) <EOL> class CarbonPasteboard ( object ) : <EOL> def __init__ ( self ) : <EOL> self . pasteboard = PasteboardRef ( ) <EOL> carbon . PasteboardCreate ( kPasteboardClipboard , byref ( self . pasteboard ) ) <EOL> def get_text ( self ) : <EOL> item_count = ItemCount ( ) <EOL> _oscheck ( carbon . PasteboardGetItemCount ( self . pasteboard , <EOL> byref ( item_count ) ) ) <EOL> item_id = PasteboardItemID ( ) <EOL> flavor_type_array = CFArrayRef ( ) <EOL> flavor_data = CFDataRef ( ) <EOL> for item_index in range ( <NUM_LIT:1> , item_count . value + <NUM_LIT:1> ) : <EOL> _oscheck ( carbon . PasteboardGetItemIdentifier ( self . pasteboard , <EOL> item_index , byref ( item_id ) ) ) <EOL> _oscheck ( carbon . PasteboardCopyItemFlavors ( self . pasteboard , item_id , <EOL> byref ( flavor_type_array ) ) ) <EOL> flavor_count = carbon . CFArrayGetCount ( flavor_type_array ) <EOL> try : <EOL> for flavor_index in range ( flavor_count ) : <EOL> flavor_type = carbon . CFArrayGetValueAtIndex ( <EOL> flavor_type_array , flavor_index ) <EOL> if carbon . UTTypeConformsTo ( flavor_type , utf16_plain_text ) : <EOL> _oscheck ( carbon . PasteboardCopyItemFlavorData ( <EOL> self . pasteboard , item_id , flavor_type , <EOL> byref ( flavor_data ) ) ) <EOL> try : <EOL> data = carbon . CFDataGetBytePtr ( flavor_data ) <EOL> length = carbon . CFDataGetLength ( flavor_data ) <EOL> s = str ( data [ : length ] ) <EOL> if sys . byteorder == '<STR_LIT>' : <EOL> return s . decode ( '<STR_LIT>' ) <EOL> else : <EOL> return s . decode ( '<STR_LIT>' ) <EOL> finally : <EOL> carbon . CFRelease ( flavor_data ) <EOL> for flavor_index in range ( flavor_count ) : <EOL> flavor_type = carbon . CFArrayGetValueAtIndex ( <EOL> flavor_type_array , flavor_index ) <EOL> if carbon . UTTypeConformsTo ( flavor_type , <EOL> traditional_mac_plain_text ) : <EOL> _oscheck ( carbon . PasteboardCopyItemFlavorData ( <EOL> self . pasteboard , item_id , flavor_type , <EOL> byref ( flavor_data ) ) ) <EOL> try : <EOL> data = carbon . CFDataGetBytePtr ( flavor_data ) <EOL> length = carbon . CFDataGetLength ( flavor_data ) <EOL> return str ( data [ : length ] ) <EOL> finally : <EOL> carbon . CFRelease ( flavor_data ) <EOL> finally : <EOL> carbon . CFRelease ( flavor_type_array ) <EOL> return None <EOL> def put_text ( self , text ) : <EOL> if not text : return <EOL> _oscheck ( carbon . PasteboardClear ( self . pasteboard ) ) <EOL> sync_flags = carbon . PasteboardSynchronize ( self . pasteboard ) <EOL> if sync_flags & kPasteboardModified : <EOL> raise ValueError , "<STR_LIT>" <EOL> if not sync_flags & kPasteboardClientIsOwner : <EOL> raise ValueError , "<STR_LIT>" <EOL> if sys . byteorder == '<STR_LIT>' : <EOL> utf16_data = text . encode ( '<STR_LIT>' ) <EOL> else : <EOL> utf16_data = text . encode ( '<STR_LIT>' ) <EOL> data_ref = carbon . CFDataCreate ( None , utf16_data , len ( utf16_data ) ) <EOL> if not data_ref : <EOL> raise ValueError , "<STR_LIT>" <EOL> try : <EOL> _oscheck ( carbon . PasteboardPutItemFlavor ( self . pasteboard , <EOL> <NUM_LIT:1> , utf16_plain_text , data_ref , <NUM_LIT:0> ) ) <EOL> finally : <EOL> carbon . CFRelease ( data_ref ) <EOL> ascii_data = text . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> data_ref = carbon . CFDataCreate ( None , ascii_data , len ( ascii_data ) ) <EOL> if not data_ref : <EOL> raise ValueError , "<STR_LIT>" <EOL> try : <EOL> _oscheck ( carbon . PasteboardPutItemFlavor ( self . pasteboard , <EOL> <NUM_LIT:1> , traditional_mac_plain_text , data_ref , <NUM_LIT:0> ) ) <EOL> finally : <EOL> carbon . CFRelease ( data_ref ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> clipboard = CarbonPasteboard ( ) <EOL> print ` clipboard . get_text ( ) ` <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> clipboard . put_text ( '<STR_LIT>' . join ( sys . argv [ <NUM_LIT:1> : ] ) . decode ( '<STR_LIT:utf8>' ) ) </s>
