omdev 0.0.0.dev80__py3-none-any.whl → 0.0.0.dev81__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of omdev might be problematic. Click here for more details.

File without changes
@@ -0,0 +1,23 @@
1
+ """
2
+ https://askubuntu.com/questions/11925/a-command-line-clipboard-copy-and-paste-utility
3
+ """
4
+ import abc
5
+ import subprocess
6
+
7
+
8
+ class Clipboard(abc.ABC):
9
+ @abc.abstractmethod
10
+ def get(self) -> str:
11
+ raise NotImplementedError
12
+
13
+ @abc.abstractmethod
14
+ def put(self, s: str) -> None:
15
+ raise NotImplementedError
16
+
17
+
18
+ class DarwinClipboard(Clipboard):
19
+ def get(self) -> str:
20
+ return subprocess.check_output(['pbpaste']).decode('utf-8')
21
+
22
+ def put(self, s: str) -> None:
23
+ subprocess.run(['pbcopy'], input=s.encode('utf-8'), check=True)
@@ -0,0 +1,229 @@
1
+ # ruff: noqa: N802 N816
2
+ import ctypes as ct
3
+ import ctypes.util
4
+ import dataclasses as dc
5
+ import sys
6
+ import typing as ta
7
+
8
+ from omlish import check
9
+
10
+
11
+ ##
12
+
13
+
14
+ if getattr(sys, 'platform') != 'darwin':
15
+ raise OSError(sys.platform)
16
+
17
+
18
+ ##
19
+ # CoreFoundation
20
+
21
+ cf = ct.cdll.LoadLibrary(check.not_none(ct.util.find_library('CoreFoundation')))
22
+
23
+ #
24
+
25
+ CFArrayRef = ct.c_void_p
26
+ CFDataRef = ct.c_void_p
27
+ CFIndex = ct.c_long
28
+ CFStringEncoding = ct.c_uint32
29
+ CFStringRef = ct.c_void_p
30
+ CFTypeID = ct.c_ulong
31
+
32
+ #
33
+
34
+ cf.CFArrayGetCount.argtypes = [CFArrayRef]
35
+ cf.CFArrayGetCount.restype = CFIndex
36
+
37
+ cf.CFArrayGetValueAtIndex.argtypes = [CFArrayRef, CFIndex]
38
+ cf.CFArrayGetValueAtIndex.restype = CFStringRef
39
+
40
+ cf.CFDataGetBytePtr.argtypes = [CFDataRef]
41
+ cf.CFDataGetBytePtr.restype = ct.POINTER(ct.c_uint8)
42
+
43
+ cf.CFDataGetLength.argtypes = [CFDataRef]
44
+ cf.CFDataGetLength.restype = CFIndex
45
+
46
+ cf.CFGetTypeID.argtypes = [ct.c_void_p]
47
+ cf.CFGetTypeID.restype = CFTypeID
48
+
49
+ cf.CFRelease.argtypes = [ct.c_void_p]
50
+ cf.CFRelease.restype = None
51
+
52
+ cf.CFStringCreateWithCString.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_int32]
53
+ cf.CFStringCreateWithCString.restype = CFStringRef
54
+
55
+ cf.CFStringGetCString.argtypes = [CFStringRef, ct.c_char_p, ct.c_long, ct.c_uint32]
56
+ cf.CFStringGetCString.restype = ct.c_bool
57
+
58
+ cf.CFStringGetLength.argtypes = [CFStringRef]
59
+ cf.CFStringGetLength.restype = CFIndex
60
+
61
+ cf.CFStringGetMaximumSizeForEncoding.argtypes = [CFIndex, CFStringEncoding]
62
+ cf.CFStringGetMaximumSizeForEncoding.restype = CFIndex
63
+
64
+ cf.CFStringGetTypeID.argtypes = []
65
+ cf.CFStringGetTypeID.restype = CFTypeID
66
+
67
+
68
+ ##
69
+ # ApplicationServices
70
+
71
+ aps = ct.cdll.LoadLibrary(check.not_none(ct.util.find_library('ApplicationServices')))
72
+
73
+ #
74
+
75
+ OSStatus = ct.c_int32
76
+
77
+ PasteboardItemID = ct.c_ulong
78
+ PasteboardRef = ct.c_void_p
79
+
80
+ #
81
+
82
+ aps.PasteboardCopyItemFlavorData.argtypes = [PasteboardRef, PasteboardItemID, CFStringRef, ct.POINTER(CFDataRef)]
83
+ aps.PasteboardCopyItemFlavorData.restype = OSStatus
84
+
85
+ aps.PasteboardCopyItemFlavors.argtypes = [PasteboardRef, PasteboardItemID, ct.POINTER(CFArrayRef)]
86
+ aps.PasteboardCopyItemFlavors.restype = OSStatus
87
+
88
+ aps.PasteboardCreate.argtypes = [CFStringRef, ct.POINTER(PasteboardRef)]
89
+ aps.PasteboardCreate.restype = OSStatus
90
+
91
+ aps.PasteboardGetItemCount.argtypes = [PasteboardRef, ct.POINTER(ct.c_ulong)]
92
+ aps.PasteboardGetItemCount.restype = OSStatus
93
+
94
+ aps.PasteboardGetItemIdentifier.argtypes = [PasteboardRef, ct.c_ulong, ct.POINTER(PasteboardItemID)]
95
+ aps.PasteboardGetItemIdentifier.restype = OSStatus
96
+
97
+
98
+ ##
99
+
100
+
101
+ def CFSTR(string):
102
+ return cf.CFStringCreateWithCString(None, string.encode('utf-8'), 0)
103
+
104
+
105
+ kCFStringEncodingUTF8 = 0x08000100
106
+ kPasteboardClipboard = CFSTR('com.apple.pasteboard.clipboard')
107
+
108
+
109
+ def cfstring_to_string(cf_string: CFStringRef) -> str:
110
+ if not cf_string:
111
+ return ''
112
+
113
+ length = cf.CFStringGetLength(cf_string)
114
+ max_size = cf.CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1
115
+ buffer = ct.create_string_buffer(max_size)
116
+
117
+ if success := cf.CFStringGetCString(cf_string, buffer, max_size, kCFStringEncodingUTF8): # noqa
118
+ return ''
119
+
120
+ return buffer.value.decode('utf-8')
121
+
122
+
123
+ ##
124
+
125
+
126
+ class OsxClipboardError(Exception):
127
+ pass
128
+
129
+
130
+ @dc.dataclass(frozen=True)
131
+ class StatusOsxClipboardError(OsxClipboardError):
132
+ fn: str
133
+ status: int
134
+
135
+
136
+ @dc.dataclass(frozen=True)
137
+ class OsxClipboardItem:
138
+ type: str | None
139
+ data: bytes | None
140
+
141
+
142
+ def get_osx_clipboard_data(
143
+ *,
144
+ types: ta.Container[str | None] | None = None,
145
+ strict: bool = False,
146
+ types_only: bool = False,
147
+ ) -> list[OsxClipboardItem]:
148
+ lst: list[OsxClipboardItem] = []
149
+
150
+ pasteboard = PasteboardRef()
151
+ if status := aps.PasteboardCreate(kPasteboardClipboard, ct.byref(pasteboard)):
152
+ raise StatusOsxClipboardError('PasteboardCreate', status)
153
+
154
+ try:
155
+ item_count = ct.c_ulong(0)
156
+ if status := aps.PasteboardGetItemCount(pasteboard, ct.byref(item_count)):
157
+ raise StatusOsxClipboardError('PasteboardGetItemCount', status)
158
+
159
+ for i in range(1, item_count.value + 1):
160
+ item_id = PasteboardItemID()
161
+ if status := aps.PasteboardGetItemIdentifier(pasteboard, i, ct.byref(item_id)):
162
+ raise StatusOsxClipboardError('PasteboardGetItemIdentifier', status)
163
+
164
+ data_types = CFArrayRef()
165
+ if status := aps.PasteboardCopyItemFlavors(pasteboard, item_id, ct.byref(data_types)):
166
+ raise StatusOsxClipboardError('PasteboardCopyItemFlavors', status)
167
+ if not data_types:
168
+ continue
169
+
170
+ try:
171
+ type_count = cf.CFArrayGetCount(data_types)
172
+ for j in range(type_count):
173
+ data_type = cf.CFArrayGetValueAtIndex(data_types, j)
174
+
175
+ if cf.CFGetTypeID(data_type) == cf.CFStringGetTypeID():
176
+ data_type_str = cfstring_to_string(data_type)
177
+ else:
178
+ data_type_str = None
179
+
180
+ if types is not None and data_type_str not in types:
181
+ continue
182
+
183
+ if types_only:
184
+ lst.append(OsxClipboardItem(
185
+ type=data_type_str,
186
+ data=None,
187
+ ))
188
+ continue
189
+
190
+ data = CFDataRef()
191
+ if status := aps.PasteboardCopyItemFlavorData(pasteboard, item_id, data_type, ct.byref(data)):
192
+ if not strict:
193
+ continue
194
+ raise StatusOsxClipboardError('PasteboardCopyItemFlavorData', status)
195
+ if not data:
196
+ continue
197
+
198
+ try:
199
+ data_size = cf.CFDataGetLength(data)
200
+ data_ptr = cf.CFDataGetBytePtr(data)
201
+ data_bytes = ct.string_at(data_ptr, data_size)
202
+
203
+ lst.append(OsxClipboardItem(
204
+ type=data_type_str,
205
+ data=data_bytes,
206
+ ))
207
+
208
+ finally:
209
+ cf.CFRelease(data)
210
+
211
+ finally:
212
+ cf.CFRelease(data_types)
213
+
214
+ finally:
215
+ cf.CFRelease(pasteboard)
216
+
217
+ return lst
218
+
219
+
220
+ ##
221
+
222
+
223
+ def _main() -> None:
224
+ for i in get_osx_clipboard_data():
225
+ print(i)
226
+
227
+
228
+ if __name__ == '__main__':
229
+ _main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omdev
3
- Version: 0.0.0.dev80
3
+ Version: 0.0.0.dev81
4
4
  Summary: omdev
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -12,7 +12,7 @@ Classifier: Operating System :: OS Independent
12
12
  Classifier: Operating System :: POSIX
