From 64062f7cd7eee9ed5701209618b0564a211c494b Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 2 Aug 2018 20:04:08 -0400 Subject: WIP --- lib/commands.py | 79 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 22 deletions(-) (limited to 'lib/commands.py') diff --git a/lib/commands.py b/lib/commands.py index 02c13b25..74cf32b8 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -5,28 +5,33 @@ from .svg import apply_transforms from .svg.tags import SVG_USE_TAG, SVG_SYMBOL_TAG, CONNECTION_START, CONNECTION_END, XLINK_HREF -class Command(object): - def __init__(self, connector): - self.connector = connector - self.svg = self.connector.getroottree().getroot() +class CommandParseError(Exception): + pass - self.parse_command() - def get_node_by_url(self, url): - # url will be #path12345. Find the object at the other end. +def get_node_by_url(svg, url): + # url will be #path12345. Find the object at the other end. - if url is None: - raise ValueError("url is None") + if url is None: + raise CommandParseError("url is None") - if not url.startswith('#'): - raise ValueError("invalid connection url: %s" % url) + if not url.startswith('#'): + raise CommandParseError("invalid connection url: %s" % url) - id = url[1:] + id = url[1:] - try: - return self.svg.xpath(".//*[@id='%s']" % id)[0] - except (IndexError, AttributeError): - raise ValueError("could not find node by url %s" % id) + try: + return svg.xpath(".//*[@id='%s']" % id)[0] + except (IndexError, AttributeError): + raise CommandParseError("could not find node by url %s" % id) + + +class Command(object): + def __init__(self, connector): + self.connector = connector + self.svg = self.connector.getroottree().getroot() + + self.parse_command() def parse_connector_path(self): path = cubicsuperpath.parsePath(self.connector.get('d')) @@ -36,27 +41,27 @@ class Command(object): path = self.parse_connector_path() neighbors = [ - (self.get_node_by_url(self.connector.get(CONNECTION_START)), path[0][0][1]), - (self.get_node_by_url(self.connector.get(CONNECTION_END)), path[0][-1][1]) + (get_node_by_url(self.svg, self.connector.get(CONNECTION_START)), path[0][0][1]), + (get_node_by_url(self.svg, self.connector.get(CONNECTION_END)), path[0][-1][1]) ] if neighbors[0][0].tag != SVG_USE_TAG: neighbors.reverse() if neighbors[0][0].tag != SVG_USE_TAG: - raise ValueError("connector does not point to a use tag") + raise CommandParseError("connector does not point to a use tag") - self.symbol = self.get_node_by_url(neighbors[0][0].get(XLINK_HREF)) + self.symbol = self.get_node_by_url(self.svg, neighbors[0][0].get(XLINK_HREF)) if self.symbol.tag != SVG_SYMBOL_TAG: - raise ValueError("use points to non-symbol") + raise CommandParseError("use points to non-symbol") self.command = self.symbol.get('id') if self.command.startswith('inkstitch_'): self.command = self.command[10:] else: - raise ValueError("symbol is not an Ink/Stitch command") + raise CommandParseError("symbol is not an Ink/Stitch command") self.target = neighbors[1][0] self.target_point = neighbors[1][1] @@ -64,6 +69,17 @@ class Command(object): def __repr__(self): return "Command('%s', %s)" % (self.command, self.target_point) +class StandaloneCommand(object): + def __init__(self, symbol): + self.symbol = symbol + self.svg = self.connector.getroottree().getroot() + + self.parse_command() + + def parse_command(self): + pass + + def find_commands(node): """Find the symbols this node is connected to and return them as Commands""" @@ -82,5 +98,24 @@ def find_commands(node): return commands +def layer_commands(layer, command): + """Find standalone (unconnected) command symbols in this layer.""" + pass + +def global_commands(svg): + """Find all unconnected command symbols in the SVG.""" + + xpath = ".//svg:use[starts-with(@xlink:href, '#inkstitch_')]" + symbols = svg.xpath(xpath, namespace=inkex.NSS) + + commands = [] + for symbol in symbols: + try: + commands.append(StandaloneCommand(symbol)) + except CommandParseError: + pass + + return commands + def is_command(node): return CONNECTION_START in node.attrib or CONNECTION_END in node.attrib -- cgit v1.2.3 From 751173080bbc87158619cd98d75c9854ce2d8d01 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 16 Aug 2018 20:30:37 -0400 Subject: add get_layer_commands() --- lib/commands.py | 82 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 33 deletions(-) (limited to 'lib/commands.py') diff --git a/lib/commands.py b/lib/commands.py index 74cf32b8..a9f156e4 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -9,24 +9,35 @@ class CommandParseError(Exception): pass -def get_node_by_url(svg, url): - # url will be #path12345. Find the object at the other end. +class BaseCommand(object): + def parse_symbol(self): + if self.symbol.tag != SVG_SYMBOL_TAG: + raise CommandParseError("use points to non-symbol") - if url is None: - raise CommandParseError("url is None") + self.command = self.symbol.get('id') - if not url.startswith('#'): - raise CommandParseError("invalid connection url: %s" % url) + if self.command.startswith('inkstitch_'): + self.command = self.command[10:] + else: + raise CommandParseError("symbol is not an Ink/Stitch command") - id = url[1:] + def get_node_by_url(self,url): + # url will be #path12345. Find the corresponding object. + if url is None: + raise CommandParseError("url is None") - try: - return svg.xpath(".//*[@id='%s']" % id)[0] - except (IndexError, AttributeError): - raise CommandParseError("could not find node by url %s" % id) + if not url.startswith('#'): + raise CommandParseError("invalid connection url: %s" % url) + id = url[1:] + + try: + return self.svg.xpath(".//*[@id='%s']" % id)[0] + except (IndexError, AttributeError): + raise CommandParseError("could not find node by url %s" % id) -class Command(object): + +class Command(BaseCommand): def __init__(self, connector): self.connector = connector self.svg = self.connector.getroottree().getroot() @@ -41,8 +52,8 @@ class Command(object): path = self.parse_connector_path() neighbors = [ - (get_node_by_url(self.svg, self.connector.get(CONNECTION_START)), path[0][0][1]), - (get_node_by_url(self.svg, self.connector.get(CONNECTION_END)), path[0][-1][1]) + (self.get_node_by_url(self.connector.get(CONNECTION_START)), path[0][0][1]), + (self.get_node_by_url(self.connector.get(CONNECTION_END)), path[0][-1][1]) ] if neighbors[0][0].tag != SVG_USE_TAG: @@ -51,17 +62,8 @@ class Command(object): if neighbors[0][0].tag != SVG_USE_TAG: raise CommandParseError("connector does not point to a use tag") - self.symbol = self.get_node_by_url(self.svg, neighbors[0][0].get(XLINK_HREF)) - - if self.symbol.tag != SVG_SYMBOL_TAG: - raise CommandParseError("use points to non-symbol") - - self.command = self.symbol.get('id') - - if self.command.startswith('inkstitch_'): - self.command = self.command[10:] - else: - raise CommandParseError("symbol is not an Ink/Stitch command") + self.symbol = self.get_node_by_url(neighbors[0][0].get(XLINK_HREF)) + self.parse_symbol() self.target = neighbors[1][0] self.target_point = neighbors[1][1] @@ -69,15 +71,21 @@ class Command(object): def __repr__(self): return "Command('%s', %s)" % (self.command, self.target_point) -class StandaloneCommand(object): - def __init__(self, symbol): - self.symbol = symbol - self.svg = self.connector.getroottree().getroot() + +class StandaloneCommand(BaseCommand): + def __init__(self, use): + self.node = use + self.svg = self.use.getroottree().getroot() self.parse_command() def parse_command(self): - pass + self.symbol = self.get_node_by_url(self.node.get(XLINK_HREF)) + + if self.symbol.tag != SVG_SYMBOL_TAG: + raise CommandParseError("use points to non-symbol") + + self.parse_symbol() def find_commands(node): @@ -92,7 +100,7 @@ def find_commands(node): for connector in connectors: try: commands.append(Command(connector)) - except ValueError: + except CommandParseError: # Parsing the connector failed, meaning it's not actually an Ink/Stitch command. pass @@ -100,9 +108,17 @@ def find_commands(node): def layer_commands(layer, command): """Find standalone (unconnected) command symbols in this layer.""" - pass -def global_commands(svg): + commands = [] + + for standalone_command in standalone_commands(layer.getroottree().getroot()): + if standalone_command.command == command: + if layer in command.iterancestors(): + commands.append(command) + + return commands + +def standalone_commands(svg): """Find all unconnected command symbols in the SVG.""" xpath = ".//svg:use[starts-with(@xlink:href, '#inkstitch_')]" -- cgit v1.2.3 From 19bb8a5a6dbe4244a590147187949d0555eb9fbc Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 16 Aug 2018 22:50:34 -0400 Subject: new extension to add layer commands --- lib/commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/commands.py') diff --git a/lib/commands.py b/lib/commands.py index a9f156e4..cadfa080 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -75,7 +75,7 @@ class Command(BaseCommand): class StandaloneCommand(BaseCommand): def __init__(self, use): self.node = use - self.svg = self.use.getroottree().getroot() + self.svg = self.node.getroottree().getroot() self.parse_command() @@ -113,7 +113,7 @@ def layer_commands(layer, command): for standalone_command in standalone_commands(layer.getroottree().getroot()): if standalone_command.command == command: - if layer in command.iterancestors(): + if layer in standalone_command.node.iterancestors(): commands.append(command) return commands @@ -122,7 +122,7 @@ def standalone_commands(svg): """Find all unconnected command symbols in the SVG.""" xpath = ".//svg:use[starts-with(@xlink:href, '#inkstitch_')]" - symbols = svg.xpath(xpath, namespace=inkex.NSS) + symbols = svg.xpath(xpath, namespaces=inkex.NSS) commands = [] for symbol in symbols: -- cgit v1.2.3