File: azure_configuration.py

package info (click to toggle)
python-msrestazure 0.6.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: python: 3,942; makefile: 8
file content (97 lines) | stat: -rw-r--r-- 3,875 bytes parent folder | download | duplicates (4)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------

try:
    from configparser import NoOptionError
except ImportError:
    from ConfigParser import NoOptionError

import logging

from msrest import Configuration
from msrest.exceptions import raise_with_traceback

from .version import msrestazure_version
from .tools import register_rp_hook

_LOGGER = logging.getLogger(__name__)

class AzureConfiguration(Configuration):
    """Azure specific client configuration.

    :param str base_url: REST Service base URL.
    :param str filepath: Path to an existing config file (optional).
    """

    def __init__(self, base_url, filepath=None):
        super(AzureConfiguration, self).__init__(base_url)
        self.long_running_operation_timeout = 30
        self.accept_language = 'en-US'
        self.generate_client_request_id = True
        self.add_user_agent("msrest_azure/{}".format(msrestazure_version))

        # ARM requires 20seconds at least. Putting 4 here is 24seconds
        self.retry_policy.retries = 4

        if filepath:
            self.load(filepath)

        # Check if "hasattr", just in case msrest is older than msrestazure
        if hasattr(self, 'hooks'):
            self.hooks.append(register_rp_hook)
        else:
            _LOGGER.warning(("Your 'msrest' version is too old to activate all the "
                             "features of 'msrestazure'. Please update using"
                             "'pip install -U msrest'"))

    def save(self, filepath):
        """Save current configuration to file.

        :param str filepath: Path to save file to.
        :raises: ValueError if supplied filepath cannot be written to.
        """
        self._config.add_section("Azure")
        self._config.set("Azure",
                         "long_running_operation_timeout",
                         self.long_running_operation_timeout)
        return super(AzureConfiguration, self).save(filepath)

    def load(self, filepath):
        """Load configuration from existing file.

        :param str filepath: Path to existing config file.
        :raises: ValueError if supplied config file is invalid.
        """
        try:
            self._config.read(filepath)
            self.long_running_operation_timeout = self._config.getint(
                "Azure", "long_running_operation_timeout")
        except (ValueError, EnvironmentError, NoOptionError):
            msg = "Supplied config file incompatible"
            raise_with_traceback(ValueError, msg)
        finally:
            self._clear_config()
        return super(AzureConfiguration, self).load(filepath)