Created
January 17, 2020 09:59
-
-
Save rondreas/9e9c02037af5589876872bfe916dceba to your computer and use it in GitHub Desktop.
Context manager for setting temporary pref.value(s) during a context.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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