Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clipette/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from os.path import join as path_join
from sys import getfilesystemencoding
import utils
from contextlib import contextmanager


class ClipetteWin32ClipboardError(Exception):
Expand Down Expand Up @@ -97,6 +98,19 @@ def close_clipboard() -> int:
"""
return utils.user32.CloseClipboard()

@contextmanager
def clipboard():
"""
Context manager to open and close clipboard automatically.

:return: if function fails, 1 otherwise.
:rtype: int
"""
res = open_clipboard()
yield res
if res: close_clipboard()


def empty_clipboard() -> int:
"""
Empties clipboard. Should be called before any setter actions.
Expand Down
7 changes: 6 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
if clipette.open_clipboard():
clipette.empty_cliboard()
clipette.set_UNICODETEXT("<some text>")
clipette.close_clipboard()
clipette.close_clipboard()

# safe clipboard
with clipette.clipboard():
clipette.empty_cliboard()
clipette.set_UNICODETEXT("<some text>")