2020-12-20

12: Linux or Windows Clipboard with Python: in Multi-Formats

<The previous article in this series | The table of contents of this series |

wxPython is used. Not just the text format. Many formats are supported.

Topics


About: the Python programming language

The table of contents of this article


Starting Context


  • The reader has a basic knowledge on the Python programming language.

Target Context


  • The reader will know how to get and set a composite of format-specific data from and to the operating system clipboard, in Python, with wxPython.

Orientation


Many, but not all the, formats are supported by this technique.

More , but still not all the, formats are supported by a technique in C# for Microsoft Windows, as will be introduced in a future article of another series.

All the formats are supported by a technique in C++ for Linux or Microsoft Windows, as will be introduced in a future article of another series.

The reason why any Python variable is a pointer (a prerequisite knowledge) has been explained in a previous article.


Main Body


1: Aim: to Implement Multiple Copy Buffers for a Word Processor


Hypothesizer 7
As I use Vim as a text editor, one of the benefits is that it has multiple copy buffers: I can copy a piece of text to the buffer 'a', another piece of text to the buffer 'b', etc.. That is huge help.

As I use LibreOffice Writer as a word processor, I want that functionality in it.

In fact, having been accustomed to the functionality, the lack of it is a considerable frustration.

A Python macro will implement the functionality, if it can get and set clipboard data.


2: The Text Format Is Not Enough


Hypothesizer 7
Getting and setting a text format clipboard datum is easy; for example, 'pyperclip' can do the work.

But, obviously, that is not enough for word processor: I need the fonts information to be copied, at least. And some image data and objects like tables may want to be copied to multiple buffers, too.

Ideally, the whole clipboard data should be copied as they are.


3: I Will Use wxPython


Hypothesizer 7
As I searched the internet, I could not find any tutorial for getting and setting the whole clipboard data in Python (ones that handle text data abound, though).

As I use wxPython, hoping that it may have the functionality, I searched the official wxPython document and found some information.

'wx.CustomDataObjec' sounded promising, but turned out to be for a datum of a single format, not for any composite of format-specific data.

'wx.DataObjectComposite' is for composites, but seems rather cumbersome unless I have a predetermined set of supported datum formats.

Well, 'wx.DataObject' (which is described as "for maximum flexibility and efficiency, but it is also the most difficult to implement" in the document) seems to be the one I should use, but even it seems to be supposing that I have a predetermined set of supported datum formats, which I do not.

After all, the concept of wxPython clipboard handling does not match my interest of getting and setting the whole clipboard data of whatever multiple datum formats, but I will see what I can do.


4: Some Basics of Clipboard Datum Formats


Hypothesizer 7
Although the clipboard mechanism of X Window system and that of Microsoft Windows are very different, at least wxPython-wise, each clipboard datum format always has a number as the identification of the format.

However, the number may be ephemeral, which means that a format may have '49161' at a time and '49162' at another; so, generally speaking, I cannot permanently identify a format with the number (although some usual formats like 'Text' have fixed numbers).

On the other hand, some formats do not have names (or at least the names cannot be retrieved API-wise).

However, such formats should have fixed numbers.

So, each of the formats that have names can be identified by the name, while each of the formats that have no name can be identified by the number.


5: The Specifications of 'wx.DataObject'


Hypothesizer 7
As the official document is not so clear on the specifications of the class (especially on when each method is called), I had to experiment to know necessary information.

An important point I have found is that any instance of 'wx.DataObject' becomes invalid after it has been passed into the 'SetData' method of 'wx.Clipboard', where 'invalid'.means that it cannot be passed into the 'SetData' method again

Well, that means that 'wx.DataObject' cannot be used as a clipboard data container for my purpose (as my clipboard data have to be able to be set into the clipboard repeatedly). So, 'wx.DataObject' is a clipboard events handler, for me.

These are what I have found out about the timings at which the methods are called.

