Skip to content

Stable API for getting library and resource file info #3448

Closed

Description

@pekkaklarck

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.

Activity

  1. added this to the v3.2 milestone on Feb 3, 2020
  2. pekkaklarck commented on Feb 3, 2020

    @pekkaklarck
    MemberAuthor

    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.
    • Is the method named well?
  3. pekkaklarck commented on Feb 6, 2020

    @pekkaklarck
    MemberAuthor

    I was playing with LibraryDocumentation and it seems to work pretty well:

    >>> from robot.libdocpkg import LibraryDocumentation
    >>> 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.
  4. pekkaklarck commented on Feb 6, 2020

    @pekkaklarck
    MemberAuthor

    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:

    python -m robot.libdoc SeleniumLibrary SeleniumLibrary.xml
    

    I can then use it like:

    >>> from robot.libdocpkg import LibraryDocumentation
    >>> doc = LibraryDocumentation('SeleniumLibrary.xml')
    >>> doc.name
    'SeleniumLibrary'
  5. pekkaklarck commented on Feb 7, 2020

    @pekkaklarck
    MemberAuthor

    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:

    >>> doc = LibraryDocumentation('SeleniumLibrary')
    >>> doc.has_keyword('Open Browser')
    True
    >>> doc.has_keyword('open browser')
    True
    >>> doc.has_keyword('xxx')
    False

    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:

    >>> doc.validate_keyword_call('Click Link', ['example'])
    >>> doc.validate_keyword_call('Click Link', ['too', 'many', 'arguments'])
    Traceback (most recent call last):
    [snip]
    DataError: Keyword 'Click Link' expected 1 to 2 arguments, got 3.

    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.

  6. fabioz commented on Feb 9, 2020

    @fabioz
    Contributor

    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).

  7. pekkaklarck commented on Feb 9, 2020

    @pekkaklarck
    MemberAuthor

    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.

  8. pekkaklarck commented on Mar 2, 2020

    @pekkaklarck
    MemberAuthor

    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.

  9. fabioz commented on Mar 4, 2020

    @fabioz
    Contributor

    @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).

  10. pekkaklarck commented on Mar 4, 2020

    @pekkaklarck
    MemberAuthor
    1. LibraryDocumentation can read also these xml files. It would thus be a very convenient API.

    2. I think you can explicitly specify the output format and then thr extension doesn't matter.

    3. I consider .robotapi somewhat too generic. Perhaps .libapi, .libspec, .kwapi or .kwspec would be better.

  11. fabioz commented on Mar 4, 2020

    @fabioz
    Contributor

    @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?

  12. pekkaklarck commented on Mar 5, 2020

    @pekkaklarck
    MemberAuthor

    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:

    1. 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.

    2. 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.

    3. 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.

  13. fabioz commented on Mar 5, 2020

    @fabioz
    Contributor

    Option 2 seems fine for me (as is using kwspec).

  14. pekkaklarck commented on Mar 6, 2020

    @pekkaklarck
    MemberAuthor

    @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.

  15. pekkaklarck commented on Mar 9, 2020

    @pekkaklarck
    MemberAuthor

    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

    libdoc = LibraryDocumentation(path, html_doc=True)

    or

    libdoc = LibraryDocumentation(path)
    libdoc.docs_to_html()
    
  16. fabioz commented on Mar 9, 2020

    @fabioz
    Contributor

    Plain text would be better (the LSP only supports text and markdown and markdown support is not guaranteed for all clients).

  17. fabioz commented on Mar 10, 2020

    @fabioz
    Contributor

    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?)

  18. pekkaklarck commented on Mar 10, 2020

    @pekkaklarck
    MemberAuthor

    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.

  19. Snooz82 commented on Mar 11, 2020

    @Snooz82
    Member

    This could be something that would be specific for *.libspec.
    If you choose libspec as extension Libdoc could put a path into the xml.

  20. pekkaklarck commented on Mar 16, 2020

    @pekkaklarck
    MemberAuthor

    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.

  21. fabioz commented on Mar 16, 2020

    @fabioz
    Contributor

    I think things are ok (and I created #3507 to address the issue on the .libspec having access to the sources that generated it).

  22. pekkaklarck commented on Mar 17, 2020

    @pekkaklarck
    MemberAuthor

    OK, I'll remove this issue from RF 3.2 scope. Let's talk about including the source information in #3507.

  23. removed this from the v3.2 milestone on Mar 17, 2020
  24. Snooz82 commented on Sep 15, 2020

    @Snooz82
    Member

    @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?!

  25. pekkaklarck commented on Sep 24, 2024

    @pekkaklarck
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions