mxcube / mxcubecore

Backend used by MXCuBE
http://mxcube.github.io/mxcube/
GNU Lesser General Public License v3.0
11 stars 52 forks source link

Update of the TangoShutter Hardware object and class #834

Closed HilbertCorsair closed 8 months ago

HilbertCorsair commented 8 months ago

This code was based on a model provided by Antonia Beteva @beteva and it aims to be a general tango shutter class. The specific HardwareObject values have to be mapped to existing predefined values in the dictionary within the values tags of the xml configuration file corresponding to the specific shutter Hardware object. This version uses the AbstractShutter class.

HilbertCorsair commented 8 months ago

Thank you very much for your input. I made changes to the code according to you comments. This latest version of the code is a bit more elegant and it can also handle cases in which using a values tag in the xml configuration file isn't necessary.

beteva commented 8 months ago

Hi @HilbertCorsair, this gets better and better. Did you run black and pylint (or similar) ?

HilbertCorsair commented 8 months ago

In this latest push I made the change mentioned by @beteva and also used black to analyse and format the code. In summary with this new class there are 2 kinds of tango shutters: those whit conventional values and those who require a json dictionary in the values tag of the xml configuration file, to map the local specific values to conventional ones :)

fabcor-maxiv commented 8 months ago

Did you run black and pylint (or similar) ?

Seems like the GitHub Actions workflows were not allowed to run. GitHub was waiting for some manual approval first. I have now given this approval (which I did not know I had the permission to do).

We can now see there are some linting/formatting issues.

@HilbertCorsair Do you have the pre-commit hooks installed in your local development environment?

HilbertCorsair commented 8 months ago

I have just now installed git pre-commit and updated it. I tried to re-commit the code but now I get this error : failed to find interpreter for Builtin discover of python_spec='python3.8' stderr: (none). It seems to be looking for python 3.8 but my conda environment of the mxcube core uses python 3.10.12 Should I modify the pre-comit-config.yml file to use python 3.10 instead of 3.8?

fabcor-maxiv commented 8 months ago

I have just now installed git pre-commit and updated it. I tried to re-commit the code but now I get this error : failed to find interpreter for Builtin discover of python_spec='python3.8' stderr: (none). It seems to be looking for python 3.8 but my conda environment of the mxcube core uses python 3.10.12

Oh... I had not noticed this... It did not show up as an issue for me probably because I happen to have a Python 3.8 installed as well.

Should I modify the pre-comit-config.yml file to use python 3.10 instead of 3.8?

@HilbertCorsair I guess you can try to modify locally, and see if it allows you to move forward with your work. Otherwise you can also uninstall the pre-commit hooks for now (the GitHub workflow will run the same checks anyway).

In a separate pull request we should probably address this inconsistency. I opened a ticket: https://github.com/mxcube/mxcubecore/issues/836

HilbertCorsair commented 8 months ago

OK I changes the pre-commit config file to use python 3.10 and managed to push the commit. I made no changes to the code other than removing 2 empty lines. The code should work on python 3.8 as well as it doesn't contain any features specific to python 3.10 The broader issue is now whether I should chance my conda environment to python3.8 or keep using python3.10 ? SInce Python 3.10 was installed by default with the mxcube web app conda environment I would be tempted to stick to this newer version.

fabcor-maxiv commented 8 months ago

OK I changes the pre-commit config file to use python 3.10 and managed to push the commit. I made no changes to the code other than removing 2 empty lines. The code should work on python 3.8 as well as it doesn't contain any features specific to python 3.10 The broader issue is now whether I should chance my conda environment to python3.8 or keep using python3.10 ? SInce Python 3.10 was installed by default with the mxcube web app conda environment I would be tempted to stick to this newer version.

@HilbertCorsair See my previous comment.

The Python version topic should have been further addressed in separate ticket and pull request. Now that you have introduced this unrelated change in the branch I do not know how we should proceed. If it were just me I would tell you to remove this change from the pull request (not with a revert commit, but with a hard modification of the commits on the branch and then a force-push of the branch), but I am not sure it is in line with the policy for this repository. Maybe a squash and merge would be fine if we do a revert commit, I do not know.

Keep using Python 3.10. Keep your modifications of the pre-commit hooks local to your computer, for now until this is fixed in the repository.

marcus-oscarsson commented 8 months ago

Looks pretty good to me, I agree with @fabcor-maxiv that its nice if you can remove the commit that changes the pre-commit hook.

You don't have to install it locally you can simply run it with the pre-commit command if that makes things easier for you.

HilbertCorsair commented 8 months ago

OK, I have uninstalled local git hooks and reverted my last commit.

marcus-oscarsson commented 8 months ago

I think its more or less fine, just a run of black and it should pass, did you try to run pre-commit in the console ?

HilbertCorsair commented 8 months ago

I noticed there was a mixed line endings issue. The file was edited on different editors.

fabcor-maxiv commented 8 months ago

@marcus-oscarsson Do you know how come the GitHub Actions workflows do not run automatically? Seems like I can trigger them manually, but it needs manual re-trigger after each modification of the pull request.