MethodCalled When
GetFormatCountWhen 'wx.Clipboard.GetData' has been called
1st for the composite
1st per format
When 'wx.Clipboard.SetData' has been called
after 'GetPreferredFormat' for the composite
1st per format
GetAllFormatsWhen 'wx.Clipboard.GetData' has been called
after 'GetFormatCount' for the composite
after 'GetFormatCount' per format
When 'wx.Clipboard.SetData' has been called
after 'GetFormatCount' for the composite
after 'GetFormatCount' per format
GetDataHereWhen 'wx.Clipboard.SetData' has been called
after 'GetDataSize' per format
GetDataSizeWhen 'wx.Clipboard.SetData' has been called
twice after 'GetAllFormats' per format
GetPreferredFormatWhen 'wx.Clipboard.SetData' has been called
1st for the composite
IsSupportedNever
SetDataWhen 'wx.Clipboard.GetData' has been called
after 'GetAllFormats' per format

For 'wx.Clipboard.GetData', 'GetFormatCount' and 'GetAllFormats' have to respond with all the possible datum formats known a priori, because the existing datum formats in the clipboard are unknown to my program. But I can be assured that the methods that are called per format are called for only datum formats that exist in the clipboard (not all the datum formats returned by 'GetAllFormats').

Anyway, I find some things enigmatic. 1st, why 'GetFormatCount' and 'GetAllFormats' are called per format? 2nd, why is 'GetDataSize' called twice per format? 3rd, 'GetPreferredFormat' is called only for 'wx.Clipboard.SetData' in spite of the official document, and I do not see any effect whatever it returns.

The 'buf' arguments of 'GetDataHere' and 'SetData' are 'memoryview' instances.


6: How to Know the Possible Datum Formats a Priori


Hypothesizer 7
How can I know possible datum formats a priori?

In Linux, this command shows the datum formats that exist in the clipboard.

@bash Source Code
xclip -t TARGETS -o

In Microsoft Windows, well, I have created a C++ Win32 API program, as will be seen in a future article of another series.

For example, when a text piece in LibreOffice Writer is copied in Linux, the available formats are these.

@Output
application/x-openoffice-embed-source-xml;windows_formatname="Star Embed Source (XML)"
text/rtf
text/richtext
text/html
text/plain;charset=utf-16
application/x-openoffice-objectdescriptor-xml;windows_formatname="Star Object Descriptor (XML)";classname="8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6";typename="LibreOffice 6.4 Text Document";viewaspect="1";width="16999";height="2995";posx="0";posy="0"
text/plain;charset=utf-8
text/plain
UTF8_STRING
STRING
TEXT
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS

For Microsoft Windows, the available formats are these.

@Output
DataObject
Star Embed Source (XML)
Rich Text Format
Richtext Format
HTML (HyperText Markup Language)
HTML Format
UnicodeText
Text
Link
Star Object Descriptor (XML)
Ole Private Data
Locale
OEMText


7: My Code


Hypothesizer 7
As I noticed earlier, 'wx.DataObject' cannot be used as a data container for my purpose. So, I have created a data container class, 'theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataComposite' (and 'theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDatum' as its component) and a data history class, 'theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataCompositesHistory', which is the multi buffers container.

theBiasPlanet/coreUtilities/clipboardHandling/ClipboardFormatSpecificDatum.py

@Python Source Code

class ClipboardFormatSpecificDatum:
	def __init__ (a_this: "ClipboardFormatSpecificDatum", a_formatName: str, a_formatNumber: int, a_datum: bytearray) -> None:
		a_this.i_formatName: str = a_formatName
		a_this.i_formatNumber: int = a_formatNumber
		a_this.i_datum: bytearray = a_datum
	
	def __del__ (a_this: "ClipboardFormatSpecificDatum") -> None:
		None
	
	def getFormatName (a_this: "ClipboardFormatSpecificDatum") -> str:
		return a_this.i_formatName
	
	def getFormatNumber (a_this: "ClipboardFormatSpecificDatum") -> int:
		return a_this.i_formatNumber
	
	def getDatumSize (a_this: "ClipboardFormatSpecificDatum") -> int:
		return len (a_this.i_datum)
	
	def getDatum (a_this: "ClipboardFormatSpecificDatum") -> bytearray:
		return a_this.i_datum


