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
{{ message }}
Repository navigation
Stable API for getting library and resource file info #3448
We need a stable API to easily import libraries and resource files to get information about keywords they provide. Such an API would be useful for external tools like the new LSP server (ping @fabioz).
Robot Framework obviously already has internal APIs to import libraries (e.g. robot.running.TestLibrary ) and resource files. These APIs are used by the built-in Libdoc tool and it probably would be a good idea to publicly expose the Libdoc APIs instead of the lower level APIs.
Whatever entry points we want to make public, we should expose explicitly via the robot.api package.
A simple solution would be just exposing the current robot.libdocpkg.LibraryDocumentation factory method via robot.api. Open questions related to it:
Is this enough?
Should there be separate entry points to get library and resource info? Is having just one that automatically detects is it a library or a resource file convenient or confusing? One handy thing is that it also automatically handles the spec files Libdoc can generate.
I was playing with LibraryDocumentation and it seems to work pretty well:
>>>fromrobot.libdocpkgimportLibraryDocumentation>>>doc=LibraryDocumentation('SeleniumLibrary')
>>>doc.name'SeleniumLibrary'>>>doc.doc.splitlines()[0]
'SeleniumLibrary is a web testing library for Robot Framework.'>>>doc.keywords[0].name'Add Cookie'>>>doc.keywords[0].shortdoc'Adds a cookie to your current session.'>>>doc.keywords[0].args
['name', 'value', 'path=None', 'domain=None', 'secure=None', 'expiry=None']
>>>
Can you @fabioz comment would the above be enough for LSP? If it is, then I'd do the following enhancement here on Robot side:
Expose LibraryDocumentation via robot.api.
Add documentation to it. There's currently none.
Simplify importing libraries that require arguments. At the moment arguments need to be embedded to the name like in SeleniumLibrary::10s which isn't too convenient. Separate args argument would be better.
One very handy LibraryDocumentation feature, especially in the editor context, is its ability to read information from "Libdoc spec files". If, for example, I create such a spec file for SeleniumLibrary like:
Another highly related thing that would probably be useful in the editor/LSP domain is detecting does a certain library implement a keyword or not. Matching normal keyword names used in data to keywords provided by libraries is pretty simple, but embedded arguments make that a bit more complicated. It would be a lot easier if the LibraryDoc instance returned by the LibraryDocumentation factory method would directly support something like this:
Even better if there would also be a way to check is the keyword called with valid arguments. The same method could optionally do that as well, but perhaps it would better to have a dedicated method instead:
We have all this logic inside Robot Framework, but it is not exposed via any public API and there's a big risk that the related code will change in future versions. Having a stable (and tested) API would help because it could be fixed if there are lower level changes.
Sorry for not answering sooner here... the truth is, I still don't know how much I'll be able to reuse it and I'll have to check it more in-depth before answering (my plan is evaluating this as my first step when I get to start implementing code-completion).
My main concerns is that there are some requisites a language server has that may not be directly there...
Some examples:
it must be able to deal with dirty buffers and not just deal with files in the filesystem (for instance, if the user has 2 files opened, one being a library and the other not, if the user changes the library file to add some keywords, asking for code-completion in a different file should provide such keywords -- the same goes for code analysis).
is that API fast enough to be used interactively in big projects? This usually means that there's an in-memory structure built from the AST which is kept in sync with the filesystem and dirty buffers which we should be able to query.
My initial guess is that this will need more work to be used in the LSP and as such we shouldn't put it into a public API for now (maybe the best approach would be creating a separate library which relies only on the robotframework public API where we can extract the needed bits -- it should probably live outside of the core robotframework project so that I can do releases without needing to synchronize with the robotframework release date -- or maybe I could vendor robotframework).
Yeah, it's hard to tell beforehand what kind of an API is needed and would be convenient to use. It's certain that some API is needed because that's the only realistic way to the LSP server to support all same library APIs that Robot itself supports. The aforementioned LIbraryDocumentation does pretty good job unifying everything so that keywords are seen the same way regardless where they originate.
I haven't though how LibraryDocumentation behaves if it is used multiple times in same process with same library so that the library source changes. Most likely it gets the library module from sys.modules meaning that changes aren't seen. If that's the case, we can see could LibraryDocumentation itself fix that somehow or should the caller just remove the library from sys.modules. I doubt there are big performance issues here. When opening a big project, only actually opened files needed to be validated fast, others can be validated on background.
I totally agree we shouldn't put anything into the public API until we know we need it and know we don't need to immediately change it. A good thing is that LibraryDocumentation is already available via robot.libdocpkg so it can be used already now and exposed publicly later if needed. Argument validation is a harder problem, but we can return to that when you get that far.
My plan is getting RF 3.2 beta 2 out next week, and I don't think it needs any changes related to this. First release candidate would hopefully be ready at the end of February and before that we should know is something needed. I don't want to wait indefinitely with RF 3.2, but we can do small and safe enhancements in RF 3.2.x releases and possibly also RF 3.3 if bigger changes are needed.
Have you @fabioz had time to think about this? My hope is to get RF 3.2 rc 1 out pretty soon, so we need to start having plans if we want something like this in.
@pekkaklarck I think we could use XML files directly as proposed in robocorp/robotframework-lsp#20... I think I can generate the XML files automatically when needed through the existing public API (i.e.: python -m robot.libdoc), so, no additional API is needed for that for now (and we don't need to worry about caches when regenerating). The file will be rebuilt in a separate process and code-completion will use the resulting xml (so, it's possible that outdated info is given to the user until the xml finishes generating, but it should not be a huge issue unless it's very slow).
As for reading it, having an API that reads that file would be nice (but I can live with just reading the XML directly too as it's a simple format if it can't be put in an API until the final 3.2 release).
One issue here is that robot.libdocpkg.builder.DocumentationBuilder checks if extension=='xml' to decide the builder and I was proposing having a .robotapi in robocorp/robotframework-lsp#20 (waiting for feedback on using that file extension in that issue).
@pekkaklarck yes, it does read those files, but only if they have an xml extension right now... I'm ok with any of those extensions -- do you have any preference?
Oh, I didn't realize the problem is with LibraryDocumentation('example.kwspec') failing totally. Apparently it tries to import a library with name example.kwspec in this case. That's not good, especially when Libdoc is happy to create output files with such extension assuming you use the --format option. We got several options how to fix that:
Automatically detect that these files should be parsed as specs. I guess we could do it so that if the given value is a path to a file we try to parse it as a spec if importing it as a library fails.
Automatically detect this based on the extension. We already do that with the xml extension, but I agree it would be good to have a specific extension just for this usage as well. Of all the discussed alternatives, kwspec is my current favorite. It is explicit and matches the <keywordspec> root element in the file.
Make it possible to specify what extension spec files uses when using LibraryDocumentation.
I think I'd go with the option 2. but could consider implementing 1. as well.
@Snooz82 mentioned in robocorp/robotframework-lsp#20 that RED already uses *.libspec and the decision was to go with that. I submitted the above issue about that change. This issue can concentrate on the public API.
One thing to think about is that the actual library and keyword documentation can be in different format. In the *.libspec issue (#3491) we've been talking about spec files automatically getting docs converted to HTML. Is that a good idea in the LSP domain as well @fabioz or would plain text be better?
If HTML is generally considered to be good for editors, we probably should make it possible to convert docs to HTML programmatically as well. Either something like
Another thing that would be useful is if the spec had some way for telling which are the sources that generated that file (when possible).
For instance, if a keyword was generated from a method in some Python file, I'd like to track that file for modifications to know that I have to regenerate the spec if that file changes... I know there are cases where this isn't possible, but when it is, it'd be nice to have it.
p.s.: I commented about it in #3491, but I think this issue is the proper place for that (or should it be a separate issue?)
Reference to the original source file and possibly line should have its own issue. As I commented #3491, there's a problem that it only works with locally generated files. Need to think what's the best way to handle this use case.
I'd like to get RF 3.2 released soon. Is there anything simple related to this that should still be added? Anything bigger can wait for the next release.
robot.running.TestLibrary was heavily enhanced in RF 7.0 and at the same time we also got new robot.running.ResourceFile. They ought to provide easy to use APIs for LSP and other usages. If they need to be enhanced, new issues can be submitted.
Activity
pekkaklarck commentedon Feb 3, 2020
A simple solution would be just exposing the current
robot.libdocpkg.LibraryDocumentationfactory method viarobot.api. Open questions related to it:pekkaklarck commentedon Feb 6, 2020
I was playing with
LibraryDocumentationand it seems to work pretty well:Can you @fabioz comment would the above be enough for LSP? If it is, then I'd do the following enhancement here on Robot side:
LibraryDocumentationviarobot.api.SeleniumLibrary::10swhich isn't too convenient. Separateargsargument would be better.pekkaklarck commentedon Feb 6, 2020
One very handy
LibraryDocumentationfeature, especially in the editor context, is its ability to read information from "Libdoc spec files". If, for example, I create such a spec file for SeleniumLibrary like:I can then use it like:
pekkaklarck commentedon Feb 7, 2020
Another highly related thing that would probably be useful in the editor/LSP domain is detecting does a certain library implement a keyword or not. Matching normal keyword names used in data to keywords provided by libraries is pretty simple, but embedded arguments make that a bit more complicated. It would be a lot easier if the
LibraryDocinstance returned by theLibraryDocumentationfactory method would directly support something like this:Even better if there would also be a way to check is the keyword called with valid arguments. The same method could optionally do that as well, but perhaps it would better to have a dedicated method instead:
We have all this logic inside Robot Framework, but it is not exposed via any public API and there's a big risk that the related code will change in future versions. Having a stable (and tested) API would help because it could be fixed if there are lower level changes.
fabioz commentedon Feb 9, 2020
Sorry for not answering sooner here... the truth is, I still don't know how much I'll be able to reuse it and I'll have to check it more in-depth before answering (my plan is evaluating this as my first step when I get to start implementing code-completion).
My main concerns is that there are some requisites a language server has that may not be directly there...
Some examples:
it must be able to deal with dirty buffers and not just deal with files in the filesystem (for instance, if the user has 2 files opened, one being a library and the other not, if the user changes the library file to add some keywords, asking for code-completion in a different file should provide such keywords -- the same goes for code analysis).
is that API fast enough to be used interactively in big projects? This usually means that there's an in-memory structure built from the AST which is kept in sync with the filesystem and dirty buffers which we should be able to query.
My initial guess is that this will need more work to be used in the LSP and as such we shouldn't put it into a public API for now (maybe the best approach would be creating a separate library which relies only on the robotframework public API where we can extract the needed bits -- it should probably live outside of the core robotframework project so that I can do releases without needing to synchronize with the robotframework release date -- or maybe I could vendor
robotframework).pekkaklarck commentedon Feb 9, 2020
Yeah, it's hard to tell beforehand what kind of an API is needed and would be convenient to use. It's certain that some API is needed because that's the only realistic way to the LSP server to support all same library APIs that Robot itself supports. The aforementioned
LIbraryDocumentationdoes pretty good job unifying everything so that keywords are seen the same way regardless where they originate.I haven't though how
LibraryDocumentationbehaves if it is used multiple times in same process with same library so that the library source changes. Most likely it gets the library module fromsys.modulesmeaning that changes aren't seen. If that's the case, we can see couldLibraryDocumentationitself fix that somehow or should the caller just remove the library fromsys.modules. I doubt there are big performance issues here. When opening a big project, only actually opened files needed to be validated fast, others can be validated on background.I totally agree we shouldn't put anything into the public API until we know we need it and know we don't need to immediately change it. A good thing is that
LibraryDocumentationis already available viarobot.libdocpkgso it can be used already now and exposed publicly later if needed. Argument validation is a harder problem, but we can return to that when you get that far.My plan is getting RF 3.2 beta 2 out next week, and I don't think it needs any changes related to this. First release candidate would hopefully be ready at the end of February and before that we should know is something needed. I don't want to wait indefinitely with RF 3.2, but we can do small and safe enhancements in RF 3.2.x releases and possibly also RF 3.3 if bigger changes are needed.
pekkaklarck commentedon Mar 2, 2020
Have you @fabioz had time to think about this? My hope is to get RF 3.2 rc 1 out pretty soon, so we need to start having plans if we want something like this in.
fabioz commentedon Mar 4, 2020
@pekkaklarck I think we could use XML files directly as proposed in robocorp/robotframework-lsp#20... I think I can generate the XML files automatically when needed through the existing public API (i.e.:
python -m robot.libdoc), so, no additional API is needed for that for now (and we don't need to worry about caches when regenerating). The file will be rebuilt in a separate process and code-completion will use the resulting xml (so, it's possible that outdated info is given to the user until the xml finishes generating, but it should not be a huge issue unless it's very slow).As for reading it, having an API that reads that file would be nice (but I can live with just reading the XML directly too as it's a simple format if it can't be put in an API until the final 3.2 release).
One issue here is that
robot.libdocpkg.builder.DocumentationBuilderchecks ifextension=='xml'to decide the builder and I was proposing having a.robotapiin robocorp/robotframework-lsp#20 (waiting for feedback on using that file extension in that issue).pekkaklarck commentedon Mar 4, 2020
LibraryDocumentationcan read also these xml files. It would thus be a very convenient API.I think you can explicitly specify the output format and then thr extension doesn't matter.
I consider
.robotapisomewhat too generic. Perhaps.libapi,.libspec,.kwapior.kwspecwould be better.fabioz commentedon Mar 4, 2020
@pekkaklarck yes, it does read those files, but only if they have an
xmlextension right now... I'm ok with any of those extensions -- do you have any preference?pekkaklarck commentedon Mar 5, 2020
Oh, I didn't realize the problem is with
LibraryDocumentation('example.kwspec')failing totally. Apparently it tries to import a library with nameexample.kwspecin this case. That's not good, especially when Libdoc is happy to create output files with such extension assuming you use the--formatoption. We got several options how to fix that:Automatically detect that these files should be parsed as specs. I guess we could do it so that if the given value is a path to a file we try to parse it as a spec if importing it as a library fails.
Automatically detect this based on the extension. We already do that with the
xmlextension, but I agree it would be good to have a specific extension just for this usage as well. Of all the discussed alternatives,kwspecis my current favorite. It is explicit and matches the<keywordspec>root element in the file.Make it possible to specify what extension spec files uses when using
LibraryDocumentation.I think I'd go with the option 2. but could consider implementing 1. as well.
fabioz commentedon Mar 5, 2020
Option
2seems fine for me (as is usingkwspec).pekkaklarck commentedon Mar 6, 2020
@Snooz82 mentioned in robocorp/robotframework-lsp#20 that RED already uses
*.libspecand the decision was to go with that. I submitted the above issue about that change. This issue can concentrate on the public API.pekkaklarck commentedon Mar 9, 2020
One thing to think about is that the actual library and keyword documentation can be in different format. In the
*.libspecissue (#3491) we've been talking about spec files automatically getting docs converted to HTML. Is that a good idea in the LSP domain as well @fabioz or would plain text be better?If HTML is generally considered to be good for editors, we probably should make it possible to convert docs to HTML programmatically as well. Either something like
or
fabioz commentedon Mar 9, 2020
Plain text would be better (the LSP only supports text and markdown and markdown support is not guaranteed for all clients).
fabioz commentedon Mar 10, 2020
Another thing that would be useful is if the spec had some way for telling which are the sources that generated that file (when possible).
For instance, if a keyword was generated from a method in some Python file, I'd like to track that file for modifications to know that I have to regenerate the spec if that file changes... I know there are cases where this isn't possible, but when it is, it'd be nice to have it.
p.s.: I commented about it in #3491, but I think this issue is the proper place for that (or should it be a separate issue?)
pekkaklarck commentedon Mar 10, 2020
Reference to the original source file and possibly line should have its own issue. As I commented #3491, there's a problem that it only works with locally generated files. Need to think what's the best way to handle this use case.
Snooz82 commentedon Mar 11, 2020
This could be something that would be specific for *.libspec.
If you choose libspec as extension Libdoc could put a path into the xml.
pekkaklarck commentedon Mar 16, 2020
I'd like to get RF 3.2 released soon. Is there anything simple related to this that should still be added? Anything bigger can wait for the next release.
fabioz commentedon Mar 16, 2020
I think things are ok (and I created #3507 to address the issue on the .libspec having access to the sources that generated it).
pekkaklarck commentedon Mar 17, 2020
OK, I'll remove this issue from RF 3.2 scope. Let's talk about including the source information in #3507.
Snooz82 commentedon Sep 15, 2020
@pekkaklarck
I think we should refactor the libdocpkg.model so that it can be accessed and directly used as is from robot.api.
once we have all missing information like argument types and return values / types we could consider the model as stable?!
pekkaklarck commentedon Sep 24, 2024
robot.running.TestLibrarywas heavily enhanced in RF 7.0 and at the same time we also got newrobot.running.ResourceFile. They ought to provide easy to use APIs for LSP and other usages. If they need to be enhanced, new issues can be submitted.