Passed
Pull Request — master (#3556)
by Lakshmi
05:19 queued 45s
created

PackSearch.__init__()   F

Complexity

Conditions 11

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
dl 0
loc 31
rs 3.1764
c 1
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like PackSearch.__init__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
import os
17
18
from st2common.runners.base_action import Action
19
from st2common.services.packs import search_pack_index
20
21
22
class PackSearch(Action):
23
    def __init__(self, config=None, action_service=None):
24
        super(PackSearch, self).__init__(config=config, action_service=action_service)
25
        self.https_proxy = os.environ.get('https_proxy', self.config.get('https_proxy', None))
26
        self.http_proxy = os.environ.get('http_proxy', self.config.get('http_proxy', None))
27
        self.proxy_ca_bundle_path = os.environ.get(
28
            'proxy_ca_bundle_path',
29
            self.config.get('proxy_ca_bundle_path', None)
30
        )
31
        self.no_proxy = os.environ.get('no_proxy', self.config.get('no_proxy', None))
32
33
        self.proxy_config = None
34
35
        if self.http_proxy or self.https_proxy:
36
            self.proxy_config = {
37
                'https_proxy': self.https_proxy,
38
                'http_proxy': self.http_proxy,
39
                'proxy_ca_bundle_path': self.proxy_ca_bundle_path,
40
                'no_proxy': self.no_proxy
41
            }
42
43
        if self.https_proxy and not os.environ.get('https_proxy', None):
44
            os.environ['https_proxy'] = self.https_proxy
45
46
        if self.http_proxy and not os.environ.get('http_proxy', None):
47
            os.environ['http_proxy'] = self.http_proxy
48
49
        if self.no_proxy and not os.environ.get('no_proxy', None):
50
            os.environ['no_proxy'] = self.no_proxy
51
52
        if self.proxy_ca_bundle_path and not os.environ.get('proxy_ca_bundle_path', None):
53
            os.environ['no_proxy'] = self.no_proxy
54
55
    """"Search for packs in StackStorm Exchange and other directories."""
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
56
    def run(self, query):
57
        """
58
        :param query: A word or a phrase to search for
59
        :type query: ``str``
60
        """
61
        return search_pack_index(query, proxy_config=self.proxy_config)
62