Added terminate() for cleaner exits

This commit is contained in:
jeancf 2022-12-02 10:42:43 +01:00
parent 5ec7057185
commit e4251e1175

View File

@ -115,10 +115,10 @@ def build_config(args):
loaded_toml = tomllib.load(config_file)
except FileNotFoundError:
print('config file not found')
exit(-1)
terminate(-1)
except tomllib.TOMLDecodeError:
print('Malformed config file')
exit(-1)
terminate(-1)
TOML['config'] = loaded_toml['config']
for k in TOML['options'].keys():
@ -156,13 +156,13 @@ def build_config(args):
# Verify that we have a minimum config to run
if 'twitter_account' not in TOML['config'].keys() or TOML['config']['twitter_account'] == "":
print('CRITICAL: Missing Twitter account')
exit(-1)
terminate(-1)
if 'mastodon_instance' not in TOML['config'].keys() or TOML['config']['mastodon_instance'] == "":
print('CRITICAL: Missing Mastodon instance')
exit(-1)
terminate(-1)
if 'mastodon_user' not in TOML['config'].keys() or TOML['config']['mastodon_user'] == "":
print('CRITICAL: Missing Mastodon user')
exit(-1)
terminate(-1)
def deredir_url(url):
@ -487,7 +487,7 @@ def login(password):
except MastodonError as me:
logging.fatal('failed to create app on ' + TOML['config']['mastodon_instance'])
logging.fatal(me)
exit(-1)
terminate(-1)
mastodon = None
@ -509,7 +509,7 @@ def login(password):
except MastodonError as me:
logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n')
logging.fatal(me)
exit(-1)
terminate(-1)
if os.path.isfile(TOML['config']['mastodon_user'] + '.secret'):
logging.warning('You successfully logged in using a password and an access token \
@ -526,17 +526,28 @@ def login(password):
except MastodonError as me:
logging.fatal('Login to ' + TOML['config']['mastodon_instance'] + ' Failed\n')
logging.fatal(me)
exit(-1)
terminate(-1)
else:
logging.fatal('No .secret file found. Password required to log in')
exit(-1)
terminate(-1)
return mastodon
def terminate(exit_code):
"""
Cleanly stop execution with a message on execution duration
:param exit_code: return value to pass to shell when exiting
"""
logging.info('Run time : {t:2.1f} seconds.'.format(t=time.time() - START_TIME))
logging.info('_____________________________________________________________________________________')
exit(exit_code)
def main(argv):
# Start stopwatch
start_time = time.time()
global START_TIME
START_TIME = time.time()
# Build parser for command line arguments
parser = argparse.ArgumentParser(description='toot tweets.')
@ -639,16 +650,16 @@ def main(argv):
twit_account_page = session.get(url, headers=headers, timeout=HTTPS_REQ_TIMEOUT)
except requests.exceptions.ConnectionError:
logging.fatal('Host did not respond when trying to download ' + url)
exit(-1)
terminate(-1)
except requests.exceptions.Timeout:
logging.fatal(nitter_url + ' took too long to respond')
exit(-1)
terminate(-1)
# Verify that download worked
if twit_account_page.status_code != 200:
logging.fatal('The Nitter page did not download correctly from ' + url + ' (' + str(
twit_account_page.status_code) + '). Aborting')
exit(-1)
terminate(-1)
logging.debug('Nitter page downloaded successfully from ' + url)
@ -946,8 +957,8 @@ def main(argv):
sql.commit()
logging.info('Deleted ' + str(excess_count) + ' old records from database.')
logging.info('Run time : %2.1f seconds' % (time.time() - start_time))
terminate(0)
if __name__ == "__main__":