~james-page/bzr/unittest2-assertWarns

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Patch Queue Manager
  • Date: 2013-10-07 17:04:34 UTC
  • mfrom: (6588.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20131007170434-mb0ahksmrzsnhi1i
(vila) Stricter checks on configuration option names (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2552
2552
        return "".join(ret)
2553
2553
 
2554
2554
 
 
2555
_option_ref_re = lazy_regex.lazy_compile('({[^\d\W](?:\.\w|\w)*})')
 
2556
"""Describes an expandable option reference.
 
2557
 
 
2558
We want to match the most embedded reference first.
 
2559
 
 
2560
I.e. for '{{foo}}' we will get '{foo}',
 
2561
for '{bar{baz}}' we will get '{baz}'
 
2562
"""
 
2563
 
 
2564
def iter_option_refs(string):
 
2565
    # Split isolate refs so every other chunk is a ref
 
2566
    is_ref = False
 
2567
    for chunk  in _option_ref_re.split(string):
 
2568
        yield is_ref, chunk
 
2569
        is_ref = not is_ref
 
2570
 
 
2571
 
2555
2572
class OptionRegistry(registry.Registry):
2556
2573
    """Register config options by their name.
2557
2574
 
2559
2576
    some information from the option object itself.
2560
2577
    """
2561
2578
 
 
2579
    def _check_option_name(self, option_name):
 
2580
        """Ensures an option name is valid.
 
2581
 
 
2582
        :param option_name: The name to validate.
 
2583
        """
 
2584
        if _option_ref_re.match('{%s}' % option_name) is None:
 
2585
            raise errors.IllegalOptionName(option_name)
 
2586
 
2562
2587
    def register(self, option):
2563
2588
        """Register a new option to its name.
2564
2589
 
2565
2590
        :param option: The option to register. Its name is used as the key.
2566
2591
        """
 
2592
        self._check_option_name(option.name)
2567
2593
        super(OptionRegistry, self).register(option.name, option,
2568
2594
                                             help=option.help)
2569
2595
 
2578
2604
        :param member_name: the member of the module to return.  If empty or 
2579
2605
                None, get() will return the module itself.
2580
2606
        """
 
2607
        self._check_option_name(key)
2581
2608
        super(OptionRegistry, self).register_lazy(key,
2582
2609
                                                  module_name, member_name)
2583
2610
 
3622
3649
            yield self.store, section
3623
3650
 
3624
3651
 
3625
 
_option_ref_re = lazy_regex.lazy_compile('({[^{}\n]+})')
3626
 
"""Describes an expandable option reference.
3627
 
 
3628
 
We want to match the most embedded reference first.
3629
 
 
3630
 
I.e. for '{{foo}}' we will get '{foo}',
3631
 
for '{bar{baz}}' we will get '{baz}'
3632
 
"""
3633
 
 
3634
 
def iter_option_refs(string):
3635
 
    # Split isolate refs so every other chunk is a ref
3636
 
    is_ref = False
3637
 
    for chunk  in _option_ref_re.split(string):
3638
 
        yield is_ref, chunk
3639
 
        is_ref = not is_ref
3640
 
 
3641
3652
# FIXME: _shared_stores should be an attribute of a library state once a
3642
3653
# library_state object is always available.
3643
3654
_shared_stores = {}