3383
3384
# on the relevant parts of the API that needs testing -- vila 20110503 (based
3384
3385
# on a poolie's remark)
3385
3386
class GlobalStore(LockableIniFileStore):
3387
"""A config store for global options.
3389
There is a single GlobalStore for a given process.
3387
3392
def __init__(self, possible_transports=None):
3388
3393
t = transport.get_transport_from_path(
3394
3399
class LocationStore(LockableIniFileStore):
3400
"""A config store for global options.
3402
There is a single GlobalStore for a given process.
3396
3405
def __init__(self, possible_transports=None):
3397
3406
t = transport.get_transport_from_path(
3403
3412
class BranchStore(TransportIniFileStore):
3413
"""A config store for branch options.
3415
There is a single BranchStore for a given branch.
3405
3418
def __init__(self, branch):
3406
3419
super(BranchStore, self).__init__(branch.control_transport,
3623
3636
yield is_ref, chunk
3624
3637
is_ref = not is_ref
3639
# FIXME: _shared_stores should be an attribute of a library state once a
3640
# library_state object is always available.
3642
_shared_stores_at_exit_installed = False
3627
3644
class Stack(object):
3628
3645
"""A stack of configurations where an option can be defined"""
3817
3834
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
3819
3836
def _get_overrides(self):
3820
# Hack around library_state.initialize never called
3837
# FIXME: Hack around library_state.initialize never called
3821
3838
if bzrlib.global_state is not None:
3822
3839
return bzrlib.global_state.cmdline_overrides.get_sections()
3842
def get_shared_store(self, store, state=None):
3843
"""Get a known shared store.
3845
Store urls uniquely identify them and are used to ensure a single copy
3846
is shared across all users.
3848
:param store: The store known to the caller.
3850
:param state: The library state where the known stores are kept.
3852
:returns: The store received if it's not a known one, an already known
3856
state = bzrlib.global_state
3858
global _shared_stores_at_exit_installed
3859
stores = _shared_stores
3860
def save_config_changes():
3861
for k, store in stores.iteritems():
3862
store.save_changes()
3863
if not _shared_stores_at_exit_installed:
3864
# FIXME: Ugly hack waiting for library_state to always be
3865
# available. -- vila 20120731
3867
atexit.register(save_config_changes)
3868
_shared_stores_at_exit_installed = True
3870
stores = state.config_stores
3871
url = store.external_url()
3826
3879
class MemoryStack(Stack):
3827
3880
"""A configuration stack defined from a string.
3893
3946
def __init__(self):
3894
gstore = GlobalStore()
3947
gstore = self.get_shared_store(GlobalStore())
3895
3948
super(GlobalStack, self).__init__(
3896
3949
[self._get_overrides,
3897
3950
NameMatcher(gstore, 'DEFAULT').get_sections],
3898
3951
gstore, mutable_section_id='DEFAULT')
3901
class LocationStack(_CompatibleStack):
3954
class LocationStack(Stack):
3902
3955
"""Per-location options falling back to global options stack.
3920
3973
"""Make a new stack for a location and global configuration.
3922
3975
:param location: A URL prefix to """
3923
lstore = LocationStore()
3976
lstore = self.get_shared_store(LocationStore())
3924
3977
if location.startswith('file://'):
3925
3978
location = urlutils.local_path_from_url(location)
3926
gstore = GlobalStore()
3979
gstore = self.get_shared_store(GlobalStore())
3927
3980
super(LocationStack, self).__init__(
3928
3981
[self._get_overrides,
3929
3982
LocationMatcher(lstore, location).get_sections,
3953
4006
def __init__(self, branch):
3954
lstore = LocationStore()
4007
lstore = self.get_shared_store(LocationStore())
3955
4008
bstore = branch._get_config_store()
3956
gstore = GlobalStore()
4009
gstore = self.get_shared_store(GlobalStore())
3957
4010
super(BranchStack, self).__init__(
3958
4011
[self._get_overrides,
3959
4012
LocationMatcher(lstore, branch.base).get_sections,
3981
4034
# unlock saves all the changes.
3984
class RemoteControlStack(_CompatibleStack):
4037
class RemoteControlStack(Stack):
3985
4038
"""Remote control-only options stack."""
3987
4040
# FIXME 2011-11-22 JRV This should probably be renamed to avoid confusion