theBiasPlanet/coreUtilities/clipboardHandling/ClipboardFormatSpecificDataComposite.py

@Python Source Code
from typing import List
from collections import OrderedDict
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDatum import ClipboardFormatSpecificDatum

class ClipboardFormatSpecificDataComposite:
	def __init__ (a_this: "ClipboardFormatSpecificDataComposite") -> None:
		a_this.i_formatNameToDatumMap: "OrderedDict [str, ClipboardFormatSpecificDatum]" = OrderedDict ()
	
	def __del__ (a_this: "ClipboardFormatSpecificDataComposite") -> None:
		None
	
	def addFormatSpecificDatum (a_this: "ClipboardFormatSpecificDataComposite", a_formatSpecificDatum: "ClipboardFormatSpecificDatum") -> bool:
		a_this.i_formatNameToDatumMap [a_formatSpecificDatum.getFormatName ()] = a_formatSpecificDatum
		return True
	
	def getFormatNames (a_this: "ClipboardFormatSpecificDataComposite") -> List [str]:
		l_formatNames: List [str] = []
		l_formatName: str = None
		for l_formatName in a_this.i_formatNameToDatumMap:
			l_formatNames.append (l_formatName)
		return l_formatNames
	
	def getFormatSpecificDatum (a_this: "ClipboardFormatSpecificDataComposite", a_formatName: str) -> "ClipboardFormatSpecificDatum":
		l_formatSpecificDatum: "ClipboardFormatSpecificDatum" = None
		try:
			l_formatSpecificDatum = a_this.i_formatNameToDatumMap [a_formatName]
		except (KeyError) as l_exception:
			None
		return l_formatSpecificDatum


theBiasPlanet/coreUtilities/clipboardHandling/ClipboardFormatSpecificDataCompositesHistory.py

@Python Source Code
from collections import OrderedDict
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataComposite import ClipboardFormatSpecificDataComposite

class ClipboardFormatSpecificDataCompositesHistory:
	def __init__ (a_this: "ClipboardFormatSpecificDataCompositesHistory") -> None:
		a_this.i_dataCompositeKeyToDataCompositeMap: "OrderedDict [str, ClipboardFormatSpecificDataComposite]" = OrderedDict ()
	
	def __del__ (a_this: "ClipboardFormatSpecificDataCompositesHistory") -> None:
		None
	
	def addDataComposite (a_this: "ClipboardFormatSpecificDataCompositesHistory", a_dataCompositeKey: str, a_dataComposite: "ClipboardFormatSpecificDataComposite") -> bool:
		a_this.i_dataCompositeKeyToDataCompositeMap [a_dataCompositeKey] = a_dataComposite
		return True
	
	def removeDataComposite (a_this: "ClipboardFormatSpecificDataCompositesHistory", a_dataCompositeKey: str) -> bool:
		try:
			a_this.i_dataCompositeKeyToDataCompositeMap.pop (a_dataCompositeKey)
			return True
		except (KeyError) as l_exception:
			return False
	
	def getDataComposite (a_this: "ClipboardFormatSpecificDataCompositesHistory", a_dataCompositeKey: str) -> "ClipboardFormatSpecificDataComposite":
		l_dataComposite: "ClipboardFormatSpecificDataComposite" = None
		try:
			l_dataComposite = a_this.i_dataCompositeKeyToDataCompositeMap [a_dataCompositeKey]
		except (KeyError) as l_exception:
			None
		return l_dataComposite


The 'wx.DataObject' extension class is this.

theBiasPlanet/coreUtilities/clipboardHandling/WxPythonClipboardEventsHandler.py

