Skip to content

Commit

Permalink
add WITH statement when opening file to automatically close it after use
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-mal committed Aug 21, 2023
1 parent fd0bbf5 commit 74ca310
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
5 changes: 2 additions & 3 deletions osc_tui/createKeyPair.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def back():
self.inspector = None

def write_key(path, private_key):
key_file = open(os.path.expanduser(path), 'w')
key_file.write(private_key)
key_file.close()
with open(os.path.expanduser(path), 'w') as key_file :
key_file.write(private_key)

def create():
if(NAME.get_value()):
Expand Down
30 changes: 15 additions & 15 deletions osc_tui/guiRules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def objectify():
ret = None
if os.path.isfile(PATH):
ret = {}
configFile = open(PATH)
j = json.loads(configFile.read())
for rule_name in j:
rule=j[rule_name]
if "mode" not in rule:
print('"mode" is a requirre argument of costum rule',
file=sys.stderr)
if rule["mode"] not in ret:
ret[rule["mode"]] = {}
ret[rule["mode"]][rule_name] = {
"ports": rule["ports"],
"protocols": rule["protocols"]
}
if "ips" in rule:
ret[rule["mode"]][rule_name]["ips"] = rule["ips"]
with open(PATH) as configFile :
j = json.loads(configFile.read())
for rule_name in j:
rule=j[rule_name]
if "mode" not in rule:
print('"mode" is a requirre argument of costum rule',
file=sys.stderr)
if rule["mode"] not in ret:
ret[rule["mode"]] = {}
ret[rule["mode"]][rule_name] = {
"ports": rule["ports"],
"protocols": rule["protocols"]
}
if "ips" in rule:
ret[rule["mode"]][rule_name]["ips"] = rule["ips"]
return ret

def parse():
Expand Down
10 changes: 5 additions & 5 deletions osc_tui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ def main():
i += 1
if i == argc:
if os.path.isfile(profileSelector.dst_file):
configFile = open(profileSelector.dst_file)
OAPI_CREDENTIALS = json.loads(configFile.read())
print("Profiles:")
for c in OAPI_CREDENTIALS:
print(str(c))
with open(profileSelector.dst_file) as configFile:
OAPI_CREDENTIALS = json.loads(configFile.read())
print("Profiles:")
for c in OAPI_CREDENTIALS:
print(str(c))
else:
print("{} not found, can't read profile !!!".
format(profileSelector.dst_file),
Expand Down
1 change: 0 additions & 1 deletion osc_tui/popup.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def remove_cb():


def addSecurityGroupToVm(form, form_color='STANDOUT'):
main.SECURITY_GROUP
F = ConfirmCancelPopup(name='Add New Security Group', color=form_color)
F.preserve_selected_widget = True
groups = main.VM["SecurityGroups"]
Expand Down

1 comment on commit 74ca310

@outscale-mal
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WITH statement automatically closes the file after you have completed writing it.

Please sign in to comment.