Ajout du GUI

This commit is contained in:
thatscringebro
2022-08-08 16:31:52 -04:00
parent db362ccdca
commit abd15f28b6
851 changed files with 99957 additions and 1 deletions

View File

@@ -0,0 +1,157 @@
'''
Clipboard
=========
Core class for accessing the Clipboard. If we are not able to access the
system clipboard, a fake one will be used.
Usage example:
.. code-block:: kv
#:import Clipboard kivy.core.clipboard.Clipboard
Button:
on_release:
self.text = Clipboard.paste()
Clipboard.copy('Data')
'''
__all__ = ('ClipboardBase', 'Clipboard')
from kivy import Logger
from kivy.core import core_select_lib
from kivy.utils import platform
from kivy.setupconfig import USE_SDL2
class ClipboardBase(object):
def get(self, mimetype):
'''Get the current data in clipboard, using the mimetype if possible.
You not use this method directly. Use :meth:`paste` instead.
'''
pass
def put(self, data, mimetype):
'''Put data on the clipboard, and attach a mimetype.
You should not use this method directly. Use :meth:`copy` instead.
'''
pass
def get_types(self):
'''Return a list of supported mimetypes
'''
return []
def _ensure_clipboard(self):
''' Ensure that the clipboard has been properly initialised.
'''
if hasattr(self, '_clip_mime_type'):
return
if platform == 'win':
self._clip_mime_type = 'text/plain;charset=utf-8'
# windows clipboard uses a utf-16 little endian encoding
self._encoding = 'utf-16-le'
elif platform == 'linux':
self._clip_mime_type = 'text/plain;charset=utf-8'
self._encoding = 'utf-8'
else:
self._clip_mime_type = 'text/plain'
self._encoding = 'utf-8'
def copy(self, data=''):
''' Copy the value provided in argument `data` into current clipboard.
If data is not of type string it will be converted to string.
.. versionadded:: 1.9.0
'''
if data:
self._copy(data)
def paste(self):
''' Get text from the system clipboard and return it a usable string.
.. versionadded:: 1.9.0
'''
return self._paste()
def _copy(self, data):
self._ensure_clipboard()
if not isinstance(data, bytes):
data = data.encode(self._encoding)
self.put(data, self._clip_mime_type)
def _paste(self):
self._ensure_clipboard()
_clip_types = Clipboard.get_types()
mime_type = self._clip_mime_type
if mime_type not in _clip_types:
mime_type = 'text/plain'
data = self.get(mime_type)
if data is not None:
# decode only if we don't have unicode
# we would still need to decode from utf-16 (windows)
# data is of type bytes in PY3
if isinstance(data, bytes):
data = data.decode(self._encoding, 'ignore')
# remove null strings mostly a windows issue
data = data.replace(u'\x00', u'')
return data
return u''
# load clipboard implementation
_clipboards = []
if platform == 'android':
_clipboards.append(
('android', 'clipboard_android', 'ClipboardAndroid'))
elif platform == 'macosx':
_clipboards.append(
('nspaste', 'clipboard_nspaste', 'ClipboardNSPaste'))
elif platform == 'win':
_clipboards.append(
('winctypes', 'clipboard_winctypes', 'ClipboardWindows'))
elif platform == 'linux':
_clipboards.append(
('xclip', 'clipboard_xclip', 'ClipboardXclip'))
_clipboards.append(
('xsel', 'clipboard_xsel', 'ClipboardXsel'))
_clipboards.append(
('dbusklipper', 'clipboard_dbusklipper', 'ClipboardDbusKlipper'))
_clipboards.append(
('gtk3', 'clipboard_gtk3', 'ClipboardGtk3'))
if USE_SDL2:
_clipboards.append(
('sdl2', 'clipboard_sdl2', 'ClipboardSDL2'))
else:
_clipboards.append(
('pygame', 'clipboard_pygame', 'ClipboardPygame'))
_clipboards.append(
('dummy', 'clipboard_dummy', 'ClipboardDummy'))
Clipboard = core_select_lib('clipboard', _clipboards, True)
CutBuffer = None
if platform == 'linux':
_cutbuffers = [
('xclip', 'clipboard_xclip', 'ClipboardXclip'),
('xsel', 'clipboard_xsel', 'ClipboardXsel'),
]
if Clipboard.__class__.__name__ in (c[2] for c in _cutbuffers):
CutBuffer = Clipboard
else:
CutBuffer = core_select_lib('cutbuffer', _cutbuffers, True,
basemodule='clipboard')
if CutBuffer:
Logger.info('CutBuffer: cut buffer support enabled')

View File

