GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — kale/action-datastore (#6)
by Manas
05:59
created

st2common.logging.get_logger_name_for_module()   B

Complexity

Conditions 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.0894
cc 5
1
# Licensed to the StackStorm, Inc ('StackStorm') 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
from __future__ import absolute_import
17
18
import os.path
19
import logging
20
21
__all__ = [
22
    'reopen_log_files',
23
    'set_log_level_for_all_handlers',
24
    'set_log_level_for_all_loggers'
25
]
26
27
LOG = logging.getLogger(__name__)
28
29
30
def reopen_log_files(handlers):
31
    """
32
    This method iterates through all of the providers handlers looking for the FileHandler types.
33
34
    A lock is acquired, the underlying stream closed, reopened, then the lock is released.
35
36
37
    This method should be called when logs are to be rotated by an external process. The simplest
38
    way to do this is via a signal handler.
39
    """
40
    for handler in handlers:
41
        if not isinstance(handler, logging.FileHandler):
42
            continue
43
44
        LOG.info('Re-opening log file "%s" with mode "%s"\n' %
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
45
                 (handler.baseFilename, handler.mode))
46
        try:
47
            handler.acquire()
48
            handler.stream.close()
49
            handler.stream = open(handler.baseFilename, handler.mode)
50
        finally:
51
            handler.release()
52
53
54
def set_log_level_for_all_handlers(logger, level=logging.DEBUG):
55
    """
56
    Set a log level for all the handlers on the provided logger.
57
    """
58
    logger.setLevel(level)
59
60
    handlers = logger.handlers
61
    for handler in handlers:
62
        handler.setLevel(level)
63
64
    return logger
65
66
67
def set_log_level_for_all_loggers(level=logging.DEBUG):
68
    """
69
    Set a log level for all the loggers and handlers to the provided level.
70
    """
71
    root_logger = logging.getLogger()
72
    loggers = logging.Logger.manager.loggerDict.values()
73
    loggers += [root_logger]
74
75
    for logger in loggers:
76
        if not isinstance(logger, logging.Logger):
77
            continue
78
79
        set_log_level_for_all_handlers(logger=logger)
80
81
82
def get_logger_name_for_module(module):
83
    """
84
    Retrieve fully qualified logger name for current module (e.g.
85
    st2common.cmd.sensormanager)
86
87
    :type: ``str``
88
    """
89
    module_file = module.__file__
90
    base_dir = os.path.dirname(os.path.abspath(module_file))
91
    module_name = os.path.basename(module_file)
92
    module_name = module_name.replace('.pyc', '').replace('.py', '')
93
94
    split = base_dir.split(os.path.sep)
95
    split = [component for component in split if component]
96
97
    # Find first component which starts with st2 and use that as a starting point
98
    start_index = 0
99
    for index, component in enumerate(reversed(split)):
100
        if component.startswith('st2'):
101
            start_index = ((len(split) - 1) - index)
102
            break
103
104
    split = split[start_index:]
105
    name = '.'.join(split) + '.' + module_name
106
107
    return name
108