Skip to content

Fix handling of relative paths in CLI

Importing documents with paperwork-cli in the most ovious unix fashion gives a verbose error (see below) because pathlib as_uri needs an absolute path. Something that paperwork used to handle before the refactor at e70bab7b.

This fix forces path resolution before calling as_uri. As a bonus, we get an early failure on non-existent files.

Potential issues:

  • The resolve(strict=False) method accepts a strict argument only for python >= 3.6. Not sure about the backward compatibility policy of paperwork
  • Does anyone see use case for unresolved unix paths ? These use cases would no more be possible.
paperwork-cli import documents/brother_uploads/E79221D0X112471_05202021_221*
[ERROR ] [openpaperwork_core            ] Callback '<function CommonFsPluginBase.fs_safe at 0x7fe7cd7760d0>' failed
[ERROR ] [openpaperwork_core.logs.print ] === UNCAUGHT EXCEPTION ===
Traceback (most recent call last):
  File "/nix/store/a84p56jjjxvwgdpa2d57vsizmzhyr7r9-paperwork-2.0.2/bin/.paperwork-cli-wrapped", line 10, in <module>
    sys.exit(cli_main())
  File "/nix/store/n5lkzjm24ah74i8jx9ma6kss5y79n419-python3.8-paperwork-shell-2.0.2/lib/python3.8/site-packages/paperwork_shell/main.py", line 115, in cli_main
    main_main(
  File "/nix/store/n5lkzjm24ah74i8jx9ma6kss5y79n419-python3.8-paperwork-shell-2.0.2/lib/python3.8/site-packages/paperwork_shell/main.py", line 77, in main_main
    r = core.call_all("cmd_run", args)
  File "/nix/store/h7hxyvvjnsw8xfxzf4c0jkcrkza97asf-python3.8-openpaperwork-core-2.0.2/lib/python3.8/site-packages/openpaperwork_core/__init__.py", line 411, in call_all
    callback(*args, **kwargs)
  File "/nix/store/n5lkzjm24ah74i8jx9ma6kss5y79n419-python3.8-paperwork-shell-2.0.2/lib/python3.8/site-packages/paperwork_shell/cmd/import.py", line 93, in cmd_run
    [self.core.call_success("fs_safe", f) for f in args.files],
  File "/nix/store/n5lkzjm24ah74i8jx9ma6kss5y79n419-python3.8-paperwork-shell-2.0.2/lib/python3.8/site-packages/paperwork_shell/cmd/import.py", line 93, in <listcomp>
    [self.core.call_success("fs_safe", f) for f in args.files],
  File "/nix/store/h7hxyvvjnsw8xfxzf4c0jkcrkza97asf-python3.8-openpaperwork-core-2.0.2/lib/python3.8/site-packages/openpaperwork_core/__init__.py", line 510, in call_success
    r = callback(*args, **kwargs)
  File "/nix/store/h7hxyvvjnsw8xfxzf4c0jkcrkza97asf-python3.8-openpaperwork-core-2.0.2/lib/python3.8/site-packages/openpaperwork_core/fs/__init__.py", line 35, in fs_safe
    return pathlib.Path(uri).as_uri()
  File "/nix/store/q6gfck5czr67090pwm53xrdyhpg6bx67-python3-3.8.9/lib/python3.8/pathlib.py", line 747, in as_uri
    raise ValueError("relative path can't be expressed as a file URI")
ValueError: relative path can't be expressed as a file URI

PS: My first attempt at using gitlab online editor for a small change. Here be dragons.

Edited by Jerome Flesch

Merge request reports