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

[cloud][aws] Use with statement for file rw in EC2 dynamic inventory (#21390)

* Use with statement when doing rw on files

* Deserialize file-like object directly instead of a string

For python 2/3 compatibility reasons, per PR feedback.
This commit is contained in:
Alex Trevino 2017-02-16 09:41:55 -05:00 committed by Ryan Brown
parent a077aeb693
commit 413dfa7273

View file

@ -1519,26 +1519,22 @@ class Ec2Inventory(object):
''' Reads the inventory from the cache file and returns it as a JSON ''' Reads the inventory from the cache file and returns it as a JSON
object ''' object '''
cache = open(self.cache_path_cache, 'r') with open(self.cache_path_cache, 'r') as f:
json_inventory = cache.read() json_inventory = f.read()
return json_inventory return json_inventory
def load_index_from_cache(self): def load_index_from_cache(self):
''' Reads the index from the cache file sets self.index ''' ''' Reads the index from the cache file sets self.index '''
cache = open(self.cache_path_index, 'r') with open(self.cache_path_index, 'r') as f:
json_index = cache.read() self.index = json.load(f)
self.index = json.loads(json_index)
def write_to_cache(self, data, filename): def write_to_cache(self, data, filename):
''' Writes data in JSON format to a file ''' ''' Writes data in JSON format to a file '''
json_data = self.json_format_dict(data, True) json_data = self.json_format_dict(data, True)
cache = open(filename, 'w') with open(filename, 'w') as f:
cache.write(json_data) f.write(json_data)
cache.close()
def uncammelize(self, key): def uncammelize(self, key):
temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', key) temp = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', key)