lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
"""This module contains Google Storage utilities.
 
TODO(ayatane): This should be merged into chromite.lib.gs
"""
 
import re
 
# See https://cloud.google.com/storage/docs/bucket-naming#objectnames
# Banned characters: []*?#
# and control characters (hex): 00-1f, 7f-84, 86-ff
_INVALID_GS_PATTERN = r'[[\]*?#\x00-\x1f\x7f-\x84\x86-\xff]'
 
 
def escape(name):
    """Escape GS object name.
 
    @param name: Name string.
    @return: Escaped name string.
    """
    return re.sub(_INVALID_GS_PATTERN,
                  lambda x: _percent_escape(x.group(0)), name)
 
 
def _percent_escape(char):
    """Percent escape a character.
 
    @param char: character to escape.
    @return: Escaped string.
    """
    return '%{:02x}'.format(ord(char))