-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzenity_wrapper.py
More file actions
778 lines (659 loc) · 24.9 KB
/
Copy pathzenity_wrapper.py
File metadata and controls
778 lines (659 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/env python3
"""
Python Zenity Wrapper
A comprehensive Python wrapper for Zenity dialogs, making it easy to create
native desktop dialogs in your applications.
"""
import subprocess
import os
from typing import Optional, List, Any, Union, Literal
from dataclasses import dataclass
@dataclass
class CommonOptions:
"""Common options for all dialog types."""
title: Optional[str] = None
width: Optional[int] = None
height: Optional[int] = None
timeout: Optional[int] = None
ok_label: Optional[str] = None
cancel_label: Optional[str] = None
extra_button: Optional[str] = None
modal_hint: bool = False
attach_parent: Optional[int] = None
@dataclass
class InfoOptions(CommonOptions):
"""Options for info, warning, and error dialogs."""
no_wrap: bool = False
no_markup: bool = False
ellipsize: bool = False
icon_name: Optional[str] = None
@dataclass
class QuestionOptions(CommonOptions):
"""Options for question dialogs."""
default_cancel: bool = False
no_wrap: bool = False
no_markup: bool = False
ellipsize: bool = False
@dataclass
class EntryOptions(CommonOptions):
"""Options for entry dialogs."""
entry_text: Optional[str] = None
hide_text: bool = False
@dataclass
class PasswordOptions(CommonOptions):
"""Options for password dialogs."""
username: bool = False
@dataclass
class ScaleOptions(CommonOptions):
"""Options for scale/slider dialogs."""
value: Optional[int] = None
min_value: Optional[int] = None
max_value: Optional[int] = None
step: Optional[int] = None
hide_value: bool = False
print_partial: bool = False
@dataclass
class CalendarOptions(CommonOptions):
"""Options for calendar dialogs."""
day: Optional[int] = None
month: Optional[int] = None
year: Optional[int] = None
date_format: str = "%Y-%m-%d"
@dataclass
class ListOptions(CommonOptions):
"""Options for list dialogs."""
checklist: bool = False
radiolist: bool = False
multiple: bool = False
editable: bool = False
separator: str = "|"
print_column: Optional[int] = None
hide_column: Optional[int] = None
hide_header: bool = False
@dataclass
class ColorSelectionOptions(CommonOptions):
"""Options for color selection dialogs."""
color: Optional[str] = None
show_palette: bool = False
@dataclass
class FileSelectionOptions(CommonOptions):
"""Options for file selection dialogs."""
multiple: bool = False
directory: bool = False
save: bool = False
separator: str = "|"
filename: Optional[str] = None
confirm_overwrite: bool = False
@dataclass
class ProgressOptions(CommonOptions):
"""Options for progress dialogs."""
percentage: Optional[int] = None
pulsate: bool = False
auto_close: bool = False
auto_kill: bool = False
no_cancel: bool = False
time_remaining: bool = False
@dataclass
class FormField:
"""Base class for form fields."""
type: Literal['entry', 'password', 'multiline', 'calendar', 'list', 'combo']
label: str
value: Optional[str] = None
values: Optional[List[str]] = None
header: Optional[str] = None
column_values: Optional[List[str]] = None
@dataclass
class FormsOptions(CommonOptions):
"""Options for forms dialogs."""
text: Optional[str] = None
separator: str = "|"
forms_date_format: Optional[str] = None
show_header: bool = False
@dataclass
class FormsResult:
"""Result from forms dialog."""
button: Literal['ok', 'cancel', 'extra']
values: Optional[List[str]] = None
@dataclass
class TextOptions(CommonOptions):
"""Options for text dialogs."""
filename: Optional[str] = None
editable: bool = False
font_name: Optional[str] = None
checkbox: Optional[str] = None
html_mode: bool = False
url: Optional[str] = None
auto_scroll: bool = False
class Zenity:
"""
A comprehensive Python wrapper for Zenity dialogs.
Provides easy-to-use methods for creating various types of native dialogs
including message dialogs, input dialogs, file selectors, and more.
"""
def __init__(self):
"""Initialize the Zenity wrapper."""
# Set environment variables to prevent GTK4 crashes on macOS
self.env = os.environ.copy()
self.env['GSETTINGS_BACKEND'] = 'memory'
self.env['GSETTINGS_SCHEMA_DIR'] = '/dev/null'
self.env['G_MESSAGES_DEBUG'] = ''
@staticmethod
def _sanitize_input(value: str) -> str:
"""Sanitize input to prevent command injection."""
# Zenity handles most escaping, but we validate for safety
if not isinstance(value, str):
value = str(value)
# Remove null bytes that could cause issues
return value.replace('\0', '')
def _add_common_options(self, args: List[str], options: CommonOptions) -> None:
"""Add common options to the arguments list."""
if options.title:
args.append(f'--title={options.title}')
if options.width:
args.append(f'--width={options.width}')
if options.height:
args.append(f'--height={options.height}')
if options.timeout:
args.append(f'--timeout={options.timeout}')
if options.ok_label:
args.append(f'--ok-label={options.ok_label}')
if options.cancel_label:
args.append(f'--cancel-label={options.cancel_label}')
if options.extra_button:
args.append(f'--extra-button={options.extra_button}')
if options.modal_hint:
args.append('--modal')
if options.attach_parent:
args.append(f'--attach={options.attach_parent}')
def _run(self, args: List[str]) -> str:
"""Run a Zenity command and return the output."""
try:
result = subprocess.run(
['zenity'] + args,
capture_output=True,
text=True,
env=self.env,
check=False
)
except FileNotFoundError:
raise RuntimeError("Zenity is not installed. Please install it first.")
except Exception as e:
raise RuntimeError(f"Failed to run zenity: {str(e)}")
if result.returncode == 0:
return result.stdout.strip()
else:
raise subprocess.CalledProcessError(result.returncode, args, result.stdout, result.stderr)
def _run_with_exit_code(self, args: List[str]) -> tuple[str, str, int]:
"""Run a Zenity command and return output, stderr, and exit code."""
try:
result = subprocess.run(
['zenity'] + args,
capture_output=True,
text=True,
env=self.env,
check=False
)
return result.stdout.strip(), result.stderr.strip(), result.returncode
except FileNotFoundError:
raise RuntimeError("Zenity is not installed. Please install it first.")
except Exception as e:
raise RuntimeError(f"Failed to run zenity: {str(e)}")
def _run_with_input(self, args: List[str], input_text: str) -> str:
"""Run a Zenity command with input piped to stdin."""
try:
result = subprocess.run(
['zenity'] + args,
input=input_text,
capture_output=True,
text=True,
env=self.env,
check=False
)
except FileNotFoundError:
raise RuntimeError("Zenity is not installed. Please install it first.")
except Exception as e:
raise RuntimeError(f"Failed to run zenity: {str(e)}")
if result.returncode == 0:
return result.stdout.strip()
else:
raise subprocess.CalledProcessError(result.returncode, args, result.stdout, result.stderr)
# Message Dialogs
def info(self, message: str, options: Optional[InfoOptions] = None) -> None:
"""Display an information dialog."""
if options is None:
options = InfoOptions()
message = self._sanitize_input(message)
args = ['--info', f'--text={message}']
self._add_common_options(args, options)
if options.no_wrap:
args.append('--no-wrap')
if options.no_markup:
args.append('--no-markup')
if options.ellipsize:
args.append('--ellipsize')
if options.icon_name:
args.append(f'--icon-name={options.icon_name}')
try:
self._run(args)
except subprocess.CalledProcessError:
# Info dialogs don't need to throw
pass
def warning(self, message: str, options: Optional[InfoOptions] = None) -> None:
"""Display a warning dialog."""
if options is None:
options = InfoOptions()
message = self._sanitize_input(message)
args = ['--warning', f'--text={message}']
self._add_common_options(args, options)
if options.no_wrap:
args.append('--no-wrap')
if options.no_markup:
args.append('--no-markup')
if options.ellipsize:
args.append('--ellipsize')
if options.icon_name:
args.append(f'--icon-name={options.icon_name}')
try:
self._run(args)
except subprocess.CalledProcessError:
# Warning dialogs don't need to throw
pass
def error(self, message: str, options: Optional[InfoOptions] = None) -> None:
"""Display an error dialog."""
if options is None:
options = InfoOptions()
message = self._sanitize_input(message)
args = ['--error', f'--text={message}']
self._add_common_options(args, options)
if options.no_wrap:
args.append('--no-wrap')
if options.no_markup:
args.append('--no-markup')
if options.ellipsize:
args.append('--ellipsize')
if options.icon_name:
args.append(f'--icon-name={options.icon_name}')
try:
self._run(args)
except subprocess.CalledProcessError:
# Error dialogs don't need to throw
pass
def question(self, message: str, options: Optional[QuestionOptions] = None) -> bool:
"""
Display a question dialog with OK/Cancel buttons.
Returns True if OK clicked, False if cancelled.
"""
if options is None:
options = QuestionOptions()
message = self._sanitize_input(message)
args = ['--question', f'--text={message}']
self._add_common_options(args, options)
if options.default_cancel:
args.append('--default-cancel')
if options.no_wrap:
args.append('--no-wrap')
if options.no_markup:
args.append('--no-markup')
if options.ellipsize:
args.append('--ellipsize')
try:
self._run(args)
return True # User clicked Yes/OK
except subprocess.CalledProcessError as e:
# Exit code 1 means user clicked No/Cancel
if e.returncode in (1, 5):
return False
raise e
# Input Dialogs
def entry(self, text: str, options: Optional[EntryOptions] = None) -> Optional[str]:
"""Display a text entry dialog. Returns the entered text or None if cancelled."""
if options is None:
options = EntryOptions()
args = ['--entry']
if text:
args.append(f'--text={text}')
if options.entry_text:
args.append(f'--entry-text={options.entry_text}')
if options.hide_text:
args.append('--hide-text')
self._add_common_options(args, options)
try:
return self._run(args)
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
def password(self, options: Optional[PasswordOptions] = None) -> Optional[str]:
"""
Display a password entry dialog.
Returns the password, or 'username|password' if username option is enabled.
Returns None if cancelled.
"""
if options is None:
options = PasswordOptions()
args = ['--password']
if options.username:
args.append('--username')
self._add_common_options(args, options)
try:
return self._run(args)
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
def scale(self, text: str, options: Optional[ScaleOptions] = None) -> Optional[int]:
"""Display a slider/scale dialog. Returns the selected number or None if cancelled."""
if options is None:
options = ScaleOptions()
args = ['--scale']
if text:
args.append(f'--text={text}')
if options.value is not None:
args.append(f'--value={options.value}')
if options.min_value is not None:
args.append(f'--min-value={options.min_value}')
if options.max_value is not None:
args.append(f'--max-value={options.max_value}')
if options.step is not None:
args.append(f'--step={options.step}')
if options.print_partial:
args.append('--print-partial')
if options.hide_value:
args.append('--hide-value')
self._add_common_options(args, options)
try:
result = self._run(args)
return int(result) if result else None
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
def calendar(self, text: str, options: Optional[CalendarOptions] = None) -> Optional[str]:
"""Display a calendar date picker. Returns the selected date or None if cancelled."""
if options is None:
options = CalendarOptions()
args = ['--calendar']
if text:
args.append(f'--text={text}')
if options.day:
args.append(f'--day={options.day}')
if options.month:
args.append(f'--month={options.month}')
if options.year:
args.append(f'--year={options.year}')
if options.date_format:
args.append(f'--date-format={options.date_format}')
self._add_common_options(args, options)
try:
return self._run(args)
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
# Selection Dialogs
def list(
self,
text: str,
columns: List[str],
data: List[List[Any]],
options: Optional[ListOptions] = None
) -> Optional[Union[str, List[str]]]:
"""
Display a list selection dialog.
Returns selected value(s) or None if cancelled.
"""
if options is None:
options = ListOptions()
args = ['--list']
if text:
args.append(f'--text={text}')
if options.checklist:
args.append('--checklist')
if options.radiolist:
args.append('--radiolist')
if options.multiple:
args.append('--multiple')
if options.editable:
args.append('--editable')
if options.separator:
args.append(f'--separator={options.separator}')
if options.print_column is not None:
args.append(f'--print-column={options.print_column}')
if options.hide_column is not None:
args.append(f'--hide-column={options.hide_column}')
if options.hide_header:
args.append('--hide-header')
self._add_common_options(args, options)
# Add columns
for col in columns:
args.append(f'--column={col}')
# Add data
for row in data:
for item in row:
args.append(str(item))
try:
result = self._run(args)
if result and options.multiple and options.separator:
return result.split(options.separator)
return result
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
def color_selection(self, options: Optional[ColorSelectionOptions] = None) -> Optional[str]:
"""
Display a color picker dialog.
Returns selected color in rgb format or None if cancelled.
Note: Does not work on macOS due to Zenity GTK limitations.
"""
if options is None:
options = ColorSelectionOptions()
args = ['--color-selection']
if options.color:
color = options.color
# Convert hex to rgb format if needed
if color.startswith('#'):
hex_color = color[1:]
# Convert 8-bit (0-255) to 16-bit (0-65535) by multiplying by 257
r = int(hex_color[0:2], 16) * 257
g = int(hex_color[2:4], 16) * 257
b = int(hex_color[4:6], 16) * 257
color = f'rgb({r},{g},{b})'
args.append(f'--color={color}')
if options.show_palette:
args.append('--show-palette')
self._add_common_options(args, options)
try:
return self._run(args)
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
# File Dialogs
def file_selection(
self,
options: Optional[FileSelectionOptions] = None
) -> Optional[Union[str, List[str]]]:
"""
Display a file or directory selection dialog.
Returns file path(s) or None if cancelled.
"""
if options is None:
options = FileSelectionOptions()
args = ['--file-selection']
if options.multiple:
args.append('--multiple')
if options.directory:
args.append('--directory')
if options.save:
args.append('--save')
if options.filename:
args.append(f'--filename={options.filename}')
if options.confirm_overwrite:
args.append('--confirm-overwrite')
if options.separator:
args.append(f'--separator={options.separator}')
self._add_common_options(args, options)
try:
result = self._run(args)
if result and options.multiple and options.separator:
return result.split(options.separator)
return result
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e
# Progress Dialogs
def progress(
self,
text: str,
options: Optional[ProgressOptions] = None
) -> subprocess.Popen:
"""
Display a progress dialog.
Returns a Popen object for controlling the progress.
Usage:
proc = zenity.progress("Loading...", ProgressOptions(percentage=0, auto_close=True))
proc.stdin.write("50\\n".encode()) # Update to 50%
proc.stdin.write("# Processing...\\n".encode()) # Update message
proc.stdin.close() # Close when done
proc.wait()
"""
if options is None:
options = ProgressOptions()
args = ['--progress']
if text:
args.append(f'--text={text}')
if options.percentage is not None:
args.append(f'--percentage={options.percentage}')
if options.auto_close:
args.append('--auto-close')
if options.auto_kill:
args.append('--auto-kill')
if options.pulsate:
args.append('--pulsate')
if options.no_cancel:
args.append('--no-cancel')
if options.time_remaining:
args.append('--time-remaining')
self._add_common_options(args, options)
try:
proc = subprocess.Popen(
['zenity'] + args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
env=self.env,
text=True
)
return proc
except Exception as e:
raise RuntimeError(f"Failed to create progress dialog: {str(e)}")
def update_progress(
self,
process: subprocess.Popen,
percentage: int,
text: str = ''
) -> None:
"""Helper method to update a progress dialog."""
if process.stdin:
process.stdin.write(f'{percentage}\n')
if text:
process.stdin.write(f'# {text}\n')
process.stdin.flush()
# Advanced Dialogs
def forms(
self,
fields: List[FormField],
options: Optional[FormsOptions] = None
) -> FormsResult:
"""
Display a multi-field form dialog.
Returns FormsResult with button type and values.
"""
if options is None:
options = FormsOptions()
args = ['--forms']
if options.text:
args.append(f'--text={options.text}')
if options.separator:
args.append(f'--separator={options.separator}')
if options.forms_date_format:
args.append(f'--forms-date-format={options.forms_date_format}')
if options.show_header:
args.append('--show-header')
self._add_common_options(args, options)
# Add form fields
for field in fields:
if field.type == 'entry':
args.append(f'--add-entry={field.label}')
elif field.type == 'password':
args.append(f'--add-password={field.label}')
elif field.type == 'multiline':
args.append(f'--add-multiline-entry={field.label}')
elif field.type == 'calendar':
args.append(f'--add-calendar={field.label}')
elif field.type == 'list':
args.append(f'--add-list={field.label}')
if field.header:
args.append(f'--list-header={field.header}')
if field.values:
args.append(f'--list-values={"|".join(field.values)}')
if field.column_values:
for val in field.column_values:
args.append(f'--column-values={val}')
elif field.type == 'combo':
args.append(f'--add-combo={field.label}')
if field.values:
args.append(f'--combo-values={"|".join(field.values)}')
result, stderr, exit_code = self._run_with_exit_code(args)
separator = options.separator or '|'
if exit_code == 0:
# OK button clicked
return FormsResult(button='ok', values=result.split(separator) if result else [])
elif exit_code == 1:
# Check if extra button was clicked
if options.extra_button and result == options.extra_button:
return FormsResult(button='extra', values=None)
else:
# User cancelled
return FormsResult(button='cancel', values=None)
elif exit_code == 5:
# Timeout
return FormsResult(button='cancel', values=None)
else:
# Error occurred
raise subprocess.CalledProcessError(exit_code, args, result, stderr)
def text(
self,
message: str,
options: Optional[TextOptions] = None
) -> Optional[str]:
"""
Display a text information dialog with optional editing.
Returns the (possibly edited) text or None if cancelled.
"""
if options is None:
options = TextOptions()
args = ['--text-info']
if options.filename:
args.append(f'--filename={options.filename}')
if options.editable:
args.append('--editable')
if options.html_mode:
args.append('--html')
if options.url:
args.append(f'--url={options.url}')
if options.font_name:
args.append(f'--font={options.font_name}')
if options.checkbox:
args.append(f'--checkbox={options.checkbox}')
if options.auto_scroll:
args.append('--auto-scroll')
self._add_common_options(args, options)
try:
return self._run_with_input(args, message)
except subprocess.CalledProcessError as e:
if e.returncode in (1, 5):
return None
raise e