generated from brennen/python_project
WIP implmenting clipboard functions using .NET
This commit is contained in:
0
src/clip_py/__init__.py
Normal file
0
src/clip_py/__init__.py
Normal file
123
src/clip_py/dotnet.py
Normal file
123
src/clip_py/dotnet.py
Normal file
@@ -0,0 +1,123 @@
|
||||
from importlib.util import find_spec
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from os import PathLike
|
||||
from typing import AnyStr, ByteString, Collection, Generator, Optional, Union
|
||||
|
||||
# Check that the Python.Net CLR is importable and import it
|
||||
if find_spec("clr") is None:
|
||||
raise RuntimeError(".NET API requres pythonnet (http://pythonnet.github.io/)")
|
||||
else:
|
||||
import clr
|
||||
|
||||
# Load assemblies used in this module
|
||||
try:
|
||||
clr.AddReference("System")
|
||||
clr.AddReference("System.Collections.Specialized")
|
||||
clr.AddReference("System.Drawing")
|
||||
clr.AddReference("System.IO")
|
||||
clr.AddReference("System.Runtime.InteropServices")
|
||||
clr.AddReference("System.Windows")
|
||||
clr.AddReference("System.Windows.Forms")
|
||||
except FileNotFoundError:
|
||||
raise RuntimeError(
|
||||
"A .NET Runtime is required. Please install .NET Framework, .NET Core, or Mono"
|
||||
)
|
||||
|
||||
# Import classes from loaded assemblies
|
||||
from System import ArgumentException, ArgumentNullException
|
||||
from System.Collections.Specialized import StringCollection
|
||||
from System.Drawing import Image
|
||||
from System.IO import MemoryStream
|
||||
from System.Runtime.InteropServices import ExternalException
|
||||
from System.Windows.Forms import Clipboard, DataFormats, DataObject, TextDataFormat
|
||||
|
||||
|
||||
def clear() -> None:
|
||||
try:
|
||||
Clipboard.clear()
|
||||
except ExternalException as e:
|
||||
raise RuntimeError from e
|
||||
|
||||
|
||||
def contains_audio() -> bool:
|
||||
return Clipboard.ContainsAudio()
|
||||
|
||||
|
||||
def contains_data(format: AnyStr) -> bool:
|
||||
return Clipboard.ContainsData(getattr(DataFormats, format))
|
||||
|
||||
|
||||
def contains_file_drop_list() -> bool:
|
||||
return Clipboard.ContainsFileDropList()
|
||||
|
||||
|
||||
def contains_image() -> bool:
|
||||
return Clipboard.ContainsImage()
|
||||
|
||||
|
||||
def contains_text(format: Optional[AnyStr] = None) -> bool:
|
||||
if format is None:
|
||||
return Clipboard.ContainsText()
|
||||
else:
|
||||
return Clipboard.ContainsText(getattr(TextDataFormat, format))
|
||||
|
||||
|
||||
def get_audio_stream():
|
||||
return Clipboard.GetAudioStream()
|
||||
|
||||
|
||||
def get_data(format: AnyStr):
|
||||
return Clipboard.GetData(format)
|
||||
|
||||
|
||||
def get_data_object() -> DataObject:
|
||||
return Clipboard.GetDataObject()
|
||||
|
||||
|
||||
def get_file_drop_list() -> Generator:
|
||||
for filename in Clipboard.GetFileDropList():
|
||||
yield filename
|
||||
|
||||
|
||||
def get_image():
|
||||
return Clipboard.GetImage()
|
||||
|
||||
|
||||
def get_text() -> str:
|
||||
return Clipboard.GetText()
|
||||
|
||||
|
||||
def set_audio(audio_bytes: ByteString) -> None:
|
||||
Clipboard.SetAudio(audio_bytes)
|
||||
|
||||
|
||||
def set_data(format: str, data: Union[AnyStr, ByteString]) -> None:
|
||||
Clipboard.SetData(getattr(DataFormats, format), data)
|
||||
|
||||
|
||||
def set_file_drop_list(file_paths: Collection[PathLike]) -> None:
|
||||
|
||||
file_path_collection = StringCollection()
|
||||
|
||||
for f in filter(lambda p: p.is_absolute(), map(lambda s: Path(s).resolve(), file_paths)):
|
||||
file_path_collection.Add(str(f))
|
||||
|
||||
try:
|
||||
Clipboard.SetFileDropList(file_path_collection)
|
||||
except ArgumentException as e:
|
||||
raise ValueError("file_paths does not contain any valid, resolvable paths") from e
|
||||
|
||||
|
||||
def set_image(image: ByteString) -> None:
|
||||
pass
|
||||
|
||||
|
||||
def set_text(text: AnyStr, format=None) -> None:
|
||||
try:
|
||||
if format:
|
||||
Clipboard.SetText(text, getattr(TextDataFormat, format))
|
||||
else:
|
||||
Clipboard.SetText(text)
|
||||
except ArgumentNullException as e:
|
||||
raise ValueError("text is empty") from e
|
||||
Reference in New Issue
Block a user