Completed
Pull Request — master (#2304)
by Arma
07:07
created

PascalRowAction._compute_pascal_row()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 2
1
#!/usr/bin/env python
2
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
3
# contributor license agreements.  See the NOTICE file distributed with
4
# this work for additional information regarding copyright ownership.
5
# The ASF licenses this file to You under the Apache License, Version 2.0
6
# (the "License"); you may not use this file except in compliance with
7
# the License.  You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
17
import collections
18
import importlib
19
import six
20
import sys
21
import traceback
22
23
from oslo_config import cfg
24
25
26
CONFIGS = ['st2actions.config',
27
           'st2actions.notifier.config',
28
           'st2actions.resultstracker.config',
29
           'st2api.config',
30
           'st2auth.config',
31
           'st2common.config',
32
           'st2exporter.config',
33
           'st2reactor.rules.config',
34
           'st2reactor.sensor.config']
35
36
SKIP_GROUPS = ['api_pecan']
37
38
# We group auth options together to nake it a bit more clear what applies where
39
AUTH_OPTIONS = {
40
    'common': [
41
        'enable',
42
        'mode',
43
        'logging',
44
        'api_url',
45
        'token_ttl',
46
        'debug'
47
    ],
48
    'standalone': [
49
        'host',
50
        'port',
51
        'use_ssl',
52
        'cert',
53
        'key',
54
        'backend',
55
        'backend_kwargs'
56
    ]
57
}
58
59
COMMON_AUTH_OPTIONS_COMMENT = """
60
# Common option - options below apply in both scenarios - when auth service is running as a WSGI
61
# service (e.g. under Apache or Nginx) and when it's running in the standalone mode.
62
""".strip()
63
64
STANDALONE_AUTH_OPTIONS_COMMENT = """
65
# Standalone mode options - options below only apply when auth service is running in the standalone
66
# mode.
67
""".strip()
68
69
70
def _import_config(config):
71
    try:
72
        return importlib.import_module(config)
73
    except:
74
        traceback.print_exc()
75
    return None
76
77
78
def _read_current_config(opt_groups):
79
    for k, v in six.iteritems(cfg.CONF._groups):
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _groups was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
80
        if k in SKIP_GROUPS:
81
            continue
82
        if k not in opt_groups:
83
            opt_groups[k] = v
84
    return opt_groups
85
86
87
def _clear_config():
88
    cfg.CONF.reset()
89
90
91
def _read_group(opt_group):
92
    all_options = opt_group._opts.values()
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _opts was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
93
94
    if opt_group.name == 'auth':
95
        print(COMMON_AUTH_OPTIONS_COMMENT)
96
        print('')
97
        common_options = [option for option in all_options if option['opt'].name in
98
                   AUTH_OPTIONS['common']]
99
        _print_options(options=common_options)
100
101
        print('')
102
        print(STANDALONE_AUTH_OPTIONS_COMMENT)
103
        print('')
104
        standalone_options = [option for option in all_options if option['opt'].name in
105
                              AUTH_OPTIONS['standalone']]
106
        _print_options(options=standalone_options)
107
108
        if len(common_options) + len(standalone_options) != len(all_options):
109
            msg = ('Not all options are declared in AUTH_OPTIONS dict, please update it')
110
            raise Exception(msg)
111
    else:
112
        options = all_options
113
        _print_options(options=options)
114
115
116
def _read_groups(opt_groups):
117
    opt_groups = collections.OrderedDict(sorted(opt_groups.items()))
118
    for name, opt_group in six.iteritems(opt_groups):
119
        print('[%s]' % name)
120
        _read_group(opt_group)
121
        print('')
122
123
124
def _print_options(options):
125
    for opt in options:
126
        opt = opt['opt']
127
        print('# %s' % opt.help)
128
        print('%s = %s' % (opt.name, opt.default))
129
130
131
def main(args):
132
    opt_groups = {}
133
    for config in CONFIGS:
134
        _import_config(config)
135
        _read_current_config(opt_groups)
136
        _clear_config()
137
    _read_groups(opt_groups)
138
139
140
if __name__ == '__main__':
141
    main(sys.argv)
142