Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/boto3/__init__.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

31 statements  

1# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"). You 

4# may not use this file except in compliance with the License. A copy of 

5# the License is located at 

6# 

7# https://aws.amazon.com/apache2.0/ 

8# 

9# or in the "license" file accompanying this file. This file is 

10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 

11# ANY KIND, either express or implied. See the License for the specific 

12# language governing permissions and limitations under the License. 

13 

14import logging 

15 

16from boto3.compat import _warn_deprecated_python 

17from boto3.session import Session 

18 

19__author__ = 'Amazon Web Services' 

20__version__ = '1.35.30' 

21 

22 

23# The default Boto3 session; autoloaded when needed. 

24DEFAULT_SESSION = None 

25 

26 

27def setup_default_session(**kwargs): 

28 """ 

29 Set up a default session, passing through any parameters to the session 

30 constructor. There is no need to call this unless you wish to pass custom 

31 parameters, because a default session will be created for you. 

32 """ 

33 global DEFAULT_SESSION 

34 DEFAULT_SESSION = Session(**kwargs) 

35 

36 

37def set_stream_logger(name='boto3', level=logging.DEBUG, format_string=None): 

38 """ 

39 Add a stream handler for the given name and level to the logging module. 

40 By default, this logs all boto3 messages to ``stdout``. 

41 

42 >>> import boto3 

43 >>> boto3.set_stream_logger('boto3.resources', logging.INFO) 

44 

45 For debugging purposes a good choice is to set the stream logger to ``''`` 

46 which is equivalent to saying "log everything". 

47 

48 .. WARNING:: 

49 Be aware that when logging anything from ``'botocore'`` the full wire 

50 trace will appear in your logs. If your payloads contain sensitive data 

51 this should not be used in production. 

52 

53 :type name: string 

54 :param name: Log name 

55 :type level: int 

56 :param level: Logging level, e.g. ``logging.INFO`` 

57 :type format_string: str 

58 :param format_string: Log message format 

59 """ 

60 if format_string is None: 

61 format_string = "%(asctime)s %(name)s [%(levelname)s] %(message)s" 

62 

63 logger = logging.getLogger(name) 

64 logger.setLevel(level) 

65 handler = logging.StreamHandler() 

66 handler.setLevel(level) 

67 formatter = logging.Formatter(format_string) 

68 handler.setFormatter(formatter) 

69 logger.addHandler(handler) 

70 

71 

72def _get_default_session(): 

73 """ 

74 Get the default session, creating one if needed. 

75 

76 :rtype: :py:class:`~boto3.session.Session` 

77 :return: The default session 

78 """ 

79 if DEFAULT_SESSION is None: 

80 setup_default_session() 

81 _warn_deprecated_python() 

82 

83 return DEFAULT_SESSION 

84 

85 

86def client(*args, **kwargs): 

87 """ 

88 Create a low-level service client by name using the default session. 

89 

90 See :py:meth:`boto3.session.Session.client`. 

91 """ 

92 return _get_default_session().client(*args, **kwargs) 

93 

94 

95def resource(*args, **kwargs): 

96 """ 

97 Create a resource service client by name using the default session. 

98 

99 See :py:meth:`boto3.session.Session.resource`. 

100 """ 

101 return _get_default_session().resource(*args, **kwargs) 

102 

103 

104# Set up logging to ``/dev/null`` like a library is supposed to. 

105# https://docs.python.org/3.3/howto/logging.html#configuring-logging-for-a-library 

106class NullHandler(logging.Handler): 

107 def emit(self, record): 

108 pass 

109 

110 

111logging.getLogger('boto3').addHandler(NullHandler())