@Python Source Code
from typing import List
from typing import cast
from collections import OrderedDict
import sys
from wx import DataFormat as wx_DataFormat
from wx import DataObject as wx_DataObject
import wx
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataComposite import ClipboardFormatSpecificDataComposite
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDatum import ClipboardFormatSpecificDatum

class WxPythonClipboardEventsHandler (wx_DataObject):
	s_possibleDatumFormatNameToFormatMap: "OrderedDict [str, wx_DataFormat]" = None
	s_preferedDatumFormatName: str = None
	s_possibleDatumFormats: List [wx_DataFormat] = None
	
	@staticmethod
	def getDatumFormatName (a_datumFormat: wx_DataFormat) -> str:
		l_datumFormatNumber: int = a_datumFormat.GetType ()
		l_datumFormatName: str = None
		if l_datumFormatNumber == wx.DF_TEXT:
			l_datumFormatName = "Text"
		elif l_datumFormatNumber == wx.DF_BITMAP:
			l_datumFormatName = "Bitmap"
		elif l_datumFormatNumber == wx.DF_METAFILE:
			l_datumFormatName = "Metafile"
		elif l_datumFormatNumber == wx.DF_FILENAME:
			l_datumFormatName = "Filename"
		elif l_datumFormatNumber == wx.DF_HTML:
			l_datumFormatName = "HTML Format"
		else:
			l_datumFormatName = a_datumFormat.GetId ()
		return l_datumFormatName
	
	def __init__ (a_this: "WxPythonClipboardEventsHandler", a_formatSpecificDataComposite: "ClipboardFormatSpecificDataComposite") -> None:
		a_this.i_formatSpecificDataComposite: "ClipboardFormatSpecificDataComposite" = a_formatSpecificDataComposite
		a_this.i_supportedDatumFormats: List [wx_DataFormat] = None
		
		super ().__init__ ()
	l_availableDatumFormatNames: List [str] = a_this.i_formatSpecificDataComposite.getFormatNames ()
		if len (l_availableDatumFormatNames) > 0:
			a_this.i_supportedDatumFormats = []
		l_datumFormatName: str = None
			for l_datumFormatName in l_availableDatumFormatNames:
				a_this.i_supportedDatumFormats.append (WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap [l_datumFormatName])
		else:
			a_this.i_supportedDatumFormats = WxPythonClipboardEventsHandler.s_possibleDatumFormats
	
	def getFormatSpecificDataComposite (a_this: "WxPythonClipboardEventsHandler") -> "ClipboardFormatSpecificDataComposite":
		return a_this.i_formatSpecificDataComposite
	
	def GetFormatCount (a_this: "WxPythonClipboardEventsHandler", a_datumTransferDirection: wx_DataObject.Direction = wx_DataObject.Direction.Get) -> int:
		sys.stdout.write ("### GetFormatCount: {0:d}\n".format (len (a_this.i_supportedDatumFormats)))
		return len (a_this.i_supportedDatumFormats)
	
	def GetAllFormats (a_this: "WxPythonClipboardEventsHandler", a_datumTransferDirection: wx_DataObject.Direction = wx_DataObject.Direction.Get) -> List [wx_DataFormat]:
		sys.stdout.write ("### GetAllFormats:\n")
		return a_this.i_supportedDatumFormats
	
	# 'a_formatSpecificDatum' is not really 'bytearray', but 'memoryview', but the stub of 'memoryview' seems to be mistaken, which makes me use 'bytearray' instead.
	def GetDataHere (a_this: "WxPythonClipboardEventsHandler", a_datumFormat: wx_DataFormat, a_formatSpecificDatum: bytearray) -> bool:
		sys.stdout.write ("### GetDataHere: {0:s}\n".format (WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat)))
		l_formatSpecificDatumIsFound: bool = False
		if a_formatSpecificDatum is not None:
			l_formatSpecificDatum: "ClipboardFormatSpecificDatum" = a_this.i_formatSpecificDataComposite.getFormatSpecificDatum (WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat))
			if l_formatSpecificDatum is not None:
				l_copiedFormatSpecificDatum: bytearray = l_formatSpecificDatum.getDatum ()
				l_byteIndex: int = 0
				for l_byteIndex in range (0, len (a_formatSpecificDatum), 1):
					#a_formatSpecificDatum [l_byteIndex:l_byteIndex + 1] = cast (Sequence [bytes], bytes ([l_copiedFormatSpecificDatum [l_byteIndex]])) # This is if I rather use 'memoryview'.
					a_formatSpecificDatum [l_byteIndex] = l_copiedFormatSpecificDatum [l_byteIndex]
				l_formatSpecificDatumIsFound = True
		return l_formatSpecificDatumIsFound
	
	def GetDataSize (a_this: "WxPythonClipboardEventsHandler", a_datumFormat: wx_DataFormat) -> int:
		sys.stdout.write ("### GetDataSize:\n")
		l_size: int = 0
		l_formatSpecificDatum: "ClipboardFormatSpecificDatum" = a_this.i_formatSpecificDataComposite.getFormatSpecificDatum (WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat))
		if l_formatSpecificDatum is not None:
			l_size = len (l_formatSpecificDatum.getDatum ())
		return l_size
	
	def GetPreferredFormat (a_this: "WxPythonClipboardEventsHandler", a_datumTransferDirection: wx_DataObject.Direction = wx_DataObject.Direction.Get) -> wx_DataFormat:
		sys.stdout.write ("### GetPreferredFormat:\n")
		return WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap [WxPythonClipboardEventsHandler.s_preferedDatumFormatName]
	
	def IsSupported (a_this: "WxPythonClipboardEventsHandler", a_datumFormat: wx_DataFormat, a_datumTransferDirection: wx_DataObject.Direction = wx_DataObject.Direction.Get) -> bool:
		sys.stdout.write ("### IsSupported:\n")
		try:
			WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap [WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat)]
			return True
		except (KeyError) as l_exception:
			return False
	
	def SetData (a_this: "WxPythonClipboardEventsHandler", a_datumFormat: wx_DataFormat, a_formatSpecificDatum: memoryview) -> bool:
		sys.stdout.write ("### SetData: {0:s}\n".format (WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat)))
		l_datumFormatName: str = WxPythonClipboardEventsHandler.getDatumFormatName (a_datumFormat)
		if a_formatSpecificDatum is not None:
			l_datumSize = len (a_formatSpecificDatum)
			l_copiedFormatSpecificDatum: bytearray = None
			if l_datumFormatName == "Text" or l_datumFormatName == "HTML Format":
				l_copiedFormatSpecificDatum = bytearray (l_datumSize + 1)
				l_copiedFormatSpecificDatum [l_datumSize] = 0
			else:
				l_copiedFormatSpecificDatum = bytearray (l_datumSize)
			l_byteIndex: int = 0
			for l_byteIndex in range (0, len (a_formatSpecificDatum), 1):
				l_copiedFormatSpecificDatum [l_byteIndex] = a_formatSpecificDatum [l_byteIndex]
			a_this.i_formatSpecificDataComposite.addFormatSpecificDatum (ClipboardFormatSpecificDatum (l_datumFormatName, a_datumFormat.GetType (), l_copiedFormatSpecificDatum))
		return False