13
13
  Requires-Python: ~=3.12
14
14
  License-File: LICENSE
15
- Requires-Dist: omlish ==0.0.0.dev80
15
+ Requires-Dist: omlish ==0.0.0.dev81
16
16
  Provides-Extra: all
17
17
  Requires-Dist: black ~=24.10 ; extra == 'all'
18
18
  Requires-Dist: pycparser ~=2.22 ; extra == 'all'
@@ -66,6 +66,9 @@ omdev/cli/install.py,sha256=C-W171YlIHt4Cfok-nWSMbHwWhqF_PFqq2HixFttYx8,4460
66
66
  omdev/cli/main.py,sha256=nD3bW3MkI0KuDSLpX_PTnl-lBnm4hIxVqczvvMHtupo,7058
67
67
  omdev/cli/managers.py,sha256=BV98_n30Jj63OJrFgRoVZRfICxMLXEZKoEn4rMj9LV4,1160
68
68
  omdev/cli/types.py,sha256=bqKw9SbtBtAip2vF9v4khh0CqKG6LBr6n9VzWBz7AJE,474
69
+ omdev/clipboard/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
70
+ omdev/clipboard/clipboard.py,sha256=CmnozkRljJ9UQDHLfCS8wKU0Xg-HfbsAkagAfMEias0,567
71
+ omdev/clipboard/darwin.py,sha256=AE4oYrDXTV0uBgZ-8pWxtqXes3k0IL2pxrJN8WjK1C4,6370
69
72
  omdev/interp/__init__.py,sha256=Y3l4WY4JRi2uLG6kgbGp93fuGfkxkKwZDvhsa0Rwgtk,15
