Skip to content

Commit 7b3a95e

Browse files
committed
Use C-accelerated safe_load for YAML
We were generally already doing the right thing (calling load() with a CSafeLoader Loader), but this should make this a little more ergonomic (i.e., you just need to call safe_load() and not worry about passing in the right Loader to load())
1 parent 78b5484 commit 7b3a95e

File tree

8 files changed

+48
-19
lines changed

8 files changed

+48
-19
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ omit =
77
venv/*
88
/usr/*
99
setup.py
10+
# this file has no real logic
11+
service_configuration_lib/yaml_tools.py
1012

1113
[report]
1214
show_missing = True

scripts/dump_service_configuration_yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
# limitations under the License.
1515
import sys
1616

17-
import yaml
18-
1917
import service_configuration_lib
18+
from service_configuration_lib import yaml_tools as yaml
2019
sys.path.append('/nail/sys/srv-deploy/lib/')
2120

2221
print(yaml.dump(service_configuration_lib.read_services_configuration(), default_flow_style=False))

service_configuration_lib/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@
2424
from typing import Mapping
2525

2626
import ephemeral_port_reserve
27-
import yaml
2827

29-
try:
30-
from yaml.cyaml import CSafeLoader as Loader
31-
except ImportError: # pragma: no cover (no libyaml-dev / pypy)
32-
Loader = yaml.SafeLoader # type: ignore
28+
from service_configuration_lib import yaml_tools as yaml
3329

3430
DEFAULT_SOA_DIR = '/nail/etc/services'
3531
log = logging.getLogger(__name__)
@@ -60,7 +56,7 @@ def read_port(port_file):
6056

6157

6258
def load_yaml(fd):
63-
return yaml.load(fd, Loader=Loader)
59+
return yaml.safe_load(fd)
6460

6561

6662
def read_monitoring(monitoring_file):

service_configuration_lib/spark_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import boto3
1919
import requests
20-
import yaml
2120
from boto3 import Session
2221

2322
from service_configuration_lib import utils
23+
from service_configuration_lib import yaml_tools as yaml
2424
from service_configuration_lib.text_colors import TextColors
2525
from service_configuration_lib.utils import EPHEMERAL_PORT_END
2626
from service_configuration_lib.utils import EPHEMERAL_PORT_START
@@ -193,7 +193,7 @@ def assume_aws_role(
193193
"""
194194
try:
195195
with open(key_file) as creds_file:
196-
creds_dict = yaml.load(creds_file.read(), Loader=yaml.SafeLoader)
196+
creds_dict = yaml.safe_load(creds_file.read())
197197
access_key = creds_dict['AccessKeyId']
198198
secret_key = creds_dict['SecretAccessKey']
199199
except (PermissionError, FileNotFoundError):

service_configuration_lib/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
from typing import Dict
1616
from typing import Tuple
1717

18-
import yaml
1918
from typing_extensions import Literal
2019

20+
from service_configuration_lib import yaml_tools as yaml
21+
2122
DEFAULT_SPARK_RUN_CONFIG = '/nail/srv/configs/spark.yaml'
2223
POD_TEMPLATE_PATH = '/nail/tmp/spark-pt-{file_uuid}.yaml'
2324
SPARK_EXECUTOR_POD_TEMPLATE = '/nail/srv/configs/spark_executor_pod_template.yaml'

service_configuration_lib/yaml_cached_view.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import logging
22
from collections import defaultdict
33

4-
import yaml
5-
try:
6-
from yaml import CSafeLoader as SafeLoader # type: ignore
7-
except ImportError: # pragma: no cover
8-
from yaml import SafeLoader # type: ignore
4+
from yaml import YAMLError
95

6+
from service_configuration_lib import yaml_tools as yaml
107
from service_configuration_lib.cached_view import BaseCachedView
118

129
log = logging.getLogger(__name__)
@@ -38,10 +35,10 @@ def __init__(self):
3835
def add(self, path: str, service_name: str, config_name: str, config_suffix: str) -> None:
3936
try:
4037
with open(path, encoding='utf-8') as fd:
41-
self.configs[service_name][config_name] = yaml.load(fd, Loader=SafeLoader)
38+
self.configs[service_name][config_name] = yaml.safe_load(fd)
4239
except OSError as exn:
4340
log.warning(f'Error reading {path}: {exn}')
44-
except yaml.YAMLError as exn:
41+
except YAMLError as exn:
4542
log.warning(f'Error parsing {path}: {exn}')
4643

4744
def remove(self, path: str, service_name: str, config_name: str, config_suffix: str) -> None:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import yaml
2+
3+
try:
4+
from yaml import CSafeLoader as Loader
5+
from yaml import CSafeDumper as Dumper
6+
except ImportError: # pragma: no cover
7+
from yaml import SafeLoader as Loader # type: ignore
8+
from yaml import SafeDumper as Dumper # type: ignore
9+
10+
11+
def dump(*args, **kwargs):
12+
kwargs['Dumper'] = Dumper
13+
return yaml.dump(*args, **kwargs)
14+
15+
16+
def dump_all(*args, **kwargs):
17+
kwargs['Dumper'] = Dumper
18+
return yaml.dump_all(*args, **kwargs)
19+
20+
21+
def load(*args, **kwargs):
22+
kwargs['Loader'] = Loader
23+
return yaml.load(*args, **kwargs)
24+
25+
26+
def load_all(*args, **kwargs):
27+
kwargs['Loader'] = Loader
28+
return yaml.load_all(*args, **kwargs)
29+
30+
31+
safe_dump = dump
32+
safe_dump_all = dump_all
33+
safe_load = load
34+
safe_load_all = load_all

tests/spark_config_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from unittest import mock
88

99
import pytest
10-
import yaml
1110

1211
from service_configuration_lib import spark_config
1312
from service_configuration_lib import utils
13+
from service_configuration_lib import yaml_tools as yaml
1414

1515

1616
TEST_ACCOUNT_ID = '123456789'

0 commit comments

Comments
 (0)