Skip to content

Commit b2bd2a8

Browse files
Refactoring: Moved hard coded environment variables' names into constants. (#25365)
* Refactoring: Moved hard coded environment names into constants * Add apache license header to playground/infrastructure/constants.py * Fix imports in playground/infrastructure/ * [Playground]: refactor error messages * Update playground/infrastructure/constants.py Co-authored-by: Timur Sultanov <tssultanov@outlook.com> --------- Co-authored-by: Timur Sultanov <tssultanov@outlook.com>
1 parent ca07876 commit b2bd2a8

6 files changed

Lines changed: 37 additions & 11 deletions

File tree

playground/infrastructure/checker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from api.v1.api_pb2 import Sdk
3232
from config import Config
3333
from helper import get_tag, load_supported_categories
34+
from constants import BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY, BEAM_ROOT_DIR_ENV_VAR_KEY
3435

3536

3637
def parse_args() -> argparse.Namespace:
@@ -90,12 +91,12 @@ def check_sdk_examples(paths: List[PurePath], sdk: Sdk, root_dir: str) -> bool:
9091
def main():
9192
args = parse_args()
9293

93-
root_dir = os.getenv("BEAM_ROOT_DIR")
94+
root_dir = os.getenv(BEAM_ROOT_DIR_ENV_VAR_KEY)
9495
if root_dir is None:
95-
raise KeyError("BEAM_ROOT_DIR environment variable should be specified in os")
96-
categories_file = os.getenv("BEAM_EXAMPLE_CATEGORIES")
96+
raise KeyError(f"{BEAM_ROOT_DIR_ENV_VAR_KEY} environment variable should be specified in os")
97+
categories_file = os.getenv(BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY)
9798
if categories_file is None:
98-
raise KeyError("BEAM_EXAMPLE_CATEGORIES environment variable should be specified in os")
99+
raise KeyError(f"{BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY} environment variable should be specified in os")
99100

100101
load_supported_categories(categories_file)
101102

playground/infrastructure/ci_cd.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
from typing import List
2424

25+
from constants import BEAM_ROOT_DIR_ENV_VAR_KEY, BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY
2526
from models import SdkEnum, Example, StringToSdkEnum
2627
from config import Config, Origin
2728
from datastore_client import DatastoreClient
@@ -77,8 +78,8 @@
7778
help="limit sub directories to walk through, relative to BEAM_ROOT_DIR",
7879
)
7980

80-
root_dir = os.getenv("BEAM_ROOT_DIR")
81-
categories_file = os.getenv("BEAM_EXAMPLE_CATEGORIES")
81+
root_dir = os.getenv(BEAM_ROOT_DIR_ENV_VAR_KEY)
82+
categories_file = os.getenv(BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY)
8283

8384

8485
def _check_envs():

playground/infrastructure/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
STATUS_PREPARATION_ERROR, STATUS_COMPILE_ERROR, \
2727
STATUS_RUN_TIMEOUT, STATUS_RUN_ERROR, SDK_JAVA, SDK_GO, SDK_PYTHON, \
2828
SDK_SCIO, Sdk
29+
from constants import SERVER_ADDRESS_ENV_VAR_KEY, SDK_CONFIG_ENV_VAR_KEY
2930

3031

3132
@dataclass(frozen=True)
3233
class Config:
3334
"""
3435
General configuration for CI/CD steps
3536
"""
36-
SERVER_ADDRESS = os.getenv("SERVER_ADDRESS", "localhost:8080")
37+
SERVER_ADDRESS = os.getenv(SERVER_ADDRESS_ENV_VAR_KEY, "localhost:8080")
3738
EXTENSION_TO_SDK = {
3839
"java": SDK_JAVA, "go": SDK_GO, "py": SDK_PYTHON, "scala": SDK_SCIO
3940
}
@@ -60,7 +61,7 @@ class Config:
6061
CD_STEP_NAME = "CD"
6162
CI_CD_LITERAL = Literal["CI", "CD"]
6263
URL_VCS_PREFIX = "https://github.com/apache/beam/blob/master"
63-
SDK_CONFIG = os.getenv("SDK_CONFIG", "../../playground/sdks.yaml")
64+
SDK_CONFIG = os.getenv(SDK_CONFIG_ENV_VAR_KEY, "../../playground/sdks.yaml")
6465
DEFAULT_NAMESPACE = "Playground"
6566

6667

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. 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+
BEAM_ROOT_DIR_ENV_VAR_KEY = "BEAM_ROOT_DIR"
17+
BEAM_EXAMPLE_CATEGORIES_ENV_VAR_KEY = "BEAM_EXAMPLE_CATEGORIES"
18+
SERVER_ADDRESS_ENV_VAR_KEY = "SERVER_ADDRESS"
19+
SDK_CONFIG_ENV_VAR_KEY = "SDK_CONFIG"
20+
BEAM_USE_WEBGRPC_ENV_VAR_KEY = "BEAM_USE_WEBGRPC"
21+
GRPC_TIMEOUT_ENV_VAR_KEY = "GRPC_TIMEOUT"

playground/infrastructure/grpc_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626

2727
from api.v1 import api_pb2_grpc, api_pb2
2828
from config import Config
29+
from constants import BEAM_USE_WEBGRPC_ENV_VAR_KEY, GRPC_TIMEOUT_ENV_VAR_KEY
2930
from models import SdkEnum
3031

3132

3233
class GRPCClient:
3334
"""GRPCClient is gRPC client for sending a request to the backend."""
3435

3536
def __init__(self, wait_for_ready=True):
36-
use_webgrpc = os.getenv("BEAM_USE_WEBGRPC", False)
37-
timeout = int(os.getenv("GRPC_TIMEOUT", 30))
37+
use_webgrpc = os.getenv(BEAM_USE_WEBGRPC_ENV_VAR_KEY, False)
38+
timeout = int(os.getenv(GRPC_TIMEOUT_ENV_VAR_KEY, 30))
3839
logging.info("grpc timeout: %d", timeout)
3940
if use_webgrpc:
4041
self._channel = sonora.aio.insecure_web_channel(Config.SERVER_ADDRESS)

playground/infrastructure/helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
)
4545
from config import Config, TagFields, PrecompiledExampleType
4646
from grpc_client import GRPCClient
47+
from constants import BEAM_ROOT_DIR_ENV_VAR_KEY
4748

4849
from models import Example, Tag, SdkEnum, Dataset
4950

@@ -261,7 +262,7 @@ def _get_url_vcs(filepath: str) -> str:
261262
"""
262263
Construct VCS URL from example's filepath
263264
"""
264-
root_dir = os.getenv("BEAM_ROOT_DIR", "../..")
265+
root_dir = os.getenv(BEAM_ROOT_DIR_ENV_VAR_KEY, "../..")
265266
rel_path = os.path.relpath(filepath, root_dir)
266267
url_vcs = "{}/{}".format(Config.URL_VCS_PREFIX, urllib.parse.quote(rel_path))
267268
return url_vcs

0 commit comments

Comments
 (0)