1# -*- coding: utf-8 -*- #
2# Copyright 2021 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# 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"""A library for working with environment variables on functions."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21
22def GetEnvVarsAsDict(env_vars):
23  if env_vars:
24    return {prop.key: prop.value for prop in env_vars.additionalProperties}
25  else:
26    return {}
27
28
29def DictToEnvVarsProperty(env_vars_type_class=None, env_vars=None):
30  """Sets environment variables.
31
32  Args:
33    env_vars_type_class: type class of environment variables
34    env_vars: a dict of environment variables
35
36  Returns:
37    An message with the environment variables from env_vars
38  """
39  if not env_vars_type_class or not env_vars:
40    return None
41  return env_vars_type_class(additionalProperties=[
42      env_vars_type_class.AdditionalProperty(key=key, value=value)
43      for key, value in sorted(env_vars.items())
44  ])
45