1# Copyright 2021 The gRPC Authors 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# Follows convention set in objectivec_helpers.cc in the protobuf ObjC compiler. 15 16""" 17Contains generic helper utilities. 18""" 19 20_upper_segments_list = ["url", "http", "https"] 21 22def strip_extension(str): 23 return str.rpartition(".")[0] 24 25def capitalize(word): 26 if word in _upper_segments_list: 27 return word.upper() 28 else: 29 return word.capitalize() 30 31def lower_underscore_to_upper_camel(str): 32 """Converts from lower underscore case to upper camel case. 33 34 Args: 35 str: The snake case string to convert. 36 37 Returns: 38 The title case version of str. 39 """ 40 str = strip_extension(str) 41 camel_case_str = "" 42 word = "" 43 for c in str.elems(): # NB: assumes ASCII! 44 if c.isalpha(): 45 word += c.lower() 46 else: 47 # Last word is finished. 48 if len(word): 49 camel_case_str += capitalize(word) 50 word = "" 51 if c.isdigit(): 52 camel_case_str += c 53 54 # Otherwise, drop the character. See UnderscoresToCamelCase in: 55 # third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc 56 57 if len(word): 58 camel_case_str += capitalize(word) 59 return camel_case_str 60 61def file_to_upper_camel(src): 62 elements = src.rpartition("/") 63 upper_camel = lower_underscore_to_upper_camel(elements[-1]) 64 return "".join(list(elements[:-1]) + [upper_camel]) 65 66def file_with_extension(src, ext): 67 elements = src.rpartition("/") 68 return "".join(list(elements[:-1]) + [elements[-1], "." + ext]) 69 70def to_upper_camel_with_extension(src, ext): 71 src = file_to_upper_camel(src) 72 return file_with_extension(src, ext) 73