Skip to content

[FEATURE]: Support input_path in generated make() functions from mcstas-pygen #2554

Description

@MilanKlausz

Is your feature request related to a problem? Please describe.
I use mcstas-pygen to turn an .instr file into a .py script so it can be wrapped in a python package to add a complex detector geometry. The generated script creates a make() function that instantiates McStas_instr with only hardcoded parameters.

The problem is that my package expects custom local components, so McStasScript needs to know where they are (input_path) right at instantiation time. Because make() takes no arguments, I'm forced to manually edit the generated python file to add input_path to the function signature and constructor call each time the base instrument file is edited and the python file is regenerated.

Describe the solution you'd like
It would be great if mcstas-pygen generated the make function with a default parameter: def make(input_path=None):.

This would pass input_path directly to McStas_instr(..., input_path=input_path). Since the constructor signature defaults to input_path=None (resolving to "." internally (see: here)), I expet this change to be completely backward-compatible and won't break any existing scripts calling make() with no arguments.

Describe alternatives you've considered
My alternative is to continue manually editing or to automate overriding :)

Additional context
Looking at the source code, I hope this would be a very quick change in mccode/src/pygen.c:

    #if MCCODE_PROJECT == 1     /* neutron */
      cout("# Python McStas instrument description");
      cout("def make(input_path=None):");
      coutf("    instr = ms.McStas_instr(\"%s_generated\", author = \"McCode Py-Generator\", origin = \"ESS DMSC\", input_path=input_path)",instr->name);
    #elif MCCODE_PROJECT == 2   /* xray */
      cout("# Python McXtrace instrument description");
      cout("def make(input_path=None):");
      coutf("    instr = ms.McXtrace_instr(\"%s_generated\", author = \"McCode Py-Generator\", origin = \"ESS DMSC\", input_path=input_path)",instr->name);
    #endif

Note:
I wouldn't mind having other McStas_instr arguments as well (I manually add output_path and NeXus, but these can be modified through .settings() after the initialisation, so they are not really important)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions