1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

iptables_state: minor pythonisms (#5844)

* iptables_state: minor pythonisms

* add changelog fragment

* fix typo
This commit is contained in:
Alexei Znamensky 2023-01-18 20:05:31 +13:00 committed by GitHub
parent b92542dea2
commit a35b2eda4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 15 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- iptables_state - minor refactoring within the module (https://github.com/ansible-collections/community.general/pull/5844).

View file

@ -260,10 +260,7 @@ def read_state(b_path):
''' '''
with open(b_path, 'r') as f: with open(b_path, 'r') as f:
text = f.read() text = f.read()
lines = text.splitlines() return [t for t in text.splitlines() if t != '']
while '' in lines:
lines.remove('')
return lines
def write_state(b_path, lines, changed): def write_state(b_path, lines, changed):
@ -273,8 +270,7 @@ def write_state(b_path, lines, changed):
# Populate a temporary file # Populate a temporary file
tmpfd, tmpfile = tempfile.mkstemp() tmpfd, tmpfile = tempfile.mkstemp()
with os.fdopen(tmpfd, 'w') as f: with os.fdopen(tmpfd, 'w') as f:
for line in lines: f.write("{0}\n".format("\n".join(lines)))
f.write('%s\n' % line)
# Prepare to copy temporary file to the final destination # Prepare to copy temporary file to the final destination
if not os.path.exists(b_path): if not os.path.exists(b_path):
@ -335,9 +331,7 @@ def filter_and_format_state(string):
string = re.sub(r'((^|\n)# (Generated|Completed)[^\n]*) on [^\n]*', r'\1', string) string = re.sub(r'((^|\n)# (Generated|Completed)[^\n]*) on [^\n]*', r'\1', string)
if not module.params['counters']: if not module.params['counters']:
string = re.sub(r'\[[0-9]+:[0-9]+\]', r'[0:0]', string) string = re.sub(r'\[[0-9]+:[0-9]+\]', r'[0:0]', string)
lines = string.splitlines() lines = [line for line in string.splitlines() if line != '']
while '' in lines:
lines.remove('')
return lines return lines
@ -354,10 +348,7 @@ def per_table_state(command, state):
dummy, out, dummy = module.run_command(COMMAND, check_rc=True) dummy, out, dummy = module.run_command(COMMAND, check_rc=True)
out = re.sub(r'(^|\n)(# Generated|# Completed|[*]%s|COMMIT)[^\n]*' % t, r'', out) out = re.sub(r'(^|\n)(# Generated|# Completed|[*]%s|COMMIT)[^\n]*' % t, r'', out)
out = re.sub(r' *\[[0-9]+:[0-9]+\] *', r'', out) out = re.sub(r' *\[[0-9]+:[0-9]+\] *', r'', out)
table = out.splitlines() tables[t] = [tt for tt in out.splitlines() if tt != '']
while '' in table:
table.remove('')
tables[t] = table
return tables return tables
@ -548,8 +539,7 @@ def main():
if module.check_mode: if module.check_mode:
tmpfd, tmpfile = tempfile.mkstemp() tmpfd, tmpfile = tempfile.mkstemp()
with os.fdopen(tmpfd, 'w') as f: with os.fdopen(tmpfd, 'w') as f:
for line in initial_state: f.write("{0}\n".format("\n".join(initial_state)))
f.write('%s\n' % line)
if filecmp.cmp(tmpfile, b_path): if filecmp.cmp(tmpfile, b_path):
restored_state = initial_state restored_state = initial_state