Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created January 17, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondreas/9e9c02037af5589876872bfe916dceba to your computer and use it in GitHub Desktop.
Save rondreas/9e9c02037af5589876872bfe916dceba to your computer and use it in GitHub Desktop.
Context manager for setting temporary pref.value(s) during a context.
import lx
class PreferenceValue(object):
""" Context manager for setting temporary pref.value(s) during a context.
:param preferences: preferences to use for the context.
:type preferences: dict
Example::
>>> pref = {'application.defaultScene': 'foo'}
>>> with PreferenceValue(pref): # temporarily set the default scene to 'foo'
... for k,v in p.iteritems():
... print lx.eval('pref.value name:{} ?'.format(k))
"foo"
"""
def __init__(self, preferences):
self._context = preferences
self._preferences = {}
for key, value in preferences.iteritems():
# Store previously saved settings,
self._preferences[key] = lx.eval1('pref.value name:{} ?'.format(key))
# Set the preference for this context,
lx.command('pref.value', name=key, value=value)
def __enter__(self):
return self._context
def __exit__(self, type, value, traceback):
# restore the previous settings,
for key, value in self._preferences.iteritems():
lx.command('pref.value', name=key, value=value)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment