2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2019-03-28 19:52:37 +00:00
|
|
|
import atexit
|
2019-03-28 18:47:05 +00:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import time
|
2021-03-04 17:40:53 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from datetime import datetime
|
2019-03-28 18:47:05 +00:00
|
|
|
|
2019-03-28 19:52:37 +00:00
|
|
|
import inkex
|
2021-03-04 17:40:53 +00:00
|
|
|
from lxml import etree
|
2019-03-28 19:52:37 +00:00
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
from .svg import line_strings_to_path
|
|
|
|
from .svg.tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL
|
2019-03-28 19:52:37 +00:00
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
def check_enabled(func):
|
|
|
|
def decorated(self, *args, **kwargs):
|
|
|
|
if self.enabled:
|
|
|
|
func(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return decorated
|
|
|
|
|
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
class Debug(object):
|
|
|
|
def __init__(self):
|
2019-03-29 19:03:28 +00:00
|
|
|
self.enabled = False
|
2019-03-28 18:47:05 +00:00
|
|
|
self.last_log_time = None
|
2019-03-28 19:52:37 +00:00
|
|
|
self.current_layer = None
|
2019-03-29 19:03:28 +00:00
|
|
|
self.group_stack = []
|
2019-03-28 18:47:05 +00:00
|
|
|
|
|
|
|
def enable(self):
|
2019-03-29 19:03:28 +00:00
|
|
|
self.enabled = True
|
|
|
|
self.init_log()
|
|
|
|
self.init_debugger()
|
|
|
|
self.init_svg()
|
2019-03-28 18:47:05 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
def init_log(self):
|
2021-03-04 17:40:53 +00:00
|
|
|
self.log_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.log")
|
|
|
|
# delete old content
|
|
|
|
with open(self.log_file, "w"):
|
|
|
|
pass
|
2019-03-28 18:47:05 +00:00
|
|
|
self.log("Debug logging enabled.")
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
def init_debugger(self):
|
2023-02-16 04:11:13 +00:00
|
|
|
# How to debug Ink/Stitch with LiClipse:
|
2019-03-28 18:47:05 +00:00
|
|
|
#
|
|
|
|
# 1. Install LiClipse (liclipse.com) -- no need to install Eclipse first
|
|
|
|
# 2. Start debug server as described here: http://www.pydev.org/manual_adv_remote_debugger.html
|
|
|
|
# * follow the "Note:" to enable the debug server menu item
|
|
|
|
# 3. Create a file named "DEBUG" next to inkstitch.py in your git clone.
|
|
|
|
# 4. Run any extension and PyDev will start debugging.
|
|
|
|
|
2023-02-16 04:11:13 +00:00
|
|
|
###
|
|
|
|
|
|
|
|
# To debug with PyCharm:
|
|
|
|
|
2023-02-16 13:00:12 +00:00
|
|
|
# You must use the PyCharm Professional Edition and _not_ the Community
|
|
|
|
# Edition. Jetbrains has chosen to make remote debugging a Pro feature.
|
|
|
|
# To debug Inkscape python extensions, the extension code and Inkscape run
|
|
|
|
# independently of PyCharm, and only communicate with the debugger via a
|
|
|
|
# TCP socket. Thus, debugging is "remote," even if it's on the same machine,
|
|
|
|
# connected via the loopback interface.
|
2023-02-16 04:11:13 +00:00
|
|
|
#
|
|
|
|
# 1. pip install pydev_pycharm
|
|
|
|
#
|
|
|
|
# pydev_pycharm is versioned frequently. Jetbrains suggests installing
|
|
|
|
# a version at least compatible with the current build. For example, if your
|
|
|
|
# PyCharm build, as found in menu PyCharm -> About Pycharm is 223.8617.48,
|
|
|
|
# you could do:
|
|
|
|
# pip install pydevd-pycharm~=223.8617.48
|
|
|
|
#
|
2023-02-16 13:00:12 +00:00
|
|
|
# 2. From the Pycharm "Run" menu, choose "Edit Configurations..." and create a new
|
2023-02-16 04:11:13 +00:00
|
|
|
# configuration. Set "IDE host name:" to "localhost" and "Port:" to 5678.
|
|
|
|
# You can leave the default settings for all other choices.
|
|
|
|
#
|
|
|
|
# 3. Touch a file named "DEBUG" at the top of your git repo, as above.
|
|
|
|
#
|
2023-02-16 13:00:12 +00:00
|
|
|
# 4. Create a symbolic link in the Inkscape extensions directory to the
|
2023-02-16 13:02:07 +00:00
|
|
|
# top-level directory of your git repo. On a mac, for example:
|
2023-02-16 13:00:12 +00:00
|
|
|
# cd ~/Library/Application\ Support/org.inkscape.Inkscape/config/inkscape/extensions/
|
2023-02-16 13:02:07 +00:00
|
|
|
# ln -s <full path to the top level of your Ink/Stitch git repo>
|
2023-02-16 13:00:12 +00:00
|
|
|
# On other architectures it may be:
|
|
|
|
# cd ~/.config/inkscape/extensions
|
2023-02-16 13:02:07 +00:00
|
|
|
# ln -s <full path to the top level of your Ink/Stitch git repo>
|
2023-02-16 13:00:12 +00:00
|
|
|
# Remove any other Ink/Stitch files or references to Ink/Stitch from the
|
|
|
|
# extensions directory, or you'll see duplicate entries in the Ink/Stitch
|
|
|
|
# extensions menu in Inkscape.
|
|
|
|
#
|
|
|
|
# 5. In the execution env for Inkscape, set the environment variable
|
2023-02-16 04:11:13 +00:00
|
|
|
# PYCHARM_REMOTE_DEBUG to any value, and launch Inkscape. If you're starting
|
|
|
|
# Inkscape from the PyCharm Terminal pane, you can do:
|
|
|
|
# export PYCHARM_REMOTE_DEBUG=true;inkscape
|
|
|
|
#
|
2023-02-16 13:05:38 +00:00
|
|
|
# 6. In Pycharm, either click on the green "bug" icon if visible in the upper
|
|
|
|
# right or press Ctrl-D to start debugging.The PyCharm debugger pane will
|
|
|
|
# display the message "Waiting for process connection..."
|
2023-02-16 04:11:13 +00:00
|
|
|
#
|
2023-02-16 13:00:12 +00:00
|
|
|
# 7. Do some action in Inkscape which invokes Ink/Stitch extension code, and the
|
2023-02-16 04:11:13 +00:00
|
|
|
# debugger will be triggered. If you've left "Suspend after connect" checked
|
|
|
|
# in the Run configuration, PyCharm will pause in the "self.log("Enabled
|
|
|
|
# PyDev debugger.)" statement, below. Uncheck the box to have it continue
|
|
|
|
# automatically to your first set breakpoint.
|
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
try:
|
2023-02-16 04:11:13 +00:00
|
|
|
if 'PYCHARM_REMOTE_DEBUG' in os.environ:
|
|
|
|
import pydevd_pycharm
|
|
|
|
else:
|
|
|
|
import pydevd
|
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
except ImportError:
|
|
|
|
self.log("importing pydevd failed (debugger disabled)")
|
|
|
|
|
|
|
|
# pydevd likes to shout about errors to stderr whether I want it to or not
|
|
|
|
with open(os.devnull, 'w') as devnull:
|
|
|
|
stderr = sys.stderr
|
|
|
|
sys.stderr = devnull
|
|
|
|
|
|
|
|
try:
|
2023-02-16 04:11:13 +00:00
|
|
|
if 'PYCHARM_REMOTE_DEBUG' in os.environ:
|
|
|
|
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True,
|
|
|
|
stderrToServer=True)
|
|
|
|
else:
|
|
|
|
pydevd.settrace()
|
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
except socket.error as error:
|
2019-03-28 18:47:05 +00:00
|
|
|
self.log("Debugging: connection to pydevd failed: %s", error)
|
|
|
|
self.log("Be sure to run 'Start debugging server' in PyDev to enable debugging.")
|
|
|
|
else:
|
|
|
|
self.log("Enabled PyDev debugger.")
|
|
|
|
|
|
|
|
sys.stderr = stderr
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
def init_svg(self):
|
2019-03-28 19:52:37 +00:00
|
|
|
self.svg = etree.Element("svg", nsmap=inkex.NSS)
|
|
|
|
atexit.register(self.save_svg)
|
|
|
|
|
|
|
|
def save_svg(self):
|
|
|
|
tree = etree.ElementTree(self.svg)
|
2021-03-04 17:40:53 +00:00
|
|
|
debug_svg = os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.svg")
|
|
|
|
tree.write(debug_svg)
|
2019-03-28 19:52:37 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
2019-03-28 19:52:37 +00:00
|
|
|
def add_layer(self, name="Debug"):
|
|
|
|
layer = etree.Element("g", {
|
|
|
|
INKSCAPE_GROUPMODE: "layer",
|
2019-03-29 19:42:11 +00:00
|
|
|
INKSCAPE_LABEL: name,
|
|
|
|
"style": "display: none"
|
2019-03-28 19:52:37 +00:00
|
|
|
})
|
|
|
|
self.svg.append(layer)
|
|
|
|
self.current_layer = layer
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def open_group(self, name="Group"):
|
|
|
|
group = etree.Element("g", {
|
|
|
|
INKSCAPE_LABEL: name
|
|
|
|
})
|
2019-03-28 18:47:05 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
self.log_svg_element(group)
|
|
|
|
self.group_stack.append(group)
|
2019-03-28 18:47:05 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def close_group(self):
|
|
|
|
if self.group_stack:
|
|
|
|
self.group_stack.pop()
|
|
|
|
|
|
|
|
@check_enabled
|
|
|
|
def log(self, message, *args):
|
2019-03-28 18:47:05 +00:00
|
|
|
if self.last_log_time:
|
|
|
|
message = "(+%s) %s" % (datetime.now() - self.last_log_time, message)
|
|
|
|
|
|
|
|
self.raw_log(message, *args)
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
def raw_log(self, message, *args):
|
2019-03-28 18:47:05 +00:00
|
|
|
now = datetime.now()
|
|
|
|
timestamp = now.isoformat()
|
|
|
|
self.last_log_time = now
|
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
with open(self.log_file, "a") as logfile:
|
|
|
|
print(timestamp, message % args, file=logfile)
|
|
|
|
logfile.flush()
|
2019-03-28 18:47:05 +00:00
|
|
|
|
|
|
|
def time(self, func):
|
|
|
|
def decorated(*args, **kwargs):
|
2019-03-29 19:03:28 +00:00
|
|
|
if self.enabled:
|
2021-03-04 17:40:53 +00:00
|
|
|
self.raw_log("entering %s()", func.__name__)
|
2019-03-29 19:03:28 +00:00
|
|
|
start = time.time()
|
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
result = func(*args, **kwargs)
|
2019-03-29 19:03:28 +00:00
|
|
|
|
|
|
|
if self.enabled:
|
|
|
|
end = time.time()
|
2021-03-04 17:40:53 +00:00
|
|
|
self.raw_log("leaving %s(), duration = %s", func.__name__, round(end - start, 6))
|
2019-03-29 19:03:28 +00:00
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
return decorated
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
2019-03-28 19:52:37 +00:00
|
|
|
def log_svg_element(self, element):
|
|
|
|
if self.current_layer is None:
|
|
|
|
self.add_layer()
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
if self.group_stack:
|
|
|
|
self.group_stack[-1].append(element)
|
|
|
|
else:
|
|
|
|
self.current_layer.append(element)
|
2019-03-28 19:52:37 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def log_line_string(self, line_string, name=None, color=None):
|
2019-03-28 19:52:37 +00:00
|
|
|
"""Add a Shapely LineString to the SVG log."""
|
2019-03-29 19:03:28 +00:00
|
|
|
self.log_line_strings([line_string], name, color)
|
2019-03-28 19:52:37 +00:00
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def log_line_strings(self, line_strings, name=None, color=None):
|
2019-03-28 19:52:37 +00:00
|
|
|
path = line_strings_to_path(line_strings)
|
2022-06-22 14:11:12 +00:00
|
|
|
path.set('style', str(inkex.Style({"stroke": color or "#000000", "stroke-width": "0.3", "fill": None})))
|
2019-03-29 19:03:28 +00:00
|
|
|
|
|
|
|
if name is not None:
|
|
|
|
path.set(INKSCAPE_LABEL, name)
|
|
|
|
|
2019-03-28 19:52:37 +00:00
|
|
|
self.log_svg_element(path)
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def log_line(self, start, end, name="line", color=None):
|
|
|
|
self.log_svg_element(etree.Element("path", {
|
|
|
|
"d": "M%s,%s %s,%s" % (start + end),
|
2022-06-22 14:11:12 +00:00
|
|
|
"style": str(inkex.Style({"stroke": color or "#000000", "stroke-width": "0.3", "fill": None})),
|
2019-03-29 19:03:28 +00:00
|
|
|
INKSCAPE_LABEL: name
|
|
|
|
}))
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
@check_enabled
|
|
|
|
def log_point(self, point, name="point", color=None):
|
|
|
|
self.log_svg_element(etree.Element("circle", {
|
|
|
|
"cx": str(point.x),
|
|
|
|
"cy": str(point.y),
|
|
|
|
"r": "1",
|
|
|
|
"style": str(inkex.Style({"fill": "#000000"})),
|
|
|
|
}))
|
|
|
|
|
2019-03-29 19:03:28 +00:00
|
|
|
@check_enabled
|
|
|
|
def log_graph(self, graph, name="Graph", color=None):
|
2019-04-04 23:57:40 +00:00
|
|
|
d = ""
|
2019-03-29 19:03:28 +00:00
|
|
|
|
|
|
|
for edge in graph.edges:
|
2019-04-04 23:57:40 +00:00
|
|
|
d += "M%s,%s %s,%s" % (edge[0] + edge[1])
|
2019-03-29 19:03:28 +00:00
|
|
|
|
2019-04-04 23:57:40 +00:00
|
|
|
self.log_svg_element(etree.Element("path", {
|
|
|
|
"d": d,
|
2022-06-22 14:11:12 +00:00
|
|
|
"style": str(inkex.Style({"stroke": color or "#000000", "stroke-width": "0.3", "fill": None})),
|
2019-04-04 23:57:40 +00:00
|
|
|
INKSCAPE_LABEL: name
|
|
|
|
}))
|
2019-03-29 19:03:28 +00:00
|
|
|
|
2019-03-29 19:42:11 +00:00
|
|
|
@contextmanager
|
|
|
|
def time_this(self, label="code block"):
|
|
|
|
if self.enabled:
|
|
|
|
start = time.time()
|
|
|
|
self.raw_log("begin %s", label)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
if self.enabled:
|
|
|
|
self.raw_log("completed %s, duration = %s", label, time.time() - start)
|
|
|
|
|
2019-03-28 18:47:05 +00:00
|
|
|
|
|
|
|
debug = Debug()
|
2019-03-29 19:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def enable():
|
|
|
|
debug.enable()
|