Fix main.py

This commit is contained in:
VirtuBox
2019-09-15 14:54:48 +02:00
parent af2073b3af
commit ebd216c073

View File

@@ -2,7 +2,7 @@
import os import os
import sys import sys
from cement.core import foundation from cement.core.foundation import CementApp
from cement.core.exc import CaughtSignal, FrameworkError from cement.core.exc import CaughtSignal, FrameworkError
from cement.ext.ext_argparse import ArgParseArgumentHandler from cement.ext.ext_argparse import ArgParseArgumentHandler
from cement.utils.misc import init_defaults from cement.utils.misc import init_defaults
@@ -40,7 +40,7 @@ class WOArgHandler(ArgParseArgumentHandler):
super(WOArgHandler, self).error("unknown args") super(WOArgHandler, self).error("unknown args")
class WOApp(foundation.CementApp): class WOApp(CementApp):
class Meta: class Meta:
label = 'wo' label = 'wo'
@@ -79,55 +79,56 @@ class WOTestApp(WOApp):
# Define the applicaiton object outside of main, as some libraries might wish # Define the applicaiton object outside of main, as some libraries might wish
# to import it as a global (rather than passing it into another class/func) # to import it as a global (rather than passing it into another class/func)
app = WOApp()
def main(): def main():
with WOApp() as app: try:
try: global sys
global sys # Default our exit status to 0 (non-error)
# Default our exit status to 0 (non-error) code = 0
code = 0
# if not root...kick out # if not root...kick out
if not os.geteuid() == 0: if not os.geteuid() == 0:
print("\nNon-privileged users cant use WordOps. " print("\nNon-privileged users cant use WordOps. "
"Switch to root or invoke sudo.\n") "Switch to root or invoke sudo.\n")
app.close(1) app.close(1)
# Setup the application # Setup the application
app.setup() app.setup()
# Dump all arguments into wo log # Dump all arguments into wo log
app.log.debug(sys.argv) app.log.debug(sys.argv)
# Run the application # Run the application
app.run() app.run()
except exc.WOError as e: except exc.WOError as e:
# Catch our application errors and exit 1 (error) # Catch our application errors and exit 1 (error)
code = 1 code = 1
print(e) print(e)
except FrameworkError as e: except FrameworkError as e:
# Catch framework errors and exit 1 (error) # Catch framework errors and exit 1 (error)
code = 1 code = 1
print(e) print(e)
except CaughtSignal as e: except CaughtSignal as e:
# Default Cement signals are SIGINT and SIGTERM, exit 0 (non-error) # Default Cement signals are SIGINT and SIGTERM, exit 0 (non-error)
code = 0 code = 0
print(e) print(e)
except Exception as e: except Exception as e:
code = 1 code = 1
print(e) print(e)
finally: finally:
# Print an exception (if it occurred) and --debug was passed # Print an exception (if it occurred) and --debug was passed
if app.debug: if app.debug:
import sys import sys
import traceback import traceback
exc_type, exc_value, exc_traceback = sys.exc_info() exc_type, exc_value, exc_traceback = sys.exc_info()
if exc_traceback is not None: if exc_traceback is not None:
traceback.print_exc() traceback.print_exc()
# # Close the application # # Close the application
app.close(code) app.close(code)
def get_test_app(**kw): def get_test_app(**kw):