Repository navigation
Expand file tree
/
Copy pathcontroller.py
More file actions
655 lines (609 loc) · 31.7 KB
/
Copy pathcontroller.py
File metadata and controls
655 lines (609 loc) · 31.7 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
#!/usr/bin/python3
"""OmaPlugCtl: argv-only commands with datasource readback."""
import argparse, datetime, json, os, pathlib, re, stat, subprocess, sys, time, urllib.request, fcntl
import updates
import malware
import marketplace
SELF = 'moo.omaplugctl'
DEFAULT_BAR = 'omarchy.bar'
SECTIONS = ('left', 'centerBegin', 'center', 'centerEnd', 'right', 'tray')
EDGES = ('top', 'bottom', 'left', 'right')
SORT_ORDERS = ('name', 'screen', 'updates')
def configured_bar_icon_scale():
configuration = json.loads((pathlib.Path.home()/'.config/omarchy/shell.json').read_text())
for entries in configuration.get('bar', {}).get('layout', {}).values():
for entry in entries if isinstance(entries, list) else []:
if isinstance(entry, dict) and entry.get('id') == SELF:
value = entry.get('barIconScale', 100)
return value if type(value) is int and 50 <= value <= 200 else 100
return 100
def set_bar_icon_scale(value):
if not isinstance(value, str) or not re.fullmatch(r'\d{2,3}', value) or not 50 <= int(value) <= 200:
raise ValueError('Choose a bar icon size between 50% and 200%.')
percentage = int(value)
run(['omarchy', 'bar', 'set', SELF, 'barIconScale', str(percentage), '--json'])
if configured_bar_icon_scale() != percentage:
raise RuntimeError('The bar icon size was not saved. Refresh and try again.')
return {'plugins': inventory(), 'message': 'Bar icon size saved.'}
def workspace_settings():
"""Only offer a count when the active workspace widget declares support."""
configuration = json.loads((pathlib.Path.home()/'.config/omarchy/shell.json').read_text())
catalog = json.loads(run(['omarchy-plugin-catalog']))
candidates = []
for plugin in catalog:
try:
manifest = json.loads(pathlib.Path(plugin['manifestPath']).read_text())
except (KeyError, OSError, TypeError, ValueError):
continue
if not isinstance(manifest, dict):
continue
provenance = manifest.get('omarchy') or {}
if manifest.get('id') != 'omarchy.workspaces' and (not isinstance(provenance, dict) or provenance.get('clonedFrom') != 'omarchy.workspaces'):
continue
widget = manifest.get('barWidget') or {}
if not isinstance(widget, dict):
continue
schema = widget.get('schema') or []
if not isinstance(schema, list) or not any(isinstance(field, dict) and field.get('key') == 'pinnedCount' and field.get('type') == 'integer' for field in schema):
continue
defaults = widget.get('defaults') or {}
defaults = defaults if isinstance(defaults, dict) else {}
for entries in configuration.get('bar', {}).get('layout', {}).values():
for entry in entries if isinstance(entries, list) else []:
entry = {'id': entry} if isinstance(entry, str) else entry
if isinstance(entry, dict) and entry.get('id') == plugin['id']:
value = entry.get('pinnedCount', defaults.get('pinnedCount', 5))
if type(value) is int and 0 <= value <= 10:
candidates.append({'id': plugin['id'], 'count': value})
else:
return None
# One control must not silently change only one of several widget instances.
return candidates[0] if len(candidates) == 1 else None
def set_workspace_count(value):
if not isinstance(value, str) or not re.fullmatch(r'(?:[0-9]|10)', value):
raise ValueError('Choose between 0 and 10 always-visible workspaces.')
current = workspace_settings()
if current is None:
raise RuntimeError('The active workspace widget does not expose a single configurable count.')
run(['omarchy', 'bar', 'set', current['id'], 'pinnedCount', value, '--json'])
if workspace_settings() != {'id': current['id'], 'count': int(value)}:
raise RuntimeError('The workspace count was not saved. Refresh and try again.')
return {'plugins': inventory(), 'message': 'Workspace count saved.'}
def scan_before_update():
config = json.loads((pathlib.Path.home()/'.config/omarchy/shell.json').read_text())
for entries in config.get('bar', {}).get('layout', {}).values():
for entry in entries if isinstance(entries, list) else []:
if isinstance(entry, dict) and entry.get('id') == SELF:
return entry.get('scanBeforeUpdate', True) is not False
return True
def set_scan_before_update(value):
if value not in ('true', 'false'):
raise ValueError('Choose whether to scan updates before installation.')
run(['omarchy', 'bar', 'set', SELF, 'scanBeforeUpdate', value, '--json'])
if scan_before_update() != (value == 'true'):
raise RuntimeError('The malware scan preference was not confirmed.')
return {'plugins': inventory(), 'message': 'Malware scan preference saved.'}
def configured_sort():
config = json.loads((pathlib.Path.home()/'.config/omarchy/shell.json').read_text())
for entries in config.get('bar', {}).get('layout', {}).values():
for entry in entries if isinstance(entries, list) else []:
if isinstance(entry, dict) and entry.get('id') == SELF:
return entry.get('sortOrder') if entry.get('sortOrder') in SORT_ORDERS else 'screen'
return 'screen'
def sort_plugins(plugins, order):
def name(plugin): return str(plugin.get('name') or plugin['id']).casefold()
if order == 'screen':
def placement(plugin):
positions = [(SECTIONS.index(p['section']), p['index']) for p in plugin.get('instances', []) if p['section'] in SECTIONS]
return (*min(positions, default=(len(SECTIONS), 0)), name(plugin))
return sorted(plugins, key=placement)
if order == 'updates':
def pending(plugin):
update = plugin.get('update') or {}
try: date = datetime.date.fromisoformat(update.get('latestDate', '')).toordinal()
except (ValueError, TypeError): date = 0
return (update.get('status') not in ('available', 'rewritten', 'diverged'), -date, name(plugin))
return sorted(plugins, key=pending)
return sorted(plugins, key=name)
def set_sort(order):
if order not in SORT_ORDERS:
raise ValueError('Choose name, screen, or updates.')
run(['omarchy', 'bar', 'set', SELF, 'sortOrder', order])
if configured_sort() != order:
raise RuntimeError('The selected sort order was not confirmed.')
return {'plugins': inventory(), 'message': ''}
def bar_position():
position = run(['omarchy', 'bar', 'position'])
if position not in EDGES:
raise RuntimeError('Could not read the current bar position.')
return position
def set_bar_position(edge):
if edge not in EDGES:
raise ValueError('Choose top, bottom, left, or right.')
run(['omarchy', 'bar', 'position', edge])
position = bar_position()
if position != edge:
raise RuntimeError('New bar position was not confirmed. Try Refresh.')
return {'plugins': inventory(), 'barPosition': position, 'message': 'Bar position confirmed.'}
def builtin_about_url(pid):
# HEAD follows GitHub's default branch instead of assuming a branch name.
base = 'https://github.com/omacom/omarchy'
root = pathlib.Path(os.environ.get('OMARCHY_PATH') or pathlib.Path.home()/'.local/share/omarchy')
for manifest in (root/'shell/plugins').rglob('*manifest.json'):
try:
data = json.loads(manifest.read_text())
except (OSError, ValueError):
continue
if data.get('id') == pid:
source = manifest.parent if manifest.name == 'manifest.json' else manifest
kind = 'tree' if source == manifest.parent else 'blob'
return base + '/' + kind + '/HEAD/' + source.relative_to(root).as_posix()
return base
def builtin_metadata(pid):
if not isinstance(pid, str) or not re.fullmatch(r'omarchy\.[A-Za-z0-9._-]+', pid):
return {}
root = pathlib.Path(os.environ.get('OMARCHY_PATH') or pathlib.Path.home()/'.local/share/omarchy')
for path in (root/'shell/plugins').rglob('*manifest.json'):
try:
manifest = json.loads(path.read_text())
except (OSError, ValueError):
continue
if isinstance(manifest, dict) and manifest.get('id') == pid:
return {**manifest, 'sourceDir': str(path.parent), 'manifestPath': str(path)}
return {}
def run(argv, timeout=25):
p = subprocess.run(argv, capture_output=True, text=True, timeout=timeout)
if p.returncode:
raise RuntimeError(p.stderr.strip() or p.stdout.strip() or f'Command failed ({p.returncode})')
return p.stdout.strip()
def readme_description(metadata):
directory = metadata.get('sourceDir')
if not isinstance(directory, str) or not directory:
return ''
# Single-file built-ins share a directory; its README is not per-plugin.
manifest = metadata.get('manifestPath')
if isinstance(manifest, str) and pathlib.Path(manifest).name != 'manifest.json':
return ''
try:
path = pathlib.Path(directory) / 'README.md'
descriptor = os.open(path, os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW)
with os.fdopen(descriptor, 'rb') as file:
info = os.fstat(file.fileno())
if not stat.S_ISREG(info.st_mode) or info.st_size > 256 * 1024:
return ''
content = file.read(256 * 1024 + 1)
if len(content) > 256 * 1024:
return ''
lines = content.decode('utf-8-sig').splitlines()
except (OSError, UnicodeError, ValueError):
return ''
fence = None
sections = []
paragraph = []
collecting = False
finished = False
for line in lines:
marker = re.match(r'^ {0,3}(`{3,}|~{3,})(.*)$', line)
if fence:
if marker and marker[1][0] == fence[0] and len(marker[1]) >= len(fence) and not marker[2].strip():
fence = None
continue
if marker:
fence = marker[1]
if collecting:
finished = True
continue
heading = re.match(r'^ {0,3}(#{1,6})\s+(.+?)\s*$', line)
if heading:
title = re.sub(r'\s+#+$', '', heading[2]).strip()
collecting = heading[1] == '##' and title.casefold() == 'description for agents'
if collecting:
paragraph = []
sections.append(paragraph)
finished = False
continue
if not collecting or finished:
continue
text = line.strip()
if not text:
if paragraph:
finished = True
elif line.startswith((' ', '\t')) or re.match(r'^(?:>|[-*+]\s|\d+[.)]\s|!\[)', text):
finished = True
else:
paragraph.append(text)
# Ambiguous repeated sections or invalid blocks fall back to the manifest.
return ' '.join(' '.join(sections[0]).split()) if len(sections) == 1 else ''
def plugin_description(metadata):
widget = metadata.get('barWidget')
widget = widget if isinstance(widget, dict) else {}
descriptions = []
for value in (metadata.get('description'), widget.get('description')):
if not isinstance(value, str):
continue
text = ' '.join(value.split())
if text and not any(text.casefold() in existing.casefold() for existing in descriptions):
descriptions.append(text)
description = readme_description(metadata) or '\n\n'.join(descriptions)
return description if len(description) <= 1200 else description[:1199] + '…'
def plugin_author(manifest):
value = manifest.get('author') or manifest.get('authors')
if not isinstance(value, list):
value = [value]
names = []
for author in value:
if isinstance(author, dict):
author = author.get('name')
if isinstance(author, str) and author.strip():
names.append(' '.join(author.split()))
return ', '.join(names)[:200]
def local_update_date(path, manifest):
# Single-file built-ins share a widgets directory: only their manifest
# and declared entry points belong to their update date.
if path.name != 'manifest.json':
entries = manifest.get('entryPoints', {})
entries = entries if isinstance(entries, dict) else {}
files = [path] + [path.parent / entry for entry in entries.values() if isinstance(entry, str)]
else:
files = []
ignored = {'__pycache__', 'archive', 'node_modules', 'build'}
for directory, dirs, names in os.walk(path.parent):
dirs[:] = [name for name in dirs if not name.startswith('.') and name not in ignored]
files.extend(pathlib.Path(directory) / name for name in names if not name.startswith('.') and not name.endswith(('.pyc', '.pyo')))
if len(files) > 8192:
return ''
timestamps = []
for file in files:
try:
if file.is_file():
timestamps.append(file.stat().st_mtime)
except OSError:
continue
try:
return datetime.datetime.fromtimestamp(max(timestamps)).date().isoformat() if timestamps else ''
except (ValueError, OverflowError, OSError):
return ''
def plugin_details(metadata, history_cache=None):
manifest = {}
path = metadata.get('manifestPath')
if isinstance(path, str):
try:
document = json.loads(pathlib.Path(path).read_text())
if isinstance(document, dict):
manifest = document
except (OSError, ValueError):
pass
updated = local_update_date(pathlib.Path(path), manifest) if isinstance(path, str) and manifest else ''
author = plugin_author(manifest)
result = {'author': author, 'updatedDate': updated}
keywords = []
for field in ('keywords', 'tags'):
values = manifest.get(field)
if not isinstance(values, list):
continue
for value in values[:32]:
if isinstance(value, str):
keyword = ' '.join(value.split())[:80]
if keyword:
keywords.append(keyword)
if keywords:
result['keywords'] = list(dict.fromkeys(keywords))
provenance = manifest.get('omarchy') or {}
provenance = provenance if isinstance(provenance, dict) else {}
source_commit = provenance.get('clonedAtCommit', '')
if isinstance(source_commit, str) and re.fullmatch(r'[0-9a-f]{40}', source_commit):
result['sourceCommit'] = source_commit
customized = provenance.get('customizedBy')
if isinstance(customized, str) and customized.strip():
result['customizedBy'] = ' '.join(customized.split())[:200]
if not author or author.casefold() in ('omarchy', 'omarchy team', 'omacom'):
cache = history_cache if history_cache is not None else author_cache()
inferred = first_commit_author(metadata, cache)
if not inferred and provenance.get('clonedFrom'):
original = builtin_metadata(provenance['clonedFrom'])
inferred = first_commit_author(original, cache) if original else {}
if inferred:
inferred = {**inferred, 'authorSource': 'upstream-first-commit'}
if inferred:
result.update(inferred)
return result
def first_commit_author(metadata, cache):
directory = metadata.get('sourceDir')
manifest = metadata.get('manifestPath')
if not directory or not manifest:
return {}
try:
root = pathlib.Path(updates.git(directory, 'rev-parse', '--show-toplevel')).resolve()
head = updates.git(root, 'rev-parse', 'HEAD')
paths = [pathlib.Path(manifest)]
for entry in (metadata.get('entryPoints') or {}).values():
if isinstance(entry, str): paths.append(pathlib.Path(manifest).parent / entry)
paths = sorted({str(path.resolve().relative_to(root)) for path in paths})
key = str(root) + '\n' + head + '\n' + '\n'.join(paths)
if key in cache:
return cache[key]
candidates = []
for path in paths:
rows = updates.git(root, 'log', '--follow', '--diff-filter=A', '--format=%H%x1f%an%x1f%ct', '--', path, timeout=20).splitlines()
if rows:
commit, author, timestamp = rows[-1].split('\x1f')
candidates.append((int(timestamp), commit, ' '.join(author.split())[:200]))
if not candidates:
cache[key] = {}
return {}
timestamp, commit, author = min(candidates)
result = {'author': author, 'authorSource': 'first-commit', 'authorCommit': commit,
'authorDate': datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc).date().isoformat(),
'authorHistoryShallow': updates.git(root, 'rev-parse', '--is-shallow-repository') == 'true'}
cache[key] = result
return result
except (RuntimeError, OSError, ValueError, subprocess.SubprocessError):
return {}
def author_cache():
try:
path = updates.cache_path().with_name('authors.json')
if path.stat().st_size < 1_000_000:
data = json.loads(path.read_text())
return data if isinstance(data, dict) else {}
except (OSError, ValueError):
pass
return {}
def inventory():
plugins = json.loads(run(['omarchy-shell', 'shell', 'listPlugins']))
catalog = json.loads(run(['omarchy-plugin-catalog']))
metadata = {p['id']: p for p in catalog if isinstance(p, dict) and isinstance(p.get('id'), str)}
update_cache = updates.load_cache()
operations = updates.operations()
history = author_cache()
previous_history = dict(history)
config = json.loads((pathlib.Path.home()/'.config/omarchy/shell.json').read_text())
layout = config.get('bar', {}).get('layout', {})
result = []
for p in plugins:
if not isinstance(p, dict) or not isinstance(p.get('kinds'), list):
continue
p['isBarWidget'] = 'bar-widget' in p['kinds']
p['isDefaultBar'] = p['id'] == DEFAULT_BAR and 'bar' in p['kinds']
p['canOpen'] = bool(set(p['kinds']) & {'overlay', 'panel', 'menu'})
instances = []
for section in SECTIONS:
for index, entry in enumerate(layout.get(section, [])):
eid = entry if isinstance(entry, str) else entry.get('id', '')
if eid == p['id']:
instances.append({'section': section, 'index': index})
p['instances'] = instances
p['description'] = plugin_description(metadata.get(p['id'], {}))
p.update(plugin_details(metadata.get(p['id'], {}), history))
p['isSelf'] = p['id'] == SELF
p['canDisable'] = p.get('canDisable', True)
p['canRemove'] = not p['isSelf'] and not p['firstParty'] and (pathlib.Path.home()/'.config/omarchy/plugins'/p['id']).exists()
p['update'] = updates.cached_status(p, metadata.get(p['id'], {}), update_cache)
p['operation'] = operations.get(p['id'], {})
result.append(p)
if history != previous_history:
path = updates.cache_path().with_name('authors.json')
path.parent.mkdir(parents=True, exist_ok=True)
# Reuse the same atomic-cache writer with an explicit destination.
updates.save_cache(history, path=path)
return result
def check_updates():
plugins = inventory()
metadata = {p['id']: p for p in json.loads(run(['omarchy-plugin-catalog']))}
updates.check_all(plugins, metadata)
plugins = inventory()
available = sum(p['update']['status'] in ('available', 'rewritten', 'diverged') for p in plugins)
failed = sum(p['update']['status'] == 'error' for p in plugins)
return {'plugins': plugins, 'message': f'{available} updates available. {failed} source checks failed.'}
def update_target(pid):
if not isinstance(pid, str) or not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9._-]*', pid) or '..' in pid:
raise ValueError('Invalid plugin id.')
plugin = next((p for p in inventory() if p['id'] == pid), None)
if plugin is None:
raise ValueError('Unknown bar plugin.')
metadata = next((p for p in json.loads(run(['omarchy-plugin-catalog'])) if p['id'] == pid), {})
source = updates.source(plugin, metadata)
if source is None:
if plugin['firstParty'] and not plugin.get('clonedFrom'):
return plugin, None
raise RuntimeError('No independent Git update repository is configured for this plugin.')
if source['dirty']:
raise RuntimeError('This source has local changes. Review them before updating; no files were changed.')
return plugin, source
def launch_update(pid):
plugin, source = update_target(pid)
if source is None or source['kind'] != 'plugin':
raise RuntimeError('Built-in plugins are updated with Omarchy.')
if not source['tracking']:
raise RuntimeError('Choose a branch tracking origin before updating this plugin.')
log = updates.cache_path().parent / 'operations' / 'launcher.lock'
log.parent.mkdir(parents=True, exist_ok=True)
with log.open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
previous = updates.operations().get(pid, {})
if previous.get('status') == 'working':
changed = datetime.datetime.fromisoformat(previous['changedAt'])
if (datetime.datetime.now(datetime.timezone.utc) - changed).total_seconds() < 120:
return {'plugins': inventory(), 'message': 'This plugin is already updating.'}
updates.save_operation(pid, status='working', message='Updating plugin…')
try:
subprocess.Popen([sys.executable, str(pathlib.Path(__file__).resolve()), 'finish-update', pid],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
except Exception as exc:
updates.save_operation(pid, status='error', message=str(exc)[:1200])
raise
return {'plugins': inventory(), 'message': 'Updating plugin…'}
def apply_update(pid):
plugin, source = update_target(pid)
metadata = next(p for p in json.loads(run(['omarchy-plugin-catalog'])) if p['id'] == pid)
result = updates.apply_update(plugin, metadata, scan=scan_before_update())
# User plugin files are watched by Omarchy; a rescan also picks up manifest
# changes. Failure to rescan does not undo a successfully verified Git update.
message = 'Updated to ' + result['head'][:8] + '.' if result['changed'] else 'Already up to date.'
if result.get('backup'):
message += ' Upstream history reconciled; previous version backed up.'
if result.get('scan', {}).get('status') == 'no-threats-detected':
message += ' ClamAV: no threats detected.'
updates.save_operation(pid, status='done', message=message, backup=result.get('backup', ''), scan=result.get('scan', {}))
if result['changed']:
try:
run(['omarchy-shell', 'shell', 'rescanPlugins'])
except (RuntimeError, OSError, subprocess.SubprocessError):
message += ' Refresh the shell to reload the plugin.'
return {'plugins': inventory(), 'message': message}
def about_url(p):
directory = pathlib.Path.home()/'.config/omarchy/plugins'/p['id']
if directory.exists():
manifest = json.loads((directory/'manifest.json').read_text())
for key in ('homepage', 'website', 'repository', 'repo'):
value = manifest.get(key)
if isinstance(value, dict): value = value.get('url')
if isinstance(value, str) and value.startswith('https://'):
return value
if (directory/'.git').exists():
value = run(['git', '-C', str(directory), 'remote', 'get-url', 'origin'])
if value.startswith('https://'): return value.removesuffix('.git')
if value.startswith('git@github.com:'): return 'https://github.com/' + value.split(':', 1)[1].removesuffix('.git')
if p['firstParty'] or p.get('clonedFrom', '').startswith('omarchy.'):
return builtin_about_url(p.get('clonedFrom') or p['id'])
with urllib.request.urlopen('https://plugins.omarchy.org/catalog.json', timeout=12) as response:
catalog = json.load(response)
for entry in catalog['plugins']:
if entry['id'] == p['id'] and str(entry.get('repo', '')).startswith('https://'):
return entry['repo']
raise RuntimeError('No source page found for this plugin.')
def active_bar_id(plugins):
return next((p['id'] for p in plugins
if isinstance(p, dict) and p.get('active') is True
and isinstance(p.get('kinds'), list) and 'bar' in p['kinds'] and p.get('id')), None)
def switch_bar(action, plugin, before):
if 'bar' not in plugin['kinds']:
raise ValueError('Only a full-bar plugin can be used as the active bar.')
current = active_bar_id(before)
pid = plugin['id']
if action == 'reset-bar' and (current != pid or pid == DEFAULT_BAR):
raise ValueError('The selected replacement bar is no longer active. Try Refresh.')
target = DEFAULT_BAR if action == 'reset-bar' else pid
if current == target:
return {'plugins': before, 'message': 'Bar is already active.'}
run(['omarchy', 'bar', 'reset'] if target == DEFAULT_BAR else ['omarchy', 'bar', 'use', target])
deadline = time.monotonic() + 5
while time.monotonic() < deadline:
try:
active = active_bar_id(json.loads(run(['omarchy-shell', 'shell', 'listPlugins'], timeout=2)))
except (RuntimeError, ValueError, subprocess.TimeoutExpired):
active = None
if active == target:
after = inventory()
if active_bar_id(after) == target:
return {'plugins': after, 'message': 'Bar switch confirmed.'}
break
time.sleep(0.2)
raise RuntimeError('The requested bar did not become active. Try Refresh.')
def execute(action, pid, section=None, source=None, index=None, confirmed=False):
before = inventory()
p = next((p for p in before if p['id'] == pid), None)
if not p: raise ValueError('Unknown plugin.')
if p.get('isSelf') and action == 'remove':
raise ValueError('OmaPlugCtl cannot uninstall itself.')
if p.get('isSelf') and action == 'disable' and not confirmed:
raise ValueError('Confirm before pausing OmaPlugCtl.')
if action in ('use-bar', 'reset-bar'):
return switch_bar(action, p, before)
if action == 'disable' and p.get('canDisable') is False:
raise ValueError('This plugin must remain enabled.')
if action == 'open':
if not p.get('enabled') or not set(p['kinds']) & {'overlay', 'panel', 'menu'}:
raise ValueError('Enable a panel, overlay or menu plugin before opening it.')
if run(['omarchy-shell', 'shell', 'summon', pid, '{}']) != 'ok':
raise RuntimeError('The shell did not accept the open request.')
return {'plugins': before, 'message': 'Open requested.'}
if action == 'about':
url = about_url(p)
run(['xdg-open', url])
return {'plugins': before, 'message': 'Opened source page.'}
if action == 'remove':
if not p['canRemove'] or not confirmed: raise ValueError('Uninstall requires confirmation and an installed third-party plugin.')
run(['omarchy-plugin-remove', pid, '--yes'])
elif action in ('enable', 'disable'):
run(['omarchy-plugin-' + action, pid])
elif action == 'move':
if 'bar-widget' not in p['kinds']:
raise ValueError('Only bar widgets can be moved on the bar.')
if section not in SECTIONS or source not in SECTIONS or index is None:
raise ValueError('Move requires destination and source instance.')
if {'section': source, 'index': index} not in p['instances']:
raise ValueError('Widget layout changed; refresh and select again.')
placement = {'section': section, 'fromSection': source, 'fromIndex': index}
output = run(['omarchy-shell', 'shell', 'moveBarWidget', pid, json.dumps(placement)])
if output != 'ok': raise RuntimeError(output)
else: raise ValueError('Unsupported action.')
after = inventory()
q = next((q for q in after if q['id'] == pid), None)
if action == 'remove':
deadline = time.monotonic() + 5
while q is not None and time.monotonic() < deadline:
time.sleep(0.2)
after = inventory()
q = next((q for q in after if q['id'] == pid), None)
if q is not None: raise RuntimeError('Plugin removal was not confirmed by the registry; try Refresh.')
if action in ('enable', 'disable') and (q is None or q['enabled'] != (action == 'enable')):
raise RuntimeError('Plugin state was not confirmed by the registry.')
if action == 'move':
count = lambda plugin, region: sum(i['section'] == region for i in plugin['instances'])
if q is None or count(q, section) != count(p, section) + (source != section):
raise RuntimeError('New widget placement was not confirmed.')
return {'plugins': after, 'message': 'Change confirmed.'}
def main():
parser = argparse.ArgumentParser()
parser.add_argument('action', choices=['catalog', 'catalog-install', 'catalog-enable', 'catalog-source', 'list', 'settings', 'icon-scale', 'workspace-count', 'position', 'sort', 'set-scan', 'check-updates', 'update', 'finish-update', 'update-status', 'enable', 'disable', 'move', 'remove', 'about', 'open', 'use-bar', 'reset-bar'])
parser.add_argument('id', nargs='?')
parser.add_argument('--edge', choices=EDGES)
parser.add_argument('--section'); parser.add_argument('--source'); parser.add_argument('--index', type=int)
parser.add_argument('--confirmed', action='store_true')
parser.add_argument('--refresh', action='store_true')
a = parser.parse_args()
try:
if a.action.startswith('catalog'):
if a.action == 'catalog': data = marketplace.state(a.refresh)
elif a.action == 'catalog-install': data = marketplace.install(a.id)
elif a.action == 'catalog-enable': data = marketplace.enable(a.id)
else: data = marketplace.source(a.id)
print(json.dumps({'ok': True, **data}))
return 0
if a.action == 'settings':
print(json.dumps({'ok': True, 'barIconScale': configured_bar_icon_scale()}))
return 0
if a.action == 'update-status':
print(json.dumps({'ok': True, 'operations': updates.operations()}))
return 0
if a.action == 'finish-update':
try:
apply_update(a.id)
return 0
except Exception as exc:
updates.save_operation(a.id, status='error', message=str(exc)[:1200])
return 1
if a.action == 'position':
data = set_bar_position(a.edge)
else:
if a.action == 'list': data = {'plugins': inventory(), 'message': ''}
elif a.action == 'icon-scale': data = set_bar_icon_scale(a.id)
elif a.action == 'workspace-count': data = set_workspace_count(a.id)
elif a.action == 'sort': data = set_sort(a.id)
elif a.action == 'set-scan': data = set_scan_before_update(a.id)
elif a.action == 'check-updates': data = check_updates()
elif a.action == 'update': data = launch_update(a.id)
else: data = execute(a.action, a.id, a.section, a.source, a.index, a.confirmed)
data['barPosition'] = bar_position()
data['sortOrder'] = configured_sort()
data['barIconScale'] = configured_bar_icon_scale()
data['workspaceSettings'] = workspace_settings()
data['scanBeforeUpdate'] = scan_before_update()
data['scannerStatus'] = malware.status()
data['plugins'] = sort_plugins(data['plugins'], data['sortOrder'])
print(json.dumps({'ok': True, **data}))
except Exception as e:
print(json.dumps({'ok': False, 'message': str(e)})); return 1
return 0
if __name__ == '__main__': sys.exit(main())