Less indentation

merge-requests/30/head
Bob Mottram 2024-07-02 20:55:58 +01:00
parent 80ea6cb86b
commit 76d0bcf3fd
1 changed files with 30 additions and 25 deletions

View File

@ -28,36 +28,41 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
# does this entry already exist? # does this entry already exist?
if os.path.isfile(petnames_filename): if os.path.isfile(petnames_filename):
with open(petnames_filename, 'r', encoding='utf-8') as petnames_file: petnames_str = ''
petnames_str = petnames_file.read() try:
if entry in petnames_str: with open(petnames_filename, 'r',
return True encoding='utf-8') as petnames_file:
if ' ' + handle + '\n' in petnames_str: petnames_str = petnames_file.read()
petnames_list = petnames_str.split('\n') except OSError:
new_petnames_str = '' print('EX: set_pet_name unable to read ' + petnames_filename)
for pet in petnames_list: if entry in petnames_str:
if not pet.endswith(' ' + handle): return True
new_petnames_str += pet + '\n' if ' ' + handle + '\n' in petnames_str:
else: petnames_list = petnames_str.split('\n')
new_petnames_str += entry new_petnames_str = ''
# save the updated petnames file for pet in petnames_list:
try: if not pet.endswith(' ' + handle):
with open(petnames_filename, 'w+', new_petnames_str += pet + '\n'
encoding='utf-8') as petnames_file: else:
petnames_file.write(new_petnames_str) new_petnames_str += entry
except OSError: # save the updated petnames file
print('EX: unable to save ' + petnames_filename)
return False
return True
# entry does not exist in the petnames file
try: try:
with open(petnames_filename, 'a+', with open(petnames_filename, 'w+',
encoding='utf-8') as petnames_file: encoding='utf-8') as petnames_file:
petnames_file.write(entry) petnames_file.write(new_petnames_str)
except OSError: except OSError:
print('EX: unable to append ' + petnames_filename) print('EX: set_pet_name unable to save ' + petnames_filename)
return False return False
return True return True
# entry does not exist in the petnames file
try:
with open(petnames_filename, 'a+',
encoding='utf-8') as petnames_file:
petnames_file.write(entry)
except OSError:
print('EX: set_pet_name unable to append ' + petnames_filename)
return False
return True
# first entry # first entry
try: try: