Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
refactor!: Python Code Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Zulqarnain authored and UsamaSadiq committed Apr 22, 2022
1 parent 5a71d5c commit 6cb5c23
Show file tree
Hide file tree
Showing 41 changed files with 227 additions and 305 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/syntax-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
pip install -r requirements.txt
- name: Run Make test.syntax
run: |
timeout 90m make --keep-going test.syntax
timeout 90m make --keep-going test.syntax
1 change: 0 additions & 1 deletion docker/build/flower/flowerconfig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os

address = os.getenv('ADDRESS', "0.0.0.0")
Expand Down
2 changes: 0 additions & 2 deletions playbooks/active_instances_in_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"""

from __future__ import print_function
from __future__ import absolute_import
import argparse
import botocore.session
import botocore.exceptions
Expand Down
3 changes: 0 additions & 3 deletions playbooks/callback_plugins/sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

# From https://github.com/ansible/ansible/issues/31527#issuecomment-335495855
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type


import os
import sys
Expand Down
31 changes: 14 additions & 17 deletions playbooks/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@

######################################################################

from __future__ import absolute_import
from __future__ import print_function
import sys
import os
import argparse
Expand All @@ -122,15 +120,14 @@
import six.moves.configparser
import traceback
import six
from six.moves import range

try:
import json
except ImportError:
import simplejson as json


class Ec2Inventory(object):
class Ec2Inventory:
def _empty_inventory(self):
return {"_meta": {"hostvars": {}}}

Expand Down Expand Up @@ -238,9 +235,9 @@ def read_settings(self):
else:
aws_profile = ""

self.cache_path_cache = cache_path + "/{}ansible-ec2.cache".format(aws_profile)
self.cache_path_tags = cache_path + "/{}ansible-ec2.tags.cache".format(aws_profile)
self.cache_path_index = cache_path + "/{}ansible-ec2.index".format(aws_profile)
self.cache_path_cache = cache_path + f"/{aws_profile}ansible-ec2.cache"
self.cache_path_tags = cache_path + f"/{aws_profile}ansible-ec2.tags.cache"
self.cache_path_index = cache_path + f"/{aws_profile}ansible-ec2.index"
self.cache_max_age = config.getint('ec2', 'cache_max_age')

def parse_cli_args(self):
Expand Down Expand Up @@ -296,7 +293,7 @@ def get_instances_by_region(self, region):

# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
print(("region name: %s likely not supported, or AWS is down. connection to region failed." % region))
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
sys.exit(1)

reservations = conn.get_all_instances()
Expand Down Expand Up @@ -336,7 +333,7 @@ def get_instance(self, region, instance_id):

# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
if conn is None:
print(("region name: %s likely not supported, or AWS is down. connection to region failed." % region))
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
sys.exit(1)

reservations = conn.get_all_instances([instance_id])
Expand Down Expand Up @@ -393,7 +390,7 @@ def add_instance(self, instance, region):
sys.exit(1)

# Inventory: Group by tag keys
for k, v in six.iteritems(instance.tags):
for k, v in instance.tags.items():
key = self.to_safe("tag_" + k + "=" + v)
self.push(self.inventory, key, dest)
self.keep_first(self.inventory, 'first_in_' + key, dest)
Expand Down Expand Up @@ -526,16 +523,16 @@ def get_host_info(self):
value = getattr(instance, key)
key = self.to_safe('ec2_' + key)
# Handle complex types
if type(value) in [int, bool]:
if isinstance(value, (int, bool)):
instance_vars[key] = value
elif type(value) in [str, six.text_type]:
elif isinstance(value, str):
instance_vars[key] = value.strip()
elif type(value) == type(None):
instance_vars[key] = ''
elif key == 'ec2_region':
instance_vars[key] = value.name
elif key == 'ec2_tags':
for k, v in six.iteritems(value):
for k, v in value.items():
key = self.to_safe('ec2_tag_' + k)
instance_vars[key] = v
elif key == 'ec2_groups':
Expand Down Expand Up @@ -573,17 +570,17 @@ def get_inventory_from_cache(self):
''' Reads the inventory from the cache file and returns it as a JSON
object '''
if self.args.tags_only:
cache = open(self.cache_path_tags, 'r')
cache = open(self.cache_path_tags)
else:
cache = open(self.cache_path_cache, 'r')
cache = open(self.cache_path_cache)
json_inventory = cache.read()
return json_inventory


def load_index_from_cache(self):
''' Reads the index from the cache file sets self.index '''

cache = open(self.cache_path_index, 'r')
cache = open(self.cache_path_index)
json_index = cache.read()
self.index = json.loads(json_index)

Expand All @@ -603,7 +600,7 @@ def to_safe(self, word):
''' Converts 'bad' characters in a string to underscores so they can be
used as Ansible groups '''

return re.sub("[^A-Za-z0-9\-]", "_", word)
return re.sub(r"[^A-Za-z0-9\-]", "_", word)


def json_format_dict(self, data, pretty=False):
Expand Down
2 changes: 0 additions & 2 deletions playbooks/lifecycle_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
}
"""
from __future__ import absolute_import
from __future__ import print_function
import argparse
import boto3
import json
Expand Down
48 changes: 23 additions & 25 deletions playbooks/roles/config-encoders/filter_plugins/config_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
More information: https://github.com/jtyr/ansible-config_encoder_filters
"""

from __future__ import (absolute_import, division, print_function)
from ansible import errors
from copy import copy
import re
import six
from six.moves import map


def _str_is_bool(data):
Expand Down Expand Up @@ -169,7 +167,7 @@ def encode_apache(

elif block_type == 'options':
for o in data:
for key, val in sorted(six.iteritems(o)):
for key, val in sorted(o.items()):
rv += "%s%s " % (indent * (level-1), key)
rv += encode_apache(
val,
Expand Down Expand Up @@ -197,7 +195,7 @@ def encode_apache(
else:
rv += str(data)

elif isinstance(data, six.string_types):
elif isinstance(data, str):
# Value is a string
if (
quote_all_strings or
Expand Down Expand Up @@ -244,7 +242,7 @@ def encode_erlang(

rv += "\n"

for key, val in sorted(six.iteritems(data)):
for key, val in sorted(data.items()):
rv += "%s{%s," % (indent*level, key)

if not isinstance(val, dict):
Expand All @@ -268,7 +266,7 @@ def encode_erlang(

rv += str(data).lower()

elif isinstance(data, six.string_types):
elif isinstance(data, str):
# It's a string

atom_len = len(atom_value_indicator)
Expand All @@ -289,7 +287,7 @@ def encode_erlang(

for val in data:
if (
isinstance(val, six.string_types) or
isinstance(val, str) or
_is_num(val)):
rv += "\n%s" % (indent*level)

Expand Down Expand Up @@ -360,7 +358,7 @@ def encode_ini(
rv = ""

# First process all standalone properties
for prop, val in sorted(six.iteritems(data)):
for prop, val in sorted(data.items()):
if ucase_prop:
prop = prop.upper()

Expand All @@ -377,7 +375,7 @@ def encode_ini(
prop, delimiter, quote, _escape(item, quote), quote)

# Then process all sections
for section, props in sorted(six.iteritems(data)):
for section, props in sorted(data.items()):
if isinstance(props, dict):
if rv != "":
rv += "\n"
Expand Down Expand Up @@ -413,7 +411,7 @@ def encode_json(
if len(data) > 0:
rv += "\n"

items = sorted(six.iteritems(data))
items = sorted(data.items())

for key, val in items:
rv += '%s"%s": ' % (indent * (level+1), key)
Expand Down Expand Up @@ -447,7 +445,7 @@ def encode_json(

rv += str(data).lower()

elif isinstance(data, six.string_types):
elif isinstance(data, str):
# It's a string

rv += '"%s"' % _escape(_escape(data), format='control')
Expand Down Expand Up @@ -497,7 +495,7 @@ def encode_logstash(
if prevtype in ('value', 'value_hash', 'array'):
rv += "{\n"

items = sorted(six.iteritems(data))
items = sorted(data.items())

for key, val in items:
if key[0] == section_prefix:
Expand All @@ -513,7 +511,7 @@ def encode_logstash(
# Last item of the loop
if items[-1] == (key, val):
if (
isinstance(val, six.string_types) or
isinstance(val, str) or
_is_num(val) or
isinstance(val, bool) or (
isinstance(val, dict) and
Expand All @@ -540,7 +538,7 @@ def encode_logstash(

if (
items[-1] != (key, val) and (
isinstance(val, six.string_types) or
isinstance(val, str) or
_is_num(val) or
isinstance(val, bool))):
rv += "\n"
Expand All @@ -560,7 +558,7 @@ def encode_logstash(

rv += str(data).lower()

elif isinstance(data, six.string_types):
elif isinstance(data, str):
# It's a string

rv += '"%s"' % _escape(data)
Expand Down Expand Up @@ -625,7 +623,7 @@ def encode_nginx(data, indent=" ", level=0, block_semicolon=False):

item_type = 'section'

elif isinstance(item, six.string_types):
elif isinstance(item, str):
# Normal line
if item_type == 'section':
rv += "\n"
Expand Down Expand Up @@ -656,7 +654,7 @@ def encode_pam(
# Remember previous type to make newline between type blocks
prev_type = None

for label, rule in sorted(six.iteritems(data)):
for label, rule in sorted(data.items()):
if separate_types:
# Add extra newline to separate blocks of the same type
if prev_type is not None and prev_type != rule['type']:
Expand Down Expand Up @@ -714,9 +712,9 @@ def encode_toml(
# It's a dict

# First process all standalone strings, numbers, booleans and lists
for key, val in sorted(six.iteritems(data)):
for key, val in sorted(data.items()):
if (
isinstance(val, six.string_types) or
isinstance(val, str) or
_is_num(val) or
isinstance(val, bool) or (
isinstance(val, list) and
Expand All @@ -737,7 +735,7 @@ def encode_toml(
first = False

# Then process all data structures
for key, val in sorted(six.iteritems(data)):
for key, val in sorted(data.items()):
if (
isinstance(val, dict) or
isinstance(val, list) and isinstance(val[0], dict)):
Expand Down Expand Up @@ -798,7 +796,7 @@ def encode_toml(
if prevtype != 'list':
rv += "\n"

elif isinstance(data, six.string_types):
elif isinstance(data, str):
# It's a string

rv += "%s%s%s" % (
Expand Down Expand Up @@ -950,7 +948,7 @@ def encode_yaml(
if len(list(data.keys())) == 0:
rv += "{}\n"
else:
for i, (key, val) in enumerate(sorted(six.iteritems(data))):
for i, (key, val) in enumerate(sorted(data.items())):
# Skip indentation only for the first pair
rv += "%s%s:" % ("" if i == 0 and skip_indent else level*indent, key)

Expand Down Expand Up @@ -1044,17 +1042,17 @@ def template_replace(data, replacement):
if isinstance(local_data, list):
local_data = [template_replace(x, replacement) for x in local_data]
elif isinstance(local_data, dict):
for key, val in six.iteritems(local_data):
for key, val in local_data.items():
local_data[key] = template_replace(val, replacement)
elif isinstance(local_data, six.string_types):
elif isinstance(local_data, str):
# Replace the special string by it's evaluated value
p = re.compile(r'\{\[\{\s*(\w+)([^}\s]+|)\s*\}\]\}')
local_data = p.sub(__eval_replace, local_data)

return local_data


class FilterModule(object):
class FilterModule:
"""Ansible encoder Jinja2 filters."""

def filters(self):
Expand Down
Loading

0 comments on commit 6cb5c23

Please sign in to comment.