70
73
  omdev/interp/__main__.py,sha256=GMCqeGYltgt5dlJzHxY9gqisa8cRkrPfmZYuZnjg4WI,162
71
74
  omdev/interp/cli.py,sha256=sh7PZQoLletUViw1Y9OXNr9ekyNZ6YyxYuOQ_n9hyqU,2072
@@ -122,9 +125,9 @@ omdev/tools/piptools.py,sha256=-jR5q3w4sHqntxCLExFCBNIARB788FUsAbJ62PK2sBU,2774
122
125
  omdev/tools/proftools.py,sha256=8ZU9x_Dq8eT2ZFwU9sJpDIvxcIn9qBc8y2ELKPb5e5M,1382
123
126
  omdev/tools/rsttool.py,sha256=suwsfseUf8GH8rYmYygTUdif-Jk_bX1g9fYRLXaKkmM,1340
124
127
  omdev/tools/sqlrepl.py,sha256=tmFZh80-xsGM62dyQ7_UGLebChrj7IHbIPYBWDJMgVk,5741
125
- omdev-0.0.0.dev80.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
126
- omdev-0.0.0.dev80.dist-info/METADATA,sha256=68sUTkl9Uc5f4dvq8nA5q3OQ7eDcDDh0MPClMBFBAps,1492
127
- omdev-0.0.0.dev80.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
128
- omdev-0.0.0.dev80.dist-info/entry_points.txt,sha256=dHLXFmq5D9B8qUyhRtFqTGWGxlbx3t5ejedjrnXNYLU,33
129
- omdev-0.0.0.dev80.dist-info/top_level.txt,sha256=1nr7j30fEWgLYHW3lGR9pkdHkb7knv1U1ES1XRNVQ6k,6
130
- omdev-0.0.0.dev80.dist-info/RECORD,,
128
+ omdev-0.0.0.dev81.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
129
+ omdev-0.0.0.dev81.dist-info/METADATA,sha256=WkaRhTVt9fO7VrK402ddG8iRccBhkL1NOf3y4iOv3cg,1492
130
+ omdev-0.0.0.dev81.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
131
+ omdev-0.0.0.dev81.dist-info/entry_points.txt,sha256=dHLXFmq5D9B8qUyhRtFqTGWGxlbx3t5ejedjrnXNYLU,33
132
+ omdev-0.0.0.dev81.dist-info/top_level.txt,sha256=1nr7j30fEWgLYHW3lGR9pkdHkb7knv1U1ES1XRNVQ6k,6
133
+ omdev-0.0.0.dev81.dist-info/RECORD,,