Of course, you cannot do like 'a_formatSpecificDatum = l_formatSpecificDatum.getDatum ()' for 'GetDataHere' (if you think you can, you do not understand that any Python variable is a pointer).

The clipboard handling class is this.

theBiasPlanet/coreUtilities/clipboardHandling/WxPythonClipboard.py

@Python Source Code
from typing import List
from collections import OrderedDict
from wx import DataFormat as wx_DataFormat
import wx
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataComposite import ClipboardFormatSpecificDataComposite
from theBiasPlanet.coreUtilities.clipboardHandling.WxPythonClipboardEventsHandler import WxPythonClipboardEventsHandler

class WxPythonClipboard:
	@staticmethod
	def setUp (a_datumFormatNameToFormatMap: "OrderedDict [str, wx_DataFormat]", a_preferedDatumFormatName: str) -> bool:
		WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap = a_datumFormatNameToFormatMap
		WxPythonClipboardEventsHandler.s_preferedDatumFormatName = a_preferedDatumFormatName
		
		WxPythonClipboardEventsHandler.s_possibleDatumFormats = []
		l_datumFormatName: str = None
		for l_datumFormatName in WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap:
			WxPythonClipboardEventsHandler.s_possibleDatumFormats.append (WxPythonClipboardEventsHandler.s_possibleDatumFormatNameToFormatMap [l_datumFormatName])
		return True
	
	@staticmethod
	def openClipboard () -> bool:
		wx.TheClipboard.Open ()
		return True
	
	@staticmethod
	def closeClipboard () -> bool:
		wx.TheClipboard.Close ()
		return True
	
	@staticmethod
	def clearClipboard () -> bool:
		wx.TheClipboard.Clear ()
		return True
	
	@staticmethod
	def getFormatSpecificDataComposite () -> "ClipboardFormatSpecificDataComposite":
		l_wxPythonClipboardEventsHandler: "WxPythonClipboardEventsHandler" = WxPythonClipboardEventsHandler (ClipboardFormatSpecificDataComposite ())
		wx.TheClipboard.GetData (l_wxPythonClipboardEventsHandler)
		return l_wxPythonClipboardEventsHandler.getFormatSpecificDataComposite ()
	
	@staticmethod
	def setFormatSpecificDataComposite (a_formatSpecificDataComposite: "ClipboardFormatSpecificDataComposite") -> bool:
		l_wxPythonClipboardEventsHandler: "WxPythonClipboardEventsHandler" = WxPythonClipboardEventsHandler (a_formatSpecificDataComposite)
		wx.TheClipboard.SetData (l_wxPythonClipboardEventsHandler)
		wx.TheClipboard.Flush ()
		return True



