You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've created a custom Robotframework Resource module (packaged independently using Poetry etc. for distribution) which contains user keywords and resource files. Resource files not only refer to Robotframework Resource files, but any file required by the user keywords internal implementation.
I've followed the guidance provided on https://forum.robotframework.org/t/sharing-common-robot-framework-code/6977/1
In my example, the user keyword wraps an RPC style API where the contract is defined by an IDL file. The API uses binary serialisation and the IDL file is used to generate the codec for use internally.
My first naive approach used the standard Robotframework directory structure, cases, resources etc. The IDL file was referenced in the user keywords using a path relative to ${CURDIR}. This then failed when executed in a Robotframework execution environment which had the module installed.
To work around this, I added a Python utility to my Resource project, to read the file from a module on the Python Path.
This has the advantage of not relying on a search of the path that may result in a file with the same name being found elsewhere.
Conveniently, using the importlib, allowed support for different distribution, installation scenarios, e.g. downloads from pypi repo, install via *.tar.gz etc.
I've found #3317 but there's no progress on this yet.
Given the above, it may be useful to add a keyword similar to Get File from OperationSystem, to do the equivalent from a file in a module.
Thanks.
The text was updated successfully, but these errors were encountered:
Sounds reasonable. Do you @robinmackaij have opinions?
If we decide to add this, we have various ways to do that:
Add a new keyword or keywords (there probably should be a separate keyword for binary files) to OperatingSystem.
Add a module argument to the existing Get File and Get Binary File keywords.
Enhance Get File and Get Binary Fileso that the path they accept can contain both module and path, for example, like module:path/file.txt.
The last approach would have a benefit that the same syntax could be used also with other keywords without introducing a new argument. It can be too magical, though, and it's also not totally safe because on Unix paths can contain : or any other such separator.
Seems like a good addition. I recognize the issue with shared keywords taking file names as arguments and then having to deal with relative paths within the module. That can be made to work by using the local ${CURDIR} but a cleaner / more flexible solution would be nice. Using importlib.resources would work for this.
My preference would be to go with option 2 since that'd prevent having very similar keywords (i.e. Get File and Get File From Module with the same signatures, except the module argument). I would say option 3 is on the "too magical" side and it not being natively safe doesn't help it's case.
Looking more broadly
Going through the methods available on importlib.resources also gives some interesting hooks for keywords to discover / show what's inside a "Robot Framework keywords distribution". Since these are not Libraries, a user has to know the package name but that's not enough; they also must know the names of the resource files made available by the package to be able to use them (including possible subfolder structures). Perhaps the importlib.resources methods can be used in the API / LSP for resource discovery of resources installed as Python packages.
To illustrate I'll describe my scenario.
I've created a custom Robotframework Resource module (packaged independently using Poetry etc. for distribution) which contains user keywords and resource files. Resource files not only refer to Robotframework
Resource
files, but any file required by the user keywords internal implementation.I've followed the guidance provided on https://forum.robotframework.org/t/sharing-common-robot-framework-code/6977/1
In my example, the user keyword wraps an RPC style API where the contract is defined by an IDL file. The API uses binary serialisation and the IDL file is used to generate the codec for use internally.
My first naive approach used the standard Robotframework directory structure, cases, resources etc. The IDL file was referenced in the user keywords using a path relative to ${CURDIR}. This then failed when executed in a Robotframework execution environment which had the module installed.
To work around this, I added a Python utility to my Resource project, to read the file from a module on the Python Path.
Disclaimer: I'm a Python novice ;-)
This utility was then used in the user keyword.
This has the advantage of not relying on a search of the path that may result in a file with the same name being found elsewhere.
Conveniently, using the
importlib
, allowed support for different distribution, installation scenarios, e.g. downloads from pypi repo, install via *.tar.gz etc.I've found #3317 but there's no progress on this yet.
Given the above, it may be useful to add a keyword similar to
Get File
fromOperationSystem
, to do the equivalent from a file in a module.Thanks.
The text was updated successfully, but these errors were encountered: