| Conditions | 11 | 
| Total Lines | 92 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like generate_pack() 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 | """Generate pack based on inspecting PyNOS package  | 
            ||
| 85 | def generate_pack(info):  | 
            ||
| 86 | """Generate pack and write to disk.  | 
            ||
| 87 | """  | 
            ||
| 88 | for module, methods in info.iteritems():  | 
            ||
| 89 | for method, args in methods.iteritems():  | 
            ||
| 90 | action_name = "%s_%s" % (str.lower(module), method)  | 
            ||
| 91 | action_path = "%s.%s" % (str.lower(module), method)  | 
            ||
| 92 | code = """from pynos import device  | 
            ||
| 93 | from st2actions.runners.pythonrunner import Action  | 
            ||
| 94 | |||
| 95 | |||
| 96 | class %s(Action):  | 
            ||
| 97 | def run(self, **kwargs):  | 
            ||
| 98 |         conn = (str(kwargs.pop('ip')), str(kwargs.pop('port'))) | 
            ||
| 99 |         auth = (str(kwargs.pop('username')), str(kwargs.pop('password'))) | 
            ||
| 100 |         test = kwargs.pop('test', False) | 
            ||
| 101 |         callback = kwargs.pop('callback', None) | 
            ||
| 102 | with device.Device(  | 
            ||
| 103 | conn=conn, auth=auth,  | 
            ||
| 104 | test=test,  | 
            ||
| 105 | callback=callback  | 
            ||
| 106 | ) as dev:  | 
            ||
| 107 | dev.%s(**kwargs)  | 
            ||
| 108 | return 0\n""" % (action_name, action_path)  | 
            ||
| 109 |             action_yaml = { | 
            ||
| 110 | 'name': action_name,  | 
            ||
| 111 | 'runner_type': "python-script",  | 
            ||
| 112 | 'description': "",  | 
            ||
| 113 | 'enabled': True,  | 
            ||
| 114 | 'entry_point': "%s.py" % action_name,  | 
            ||
| 115 |                 'parameters': { | 
            ||
| 116 |                     'ip': { | 
            ||
| 117 | 'type': 'string',  | 
            ||
| 118 | 'description': 'IP address of VDX to connect to.',  | 
            ||
| 119 | 'required': True,  | 
            ||
| 120 | 'position': 0  | 
            ||
| 121 | },  | 
            ||
| 122 |                     'port': { | 
            ||
| 123 | 'type': 'string',  | 
            ||
| 124 | 'description': 'Port to use to connect to VDX.',  | 
            ||
| 125 | 'required': True,  | 
            ||
| 126 | 'default': '22',  | 
            ||
| 127 | 'position': 1  | 
            ||
| 128 | },  | 
            ||
| 129 |                     'username': { | 
            ||
| 130 | 'type': 'string',  | 
            ||
| 131 | 'description': 'Username used with authentication.',  | 
            ||
| 132 | 'required': True,  | 
            ||
| 133 | 'position': 2  | 
            ||
| 134 | },  | 
            ||
| 135 |                     'password': { | 
            ||
| 136 | 'type': 'string',  | 
            ||
| 137 | 'description': 'Password used with authentication.',  | 
            ||
| 138 | 'required': True,  | 
            ||
| 139 | 'secret': True,  | 
            ||
| 140 | 'position': 3  | 
            ||
| 141 | }  | 
            ||
| 142 | }  | 
            ||
| 143 | }  | 
            ||
| 144 |             type_map = { | 
            ||
| 145 | 'str': 'string',  | 
            ||
| 146 | 'bool': 'boolean',  | 
            ||
| 147 | 'int': 'integer',  | 
            ||
| 148 | 'tuple(str, str)': 'array'  | 
            ||
| 149 | }  | 
            ||
| 150 | if len(args) > 0:  | 
            ||
| 151 | position = 4  | 
            ||
| 152 | for arg in args:  | 
            ||
| 153 | if arg[0] == '__description__':  | 
            ||
| 154 | action_yaml['description'] = arg[1]  | 
            ||
| 155 | continue  | 
            ||
| 156 | if arg[1] == 'function':  | 
            ||
| 157 | continue  | 
            ||
| 158 | if type_map.get(arg[1]) is None:  | 
            ||
| 159 |                         raise Exception("%s maps to none" % arg[1]) | 
            ||
| 160 |                     yaml_arg = { | 
            ||
| 161 | 'type': type_map.get(arg[1]),  | 
            ||
| 162 | 'description': arg[2],  | 
            ||
| 163 | 'required': True,  | 
            ||
| 164 | 'position': position  | 
            ||
| 165 | }  | 
            ||
| 166 | if arg[1] == 'bool':  | 
            ||
| 167 | del yaml_arg['required']  | 
            ||
| 168 | position += 1  | 
            ||
| 169 |                     action_yaml['parameters'].update({arg[0]: yaml_arg}) | 
            ||
| 170 | |||
| 171 |             with open("pack/actions/%s.py" % action_name, "w+") as py_file: | 
            ||
| 172 | py_file.write(code)  | 
            ||
| 173 | |||
| 174 |             with open("pack/actions/%s.yaml" % action_name, "w+") as py_file: | 
            ||
| 175 | output = yaml.dump(action_yaml, default_flow_style=False)  | 
            ||
| 176 | py_file.write(output)  | 
            ||
| 177 | |||
| 190 |