I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
from .api import set_clipboard_text
from .get_clipboard_text_and_convert import get_clipboard_text_and_convert
__all__ = [
"set_clipboard_text",
"get_clipboard_text_and_convert",
]

View File

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
from pyreadline3.py3k_compat import is_ironpython
if is_ironpython:
try:
from .ironpython_clipboard import get_clipboard_text, set_clipboard_text
except ImportError:
from .no_clipboard import get_clipboard_text, set_clipboard_text
else:
try:
from .win32_clipboard import get_clipboard_text, set_clipboard_text
except ImportError:
from .no_clipboard import get_clipboard_text, set_clipboard_text
__all__ = [
"get_clipboard_text",
"set_clipboard_text",
]

View File

@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
from typing import Any, List, Tuple
from .api import get_clipboard_text
def _make_num(x: Any) -> Any:
try:
return int(x)
except ValueError:
try:
return float(x)
except ValueError:
try:
return complex(x)
except ValueError:
return x
def _make_list_of_list(txt: str) -> Tuple[
List[List[Any]],
bool,
]:
ut = []
flag = False
for line in [x for x in txt.split("\r\n") if x != ""]:
words = [_make_num(x) for x in line.split("\t")]
if str in list(map(type, words)):
flag = True
ut.append(words)
return (
ut,
flag,
)
def get_clipboard_text_and_convert(paste_list: bool = False) -> str:
"""Get txt from clipboard. if paste_list==True the convert tab separated
data to list of lists. Enclose list of list in array() if all elements are
numeric"""
txt = get_clipboard_text()
if not txt:
return ""
if not paste_list:
return txt
if "\t" not in txt:
return txt
array, flag = _make_list_of_list(txt)
if flag:
txt = repr(array)
else:
txt = "array(%s)" % repr(array)
txt = "".join([c for c in txt if c not in " \t\r\n"])
return txt

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
import clr
import System.Windows.Forms.Clipboard as cb
clr.AddReferenceByPartialName("System.Windows.Forms")
def get_clipboard_text() -> str:
text = ""
if cb.ContainsText():
text = cb.GetText()
return text
def set_clipboard_text(text: str) -> None:
cb.SetText(text)
if __name__ == "__main__":
txt = get_clipboard_text() # display last text clipped
print(txt)

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
GLOBAL_CLIPBOARD_BUFFER = ""
def get_clipboard_text() -> str:
return GLOBAL_CLIPBOARD_BUFFER
def set_clipboard_text(text: str) -> None:
global GLOBAL_CLIPBOARD_BUFFER
GLOBAL_CLIPBOARD_BUFFER = text

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
from typing import Any, List
from .api import set_clipboard_text
def _make_tab(lists: List[Any]) -> str:
if hasattr(lists, "tolist"):
lists = lists.tolist()
ut = []
for rad in lists:
if type(rad) in [list, tuple]:
ut.append("\t".join(["%s" % x for x in rad]))
else:
ut.append("%s" % rad)
return "\n".join(ut)
def _send_data(lists: List[Any]) -> None:
set_clipboard_text(_make_tab(lists))

View File

@ -0,0 +1,157 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2003-2006 Jack Trainor.
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
###################################
#
# Based on recipe posted to ctypes-users
# see archive
# http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/1771866
#
#
##########################################################################
#
# The Python win32clipboard lib functions work well enough ... except that they
# can only cut and paste items from within one application, not across
# applications or processes.
#
# I've written a number of Python text filters I like to run on the contents of
# the clipboard so I need to call the Windows clipboard API with global memory
# for my filters to work properly.
#
# Here's some sample code solving this problem using ctypes.
#
# This is my first work with ctypes. It's powerful stuff, but passing
# arguments in and out of functions is tricky. More sample code would have
# been helpful, hence this contribution.
#
##########################################################################
import ctypes
import ctypes.wintypes as wintypes
from ctypes import (
addressof,
c_buffer,
c_char_p,
c_int,
c_size_t,
c_void_p,
c_wchar_p,
cast,
create_unicode_buffer,
sizeof,
windll,
wstring_at,
)
from typing import Union
from pyreadline3.keysyms.winconstants import CF_UNICODETEXT, GHND
from pyreadline3.unicode_helper import ensure_unicode
OpenClipboard = windll.user32.OpenClipboard
OpenClipboard.argtypes = [wintypes.HWND]
OpenClipboard.restype = wintypes.BOOL
EmptyClipboard = windll.user32.EmptyClipboard
GetClipboardData = windll.user32.GetClipboardData
GetClipboardData.argtypes = [wintypes.UINT]
GetClipboardData.restype = wintypes.HANDLE
GetClipboardFormatName = windll.user32.GetClipboardFormatNameA
GetClipboardFormatName.argtypes = [wintypes.UINT, c_char_p, c_int]
SetClipboardData = windll.user32.SetClipboardData
SetClipboardData.argtypes = [wintypes.UINT, wintypes.HANDLE]
SetClipboardData.restype = wintypes.HANDLE
EnumClipboardFormats = windll.user32.EnumClipboardFormats
EnumClipboardFormats.argtypes = [c_int]
CloseClipboard = windll.user32.CloseClipboard
CloseClipboard.argtypes = []
GlobalAlloc = windll.kernel32.GlobalAlloc
GlobalAlloc.argtypes = [wintypes.UINT, c_size_t]
GlobalAlloc.restype = wintypes.HGLOBAL
GlobalLock = windll.kernel32.GlobalLock
GlobalLock.argtypes = [wintypes.HGLOBAL]
GlobalLock.restype = c_void_p
GlobalUnlock = windll.kernel32.GlobalUnlock
GlobalUnlock.argtypes = [c_int]
_strncpy = ctypes.windll.kernel32.lstrcpynW
_strncpy.restype = c_wchar_p
_strncpy.argtypes = [c_wchar_p, c_wchar_p, c_size_t]
def _enum() -> None:
OpenClipboard(0)
q = EnumClipboardFormats(0)
while q:
q = EnumClipboardFormats(q)
CloseClipboard()
def _get_format_name(format_str: str) -> bytes:
buffer = c_buffer(100)
bufferSize = sizeof(buffer)
OpenClipboard(0)
GetClipboardFormatName(format_str, buffer, bufferSize)
CloseClipboard()
return buffer.value
def get_clipboard_text() -> str:
text = ""
if OpenClipboard(0):
h_clip_mem = GetClipboardData(CF_UNICODETEXT)
if h_clip_mem:
text = wstring_at(GlobalLock(h_clip_mem))
GlobalUnlock(h_clip_mem)
CloseClipboard()
return text
def set_clipboard_text(text: Union[str, bytes]) -> None:
buffer = create_unicode_buffer(ensure_unicode(text))
buffer_size = sizeof(buffer)
h_global_mem = GlobalAlloc(GHND, c_size_t(buffer_size))
GlobalLock.restype = c_void_p
lp_global_mem = GlobalLock(h_global_mem)
_strncpy(
cast(lp_global_mem, c_wchar_p),
cast(addressof(buffer), c_wchar_p),
c_size_t(buffer_size),
)
GlobalUnlock(c_int(h_global_mem))
if OpenClipboard(0):
EmptyClipboard()
SetClipboardData(CF_UNICODETEXT, h_global_mem)
CloseClipboard()
if __name__ == "__main__":
txt = get_clipboard_text() # display last text clipped
print(txt)