Skip to content

Commit

Permalink
Fix page size auto-detect failing when using write with default HPG…
Browse files Browse the repository at this point in the history
…L device (#328)
  • Loading branch information
abey79 authored Oct 31, 2021
1 parent 5f3fd97 commit ce8026b
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 144 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ New features and improvements:
* The `read` command now accepts `-` as file path to read from the standard input (#322)

Bug fixes:
* ...
* Fixed issue with HPGL export where page size auto-detection would fail when using the default device from the config file (instead of specifying the device with `--device`) (#328)


#### 1.7 (2021-06-10)
Expand Down
309 changes: 175 additions & 134 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pnoise = "^0.1.0"
Shapely = {extras = ["vectorized"], version = "~1.7.1"}
scipy = "^1.6"
setuptools = "^51.0.0"
svgelements = "1.5.4"
svgelements = "1.6.3"
svgwrite = "~1.4"
toml = "~0.10.2"

Expand Down
37 changes: 37 additions & 0 deletions tests/test_hpgl.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@ def test_hpgl_paper_size_inference(runner):
)


def test_hpgl_paper_size_inference_default_device(runner, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text("[command.write]\ndefault_hpgl_device = 'hp7475a'\n")

res = runner.invoke(
cli, f"-c '{config_file}' rect 5cm 5cm 5cm 5cm pagesize a4 write --absolute -f hpgl -"
)

assert res.exit_code == 0
assert res.stdout.strip() == (
"IN;DF;PS4;SP1;PU1608,1849;PD3617,1849,3617,3859,1608,3859,1608,1849;PU11040,7721;"
"SP0;IN;"
)


def test_hpgl_paper_size_inference_fail(runner):
res = runner.invoke(cli, "rect 5cm 5cm 5cm 5cm pagesize a6 write -f hpgl -d hp7475a -")

Expand Down Expand Up @@ -350,3 +365,25 @@ def test_hpgl_wrong_ref(simple_printer_config):
page_size="wrong_ref",
velocity=None,
)


def test_hpgl_config_manager(config_manager, simple_printer_config, config_file_factory):
config_manager.load_config_file(simple_printer_config)

# WITHOUT DEFAULT CONFIG
assert config_manager.get_plotter_config("simple") is not None
assert config_manager.get_plotter_config(None) is None
assert config_manager.get_plotter_config("doesntexist") is None

# WITH DEFAULT CONFIG
default_device_config = config_file_factory(
"""
[command.write]
default_hpgl_device = "simple"
"""
)
config_manager.load_config_file(default_device_config)

assert config_manager.get_plotter_config("simple") is not None
assert config_manager.get_plotter_config(None) is not None
assert config_manager.get_plotter_config("doesntexist") is None
16 changes: 11 additions & 5 deletions vpype/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,24 @@ def get_plotter_list(self) -> List[str]:
"""
return list(self.config.get("device", {}).keys())

def get_plotter_config(self, name: Optional[str]) -> Optional[PlotterConfig]:
"""Returns a :class:`PlotterConfig` instance for plotter ``name``.
def get_plotter_config(self, device: Optional[str]) -> Optional[PlotterConfig]:
"""Returns a :class:`PlotterConfig` instance for plotter ``device``.
If ``None`` is passed, this function attempts to use the default device if one is
configured, or the function returns None.
Args:
name: name of desired plotter (may be ``None``, in which case ``None`` is returned)
device: name of desired plotter (or ``None`` to use the default device if
configured)
Returns:
:class:`PlotterConfig` instance or None if not found
"""
devices = self.config.get("device", {})
if name in devices:
return PlotterConfig.from_config(devices[name])
if device is None:
device = self.get_command_config("write").get("default_hpgl_device", None)
if device in devices:
return PlotterConfig.from_config(devices[device])
else:
return None

Expand Down
4 changes: 1 addition & 3 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,7 @@ def write_svg(
def _get_hpgl_config(
device: Optional[str], page_size: str
) -> Tuple[PlotterConfig, PaperConfig]:
if device is None:
device = config_manager.get_command_config("write").get("default_hpgl_device", None)
plotter_config = config_manager.get_plotter_config(str(device))
plotter_config = config_manager.get_plotter_config(device)
if plotter_config is None:
raise ValueError(f"no configuration available for plotter '{device}'")
paper_config = plotter_config.paper_config(page_size)
Expand Down

0 comments on commit ce8026b

Please sign in to comment.