From eca27321d3d06992093205e1db86417b38b3c4d5 Mon Sep 17 00:00:00 2001 From: MORITA Kazutaka Date: Thu, 31 Jul 2014 22:52:18 +0900 Subject: [PATCH] cfg: allow attribute access It is tedious to bracket and quote config names. This patch allows us to access config values with attributes. Change-Id: Ia5cec9a3438c808460da6a56830464ac50cfffb9 --- swift3/cfg.py | 12 ++++++++++++ swift3/controllers/bucket.py | 6 +++--- swift3/controllers/location.py | 4 ++-- swift3/request.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/swift3/cfg.py b/swift3/cfg.py index 797f4af6..09fa8a95 100644 --- a/swift3/cfg.py +++ b/swift3/cfg.py @@ -21,6 +21,18 @@ class Config(dict): if base is not None: self.update(base) + def __getattr__(self, name): + if name not in self: + raise AttributeError("No attribute '%s'" % name) + + return self[name] + + def __setattr__(self, name, value): + self[name] = value + + def __delattr__(self, name): + del self[name] + def update(self, other): if hasattr(other, 'keys'): for key in other.keys(): diff --git a/swift3/controllers/bucket.py b/swift3/controllers/bucket.py index 319ff84a..53e736d7 100644 --- a/swift3/controllers/bucket.py +++ b/swift3/controllers/bucket.py @@ -50,8 +50,8 @@ class BucketController(Controller): if req.params.get('max-keys').isdigit() is False: raise InvalidArgument('max-keys', req.params['max-keys']) - max_keys = int(req.params.get('max-keys', CONF['max_bucket_listing'])) - max_keys = min(max_keys, CONF['max_bucket_listing']) + max_keys = int(req.params.get('max-keys', CONF.max_bucket_listing)) + max_keys = min(max_keys, CONF.max_bucket_listing) query = { 'format': 'json', @@ -132,7 +132,7 @@ class BucketController(Controller): LOGGER.debug(e) raise MalformedXML() - if location != CONF.get('location'): + if location != CONF.location: # Swift3 cannot support multiple reagions now. raise InvalidLocationConstraint() diff --git a/swift3/controllers/location.py b/swift3/controllers/location.py index eb7b21af..6c26c5d3 100644 --- a/swift3/controllers/location.py +++ b/swift3/controllers/location.py @@ -32,8 +32,8 @@ class LocationController(Controller): req.get_response(self.app, method='HEAD') elem = Element('LocationConstraint') - if CONF['location'] != 'US': - elem.text = CONF['location'] + if CONF.location != 'US': + elem.text = CONF.location body = tostring(elem) return HTTPOk(body=body, content_type='application/xml') diff --git a/swift3/request.py b/swift3/request.py index dd53ec17..505c7df7 100644 --- a/swift3/request.py +++ b/swift3/request.py @@ -74,7 +74,7 @@ class Request(swob.Request): self.environ['swift.leave_relative_location'] = True def _parse_host(self): - storage_domain = CONF['storage_domain'] + storage_domain = CONF.storage_domain if not storage_domain: return None