8: The Limitation


Hypothesizer 7
In fact, not the data of all the formats can be gotten or set in this technique.

For example, in Microsoft Windows, for a LibreOffice Writer clipboard data, the 'UnicodeText', 'Locale', and 'OEMText' data cannot be gotten.

Why? Well, in Microsoft Windows, it is not that the datum of any format can be just gotten in a uniform way as a bytes array; the data of a format have to be gotten in a certain way, but wxPython has not made the effort of supporting all the possible formats.

If the supported formats are not satisfactory, I will have to go to another technique (there will be an article for C# and an article for C++)


9: Testing


Hypothesizer 7
My test code is this for Microsoft Windows.

@Python Source Code
from typing import List
rom typing import TextIO
import sys
import wx
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataComposite import ClipboardFormatSpecificDataComposite
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDataCompositesHistory import ClipboardFormatSpecificDataCompositesHistory
from theBiasPlanet.coreUtilities.clipboardHandling.ClipboardFormatSpecificDatum import ClipboardFormatSpecificDatum
from theBiasPlanet.coreUtilities.clipboardHandling.WxPythonClipboard import WxPythonClipboard
from theBiasPlanet.coreUtilities.clipboardHandling.WxPythonClipboardEventsHandler import WxPythonClipboardEventsHandler
from theBiasPlanet.coreUtilities.constantsGroups.ClipboardDatumFormatNameToFormatMapsConstantsGroup import ClipboardDatumFormatNameToFormatMapsConstantsGroup

class Test1Test:
	@staticmethod
	def main (a_arguments: List [str]) -> None:
		Test1Test.test ()
	
	@staticmethod
	def test () -> None:
		l_clipbardFormatSpecificDataCompositesHistory: "ClipboardFormatSpecificDataCompositesHistory" = ClipboardFormatSpecificDataCompositesHistory ()
		l_standardInputReader: TextIO = sys.stdin
		WxPythonClipboard.setUp (ClipboardDatumFormatNameToFormatMapsConstantsGroup.c_windowsDatumFormatNameToFormatMap, "Text")
		while True:
			sys.stdout.write ("### Input 'S' (Set), 'G' (Get), 'D (Display)', or 'Q' (Quit):\n")
			sys.stdout.flush ()
			l_userInput: str = l_standardInputReader.read (2)
			l_clipboardFormatSpecificDataCompositeKey: str
			if l_userInput.startswith ("S") or l_userInput.startswith ("G") or l_userInput.startswith ("D"):
				sys.stdout.write ("### Input the key:\n")
				sys.stdout.flush ()
				l_clipboardFormatSpecificDataCompositeKey = l_standardInputReader.read (2) [0]
				if l_userInput.startswith ("S"):
					WxPythonClipboard.openClipboard ()
					WxPythonClipboard.clearClipboard ()
					WxPythonClipboard.setFormatSpecificDataComposite (l_clipbardFormatSpecificDataCompositesHistory.getDataComposite (l_clipboardFormatSpecificDataCompositeKey))
					WxPythonClipboard.closeClipboard ()
				elif l_userInput.startswith ("G"):
					WxPythonClipboard.openClipboard ()
					l_clipbardFormatSpecificDataCompositesHistory.addDataComposite (l_clipboardFormatSpecificDataCompositeKey, WxPythonClipboard.getFormatSpecificDataComposite ())
					WxPythonClipboard.closeClipboard ()
				elif l_userInput.startswith ("D"):
					l_clipboardFormatSpecificDataComposite: "ClipboardFormatSpecificDataComposite " = l_clipbardFormatSpecificDataCompositesHistory.getDataComposite (l_clipboardFormatSpecificDataCompositeKey)
					l_clipboardDatumFormatNames: List [str] = l_clipboardFormatSpecificDataComposite.getFormatNames ()
					l_clipboardDatumFormatName: str = None
					for l_clipboardDatumFormatName in l_clipboardDatumFormatNames:
						l_clipboardFormatSpecificDatum: "ClipboardFormatSpecificDatum " = l_clipboardFormatSpecificDataComposite.getFormatSpecificDatum (l_clipboardDatumFormatName)
						sys.stdout.write ("### clipboard datum format number, format name, size: {0:d}, {1:s}, {2:d}\n".format (l_clipboardFormatSpecificDatum.getFormatNumber (), l_clipboardDatumFormatName, l_clipboardFormatSpecificDatum.getDatumSize ()))
						sys.stdout.flush ()
			else:
				break

if __name__ == "__main__":
	l_application = wx.App ()
	Test1Test.main (sys.argv)


theBiasPlanet/coreUtilities/constantsGroups/ClipboardDatumFormatNameToFormatMapsConstantsGroup.py

@Python Source Code
from collections import OrderedDict
from wx import DataFormat as wx_DataFormat
import wx

class ClipboardDatumFormatNameToFormatMapsConstantsGroup:
	c_linuxDatumFormatNameToFormatMap: "OrderedDict [str, wx_DataFormat]" = OrderedDict ({"Text": wx_DataFormat (wx.DF_TEXT), "Bitmap": wx_DataFormat (wx.DF_BITMAP), "Metafile": wx_DataFormat (wx.DF_METAFILE), "Filename": wx_DataFormat (wx.DF_FILENAME), "HTML Format": wx_DataFormat (wx.DF_HTML), "application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"": wx_DataFormat ("application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\""), "text/rtf": wx_DataFormat ("text/rtf"), "text/richtext": wx_DataFormat ("text/richtext"), "text/html": wx_DataFormat ("text/html"), "text/plain;charset=utf-16": wx_DataFormat ("text/plain;charset=utf-16"), "application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\";classname=\"8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6\";typename=\"LibreOffice 6.4 Text Document\";viewaspect=\"1\";width=\"16999\";height=\"2995\";posx=\"0\";posy=\"0\"": wx_DataFormat ("application/x-openoffice-objectdescriptor-xml;windows_formatname=\"Star Object Descriptor (XML)\";classname=\"8BC6B165-B1B2-4EDD-aa47-dae2ee689dd6\";typename=\"LibreOffice 6.4 Text Document\";viewaspect=\"1\";width=\"16999\";height=\"2995\";posx=\"0\";posy=\"0\""), "text/plain;charset=utf-8": wx_DataFormat ("text/plain;charset=utf-8"), "text/plain": wx_DataFormat ("text/plain"), "UTF8_STRING": wx_DataFormat ("UTF8_STRING"), "STRING": wx_DataFormat ("STRING"), "TEXT": wx_DataFormat ("TEXT"), "MULTIPLE": wx_DataFormat ("MULTIPLE"), "TIMESTAMP": wx_DataFormat ("TIMESTAMP")})
	c_windowsDatumFormatNameToFormatMap: "OrderedDict [str, wx_DataFormat]" = OrderedDict ({"Text": wx_DataFormat (wx.DF_TEXT), "Metafile": wx_DataFormat (wx.DF_METAFILE), "Filename": wx_DataFormat (wx.DF_FILENAME), "UnicodeText": wx_DataFormat ("UnicodeText"), "Locale": wx_DataFormat ("Locale"), "OEMText": wx_DataFormat ("OEMText"), "DataObject": wx_DataFormat ("DataObject"), "Preferred DropEffect": wx_DataFormat ("Preferred DropEffect"), "Ole Private Data": wx_DataFormat ("Ole Private Data"), "HTML Format": wx_DataFormat (wx.DF_HTML), "HTML (HyperText Markup Language)": wx_DataFormat ("HTML (HyperText Markup Language)"), "Star Embed Source (XML)": wx_DataFormat ("Star Embed Source (XML)"), "Rich Text Format": wx_DataFormat ("Rich Text Format"), "Richtext Format": wx_DataFormat ("Richtext Format"), "Link": wx_DataFormat ("Link"), "Star Object Descriptor (XML)": wx_DataFormat ("Star Object Descriptor (XML)"), "GDIMetaFile": wx_DataFormat ("GDIMetaFile"), "EnhancedMetafile": wx_DataFormat ("EnhancedMetafile"), "MetaFilePict": wx_DataFormat ("MetaFilePict"), "PNG": wx_DataFormat ("PNG"), "DeviceIndependentBitmap": wx_DataFormat ("DeviceIndependentBitmap"), "Windows Bitmap": wx_DataFormat ("Windows Bitmap"), "SymbolicLink": wx_DataFormat ("SymbolicLink"), "DataInterchangeFormat": wx_DataFormat ("DataInterchangeFormat"), "EditEngine ODF": wx_DataFormat ("EditEngine ODF")})


After I have copied a text piece on a 'cmd' terminal, I get an output like this for the 'G' -> 'A' -> 'D' -> 'A' inputs.

@Output
### clipboard datum format number, format name, size: 1, Text, 8

After I have copied a text piece on a LibreOffice Writer instance, I get an output like this for the 'G' -> 'A' -> 'D' -> 'A' inputs.

@Output
### clipboard datum format number, format name, size: 1, Text, 37
### clipboard datum format number, format name, size: 49161, DataObject, 8
### clipboard datum format number, format name, size: 49171, Ole Private Data, 552
### clipboard datum format number, format name, size: 30, HTML Format, 772
### clipboard datum format number, format name, size: 49819, HTML (HyperText Markup Language), 667
### clipboard datum format number, format name, size: 49817, Star Embed Source (XML), 6279
### clipboard datum format number, format name, size: 49285, Rich Text Format, 2675
### clipboard datum format number, format name, size: 49790, Richtext Format, 2675
### clipboard datum format number, format name, size: 49852, Link, 111
### clipboard datum format number, format name, size: 49853, Star Object Descriptor (XML), 164

For Linux, I have to change the expected clipboard datum formats.


References


<The previous article in this series | The table of contents of this series |