• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 Google LLC
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#     https://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
15from typing import List, Tuple, Union, Optional
16
17
18def indent(code: Union[str, List[str]], depth: int) -> str:
19    """Indent a code block to the selected depth.
20
21    Accepts as parameter a list of lines or a code block. Handles
22    line breaks in the lines as well.
23    The first line is intentionally not indented so that
24    the caller may use it as:
25
26    '''
27    def generated():
28        {codeblock}
29    '''
30    """
31    code = [code] if isinstance(code, str) else code
32    lines = [line for block in code for line in block.split('\n')]
33    sep = '\n' + (' ' * (depth * 4))
34    return sep.join(lines)
35
36
37def to_pascal_case(text: str) -> str:
38    """Convert UPPER_SNAKE_CASE strings to PascalCase."""
39    return text.replace('_', ' ').title().replace(' ', '')
40