summaryrefslogtreecommitdiff
path: root/inkstitch.py
diff options
context:
space:
mode:
Diffstat (limited to 'inkstitch.py')
-rw-r--r--inkstitch.py94
1 files changed, 56 insertions, 38 deletions
diff --git a/inkstitch.py b/inkstitch.py
index dde884d7..95226594 100644
--- a/inkstitch.py
+++ b/inkstitch.py
@@ -6,15 +6,46 @@
import os
import sys
from pathlib import Path # to work with paths as objects
-import configparser # to read DEBUG.ini
from argparse import ArgumentParser # to parse arguments and remove --extension
-import lib.debug_utils as debug_utils
+if sys.version_info >= (3, 11):
+ import tomllib # built-in in Python 3.11+
+else:
+ import tomli as tomllib
+
+import logging
+
+import lib.debug.utils as debug_utils
+import lib.debug.logging as debug_logging
+from lib.debug.utils import safe_get # mimic get method of dict with default value
+
+# --------------------------------------------------------------------------------------------
SCRIPTDIR = Path(__file__).parent.absolute()
+logger = logging.getLogger("inkstitch") # create module logger with name 'inkstitch'
+
+# TODO --- temporary --- catch old DEBUG.ini file and inform user to reformat it to DEBUG.toml
+old_debug_ini = SCRIPTDIR / "DEBUG.ini"
+if old_debug_ini.exists():
+ print("ERROR: old DEBUG.ini exists, please reformat it to DEBUG.toml and remove DEBUG.ini file", file=sys.stderr)
+ exit(1)
+# --- end of temporary ---
+
+debug_toml = SCRIPTDIR / "DEBUG.toml"
+if debug_toml.exists():
+ with debug_toml.open("rb") as f:
+ ini = tomllib.load(f) # read DEBUG.toml file if exists, otherwise use default values in ini object
+else:
+ ini = {}
+# --------------------------------------------------------------------------------------------
+
running_as_frozen = getattr(sys, 'frozen', None) is not None # check if running from pyinstaller bundle
+if not running_as_frozen: # override running_as_frozen from DEBUG.toml - for testing
+ if safe_get(ini, "DEBUG", "force_frozen", default=False):
+ running_as_frozen = True
+
if len(sys.argv) < 2:
# no arguments - prevent accidentally running this script
msg = "No arguments given, exiting!" # without gettext localization see _()
@@ -26,13 +57,14 @@ if len(sys.argv) < 2:
dlg.ShowModal()
dlg.Destroy()
except ImportError:
- print(msg)
+ print(msg, file=sys.stderr)
else:
- print(msg)
+ print(msg, file=sys.stderr)
exit(1)
-ini = configparser.ConfigParser()
-ini.read(SCRIPTDIR / "DEBUG.ini") # read DEBUG.ini file if exists
+# activate logging - must be done before any logging is done
+debug_logging.activate_logging(running_as_frozen, ini, SCRIPTDIR)
+# --------------------------------------------------------------------------------------------
# check if running from inkscape, given by environment variable
if os.environ.get('INKSTITCH_OFFLINE_SCRIPT', '').lower() in ['true', '1', 'yes', 'y']:
@@ -40,6 +72,7 @@ if os.environ.get('INKSTITCH_OFFLINE_SCRIPT', '').lower() in ['true', '1', 'yes'
else:
running_from_inkscape = True
+# initialize debug and profiler type
debug_active = bool((gettrace := getattr(sys, 'gettrace')) and gettrace()) # check if debugger is active on startup
debug_type = 'none'
profiler_type = 'none'
@@ -48,54 +81,42 @@ if not running_as_frozen: # debugging/profiling only in development mode
# specify debugger type
# but if script was already started from debugger then don't read debug type from ini file or cmd line
if not debug_active:
- debug_type = debug_utils.resole_debug_type(ini) # read debug type from ini file or cmd line
+ debug_type = debug_utils.resolve_debug_type(ini) # read debug type from ini file or cmd line
- profile_type = debug_utils.resole_profile_type(ini) # read profile type from ini file or cmd line
+ profiler_type = debug_utils.resolve_profiler_type(ini) # read profile type from ini file or cmd line
if running_from_inkscape:
# process creation of the Bash script - should be done before sys.path is modified, see below in prefere_pip_inkex
- if ini.getboolean("DEBUG", "create_bash_script", fallback=False): # create script only if enabled in DEBUG.ini
+ if safe_get(ini, "DEBUG", "create_bash_script", default=False): # create script only if enabled in DEBUG.toml
debug_utils.write_offline_debug_script(SCRIPTDIR, ini)
# disable debugger when running from inkscape
- disable_from_inkscape = ini.getboolean("DEBUG", "disable_from_inkscape", fallback=False)
+ disable_from_inkscape = safe_get(ini, "DEBUG", "disable_from_inkscape", default=False)
if disable_from_inkscape:
debug_type = 'none' # do not start debugger when running from inkscape
# prefer pip installed inkex over inkscape bundled inkex, pip version is bundled with Inkstitch
# - must be be done before importing inkex
- prefere_pip_inkex = ini.getboolean("LIBRARY", "prefer_pip_inkex", fallback=True)
+ prefere_pip_inkex = safe_get(ini, "LIBRARY", "prefer_pip_inkex", default=True)
if prefere_pip_inkex and 'PYTHONPATH' in os.environ:
debug_utils.reorder_sys_path()
-# enabling of debug depends on value of debug_type in DEBUG.ini file
+# enabling of debug depends on value of debug_type in DEBUG.toml file
if debug_type != 'none':
- from lib.debug import debug # import global variable debug - don't import whole module
- debug.enable(debug_type, SCRIPTDIR, ini)
+ from lib.debug.debugger import init_debugger
+ init_debugger(debug_type, ini)
# check if debugger is really activated
debug_active = bool((gettrace := getattr(sys, 'gettrace')) and gettrace())
-# warnings are used by some modules, we want to ignore them all in release
-# - see warnings.warn()
-if running_as_frozen or not debug_active:
- import warnings
- warnings.filterwarnings('ignore')
-
-# TODO - check if this is still needed for shapely, apparently shapely now uses only exceptions instead of io.
-# all logs were removed from version 2.0.0 and above
-# ---- plan to remove this in future ----
-# import logging # to set logger for shapely
-# from io import StringIO # to store shapely errors
-# set logger for shapely - for old versions of shapely
-# logger = logging.getLogger('shapely.geos') # attach logger of shapely
-# logger.setLevel(logging.DEBUG)
-# shapely_errors = StringIO() # in memory file to store shapely errors
-# ch = logging.StreamHandler(shapely_errors)
-# ch.setLevel(logging.DEBUG)
-# formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
-# ch.setFormatter(formatter)
-# logger.addHandler(ch)
-# ---- plan to remove this in future ----
+# activate logging for svg
+# we need to import only after possible modification of sys.path, we disable here flake8 E402
+from lib.debug import debug # noqa: E402 # import global variable debug - don't import whole module
+debug.enable() # perhaps it would be better to find a more relevant name; in fact, it's about logging and svg creation.
+
+# log startup info
+debug_logging.startup_info(logger, SCRIPTDIR, running_as_frozen, running_from_inkscape, debug_active, debug_type, profiler_type)
+
+# --------------------------------------------------------------------------------------------
# pop '--extension' from arguments and generate extension class name from extension name
# example: --extension=params will instantiate Params() class from lib.extensions.
@@ -151,7 +172,4 @@ else: # if not debug nor profile mode
finally:
restore_stderr()
- # if shapely_errors.tell(): # see above plan to remove this in future for shapely
- # errormsg(shapely_errors.getvalue())
-
sys.exit(0)