HilbertCorsair commented 8 months ago

Could the pytest-coverage fail due to Dependabot ? https://docs.github.com/en/code-security/code-scanning/troubleshooting-code-scanning/resource-not-accessible

marcus-oscarsson commented 8 months ago

@fabcor-maxiv, I think its because @HilbertCorsair have not yet had any PR merged, ill see If I can find the setting somewhere.

@HilbertCorsair, There still seems to be some black linting error, did you run black one last time before comiting ?

marcus-oscarsson commented 8 months ago

:1st_place_medal: thanks !

beteva commented 8 months ago

@fabcor-maxiv I still do not understand why using json and not ast. This is a quote from the json documentation

Be cautious when parsing JSON data from untrusted sources. A malicious JSON string may cause the decoder to consume considerable CPU and memory resources

We may need to have something like <values>{"FOO": ("MYFOO", HardwareObjectState.READY}</values> and it seems, at least for me, that json.loads does not handle well tuples, while the ast.literal_eval is just fine.

fabcor-maxiv commented 8 months ago

@beteva My advice was based on the pieces of info I had (understood) at the time. I did not know we might want to have tuples, for example. This is what I wrote:

I have not understood which kind of input we are trying to evaluate (as an educated guess I suggested the JSON parser, it should be a very good fit to read a dict-like input)

Before that I wrote that I would prefer if we used XML all the way:

Wouldn't it be possible to use XML features for this?


On AST vs. JSON: Yes, both of them have their (security) risk. Seems like AST can lead to a crash, while JSON only to high CPU and memory usage. Both are bad. And both are unlikely to happen in our case since we are not reading from untrusted sources.


Now that I am brought to think about this topic again and more deeply, I am quite warm (but cautious) to the idea of having configuration as Python files (that we read with ast.literal_eval). All the formats have their pros and cons. JSON and XML are not human-friendly. YAML is so complex and has so many pitfalls. For example, this is how the configuration is done for our Sphinx documentation build: https://github.com/mxcube/mxcubecore/blob/dcb38fdc53a34af67f5ff39f3e2bac9fbb64167d/docs/conf.py

We could consider something like this instead of migrating to YAML. I very much prefer writing and reading Python rather than XML, YAML, or JSON.

rhfogh commented 8 months ago

A few points:

There is a good overview of the weaknesses of different formats in https://pypi.org/project/strictyaml/. It shows pretty clearly that there is no single good solution (including the author's own).

GPhL will be using the full Yaml 1.2 for our own configuration (likely) and for our own inter-program communication (especially) since we need the ability to represent object graphs. For the general MXCuBE fiels we would adapt to whatever was agreed. But (with the above web site in mind) StrictYAML - which is a lot simpler than full YAML - would seem to be a good choice for general configuration files and would get my vote.

fabcor-maxiv commented 8 months ago

@rhfogh Thanks for providing some context which I was not aware of (having joined the project relatively recently).

I will note that JSON is a subset of YAML. Every valid piece of JSON is also a valid piece of YAML. I do not know if it is also valid strictyaml, though.

rhfogh commented 8 months ago

@fabcor-maxiv I know you have joined recently - and the less technical parts of my post ;-) were not directed at you as much as at the entire discussion. I should add that while we did at one point settle on YAML as the next format, it is not something that generated much commitment or work from anybody (and where I still have not kept my promise to make it easier to move from XML to YAML). Whether we should decide on another format after all (like when we should finally move out of XML) is definitely a legitimate question - which has now been raised. I just hope that we can agree on something and then apply that agreement going forward.

On the purely technical side it is true that YAML subsumes JSON, but if you stick to JSON you lose the advantages of YAML, i.e. the comments and the human readability and editability. All you get it the ability to use the same parser.

marcus-oscarsson commented 8 months ago

It's of-course a very important topic and I really appreciate the fresh view from @fabcor-maxiv. I think we did at some point even consider Python for configuration as well. I do agree with @rhfogh that even though its taken us some time to get started we have decided for YAML and I think we should stick to that unless there are apparent drawbacks that are solved with another solution.

I simply by experience, without having read https://pypi.org/project/strictyaml/, also think that there are no good single solution to configuration file formats. Perhaps simply proven by the amount of formats there are :)

I thought we could bring this up on Monday on our next meeting, but I was hoping the topic could be more how to proceed with the XML to YAML conversion rather than discussing which format to use. We will leave some room for the format discussion but then I think we should carry on, with one format and a roadmap on how to get there :)

fabcor-maxiv commented 8 months ago

rather than discussing which format to use

I knew a migration from XML to YAML was in the cards; I did not know there was already an agreement. I am fine with YAML, no problem from my side. I will have a look at "strict YAML", that seems interesting.

marcus-oscarsson commented 8 months ago

:), @fabcor-maxiv not so easy to know. We had and will have new developers coming and going and its natural that one can't know whats been decided in the past and for instance technical details that are maybe not well documented. I think we all appreciate to be challenged by/presented new angels and ideas, I think its important for the project, so please continue to voice your mind :). We also need to be better to document especially technical bits to make it easier for newcomers, and you already started a nice job on that !