C-Accel-CRIPT / sdk-archive

CRIPT Python SDK
MIT License
11 stars 0 forks source link

Improve UniqueNodeError for usability #68

Closed InnocentBug closed 1 year ago

InnocentBug commented 1 year ago

A common situation for my developing with the SDK that, I don't know if a node exists or not.

For example, a script that adds a Material to a collection, might create the collection first, but with the second material that the Collection node exists already. Or citing a paper, a different user might have added the reference already.

For these situations it would be nice to catch the exception and fetch the existing node instead. (Or course, this has to be used with a bit of care to not overwrite or use false duplicate nodes.)

So this would be ideal:

try:
       ref = cript.Reference(title="Title, doi="10.1039/D2ME00137C",  authors=["Schneider, Ludwig"])
except  cript.data_model.exceptions.UniqueNodeError as exc:
      ref = cript.Reference.get(exc.existing_url)

This requires that the UniqueNodeError transparently makes the found url accessible. In this PR, I simply parse the URL from the message and make it available.

As a precaution for changing message, I issue warnings if no such URL can be found.

InnocentBug commented 1 year ago

I also have false positive file changes, that black detected in the CodeBase.

We should really have an auto-formatter for this repo to avoid this.

Here it was black.

We could setup a pre-commit bot. Or even, better, I recommend trunk.io.

Trunk is free for open-source, and I could set it up for us if desired.