@@ -0,0 +1,36 @@
'''
Clipboard ext: base class for external command clipboards
'''
__all__ = ('ClipboardExternalBase', )
from kivy.core.clipboard import ClipboardBase
class ClipboardExternalBase(ClipboardBase):
@staticmethod
def _clip(inout, selection):
raise NotImplementedError('clip method not implemented')
def get(self, mimetype='text/plain'):
p = self._clip('out', 'clipboard')
data, _ = p.communicate()
return data
def put(self, data, mimetype='text/plain'):
p = self._clip('in', 'clipboard')
p.communicate(data)
def get_cutbuffer(self):
p = self._clip('out', 'primary')
data, _ = p.communicate()
return data.decode('utf8')
def set_cutbuffer(self, data):
if not isinstance(data, bytes):
data = data.encode('utf8')
p = self._clip('in', 'primary')
p.communicate(data)
def get_types(self):
return [u'text/plain']

View File

@@ -0,0 +1,91 @@
'''
Clipboard Android
=================
Android implementation of Clipboard provider, using Pyjnius.
'''
__all__ = ('ClipboardAndroid', )
from kivy import Logger
from kivy.core.clipboard import ClipboardBase
from jnius import autoclass, cast
from android.runnable import run_on_ui_thread
from android import python_act
AndroidString = autoclass('java.lang.String')
PythonActivity = python_act
Context = autoclass('android.content.Context')
VER = autoclass('android.os.Build$VERSION')
sdk = VER.SDK_INT
class ClipboardAndroid(ClipboardBase):
def __init__(self):
super(ClipboardAndroid, self).__init__()
self._clipboard = None
self._data = dict()
self._data['text/plain'] = None
self._data['application/data'] = None
PythonActivity._clipboard = None
def get(self, mimetype='text/plain'):
return self._get(mimetype).encode('utf-8')
def put(self, data, mimetype='text/plain'):
self._set(data, mimetype)
def get_types(self):
return list(self._data.keys())
@run_on_ui_thread
def _initialize_clipboard(self):
PythonActivity._clipboard = cast(
'android.app.Activity',
PythonActivity.mActivity).getSystemService(
Context.CLIPBOARD_SERVICE)
def _get_clipboard(f):
def called(*args, **kargs):
self = args[0]
if not PythonActivity._clipboard:
self._initialize_clipboard()
import time
while not PythonActivity._clipboard:
time.sleep(.01)
return f(*args, **kargs)
return called
@_get_clipboard
def _get(self, mimetype='text/plain'):
clippy = PythonActivity._clipboard
data = ''
if sdk < 11:
data = clippy.getText()
else:
ClipDescription = autoclass('android.content.ClipDescription')
primary_clip = clippy.getPrimaryClip()
if primary_clip:
try:
data = primary_clip.getItemAt(0)
if data:
data = data.coerceToText(
PythonActivity.mActivity.getApplicationContext())
except Exception:
Logger.exception('Clipboard: failed to paste')
return data
@_get_clipboard
def _set(self, data, mimetype):
clippy = PythonActivity._clipboard
if sdk < 11:
# versions previous to honeycomb
clippy.setText(AndroidString(data))
else:
ClipData = autoclass('android.content.ClipData')
new_clip = ClipData.newPlainText(AndroidString(""),
AndroidString(data))
# put text data onto clipboard
clippy.setPrimaryClip(new_clip)

View File

@@ -0,0 +1,41 @@
'''
Clipboard Dbus: an implementation of the Clipboard using dbus and klipper.
'''
__all__ = ('ClipboardDbusKlipper', )
from kivy.utils import platform
from kivy.core.clipboard import ClipboardBase
if platform != 'linux':
raise SystemError('unsupported platform for dbus kde clipboard')
try:
import dbus
bus = dbus.SessionBus()
proxy = bus.get_object("org.kde.klipper", "/klipper")
except:
raise
class ClipboardDbusKlipper(ClipboardBase):
_is_init = False
def init(self):
if ClipboardDbusKlipper._is_init:
return
self.iface = dbus.Interface(proxy, "org.kde.klipper.klipper")
ClipboardDbusKlipper._is_init = True
def get(self, mimetype='text/plain'):
self.init()
return str(self.iface.getClipboardContents())
def put(self, data, mimetype='text/plain'):
self.init()
self.iface.setClipboardContents(data.replace('\x00', ''))
def get_types(self):
self.init()
return [u'text/plain']

View File

@@ -0,0 +1,26 @@
'''
Clipboard Dummy: an internal implementation that does not use the system
clipboard.
'''
__all__ = ('ClipboardDummy', )
from kivy.core.clipboard import ClipboardBase
class ClipboardDummy(ClipboardBase):
def __init__(self):
super(ClipboardDummy, self).__init__()
self._data = dict()
self._data['text/plain'] = None
self._data['application/data'] = None
def get(self, mimetype='text/plain'):
return self._data.get(mimetype, None)
def put(self, data, mimetype='text/plain'):
self._data[mimetype] = data
def get_types(self):
return list(self._data.keys())

View File

@@ -0,0 +1,47 @@
'''
Clipboard Gtk3: an implementation of the Clipboard using Gtk3.
'''
__all__ = ('ClipboardGtk3',)
from kivy.utils import platform
from kivy.support import install_gobject_iteration
from kivy.core.clipboard import ClipboardBase
if platform != 'linux':
raise SystemError('unsupported platform for gtk3 clipboard')
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
class ClipboardGtk3(ClipboardBase):
_is_init = False
def init(self):
if self._is_init:
return
install_gobject_iteration()
self._is_init = True
def get(self, mimetype='text/plain;charset=utf-8'):
self.init()
if mimetype == 'text/plain;charset=utf-8':
contents = clipboard.wait_for_text()
if contents:
return contents
return ''
def put(self, data, mimetype='text/plain;charset=utf-8'):
self.init()
if mimetype == 'text/plain;charset=utf-8':
text = data.decode(self._encoding)
clipboard.set_text(text, -1)
clipboard.store()
def get_types(self):
self.init()
return ['text/plain;charset=utf-8']

View File

@@ -0,0 +1,44 @@
'''
Clipboard OsX: implementation of clipboard using Appkit
'''
__all__ = ('ClipboardNSPaste', )
from kivy.core.clipboard import ClipboardBase
from kivy.utils import platform
if platform != 'macosx':
raise SystemError('Unsupported platform for appkit clipboard.')
try:
from pyobjus import autoclass
from pyobjus.dylib_manager import load_framework, INCLUDE
load_framework(INCLUDE.AppKit)
except ImportError:
raise SystemError('Pyobjus not installed. Please run the following'
' command to install it. `pip install --user pyobjus`')
NSPasteboard = autoclass('NSPasteboard')
NSString = autoclass('NSString')
class ClipboardNSPaste(ClipboardBase):
def __init__(self):
super(ClipboardNSPaste, self).__init__()
self._clipboard = NSPasteboard.generalPasteboard()
def get(self, mimetype='text/plain'):
pb = self._clipboard
data = pb.stringForType_('public.utf8-plain-text')
if not data:
return ""
return data.UTF8String()
def put(self, data, mimetype='text/plain'):
pb = self._clipboard
pb.clearContents()
utf8 = NSString.alloc().initWithUTF8String_(data)
pb.setString_forType_(utf8, 'public.utf8-plain-text')
def get_types(self):
return list('text/plain',)

View File

@@ -0,0 +1,67 @@
'''
Clipboard Pygame: an implementation of the Clipboard using pygame.scrap.
.. warning::
Pygame has been deprecated and will be removed in the release after Kivy
1.11.0.
'''
__all__ = ('ClipboardPygame', )
from kivy.utils import platform
from kivy.core.clipboard import ClipboardBase
from kivy.utils import deprecated
if platform not in ('win', 'linux', 'macosx'):
raise SystemError('unsupported platform for pygame clipboard')
try:
import pygame
import pygame.scrap
except:
raise
class ClipboardPygame(ClipboardBase):
_is_init = False
_types = None
_aliases = {
'text/plain;charset=utf-8': 'UTF8_STRING'
}
@deprecated(
msg='Pygame has been deprecated and will be removed after 1.11.0')
def __init__(self, *largs, **kwargs):
super(ClipboardPygame, self).__init__(*largs, **kwargs)
def init(self):
if ClipboardPygame._is_init:
return
pygame.scrap.init()
ClipboardPygame._is_init = True
def get(self, mimetype='text/plain'):
self.init()
mimetype = self._aliases.get(mimetype, mimetype)
text = pygame.scrap.get(mimetype)
return text
def put(self, data, mimetype='text/plain'):
self.init()
mimetype = self._aliases.get(mimetype, mimetype)
pygame.scrap.put(mimetype, data)
def get_types(self):
if not self._types:
self.init()
types = pygame.scrap.get_types()
for mime, pygtype in list(self._aliases.items())[:]:
if mime in types:
del self._aliases[mime]
if pygtype in types:
types.append(mime)
self._types = types
return self._types

View File

