1# Copyright 2017 - The Android Open Source Project 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 implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Implement dump methods and utils to dump info from a mounter.""" 16 17from gsi_util.dumpers.dump_info_list import DUMP_LIST 18 19 20class Dumper(object): 21 22 def __init__(self, file_accessor): 23 self._file_accessor = file_accessor 24 25 @staticmethod 26 def _dump_by_dumper(dumper_instance, dump_list): 27 """Dump info by the given dumper instance according the given dump_list. 28 29 Used for dump(), see the comment of dump() for type details. 30 31 Args: 32 dumper_instance: a dumper instance to process dump. 33 dump_list: a list of dump info to be dump. The items in the list must 34 relative to dumper_instance. 35 36 Returns: 37 The dump result by dictionary maps info_name to the value of dump result. 38 """ 39 dump_result = {} 40 41 for dump_info in dump_list: 42 value = dumper_instance.dump(dump_info.lookup_key) 43 dump_result[dump_info.info_name] = value 44 45 return dump_result 46 47 def dump(self, dump_list): 48 """Dump info according the given dump_list. 49 50 Args: 51 dump_list: a list of dump info to be dump. See dump_info_list.py for 52 the detail types. 53 Returns: 54 The dump result by dictionary maps info_name to the value of dump result. 55 """ 56 dump_result = {} 57 58 # query how many different dumpers to dump 59 dumper_set = set([x.dumper_create_args for x in dump_list]) 60 for dumper_create_args in dumper_set: 61 # The type of a dumper_create_args is (Class, instantiation args...) 62 dumper_class = dumper_create_args[0] 63 dumper_args = dumper_create_args[1:] 64 # Create the dumper 65 with dumper_class(self._file_accessor, dumper_args) as dumper_instance: 66 dump_list_for_the_dumper = ( 67 x for x in dump_list if x.dumper_create_args == dumper_create_args) 68 dumper_result = self._dump_by_dumper(dumper_instance, 69 dump_list_for_the_dumper) 70 dump_result.update(dumper_result) 71 72 return dump_result 73 74 @staticmethod 75 def make_dump_list_by_name_list(name_list): 76 info_list = [] 77 for info_name in name_list: 78 info = next((x for x in DUMP_LIST if x.info_name == info_name), None) 79 if not info: 80 raise RuntimeError('Unknown info name: "{}"'.format(info_name)) 81 info_list.append(info) 82 return info_list 83 84 @staticmethod 85 def get_all_dump_list(): 86 return DUMP_LIST 87