Skip to content

Commit 91948ab

Browse files
author
Bradley A. Thornton
committed
make templated files lint within reason, add comments
1 parent c4940cd commit 91948ab

File tree

10 files changed

+235
-66
lines changed

10 files changed

+235
-66
lines changed

roles/resource_module/templates/module_directory/network_os/network_os_resource.py.j2

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
##############################################
2424
##############################################
2525

26+
"""
27+
The module file for {{ network_os }}_{{ resource }}
28+
"""
29+
2630
from __future__ import absolute_import, division, print_function
2731
from ansible.module_utils.basic import AnsibleModule
28-
from {{ import_path }}.{{ network_os }}.config.{{ resource }}.{{ resource }} import {{ resource|capitalize }}
29-
30-
__metaclass__ = type
31-
32+
from {{ import_path }}. \
33+
{{ network_os }}.config.{{ resource }}.{{ resource }} import {{ resource|capitalize }}
3234

3335
ANSIBLE_METADATA = {'metadata_version': '1.1',
3436
'status': ['preview'],
@@ -70,14 +72,16 @@ commands:
7072

7173

7274
def main():
73-
""" main entry point for module execution
75+
"""
76+
Main entry point for module execution
77+
78+
:returns: the result form module invocation
7479
"""
7580
module = AnsibleModule(argument_spec={{ resource|capitalize }}.argument_spec,
7681
supports_check_mode=True)
7782

7883
result = {{ resource|capitalize }}(module).execute_module()
7984
module.exit_json(**result)
8085

81-
8286
if __name__ == '__main__':
8387
main()

roles/resource_module/templates/module_utils/network/argspec/base.py renamed to roles/resource_module/templates/module_utils/network/argspec/base.py.j2

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# -*- coding: utf-8 -*-
33
# {{ rm['metadata']['copyright_str'] }}
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
""" The argspec base class
6+
"""
57

6-
class ArgspecBase(object):
7-
8+
class ArgspecBase(object): #pylint: disable=R0205,R0903
9+
""" The argspec base class
10+
"""
811
def __init__(self, **kwargs):
912
pass

roles/resource_module/templates/module_utils/network_os/argspec/facts/facts.py.j2

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
# -*- coding: utf-8 -*-
33
# {{ rm['metadata']['copyright_str'] }}
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The arg spec for the {{ network_os }} facts module.
7+
"""
58

6-
from {{ import_path }}.network.argspec.base import ArgspecBase
7-
8-
class FactsArgs(ArgspecBase):
9+
from {{ import_path }}. \
10+
network.argspec.base import ArgspecBase
911

12+
class FactsArgs(ArgspecBase): #pylint: disable=R0903
13+
""" The arg spec for the {{ network_os }} facts module
14+
"""
1015
argument_spec = {
1116
'gather_subset': dict(default=['all'], type='list')
1217
}

roles/resource_module/templates/module_utils/network_os/argspec/resource/resource.py.j2

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
##############################################
2323
##############################################
2424
##############################################
25+
"""
26+
The arg spec for the {{ network_os }}_{{ resource }} module
27+
"""
2528

29+
from {{ import_path }}. \
30+
network.argspec.base import ArgspecBase
2631

27-
from {{ import_path }}.network.argspec.base import ArgspecBase
28-
29-
class {{ resource|capitalize }}Args(ArgspecBase):
30-
31-
argument_spec = {{ rm|to_argspec }}
32+
class {{ resource|capitalize }}Args(ArgspecBase): #pylint: disable=R0903
33+
"""The arg spec for the {{ network_os }}_{{ resource }} module
34+
"""
35+
argument_spec = {{ rm|to_argspec }} #pylint: disable=C0301

roles/resource_module/templates/module_utils/network_os/config/base.py.j2

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
# -*- coding: utf-8 -*-
33
# {{ rm['metadata']['copyright_str'] }}
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The base class for all {{ network_os }} resource modules
7+
"""
58

69
from ansible.module_utils.connection import Connection
710

8-
class ConfigBase(object):
9-
11+
class ConfigBase(object): #pylint: disable=R0205,R0903
12+
""" The base class for all {{ network_os }} resource modules
13+
"""
1014
_connection = None
1115

1216
def __init__(self, module):
@@ -16,5 +20,5 @@ class ConfigBase(object):
1620
def _get_connection(self):
1721
if self._connection:
1822
return self._connection
19-
self._connection = Connection(self._module._socket_path)
23+
self._connection = Connection(self._module._socket_path) #pylint: disable=W0212
2024
return self._connection

roles/resource_module/templates/module_utils/network_os/config/resource/resource.py.j2

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,51 @@
22
# -*- coding: utf-8 -*-
33
# {{ rm['metadata']['copyright_str'] }}
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The {{ network_os }}_{{ resource }} class
7+
It is in this file where the current configuration (as dict)
8+
is compared to the provided configuration (as dict) and the command set
9+
necessary to bring the current configuration to it's desired end-state is
10+
created
11+
"""
512

6-
from ansible.module_utils.six import iteritems
713
from ansible.module_utils.network.common.utils import to_list
814

9-
from {{ import_path }}.{{ network_os }}.argspec.{{ resource }}.{{ resource }} import {{ resource|capitalize }}Args
10-
from {{ import_path }}.{{ network_os }}.config.base import ConfigBase
11-
from {{ import_path }}.{{ network_os }}.facts.facts import Facts
12-
15+
from {{ import_path }}. \
16+
{{ network_os }}.argspec.{{ resource }}.{{ resource }} import {{ resource|capitalize }}Args
17+
from {{ import_path }}. \
18+
{{ network_os }}. \
19+
config.base import ConfigBase
20+
from {{ import_path }}. \
21+
{{ network_os }}.facts.facts import Facts
1322

1423
class {{ resource|capitalize }}(ConfigBase, {{ resource|capitalize }}Args):
24+
"""
25+
The {{ network_os }}_{{ resource }} class
26+
"""
1527

1628
gather_subset = [
1729
'net_configuration_{{ resource }}',
1830
]
1931

2032
def get_{{ resource }}_facts(self):
33+
""" Get the 'facts' (the current configuration)
34+
35+
:rtype: A dictionary
36+
:returns: The current configuration as a dictionary
37+
"""
2138
facts = Facts().get_facts(self._module, self._connection, self.gather_subset)
2239
{{ resource }}_facts = facts['net_configuration'].get('{{ resource }}')
2340
if not {{ resource }}_facts:
2441
return []
2542
return {{ resource }}_facts
2643

2744
def execute_module(self):
45+
""" Execute the module
46+
47+
:rtype: A dictionary
48+
:returns: The result from moduel execution
49+
"""
2850
result = {'changed': False}
2951
commands = list()
3052
warnings = list()
@@ -46,12 +68,27 @@ class {{ resource|capitalize }}(ConfigBase, {{ resource|capitalize }}Args):
4668
return result
4769

4870
def set_config(self):
71+
""" Collect the configuration from the args passed to the module,
72+
collect the current configuration (as a dict from facts)
73+
74+
:rtype: A list
75+
:returns: the commands necessary to migrate the current configuration
76+
to the deisred configuration
77+
"""
4978
want = self._module.params['config']
5079
have = self.get_{{ resource }}_facts()
5180
resp = self.set_state(want, have)
5281
return to_list(resp)
5382

5483
def set_state(self, want, have):
84+
""" Select the appropriate function based on the state provided
85+
86+
:param want: the desired configuration as a dictionary
87+
:param have: the current configuration as a dictionary
88+
:rtype: A list
89+
:returns: the commands necessary to migrate the current configuration
90+
to the deisred configuration
91+
"""
5592
state = self._module.params['state']
5693
if state == 'overridden':
5794
commands = self._state_overridden(want, have)
@@ -63,18 +100,63 @@ class {{ resource|capitalize }}(ConfigBase, {{ resource|capitalize }}Args):
63100
commands = self._state_replaced(want, have)
64101
return commands
65102

66-
def _state_replaced(self, want, have):
103+
@staticmethod
104+
def _state_replaced(want, have):
105+
""" The command generator when state is replaced
106+
107+
:param want: the desired configuration as a dictionary
108+
:param have: the current configuration as a dictionary
109+
:rtype: A list
110+
:returns: the commands necessary to migrate the current configuration
111+
to the deisred configuration
112+
"""
113+
if want != have:
114+
# compare and generate command set
115+
pass
67116
commands = []
68117
return commands
69118

70-
def _state_overridden(self, want, have):
119+
@staticmethod
120+
def _state_overridden(want, have):
121+
""" The command generator when state is overridden
122+
123+
:param want: the desired configuration as a dictionary
124+
:param have: the current configuration as a dictionary
125+
:rtype: A list
126+
:returns: the commands necessary to migrate the current configuration
127+
to the deisred configuration
128+
"""
129+
if want != have:
130+
pass
71131
commands = []
72132
return commands
73133

74-
def _state_merged(self, want, have):
134+
@staticmethod
135+
def _state_merged(want, have):
136+
""" The command generator when state is merged
137+
138+
:param want: the additive configuration as a dictionary
139+
:param have: the current configuration as a dictionary
140+
:rtype: A list
141+
:returns: the commands necessary to merge the provided into
142+
the current configuration
143+
"""
144+
if want != have:
145+
pass
75146
commands = []
76147
return commands
77148

78-
def _state_deleted(self, want, have):
149+
@staticmethod
150+
def _state_deleted(want, have):
151+
""" The command generator when state is deleted
152+
153+
:param want: the objects from which the configuration should be removed
154+
:param have: the current configuration as a dictionary
155+
:rtype: A list
156+
:returns: the commands necessary to remove the current configuration
157+
of the provided objects
158+
"""
159+
if want != have:
160+
pass
79161
commands = []
80162
return commands

roles/resource_module/templates/module_utils/network_os/facts/base.py.j2

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
# -*- coding: utf-8 -*-
33
# {{ rm['metadata']['copyright_str'] }}
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The {{ network_os }} facts base class
7+
this contains methods common to all facts subsets
8+
"""
59

610
import re
7-
811
from copy import deepcopy
9-
1012
from ansible.module_utils.six import iteritems
1113

1214

13-
class FactsBase(object):
14-
15+
class FactsBase(object): #pylint: disable=R0205
16+
"""
17+
The {{ network_os }} facts base class
18+
"""
1519
generated_spec = {}
1620
ansible_facts = {'net_configuration': {}}
1721

@@ -27,7 +31,8 @@ class FactsBase(object):
2731

2832
self.generated_spec = self.generate_dict(facts_argument_spec)
2933

30-
def generate_dict(self, spec):
34+
@staticmethod
35+
def generate_dict(spec):
3136
"""
3237
Generate dictionary which is in sync with argspec
3338
@@ -39,16 +44,17 @@ class FactsBase(object):
3944
if not spec:
4045
return obj
4146

42-
for k, v in iteritems(spec):
43-
if 'default' in v:
44-
d = {k: v['default']}
47+
for key, val in iteritems(spec):
48+
if 'default' in val:
49+
dct = {key: val['default']}
4550
else:
46-
d = {k: None}
47-
obj.update(d)
51+
dct = {key: None}
52+
obj.update(dct)
4853

4954
return obj
5055

51-
def parse_conf_arg(self, cfg, arg):
56+
@staticmethod
57+
def parse_conf_arg(cfg, arg):
5258
"""
5359
Parse config based on argument
5460
@@ -59,9 +65,13 @@ class FactsBase(object):
5965
"""
6066
match = re.search(r'%s (.+)(\n|$)' % arg, cfg, re.M)
6167
if match:
62-
return match.group(1).strip()
68+
result = match.group(1).strip()
69+
else:
70+
result = None
71+
return result
6372

64-
def parse_conf_cmd_arg(self, cfg, cmd, res1, res2=None):
73+
@staticmethod
74+
def parse_conf_cmd_arg(cfg, cmd, res1, res2=None):
6575
"""
6676
Parse config based on command
6777
@@ -75,13 +85,14 @@ class FactsBase(object):
7585
match = re.search(r'\n\s+%s(\n|$)' % cmd, cfg)
7686
if match:
7787
return res1
78-
else:
79-
if res2 is not None:
80-
match = re.search(r'\n\s+no %s(\n|$)' % cmd, cfg)
81-
if match:
82-
return res2
83-
84-
def generate_final_config(self, cfg_dict):
88+
if res2 is not None:
89+
match = re.search(r'\n\s+no %s(\n|$)' % cmd, cfg)
90+
if match:
91+
return res2
92+
return None
93+
94+
@staticmethod
95+
def generate_final_config(cfg_dict):
8596
"""
8697
Generate final config dictionary
8798
@@ -93,8 +104,7 @@ class FactsBase(object):
93104
if not cfg_dict:
94105
return final_cfg
95106

96-
for k, v in iteritems(cfg_dict):
97-
if v is not None:
98-
final_cfg.update({k:v})
99-
107+
for key, val in iteritems(cfg_dict):
108+
if val is not None:
109+
final_cfg.update({key:val})
100110
return final_cfg

0 commit comments

Comments
 (0)