"Fossies" - the Fresh Open Source Software Archive

Member "swift-2.21.0/swift/account/utils.py" (25 Mar 2019, 3991 Bytes) of package /linux/misc/openstack/swift-2.21.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.19.1_vs_2.21.0.

    1 # Copyright (c) 2010-2013 OpenStack Foundation
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 import json
   17 
   18 import six
   19 
   20 from swift.common.middleware import listing_formats
   21 from swift.common.swob import HTTPOk, HTTPNoContent
   22 from swift.common.utils import Timestamp
   23 from swift.common.storage_policy import POLICIES
   24 
   25 
   26 class FakeAccountBroker(object):
   27     """
   28     Quacks like an account broker, but doesn't actually do anything. Responds
   29     like an account broker would for a real, empty account with no metadata.
   30     """
   31     def get_info(self):
   32         now = Timestamp.now().internal
   33         return {'container_count': 0,
   34                 'object_count': 0,
   35                 'bytes_used': 0,
   36                 'created_at': now,
   37                 'put_timestamp': now}
   38 
   39     def list_containers_iter(self, *_, **__):
   40         return []
   41 
   42     @property
   43     def metadata(self):
   44         return {}
   45 
   46     def get_policy_stats(self):
   47         return {}
   48 
   49 
   50 def get_response_headers(broker):
   51     info = broker.get_info()
   52     resp_headers = {
   53         'X-Account-Container-Count': info['container_count'],
   54         'X-Account-Object-Count': info['object_count'],
   55         'X-Account-Bytes-Used': info['bytes_used'],
   56         'X-Timestamp': Timestamp(info['created_at']).normal,
   57         'X-PUT-Timestamp': Timestamp(info['put_timestamp']).normal}
   58     policy_stats = broker.get_policy_stats()
   59     for policy_idx, stats in policy_stats.items():
   60         policy = POLICIES.get_by_index(policy_idx)
   61         if not policy:
   62             continue
   63         header_prefix = 'X-Account-Storage-Policy-%s-%%s' % policy.name
   64         for key, value in stats.items():
   65             header_name = header_prefix % key.replace('_', '-')
   66             resp_headers[header_name] = value
   67     resp_headers.update((key, value)
   68                         for key, (value, timestamp) in
   69                         broker.metadata.items() if value != '')
   70     return resp_headers
   71 
   72 
   73 def account_listing_response(account, req, response_content_type, broker=None,
   74                              limit='', marker='', end_marker='', prefix='',
   75                              delimiter='', reverse=False):
   76     if broker is None:
   77         broker = FakeAccountBroker()
   78 
   79     resp_headers = get_response_headers(broker)
   80 
   81     account_list = broker.list_containers_iter(limit, marker, end_marker,
   82                                                prefix, delimiter, reverse)
   83     data = []
   84     for (name, object_count, bytes_used, put_timestamp, is_subdir) \
   85             in account_list:
   86         name_ = name.decode('utf8') if six.PY2 else name
   87         if is_subdir:
   88             data.append({'subdir': name_})
   89         else:
   90             data.append(
   91                 {'name': name_, 'count': object_count, 'bytes': bytes_used,
   92                  'last_modified': Timestamp(put_timestamp).isoformat})
   93     if response_content_type.endswith('/xml'):
   94         account_list = listing_formats.account_to_xml(data, account)
   95         ret = HTTPOk(body=account_list, request=req, headers=resp_headers)
   96     elif response_content_type.endswith('/json'):
   97         account_list = json.dumps(data).encode('ascii')
   98         ret = HTTPOk(body=account_list, request=req, headers=resp_headers)
   99     elif data:
  100         account_list = listing_formats.listing_to_text(data)
  101         ret = HTTPOk(body=account_list, request=req, headers=resp_headers)
  102     else:
  103         ret = HTTPNoContent(request=req, headers=resp_headers)
  104     ret.content_type = response_content_type
  105     ret.charset = 'utf-8'
  106     return ret