Skip to content

Enhance public robot.api.Languages and robot.api.Language APIs #4494

Closed

Description

@d-biehl

In order to support languages reasonably in RobotCode (and other tools), it would be nice if we could talk about the Language API again.

  1. To give the user a good error message which language or custom language was not loaded or found, it should be possible to load individual languages and import custom language files.

    • In the current implementation I can either create the Languages class in the hope that it works, in case of error I can not easily filter out the language that does not work.
    • add_language can't load user defined language files
  2. There should be a public API that returns all available languages.

    • already exists, is just not public
  3. The process of loading custom language files should be separated from loading the real language.

    • Before loading the real languages, the custom language files should be loaded, only then you can load the language
    • I think this should also be done with the command line parameters, there should be 2 arguments, one for loading the custom language files and the current one for the real language
  4. it should be possible to create a Languages instance that does not insert English and the deprecated single forms of the headers automatically

    • I currently have to filter it out again if I want to show only the specified languages e.g. in the completion.

Let's discuss ;-)

Activity

  1. d-biehl commented on Oct 4, 2022

    @d-biehl
    ContributorAuthor

    I can make a proposal, but not before the end of the week...

  2. pekkaklarck commented on Oct 4, 2022

    @pekkaklarck
    Member

    Could you give examples of what you'd like to do and how you'd like to use these APIs? It's kind of hard to understand this otherwise. If there's something simple, we can still get it done but anything bigger needs to wait for 6.1. I definitely want rc 2 out still this week so that we get the final release out at some point as well.

  3. d-biehl commented on Oct 6, 2022

    @d-biehl
    ContributorAuthor
    • I would like to get a list of all available languages to show them in autocompletion
    • given that I have two settings, one with a list of custom language files and a list of languages to be supported, then I want to load these entries individually to filter out invalid languages and tell the user that they are invalid, but still load the valid ones.
  4. d-biehl commented on Oct 6, 2022

    @d-biehl
    ContributorAuthor
    • To make sure that the custom languages can be instantiated, you have to make sure that the custom language files are already loaded. If the user specifies them in the wrong order in the current implementation, there will be an error.
  5. pekkaklarck commented on Oct 6, 2022

    @pekkaklarck
    Member

    I was hoping you could give concrete code examples using new/enhanced APIs so that I could implement them. It seems you already started implementation (c7d4bb9) and just providing a PR obviously works as well. New APIs should get unit tests to make sure we don't accidentally break them in the future. When do you have time to continue with this? I'd hope we get rc 2 out latest on Monday.

  6. d-biehl commented on Oct 6, 2022

    @d-biehl
    ContributorAuthor

    Just got back from RoboConDE and the imbus QS-Day.
    I thought the changes speak for themselves. i didn't actually change anything, just changed the visibility of the methods and made them static and introduced a new parameter.
    But you are right, if it is a public API there should be tests for it. I will add some unittests this weekend.

  7. pekkaklarck commented on Oct 6, 2022

    @pekkaklarck
    Member

    Few things to fix and enhance in the code:

    • Static methods cannot accept self.
    • Usages of the renamed methods haven't been updated. Tests will obviously catch this and the above issue.
    • get_available_languages and import_languages should get docstrings now that they are part of the public API. Especially the latter would need it because its name isn't too clear. Perhaps it should be renamed to import_language_module to make it more explicit. Also argument name could possibly be better than the current lang.
    • Languages class would benefit from a docstring as well.
  8. d-biehl commented on Oct 6, 2022

    @d-biehl
    ContributorAuthor

    I already fixed the self and the usages, because failing tests ;-) if you go to the Files Changed tab you will see.

    The other things I will add/change.

    One point I did not implemented yet: I think there should be 2 commandline parameters regarding the language, one for loading the custom language files and one for enabling the language. The other way is, at using the --language command line parameter, we must add a hint to the documentation, that if the user want's to use a custom language module, the module must be given before the language.
    What do you think?

  9. pekkaklarck commented on Oct 6, 2022

    @pekkaklarck
    Member

    I only looked at the commit that referred this issue and thus didn't see additional changes.

    I don't think another cli option is needed. If you have a custom language in lang.py, you can simply use --language lang.py and it will be loaded and enabled.

  10. pekkaklarck commented on Oct 6, 2022

    @pekkaklarck
    Member

    The PR implementing this is #4496.

  11. d-biehl commented on Oct 10, 2022

    @d-biehl
    ContributorAuthor

    add some docs and tests for the public API's. Can you review it?

  12. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    Reviewed.

  13. added this to the v6.0 milestone on Oct 10, 2022
  14. 5 remaining items

  15. d-biehl commented on Oct 10, 2022

    @d-biehl
    ContributorAuthor
    1. Extending add_language so that also language modules can be loaded was also my first thought.
      On RobotCode side I have to create a new language instance every time I want to check if a language or a language module exists and can be loaded. This can happen very often, in the simplest case when the user changes the settings, in principle every time the user changes a file, because it always has to be parsed and analyzed again. It is enough to determine the languages to create them later if needed.

      With code completion (I type `language: ' and the code completion shows all possible languages) I only need the names and the codes of the language, but no complete instances of the languages.

      It costs time and memory to instantiate the classes each time.

      In addition, I would like to load the language modules in a new process, so I don't pollute the language server instance. Once a module is loaded, you can't get it out again. Topic language bleeding ;-) Transferring whole instances or just the class names is a difference.

    2. i think there should be a unified way to get all possible languages and also their names and codes. This is not given if I only use the Language.subclasses, because then I also have to rebuild the algorithm, how RobotFramework determines the names and codes. Whether they are normalized or not. But you can specify that. We can also use the robot.utils.normalizing.NormalizedDict as return value?

      I just noticed that: subclasses returns only the direct subclasses, if I want to override e.g. the BDD_prefixes for a certain language this does not work.

      If on RobotFramework side the implementation is changed, I have to reprogram that too.

    3. see 1 and:

      import_language_module and get_available_languages are static methods in PR, in current implementation they are not, but could be, since they do not use self.
      We can also move them from class to global namespace. Then it is no longer possible to call langs.import_language_module like this.


    Just to clarify, I want to stay as close as possible to RobotFramework with RobotCode and like to reuse as much as possible and dislike reprogramming code. Preferably with a good API!

  16. pekkaklarck commented on Oct 10, 2022

    @pekkaklarck
    Member

    I don't understand why any of these points would require adding import_language_module or get_available_languages. It would be easier to understand if you showed concrete code where enhancing add_language and making English optional doesn't work. With the earlier example you showed in the comments of the PR, that approach made the code a lot simpler.

    I understand you want as much of the logic to be on Robot side as possible, but that adds more maintenance burden to Robot which is not what I want. I want simple APIs that provide needed functionality and that we can preserve in the future. This is especially important with these new language APIs because they will certainly need to be enhanced in the future anyway. More convenience can be added to these APIs when they are otherwise stable.

  17. changed the title [-][ENHANCEMENT] Enhanced public API for detecting and loading languages[/-] [+]Enhance public `robot.api.Languages` and `robot.api.Language` APIs[/+] on Oct 11, 2022
  18. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    Most of the functionality was added in PR #4496 by @d-biehl. My commit above did these additional changes:

    • More doc strings.
    • Change exception used by Languages.add_language on error to match exception used when initializing Languages.
    • Make Language.code/name propertys accessible also from class. This required implementing new @classproperty decorator.

    I consider this issue now ready, but I leave it open to give Daniel some time to review my changes and see how everything works in practice with RobotCode. If there are small and/or critical enhancements, then can still be considered but anything bigger and less urgent can wait for RF 6.1.

  19. added
    acknowledge
    on Oct 11, 2022
  20. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    Did you @d-biehl have time to look at the latest changes? The commit above added support to use add_language with Language instances and what the earlier commit did was explained in my previous comment.

    If there's something still to be changed, let me now ASAP. I just got RF 6.0 rc 2 release notes ready and plan to create the release candidate very soon.

  21. d-biehl commented on Oct 11, 2022

    @d-biehl
    ContributorAuthor

    It's OK. Everything fits.

  22. pekkaklarck commented on Oct 11, 2022

    @pekkaklarck
    Member

    Awesome! Looking forward for the new RobotCode release with enhancements related to this and other recent features.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgepriority: mediumrc 2

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions