inkstitch/inkstitch.py

64 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

2020-04-20 18:52:50 +00:00
import logging
import os
import sys
import traceback
from argparse import ArgumentParser
2020-04-20 18:52:50 +00:00
from cStringIO import StringIO
2019-03-28 18:47:05 +00:00
import lib.debug as debug
2020-04-20 18:52:50 +00:00
from lib import extensions
from lib.i18n import _
from lib.utils import restore_stderr, save_stderr
logger = logging.getLogger('shapely.geos')
logger.setLevel(logging.DEBUG)
shapely_errors = StringIO()
ch = logging.StreamHandler(shapely_errors)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
2018-04-29 02:14:23 +00:00
parser = ArgumentParser()
parser.add_argument("--extension")
my_args, remaining_args = parser.parse_known_args()
if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), "DEBUG")):
2019-03-28 18:47:05 +00:00
debug.enable()
2018-04-29 02:14:23 +00:00
extension_name = my_args.extension
2018-07-29 00:40:14 +00:00
# example: foo_bar_baz -> FooBarBaz
extension_class_name = extension_name.title().replace("_", "")
extension_class = getattr(extensions, extension_class_name)
extension = extension_class()
if hasattr(sys, 'gettrace') and sys.gettrace():
2018-04-29 02:14:23 +00:00
extension.affect(args=remaining_args)
else:
save_stderr()
exception = None
try:
extension.affect(args=remaining_args)
except (SystemExit, KeyboardInterrupt):
raise
except Exception:
exception = traceback.format_exc()
finally:
restore_stderr()
if shapely_errors.tell():
print >> sys.stderr, shapely_errors.getvalue()
if exception:
2020-04-28 20:05:22 +00:00
print >> sys.stderr, _("Ink/Stitch experienced an unexpected error.").encode("UTF-8")
2020-04-20 18:52:50 +00:00
print >> sys.stderr, _("If you'd like to help, please file an issue at "
"https://github.com/inkstitch/inkstitch/issues "
2020-04-28 20:05:22 +00:00
"and include the entire error description below:").encode("UTF-8"), "\n"
print >> sys.stderr, exception
sys.exit(1)
else:
sys.exit(0)