[tor-commits] [stem/master] Adding a @uses_settings to our config module
atagar at torproject.org
atagar at torproject.org
Tue May 6 01:21:13 UTC 2014
commit fb65766f1e74cf3218efa9889a5479a13ba70ca4
Author: Damian Johnson <atagar at torproject.org>
Date: Fri Apr 18 10:29:22 2014 -0700
Adding a @uses_settings to our config module
The @uses_settings helper our interpretor panel uses is actually a nice way of
taking advantage of our config module. Moving the helper to our util so others
can use it.
---
stem/interpretor/__init__.py | 48 +++++++-----------------------------------
stem/util/conf.py | 44 ++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 40 deletions(-)
diff --git a/stem/interpretor/__init__.py b/stem/interpretor/__init__.py
index f1da5d9..93e4e6a 100644
--- a/stem/interpretor/__init__.py
+++ b/stem/interpretor/__init__.py
@@ -26,6 +26,14 @@ from stem.util.term import RESET, Attr, Color, format
PROMPT = format(">>> ", Color.GREEN, Attr.BOLD) + RESET * 10
+settings_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
+uses_settings = stem.util.conf.uses_settings('stem_interpretor', settings_path)
+
+
+ at uses_settings
+def msg(config, message, **attr):
+ return config.get(message).format(**attr)
+
def main():
import readline
@@ -70,43 +78,3 @@ def main():
except (KeyboardInterrupt, EOFError, stem.SocketClosed) as exc:
print # move cursor to the following line
break
-
-
-def uses_settings(func):
- """
- Loads our interpretor's internal settings. This should be treated as a fatal
- failure if unsuccessful.
-
- :raises: **IOError** if we're unable to read or parse our internal
- configurations
- """
-
- config = stem.util.conf.get_config('stem_interpretor')
-
- if not config.get('settings_loaded', False):
- settings_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
- config.load(settings_path)
- config.set('settings_loaded', 'true')
-
- return func
-
-
- at uses_settings
-def msg(message, **attr):
- """
- Provides the given message.
-
- :param str message: message handle
- :param dict attr: values to insert into the message
-
- :returns: **str** that was requested
-
- :raises: **ValueError** if string key doesn't exist
- """
-
- config = stem.util.conf.get_config('stem_interpretor')
-
- try:
- return config.get(message).format(**attr)
- except:
- raise ValueError('We attempted to use an undefined string resource (%s)' % message)
diff --git a/stem/util/conf.py b/stem/util/conf.py
index c11adf2..3f753f0 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -141,6 +141,7 @@ Here's an expanation of what happened...
config_dict - provides a dictionary that's kept in sync with our config
get_config - singleton for getting configurations
+ uses_settings - provides an annotation for functions that use configurations
parse_enum_csv - helper funcion for parsing confguration entries for enums
Config - Custom configuration
@@ -156,6 +157,7 @@ Here's an expanation of what happened...
+- get_value - provides the value for a given key as a string
"""
+import functools
import threading
from stem.util import log
@@ -233,6 +235,48 @@ def get_config(handle):
return CONFS[handle]
+def uses_settings(handle, path, lazy_load = True):
+ """
+ Provides a function that can be used as an annotation for other functions
+ that require settings to be loaded. Functions with this annotation will be
+ provided with the configuration as its first argument.
+
+ ::
+
+ uses_settings = stem.util.conf.uses_settings('my_app', '/path/to/settings.cfg')
+
+ @uses_settings
+ def my_function(config):
+ print 'hello %s!' % config.get('username', '')
+
+ :param str handle: hande for the configuration
+ :param str path: path where the configuration should be loaded from
+ :param bool lazy_load: loads the configuration file when the annotation is
+ used if true, otherwise it's loaded right away
+
+ :returns: **function** that can be used as an annotation to provide the
+ configuration
+
+ :raises: **IOError** if we fail to read the configuration file, if
+ **lazy_load** is true then this arises when we use the annotation
+ """
+
+ config = get_config(handle)
+
+ if not lazy_load and not config.get('settings_loaded', False):
+ config.load(path)
+ config.set('settings_loaded', 'true')
+
+ def annotation(func):
+ if lazy_load and not config.get('settings_loaded', False):
+ config.load(path)
+ config.set('settings_loaded', 'true')
+
+ return functools.partial(func, config)
+
+ return annotation
+
+
def parse_enum(key, value, enumeration):
"""
Provides the enumeration value for a given key. This is a case insensitive
More information about the tor-commits
mailing list