@@ -0,0 +1,36 @@
'''
Clipboard SDL2: an implementation of the Clipboard using sdl2.
'''
__all__ = ('ClipboardSDL2', )
from kivy.utils import platform
from kivy.core.clipboard import ClipboardBase
if platform not in ('win', 'linux', 'macosx', 'android', 'ios'):
raise SystemError('unsupported platform for sdl2 clipboard')
try:
from kivy.core.clipboard._clipboard_sdl2 import (
_get_text, _has_text, _set_text)
except ImportError:
from kivy.core import handle_win_lib_import_error
handle_win_lib_import_error(
'Clipboard', 'sdl2', 'kivy.core.clipboard._clipboard_sdl2')
raise
class ClipboardSDL2(ClipboardBase):
def get(self, mimetype):
return _get_text() if _has_text() else ''
def _ensure_clipboard(self):
super(ClipboardSDL2, self)._ensure_clipboard()
self._encoding = 'utf8'
def put(self, data=b'', mimetype='text/plain'):
_set_text(data)
def get_types(self):
return ['text/plain']

View File

@@ -0,0 +1,66 @@
'''
Clipboard windows: an implementation of the Clipboard using ctypes.
'''
__all__ = ('ClipboardWindows', )
from kivy.utils import platform
from kivy.core.clipboard import ClipboardBase
if platform != 'win':
raise SystemError('unsupported platform for Windows clipboard')
import ctypes
from ctypes import wintypes
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
msvcrt = ctypes.cdll.msvcrt
c_char_p = ctypes.c_char_p
c_wchar_p = ctypes.c_wchar_p
class ClipboardWindows(ClipboardBase):
def get(self, mimetype='text/plain'):
GetClipboardData = user32.GetClipboardData
GetClipboardData.argtypes = [wintypes.UINT]
GetClipboardData.restype = wintypes.HANDLE
user32.OpenClipboard(user32.GetActiveWindow())
# Standard Clipboard Format "1" is "CF_TEXT"
pcontents = GetClipboardData(13)
# if someone pastes a FILE, the content is None for SCF 13
# and the clipboard is locked if not closed properly
if not pcontents:
user32.CloseClipboard()
return ''
data = c_wchar_p(pcontents).value.encode(self._encoding)
user32.CloseClipboard()
return data
def put(self, text, mimetype='text/plain'):
text = text.decode(self._encoding) # auto converted later
text += u'\x00'
SetClipboardData = user32.SetClipboardData
SetClipboardData.argtypes = [wintypes.UINT, wintypes.HANDLE]
SetClipboardData.restype = wintypes.HANDLE
GlobalAlloc = kernel32.GlobalAlloc
GlobalAlloc.argtypes = [wintypes.UINT, ctypes.c_size_t]
GlobalAlloc.restype = wintypes.HGLOBAL
CF_UNICODETEXT = 13
user32.OpenClipboard(user32.GetActiveWindow())
user32.EmptyClipboard()
hCd = GlobalAlloc(0, len(text) * ctypes.sizeof(ctypes.c_wchar))
# ignore null character for strSource pointer
msvcrt.wcscpy_s(c_wchar_p(hCd), len(text), c_wchar_p(text[:-1]))
SetClipboardData(CF_UNICODETEXT, hCd)
user32.CloseClipboard()
def get_types(self):
return ['text/plain']

View File

@@ -0,0 +1,29 @@
'''
Clipboard xclip: an implementation of the Clipboard using xclip
command line tool.
'''
__all__ = ('ClipboardXclip', )
from kivy.utils import platform
from kivy.core.clipboard._clipboard_ext import ClipboardExternalBase
if platform != 'linux':
raise SystemError('unsupported platform for xclip clipboard')
try:
import subprocess
p = subprocess.Popen(['xclip', '-version'], stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
p.communicate()
except:
raise
class ClipboardXclip(ClipboardExternalBase):
@staticmethod
def _clip(inout, selection):
pipe = {'std' + inout: subprocess.PIPE}
return subprocess.Popen(
['xclip', '-' + inout, '-selection', selection], **pipe)

View File

@@ -0,0 +1,29 @@
'''
Clipboard xsel: an implementation of the Clipboard using xsel command line
tool.
'''
__all__ = ('ClipboardXsel', )
from kivy.utils import platform
from kivy.core.clipboard._clipboard_ext import ClipboardExternalBase
if platform != 'linux':
raise SystemError('unsupported platform for xsel clipboard')
try:
import subprocess
p = subprocess.Popen(['xsel'], stdout=subprocess.PIPE)
p.communicate()
except:
raise
class ClipboardXsel(ClipboardExternalBase):
@staticmethod
def _clip(inout, selection):
pipe = {'std' + inout: subprocess.PIPE}
sel = 'b' if selection == 'clipboard' else selection[0]
io = inout[0]
return subprocess.Popen(
['xsel', '-' + sel + io], **pipe)