Permalink
Browse files

adding option to pass exclude_from_print_conf and exclude_from_dump_c…

…onf to add_option(), r=twobraids
  • Loading branch information...
1 parent 51d9b11 commit e9867184f714fdbe57dfdbdeef0ac7ce164c5d81 @peterbe peterbe committed Mar 13, 2012
@@ -348,7 +348,11 @@ def print_conf(self):
@contextlib.contextmanager
def stdout_opener():
yield sys.stdout
- self.write_conf(config_file_type, stdout_opener)
+
+ skip_keys = [k for (k, v)
+ in self.option_definitions.iteritems()
+ if isinstance(v, Option) and v.exclude_from_print_conf]
+ self.write_conf(config_file_type, stdout_opener, skip_keys=skip_keys)
#--------------------------------------------------------------------------
def dump_conf(self, config_pathname=None):
@@ -365,10 +369,15 @@ def dump_conf(self, config_pathname=None):
opener = functools.partial(open, config_pathname, 'w')
config_file_type = os.path.splitext(config_pathname)[1][1:]
- self.write_conf(config_file_type, opener)
+
+ skip_keys = [k for (k, v)
+ in self.option_definitions.iteritems()
+ if isinstance(v, Option) and v.exclude_from_dump_conf]
+
+ self.write_conf(config_file_type, opener, skip_keys=skip_keys)
#--------------------------------------------------------------------------
- def write_conf(self, config_file_type, opener=open):
+ def write_conf(self, config_file_type, opener, skip_keys=None):
"""write a configuration file to a file-like object.
parameters:
@@ -378,8 +387,12 @@ def write_conf(self, config_file_type, opener=open):
opener - a callable object or function that returns a file like
object that works as a context in a with statement."""
+ blocked_keys = self.admin_controls_list
+ if skip_keys:
+ blocked_keys.extend(skip_keys)
+
option_iterator = functools.partial(self._walk_config,
- blocked_keys=self.admin_controls_list)
+ blocked_keys=blocked_keys)
with opener() as config_fp:
value_sources.write(config_file_type,
option_iterator,
@@ -621,7 +634,7 @@ def _block_password(qkey, key, value, block_password=True):
return qkey, key, value
#--------------------------------------------------------------------------
- def _walk_config(self, source=None, prefix='', blocked_keys=[],
+ def _walk_config(self, source=None, prefix='', blocked_keys=(),
block_password=False):
if source == None:
source = self.option_definitions
View
@@ -67,16 +67,8 @@ def __setattr__(self, name, value):
self.__setitem__(name, o)
#--------------------------------------------------------------------------
- def add_option(self, name,
- default=None,
- doc=None,
- from_string_converter=None,
- short_form=None):
- an_option = Option(name,
- doc=doc,
- default=default,
- from_string_converter=from_string_converter,
- short_form=short_form)
+ def add_option(self, name, *args, **kwargs):
+ an_option = Option(name, *args, **kwargs)
self[name] = an_option
#--------------------------------------------------------------------------
View
@@ -52,8 +52,9 @@ def __init__(self,
from_string_converter=None,
value=None,
short_form=None,
- *args,
- **kwargs):
+ exclude_from_print_conf=False,
+ exclude_from_dump_conf=False,
+ ):
self.name = name
self.short_form = short_form
self.default = default
@@ -72,6 +73,8 @@ def __init__(self,
and self.from_string_converter):
# need to convert the default too
self.default = self.from_string_converter(self.default)
+ self.exclude_from_print_conf = exclude_from_print_conf
+ self.exclude_from_dump_conf = exclude_from_dump_conf
def __eq__(self, other):
if isinstance(other, Option):
@@ -44,9 +44,6 @@
import io
from cStringIO import StringIO
import getopt
-
-import configman.config_file_future_proxy as config_proxy
-
import configman.config_manager as config_manager
from configman.dotdict import DotDict
import configman.datetime_util as dtu
@@ -1042,21 +1039,22 @@ def print_conf(self):
finally:
sys.stdout = temp_stdout
- def write_conf(inner_self, file_type, opener):
+ def write_conf(inner_self, file_type, opener, skip_keys=None):
self.assertEqual(file_type, 'ini')
with opener() as f:
self.assertEqual(f, 17)
- c = MyConfigManager(n,
- [getopt],
- use_admin_controls=True,
- use_auto_help=False,
- quit_after_admin=False,
- argv_source=['--admin.print_conf=ini',
- 'argument 1',
- 'argument 2',
- 'argument 3'],
- config_pathname='fred')
+ MyConfigManager(
+ n,
+ [getopt],
+ use_admin_controls=True,
+ use_auto_help=False,
+ quit_after_admin=False,
+ argv_source=['--admin.print_conf=ini',
+ 'argument 1',
+ 'argument 2',
+ 'argument 3'],
+ config_pathname='fred')
def test_dump_conf(self):
n = config_manager.Namespace()
@@ -1066,23 +1064,86 @@ def __init__(inner_self, *args, **kwargs):
inner_self.write_called = False
super(MyConfigManager, inner_self).__init__(*args, **kwargs)
- def write_conf(inner_self, file_type, opener):
+ def write_conf(inner_self, file_type, opener, skip_keys=None):
self.assertEqual(file_type, 'ini')
self.assertEqual(opener.args, ('fred.ini', 'w'))
- c = MyConfigManager(n,
- [getopt],
- use_admin_controls=True,
- use_auto_help=False,
- quit_after_admin=False,
- argv_source=['--admin.dump_conf=fred.ini',
- 'argument 1',
- 'argument 2',
- 'argument 3'],
- config_pathname='fred')
+ MyConfigManager(
+ n,
+ [getopt],
+ use_admin_controls=True,
+ use_auto_help=False,
+ quit_after_admin=False,
+ argv_source=['--admin.dump_conf=fred.ini',
+ 'argument 1',
+ 'argument 2',
+ 'argument 3'],
+ config_pathname='fred'
+ )
- def test_config_pathname_set(self):
+ def test_print_conf_some_options_excluded(self):
n = config_manager.Namespace()
+ n.add_option('gender',
+ default='Male',
+ doc='What kind of genitalia?')
+ n.add_option('salary',
+ default=10000,
+ doc='How much do you earn?',
+ exclude_from_print_conf=True
+ )
+
+ old_stdout = sys.stdout
+ temp_output = StringIO()
+ sys.stdout = temp_output
+ try:
+ config_manager.ConfigurationManager(
+ n,
+ [getopt],
+ use_admin_controls=True,
+ use_auto_help=False,
+ quit_after_admin=False,
+ argv_source=['--admin.print_conf=ini'],
+ config_pathname='fred'
+ )
+ finally:
+ sys.stdout = old_stdout
+
+ printed = temp_output.getvalue()
+ self.assertTrue('name: gender' in printed)
+ self.assertTrue('name: salary' not in printed)
+
+ def test_dump_conf_some_options_excluded(self):
+ n = config_manager.Namespace()
+ n.add_option('gender',
+ default='Male',
+ doc='What kind of genitalia?',
+ exclude_from_print_conf=True)
+ n.add_option('salary',
+ default=10000,
+ doc='How much do you earn?',
+ exclude_from_dump_conf=True
+ )
+
+ try:
+ config_manager.ConfigurationManager(
+ n,
+ [getopt],
+ use_admin_controls=True,
+ use_auto_help=False,
+ quit_after_admin=False,
+ argv_source=['--admin.dump_conf=foo.ini'],
+ config_pathname='fred'
+ )
+
+ printed = open('foo.ini').read()
+ self.assertTrue('name: gender' in printed)
+ self.assertTrue('name: salary' not in printed)
+
+ finally:
+ if os.path.isfile('foo.ini'):
+ os.remove('foo.ini')
+
+ def test_config_pathname_set(self):
class MyConfigManager(config_manager.ConfigurationManager):
def __init__(inner_self, *args, **kwargs):
@@ -1168,9 +1229,12 @@ def __init__(inner_self, config):
'but divorced because of arg2.')
def test_context(self):
+
class AggregatedValue(object):
+
def __init__(self, value):
self.value = value
+
def close(self):
self.value = None
@@ -1227,8 +1291,10 @@ def test_failing_aggregate_error_bubbling(self):
https://github.com/mozilla/configman/issues/21
"""
class AggregatedValue(object):
+
def __init__(self, value):
self.value = value
+
def close(self):
self.value = None
@@ -1246,7 +1312,6 @@ class MyApp(config_manager.RequiredConfig):
required_config = config_manager.Namespace()
required_config.add_aggregation('statement', aggregation_test)
-
n = config_manager.Namespace()
n.admin = config_manager.Namespace()
n.admin.add_option('application',
@@ -113,7 +113,6 @@ def test_option_constructor_basics(self):
'default': '1',
'doc': "lucy's integer",
'from_string_converter': int,
- 'other': 'no way'
}
o = Option(**data)
self.assertEqual(o.name, 'lucy')
@@ -126,7 +125,6 @@ def test_option_constructor_basics(self):
'default': '1',
'doc': "lucy's integer",
'from_string_converter': int,
- 'other': 'no way'
}
o = Option('now', **data)
self.assertEqual(o.name, 'now')
@@ -148,7 +146,6 @@ def test_option_constructor_basics(self):
'default': '1.0',
'doc': "lucy's height",
'from_string_converter': float,
- 'other': ''
}
o = Option('now', **data)
self.assertEqual(o.name, 'now')
@@ -162,7 +159,6 @@ def test_option_constructor_more_complex_default_converters(self):
'default': '2011-12-31',
'doc': "lucy's bday",
'from_string_converter': dtu.date_from_ISO_string,
- 'other': ''
}
o = Option('now', **data)
self.assertEqual(o.name, 'now')
@@ -176,7 +172,6 @@ def test_option_constructor_more_complex_default_converters(self):
'doc': "lucy's bday",
'from_string_converter': \
'configman.datetime_util.date_from_ISO_string',
- 'other': ''
}
o = Option('now', **data)
self.assertEqual(o.name, 'now')
@@ -96,7 +96,8 @@ def value_iter():
"configman.datetime_util.datetime_from_ISO_string",
"name": "aaa"
}
- self.assertEqual(jrec['aaa'], expect_to_find)
+ for key, value in expect_to_find.items():
+ self.assertEqual(jrec['aaa'][key], value)
def test_json_round_trip(self):
n = config_manager.Namespace(doc='top')

0 comments on commit e986718

Please sign in to comment.