1# 2# Copyright (C) 2016 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import itertools 18 19 20def ExpandItemDelimiters(input_list, 21 delimiter, 22 strip=False, 23 to_str=False, 24 remove_empty=True): 25 '''Expand list items that contain the given delimiter. 26 27 Args: 28 input_list: list of string, a list whose item may contain a delimiter 29 delimiter: string 30 strip: bool, whether to strip items after expanding. Default is False 31 to_str: bool, whether to convert output items in string. 32 Default is False 33 remove_empty: bool, whether to remove empty string in result list. 34 Will not remove None items. Default: True 35 36 Returns: 37 The expended list, which may be the same with input list 38 if no delimiter found; None if input list is None 39 ''' 40 if input_list is None: 41 return None 42 43 do_strip = lambda s: s.strip() if strip else s 44 do_str = lambda s: str(s) if to_str else s 45 46 expended_list_generator = (item.split(delimiter) for item in input_list) 47 result = [do_strip(do_str(s)) 48 for s in itertools.chain.from_iterable(expended_list_generator)] 49 return filter(lambda s: str(s) != '', result) if remove_empty else result 50