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"""Provides class XmlDumper.""" 16 17import logging 18import xml.etree.ElementTree as ET 19 20 21class XmlDumper(object): 22 23 def __init__(self, file_accessor, args): 24 filename_in_mount = args[0] 25 logging.debug('Parse %s...', filename_in_mount) 26 with file_accessor.prepare_file(filename_in_mount) as filename: 27 if filename: 28 self._tree = ET.parse(filename) 29 30 def __enter__(self): 31 # do nothing 32 return self 33 34 def __exit__(self, exc_type, exc_val, exc_tb): 35 if hasattr(self, '_tree'): 36 del self._tree 37 38 def dump(self, lookup_key): 39 if not hasattr(self, '_tree'): 40 return None 41 42 xpath = lookup_key 43 results = self._tree.findall(xpath) 44 return ', '.join([e.text for e in results]) if results else None 45