Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to read from stdin #322

Merged
merged 3 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ New features and improvements:
* Relative coordinates are now used by default to reduce file size. If absolute coordinates are needed, they a new `--absolute` option for the `write` command.
* A homing command (as defined by the `final_pu_params` configuration parameter) is no longer emitted between layers.
* The viewer (`show` command) now catches interruptions from the terminal (ctrl-C) and closes itself (#321)
* The `read` command now accepts `-` as file path to read from the standard input (#322)

Bug fixes:
* ...
Expand Down
17 changes: 16 additions & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

import vpype as vp
from vpype_cli import cli
from vpype_cli import DebugData, cli

from .utils import TEST_FILE_DIRECTORY

Expand Down Expand Up @@ -150,3 +150,18 @@ def test_read_with_viewbox(tmp_path):
assert height == 100
assert len(lc) == 1
assert np.all(np.isclose(lc[0], np.array([0, 100 + 100j])))


def test_read_stdin(runner):
svg = f"""<?xml version="1.0"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"
width="1000" height="1000">
<circle cx="500" cy="500" r="40"/>
</svg>
"""

result = runner.invoke(cli, "read - dbsample dbdump", input=svg)
data = DebugData.load(result.output)

assert result.exit_code == 0
assert data[0].count == 1
12 changes: 6 additions & 6 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _extract_paths(group: svgelements.Group, recursive) -> _PathListType:


def read_svg(
filename: str,
file: Union[str, TextIO],
quantization: float,
crop: bool = True,
simplify: bool = False,
Expand All @@ -215,7 +215,7 @@ def read_svg(
as tolerance.

Args:
filename: path of the SVG file
file: path of the SVG file or stream object
quantization: maximum size of segment used to approximate curved geometries
crop: crop the geometries to the SVG boundaries
simplify: run Shapely's simplify on loaded geometry
Expand All @@ -231,7 +231,7 @@ def read_svg(
"""

# default width is for SVG with % width/height
svg = svgelements.SVG.parse(filename, width=default_width, height=default_height)
svg = svgelements.SVG.parse(file, width=default_width, height=default_height)
paths = _extract_paths(svg, recursive=True)
lc = _convert_flattened_paths(paths, quantization, simplify, parallel)

Expand All @@ -242,7 +242,7 @@ def read_svg(


def read_multilayer_svg(
filename: str,
file: Union[str, TextIO],
quantization: float,
crop: bool = True,
simplify: bool = False,
Expand All @@ -266,7 +266,7 @@ def read_multilayer_svg(
as tolerance.

Args:
filename: path of the SVG file
file: path of the SVG file or stream object
quantization: maximum size of segment used to approximate curved geometries
crop: crop the geometries to the SVG boundaries
simplify: run Shapely's simplify on loaded geometry
Expand All @@ -281,7 +281,7 @@ def read_multilayer_svg(
SVG dimensions
"""

svg = svgelements.SVG.parse(filename, width=default_width, height=default_height)
svg = svgelements.SVG.parse(file, width=default_width, height=default_height)

document = Document()

Expand Down
11 changes: 8 additions & 3 deletions vpype_cli/read.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
from typing import Optional, Tuple, cast
import sys
from typing import Optional, Tuple

import click

from vpype import (
Document,
LayerType,
LengthType,
LineCollection,
PageSizeType,
global_processor,
read_multilayer_svg,
Expand All @@ -21,7 +21,7 @@


@cli.command(group="Input")
@click.argument("file", type=click.Path(exists=True, dir_okay=False))
@click.argument("file", type=click.Path(exists=True, dir_okay=False, allow_dash=True))
@click.option("-m", "--single-layer", is_flag=True, help="Single layer mode.")
@click.option(
"-l",
Expand Down Expand Up @@ -89,6 +89,8 @@ def read(
) -> Document:
"""Extract geometries from a SVG file.

FILE may be a file path path or a dash (-) to read from the standard input instead.

By default, the `read` command attempts to preserve the layer structure of the SVG. In this
context, top-level groups (<svg:g>) are each considered a layer. If any, all non-group,
top-level SVG elements are imported into layer 1.
Expand Down Expand Up @@ -162,6 +164,9 @@ def read(
if display_landscape:
width, height = height, width

if file == "-":
file = sys.stdin

if single_layer:
lc, width, height = read_svg(
file,
Expand Down