Batch HyprGlass option reads
The HyprGlass helper read each option with its own hyprctl process: about 40 at startup and on every Hyprland config reload. Reads now go through one `hyprctl -j --batch` call; writes are still verified one by one. Startup goes from 43 hyprctl calls to 3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9aa61559eb
commit
0e43c47bbb
2 files changed
+21
-6
No files matched your search
@@ -25,6 +25,20 @@ def get(key):
|
|||||||
if field in data:return data[field]
|
if field in data:return data[field]
|
||||||
raise ValueError('HyprGlass option unavailable: '+key)
|
raise ValueError('HyprGlass option unavailable: '+key)
|
||||||
|
|
||||||
|
def get_many(keys):
|
||||||
|
# One hyprctl call instead of one per option (apply reads about 40).
|
||||||
|
out=run(['-j','--batch',';'.join('getoption '+PREFIX+k for k in keys)])
|
||||||
|
decoder,pos,found=json.JSONDecoder(),0,{}
|
||||||
|
while True:
|
||||||
|
while pos<len(out) and out[pos].isspace():pos+=1
|
||||||
|
if pos>=len(out):break
|
||||||
|
data,pos=decoder.raw_decode(out,pos)
|
||||||
|
value=next((data[f] for f in ['str','int','float'] if f in data),None)
|
||||||
|
if value is not None:found[str(data.get('option','')).removeprefix(PREFIX)]=value
|
||||||
|
missing=[k for k in keys if k not in found]
|
||||||
|
if missing:raise ValueError('HyprGlass option unavailable: '+missing[0])
|
||||||
|
return found
|
||||||
|
|
||||||
def put(key,value):
|
def put(key,value):
|
||||||
result=run(['keyword',PREFIX+key,str(value)])
|
result=run(['keyword',PREFIX+key,str(value)])
|
||||||
if result!='ok':raise ValueError(result[:350])
|
if result!='ok':raise ValueError(result[:350])
|
||||||
@@ -59,8 +73,9 @@ def save(path,data):
|
|||||||
|
|
||||||
def probe():
|
def probe():
|
||||||
try:
|
try:
|
||||||
return dict(available=True,preset=get('default_preset'),enabled=bool(get('enabled')),layers=bool(get('layers:enabled')),
|
o=get_many(['default_preset','enabled','layers:enabled','layers:namespaces','layers:exclude_namespaces','layers:namespace_presets'])
|
||||||
namespaces=get('layers:namespaces'),exclusions=get('layers:exclude_namespaces'),overrides=get('layers:namespace_presets'))
|
return dict(available=True,preset=o['default_preset'],enabled=bool(o['enabled']),layers=bool(o['layers:enabled']),
|
||||||
|
namespaces=o['layers:namespaces'],exclusions=o['layers:exclude_namespaces'],overrides=o['layers:namespace_presets'])
|
||||||
except (OSError,ValueError,subprocess.TimeoutExpired):return dict(available=False)
|
except (OSError,ValueError,subprocess.TimeoutExpired):return dict(available=False)
|
||||||
|
|
||||||
def dispatch(req):
|
def dispatch(req):
|
||||||
@@ -78,7 +93,7 @@ def dispatch(req):
|
|||||||
return dict(**probe(),notice='Restored previous glass settings.' if not skipped else 'Preserved externally changed options: '+', '.join(skipped))
|
return dict(**probe(),notice='Restored previous glass settings.' if not skipped else 'Preserved externally changed options: '+', '.join(skipped))
|
||||||
if action!='apply':raise ValueError('Unknown glass request')
|
if action!='apply':raise ValueError('Unknown glass request')
|
||||||
target=values(req['options'])
|
target=values(req['options'])
|
||||||
current={key:get(key) for key in target}
|
current=get_many(list(target))
|
||||||
before=journal.get('before',current)
|
before=journal.get('before',current)
|
||||||
# Persist before writing so a partial operation can be restored after a crash.
|
# Persist before writing so a partial operation can be restored after a crash.
|
||||||
record=dict(session=session,before=before,after=target)
|
record=dict(session=session,before=before,after=target)
|
||||||
|
|||||||
+3
-3
@@ -12,14 +12,14 @@ class GlassTests(unittest.TestCase):
|
|||||||
self.state=self.original.copy()
|
self.state=self.original.copy()
|
||||||
def fake_put(self,key,value):self.state[key]=value
|
def fake_put(self,key,value):self.state[key]=value
|
||||||
def test_apply_restore_preserves_other_rules(self):
|
def test_apply_restore_preserves_other_rules(self):
|
||||||
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'put',self.fake_put):
|
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'get_many',lambda keys:{k:self.state[k] for k in keys}),patch.object(glass,'put',self.fake_put):
|
||||||
self.assertTrue(glass.dispatch({'action':'apply','options':self.options})['available'])
|
self.assertTrue(glass.dispatch({'action':'apply','options':self.options})['available'])
|
||||||
self.assertEqual(self.state['default_preset'],'default')
|
self.assertEqual(self.state['default_preset'],'default')
|
||||||
self.assertEqual(self.state['layers:namespaces'],'other-shell')
|
self.assertEqual(self.state['layers:namespaces'],'other-shell')
|
||||||
glass.dispatch({'action':'restore'})
|
glass.dispatch({'action':'restore'})
|
||||||
self.assertEqual(self.state,self.original)
|
self.assertEqual(self.state,self.original)
|
||||||
def test_restore_keeps_external_changes(self):
|
def test_restore_keeps_external_changes(self):
|
||||||
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'put',self.fake_put):
|
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'get_many',lambda keys:{k:self.state[k] for k in keys}),patch.object(glass,'put',self.fake_put):
|
||||||
glass.dispatch({'action':'apply','options':self.options})
|
glass.dispatch({'action':'apply','options':self.options})
|
||||||
self.state['default_preset']='external'
|
self.state['default_preset']='external'
|
||||||
result=glass.dispatch({'action':'restore'})
|
result=glass.dispatch({'action':'restore'})
|
||||||
@@ -28,7 +28,7 @@ class GlassTests(unittest.TestCase):
|
|||||||
def fail(key,value):
|
def fail(key,value):
|
||||||
if key=='dark:brightness' and value!=self.original[key]:raise ValueError('fixture failure')
|
if key=='dark:brightness' and value!=self.original[key]:raise ValueError('fixture failure')
|
||||||
self.fake_put(key,value)
|
self.fake_put(key,value)
|
||||||
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'put',fail):
|
with tempfile.TemporaryDirectory() as temp,patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':'fixture'}),patch.object(glass,'get',lambda k:self.state[k]),patch.object(glass,'get_many',lambda keys:{k:self.state[k] for k in keys}),patch.object(glass,'put',fail):
|
||||||
with self.assertRaises(ValueError):glass.dispatch({'action':'apply','options':self.options})
|
with self.assertRaises(ValueError):glass.dispatch({'action':'apply','options':self.options})
|
||||||
self.assertEqual(self.state,self.original)
|
self.assertEqual(self.state,self.original)
|
||||||
def test_validation_before_writes(self):
|
def test_validation_before_writes(self):
|
||||||
|
|||||||
Reference in new issue
Block a user